def test_serializer_save(self): with DBHandler(): input_data = { 'name': 'correct name', 'number': 55, 'data': 'some data to store second', 'sample_model': 'model_1', } serializer = CorrectSerializerTwo(data=input_data) is_valid = asyncio.get_event_loop().run_until_complete( serializer.is_valid()) assert is_valid is True assert not serializer.errors instance = asyncio.get_event_loop().run_until_complete( serializer.save()) assert isinstance(instance, SampleModelChild) for name, value in serializer.validated_data.items(): assert getattr(instance, name) == value dict_instance = asyncio.get_event_loop().run_until_complete( serializer.save(to_dict=True)) assert isinstance(dict_instance, dict) assert all(attr in dict_instance for attr in input_data)
def test_serializer_get_dict_for_valid_data(self): with DBHandler(): input_data = { 'name': 'correct name', 'number': 55, 'data': 'some data to store second', 'sample_model': 'model_1', } serializer = CorrectSerializerTwo(data=input_data) is_valid = asyncio.get_event_loop().run_until_complete( serializer.is_valid()) assert is_valid is True assert not serializer.errors instance = asyncio.get_event_loop().run_until_complete( serializer.save()) assert instance dict_instance = asyncio.get_event_loop().run_until_complete( serializer.to_dict()) assert isinstance(dict_instance, dict) assert 'id' in dict_instance assert all(attr in dict_instance for attr in input_data)
def test_serializer_is_valid_if_no_data(self): with DBHandler(): serializer = CorrectSerializerTwo(data={}) with self.assertRaises(ValidationError): is_valid = asyncio.get_event_loop().run_until_complete( serializer.is_valid()) assert is_valid
def test_serializer_cannot_set_not_tortoise_model_instance(self): with self.assertRaises(ValidationError): serializer = CorrectSerializerTwo(instance=SampleModel()) assert serializer with self.assertRaises(ValidationError): serializer = CorrectSerializerTwo(instance=IncorrectModel()) assert serializer
def test_check_input_data_if_missing_input(self): input_data = { 'name': 'test_name', 'data': 'some data to store', 'sample_model': 'sample_model', } serializer = CorrectSerializerTwo(data=input_data) serializer._check_input_data_for_missing_values() assert serializer.errors == {'number': 'missing in input'}
def test_check_input_data_if_serialized_method_in_input(self): input_data = { 'name': 'test_name', 'number': 1, 'ser_test': 'this field should not be included, its method field', 'data': 'some data to store', 'sample_model': 'sample_model', } serializer = CorrectSerializerTwo(data=input_data) serializer._check_input_data_for_read_only_values() assert serializer.errors == {'ser_test': 'field is read only'}
def test_check_input_data_if_read_only_in_input(self): input_data = { 'name': 'test_name', 'number': 1, 'created': '1990-01-01', 'data': 'some data to store', 'sample_model': 'sample_model', } serializer = CorrectSerializerTwo(data=input_data) serializer._check_input_data_for_read_only_values() assert serializer.errors == {'created': 'field is read only'}
def test_check_input_data_if_pk_in_input(self): input_data = { 'id': 23, 'name': 'aaaa', 'number': 1, 'data': 'some data to store', 'sample_model': 'sample_model', } serializer = CorrectSerializerTwo(data=input_data) serializer._check_input_data_for_primary_key() assert serializer.errors == {'id': 'primary key, cannot be in input'}
def test_serializer_is_valid_for_valid_data(self): with DBHandler(): input_data = { 'name': 'correct name', 'number': 23, 'data': 'some data to store', 'sample_model': 'model_1', } serializer = CorrectSerializerTwo(data=input_data) is_valid = asyncio.get_event_loop().run_until_complete( serializer.is_valid()) assert is_valid is True assert not serializer.errors
def test_serializer_is_valid_if_invalid_instance(self): class InvalidModel: pass with self.assertRaises(ValidationError): serializer = CorrectSerializerTwo(instance=InvalidModel()) assert serializer
def test_serializer_save_invalid_data(self): with DBHandler(): input_data = { 'name': 'test name', 'number': 'aaa', 'created': 'aaaa', 'data': 'some data to store', 'sample_model': 'name number 1', } serializer = CorrectSerializerTwo(data=input_data) is_valid = asyncio.get_event_loop().run_until_complete( serializer.is_valid()) assert not is_valid with self.assertRaises(ValidationError): instance = asyncio.get_event_loop().run_until_complete( serializer.save()) assert instance serializer = CorrectSerializerTwo(data={}) with self.assertRaises(ValidationError): instance = asyncio.get_event_loop().run_until_complete( serializer.save()) assert instance
def test_serializer_fields_initialization(self): correct_serializer = CorrectSerializerTwo() another_serializer = CorrectSerializerTwo() correct_serializer_fields = { 'id': StringField, 'name': StringField, 'number': IntegerField, 'created': DateTimeField, 'data': BinaryField, 'sample_model': SlugRelatedField, 'ser_test': MethodField } assert hasattr(correct_serializer, 'fields') for field_name, field in correct_serializer.fields.items(): assert isinstance(field, correct_serializer_fields.get(field_name)) assert id(field) == id(another_serializer.fields.get(field_name))
def test_serializer_is_valid_for_invalid_data(self): with DBHandler(): input_data = { 'name': 'test name', 'number': 'aaa', 'created': 'aaaa', 'data': 'some data to store', 'sample_model': 'name number 1', } serializer = CorrectSerializerTwo(data=input_data) is_valid = asyncio.get_event_loop().run_until_complete( serializer.is_valid()) assert not is_valid assert serializer.errors == {'created': 'field is read only'} input_data.pop('created') serializer = CorrectSerializerTwo(data=input_data) is_valid = asyncio.get_event_loop().run_until_complete( serializer.is_valid()) assert not is_valid assert serializer.errors == { 'number': 'incorrect value, cannot transform to integer', 'sample_model': 'name number 1 does not exists' }
def test_serializer_get_dict_without_is_valid(self): with self.assertRaises(ValidationError): serializer = CorrectSerializerTwo(data={'data': {'some_data'}}) asyncio.get_event_loop().run_until_complete(serializer.to_dict())
def test_passing_not_dict_data_to_serializer(self): with self.assertRaises(ValidationError): serializer = CorrectSerializerTwo(data='not dict') assert serializer
def test_serializer_has_errors(self): serializer = CorrectSerializerTwo() assert getattr(serializer, 'errors', False) == {}