def test_serialization(self): project = ProjectFactory.create() self.db.session.commit() serializer = ProjectSerializer(strict=True) data = serializer.dump(project).data assert data['id'] == project.id assert data['name'] == project.name assert data['description'] == project.description assert data['last_task'] is None assert not data['tasks'] assert data['client']['id'] == project.client.id task_1 = TaskFactory.create(project=project, status='open') task_2 = TaskFactory.create(project=project, status='closed') self.db.session.commit() # Last task data = serializer.dump(project).data assert data['last_task'] is not None assert data['last_task']['id'] == task_1.id assert data['last_task']['title'] == task_1.title assert 'time_entries' not in data['last_task'] # Tasks (with filtering) data = ProjectSerializer( strict=True, task_status='open').dump(project).data assert len(data['tasks']) == 1 assert data['tasks'][0]['id'] == task_1.id assert data['tasks'][0]['project_id'] == project.id data = ProjectSerializer( strict=True, task_status='closed').dump(project).data assert len(data['tasks']) == 1 assert data['tasks'][0]['id'] == task_2.id assert data['tasks'][0]['project_id'] == project.id
def test_validate_client_id(self): data = ProjectFactory.stub(client=None).__dict__ data['client_id'] = 0 serializer = ProjectSerializer( only=['id', 'name', 'description', 'client_id']) instance, errors = serializer.load(data) assert 'client_id' in errors
def test_validate_name_length(self): serializer = ProjectSerializer( only=['id', 'name', 'description', 'client_id']) # Min data = ProjectFactory.stub(name='x').__dict__ instance, errors = serializer.load(data) assert 'name' in errors # Max data = ProjectFactory.stub(name='x' * 101).__dict__ instance, errors = serializer.load(data) assert 'name' in errors
def get(project_id, task_status): project = db.session.query(Project).get(project_id) if not project: raise ProjectError("Project #{0} not found".format(project_id), 404) if task_status not in TaskStatus.enums: raise ProjectError('Invalid status') serializer = ProjectSerializer( exclude=['last_task'], strict=True, task_status=task_status) return serializer.dump(project).data
def test_create(self): client = ClientFactory.create() self.db.session.commit() data = ProjectFactory.stub(client=None).__dict__ data['client_id'] = client.id serializer = ProjectSerializer( only=['id', 'name', 'description', 'client_id']) instance, errors = serializer.load(data) assert not errors assert instance.id is None assert instance.name == data['name'] assert instance.client_id == client.id
def test_serialization(self): project = ProjectFactory.create() self.db.session.commit() serializer = ProjectSerializer(strict=True) data = serializer.dump(project).data assert data['id'] == project.id assert data['name'] == project.name assert data['description'] == project.description assert data['last_task'] is None assert not data['tasks'] assert data['client']['id'] == project.client.id task_1 = TaskFactory.create(project=project, status='open') task_2 = TaskFactory.create(project=project, status='closed') self.db.session.commit() # Last task data = serializer.dump(project).data assert data['last_task'] is not None assert data['last_task']['id'] == task_1.id assert data['last_task']['title'] == task_1.title assert 'time_entries' not in data['last_task'] # Tasks (with filtering) data = ProjectSerializer(strict=True, task_status='open').dump(project).data assert len(data['tasks']) == 1 assert data['tasks'][0]['id'] == task_1.id assert data['tasks'][0]['project_id'] == project.id data = ProjectSerializer(strict=True, task_status='closed').dump(project).data assert len(data['tasks']) == 1 assert data['tasks'][0]['id'] == task_2.id assert data['tasks'][0]['project_id'] == project.id
def save(data, project_id=None): data = data or {} if project_id is not None: if not Project.query.get(project_id): raise ProjectError( 'Project #{0} not found'.format(project_id), 404) data['id'] = project_id serializer = ProjectSerializer( only=['id', 'name', 'description', 'client_id']) project, errors = serializer.load(data) if errors: raise ProjectError(errors, 400) project = db.session.merge(project) db.session.commit() serializer = ProjectSerializer( only=['id', 'name', 'description', 'client_id', 'last_task'], strict=True) return serializer.dump(project).data
def test_required(self): serializer = ProjectSerializer( only=['id', 'name', 'description', 'client_id']) instance, errors = serializer.load({}) assert 'name' in errors assert 'client_id' in errors