def wrapper(self, value: t.Any): try: return to_serializable(value) except TypeError: # Should just raise another typerror - # but it is the recomended way of using stdlib.json's encoder return func(value)
def test_get_item(self, app, obj_payload): """Test get a item.""" payload = obj_payload obj_id = payload['id'] request = app.get('{base}/{id}'.format(base=self.base_path, id=obj_id), headers=self.headers, status=200) result = request.json db_obj = self.model.query().get(obj_id) for key, value in db_obj.to_dict().items(): if key not in self.ignore_validation_fields: if isinstance( value, (date, datetime, uuid.UUID, enum.Enum, PhoneNumber)): value = to_serializable(value) elif key in self.serialize_attrs: value = json.loads( json.dumps(value, default=to_serializable)) result_value = result.get(key) if isinstance(value, list): for item in result_value: assert item in value else: assert result_value == value # state can be automatic changed by after_insert event listener assert self.initial_wf_state == getattr(db_obj, 'state')
def test_versions_get_item(self, app, obj_payload): """Test get a item.""" payload = obj_payload obj_id = payload['id'] # Get original version request = app.get('{base}/{id}/versions/0'.format(base=self.base_path, id=obj_id), headers=self.headers, status=200) result = request.json db_obj = self.model.query().get(obj_id) obj_field = self.check_versions_field result_field = obj_field[1:] if obj_field.startswith( '_') else obj_field version = db_obj.versions[0] assert to_serializable(getattr(version, obj_field)) == result[result_field] version = db_obj.versions[1] assert to_serializable(getattr(version, obj_field)) != result[result_field]
def test_successful_creation(self, obj_payload, app): """Test successful creation of a new model.""" payload = obj_payload request = app.post_json(self.base_path, payload, headers=self.headers, status=200) assert 'application/json' == request.content_type result = request.json db_obj = self.model.get(payload['id']) self.ignore_validation_fields.extend(self.model.__actors__) # validate response payload against sent payload for key, value in payload.items(): if key not in self.ignore_validation_fields: result_value = result.get(key) if key in self.serialize_attrs: value = json.loads( json.dumps(value, default=to_serializable)) assert result_value == value elif isinstance(value, list): for item in result_value: assert item in value else: assert result_value == value # state can be automatic changed by after_insert event listener assert self.initial_wf_state == result.get('state') # validate database model data against sent payload for key, value in payload.items(): if key not in self.ignore_validation_fields: obj_value = getattr(db_obj, key) if isinstance( obj_value, (date, datetime, uuid.UUID, enum.Enum, PhoneNumber)): obj_value = to_serializable(obj_value) if isinstance(value, list): for item in obj_value: assert item in value elif key in self.serialize_attrs: obj_value = json.loads( json.dumps(value, default=to_serializable)) assert obj_value == value else: assert obj_value == value # state can be automatic changed by after_insert event listener assert self.initial_wf_state == getattr(db_obj, 'state')
def test_successful_update(self, obj_payload, app): """Teste put Data to existing object.""" payload = self.update_map obj_id = obj_payload['id'] request = app.put_json(f'{self.base_path}/{obj_id}', payload, headers=self.headers, status=200) result = request.json assert 'application/json' == request.content_type # validate response payload against sent payload for key, value in payload.items(): if key not in self.ignore_validation_fields: assert result.get(key) == value # state can be automatic changed by after_insert event listener assert self.initial_wf_state == result.get('state') updated_obj = self.model.get(obj_id) for key, value in payload.items(): obj_value = getattr(updated_obj, key) if isinstance(obj_value, (date, datetime, uuid.UUID, enum.Enum)): obj_value = to_serializable(obj_value) assert obj_value == value elif isinstance(obj_value, list): for item in obj_value: assert item in value elif key in self.serialize_attrs: obj_value = json.loads( json.dumps(obj_value, default=to_serializable)) assert obj_value == value else: assert obj_value == value # state can be automatic changed by after_insert event listener assert self.initial_wf_state == getattr(updated_obj, 'state')