示例#1
0
文件: tests.py 项目: t1waz/rest_utils
    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)
示例#2
0
文件: tests.py 项目: t1waz/rest_utils
    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)
示例#3
0
文件: tests.py 项目: t1waz/rest_utils
    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
示例#4
0
文件: tests.py 项目: t1waz/rest_utils
    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
示例#5
0
文件: tests.py 项目: t1waz/rest_utils
    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'}
示例#6
0
文件: tests.py 项目: t1waz/rest_utils
    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'}
示例#7
0
文件: tests.py 项目: t1waz/rest_utils
    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'}
示例#8
0
文件: tests.py 项目: t1waz/rest_utils
    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'}
示例#9
0
文件: tests.py 项目: t1waz/rest_utils
    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
示例#10
0
文件: tests.py 项目: t1waz/rest_utils
    def test_serializer_is_valid_if_invalid_instance(self):
        class InvalidModel:
            pass

        with self.assertRaises(ValidationError):
            serializer = CorrectSerializerTwo(instance=InvalidModel())

            assert serializer
示例#11
0
文件: tests.py 项目: t1waz/rest_utils
    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
示例#12
0
文件: tests.py 项目: t1waz/rest_utils
    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))
示例#13
0
文件: tests.py 项目: t1waz/rest_utils
    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'
            }
示例#14
0
文件: tests.py 项目: t1waz/rest_utils
 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())
示例#15
0
文件: tests.py 项目: t1waz/rest_utils
    def test_passing_not_dict_data_to_serializer(self):
        with self.assertRaises(ValidationError):
            serializer = CorrectSerializerTwo(data='not dict')

            assert serializer
示例#16
0
文件: tests.py 项目: t1waz/rest_utils
    def test_serializer_has_errors(self):
        serializer = CorrectSerializerTwo()

        assert getattr(serializer, 'errors', False) == {}