Ejemplo n.º 1
0
    def test_create_for_incorrect_data(self):
        with DBHandler():
            response = asyncio.get_event_loop().run_until_complete(
                self.sample_model_view.create(FakeRequest(data=b'/x///')))
            response_data = json.loads(response.body.decode())

            assert response_data == {'detail': 'invalid request for create.'}
Ejemplo n.º 2
0
    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)
Ejemplo n.º 3
0
    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)
Ejemplo n.º 4
0
    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
Ejemplo n.º 5
0
    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'
            }
Ejemplo n.º 6
0
    def test_create_for_invalid_url(self):
        with DBHandler():
            response = asyncio.get_event_loop().run_until_complete(
                self.sample_model_view.create(
                    FakeRequest(url_params={'id': 'invalid id'})))
            response_data = json.loads(response.body.decode())

            assert response_data == {'detail': 'Method POST not allowed.'}
Ejemplo n.º 7
0
    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
Ejemplo n.º 8
0
    def test_get_incorrect_instance(self):
        with DBHandler():
            response = asyncio.get_event_loop().run_until_complete(
                self.sample_model_view.instance(
                    FakeRequest(url_params={'id': 'invalid id'})))
            response_data = json.loads(response.body.decode())

            assert response_data == {'detail': 'not found'}
Ejemplo n.º 9
0
    def test_update_for_incorrect_data(self):
        with DBHandler():
            sample_model = asyncio.get_event_loop().run_until_complete(
                SampleModel.first())
            response = asyncio.get_event_loop().run_until_complete(
                self.sample_model_view.update(
                    FakeRequest(url_params={'id': sample_model.id},
                                data=b'/x///')))
            response_data = json.loads(response.body.decode())

            assert response_data == {'detail': 'invalid request for update'}
Ejemplo n.º 10
0
    def test_create_for_invalid_data(self):
        with DBHandler():
            response = asyncio.get_event_loop().run_until_complete(
                self.sample_model_view.create(
                    FakeRequest(data={'name': [1, 2, 3]})))
            response_data = json.loads(response.body.decode())

            assert response_data == {
                'detail': {
                    'name': 'incorrect value, cannot transform to string'
                }
            }
Ejemplo n.º 11
0
    def test_delete(self):
        with DBHandler():
            sample_model = asyncio.get_event_loop().run_until_complete(
                SampleModel.first())
            response = asyncio.get_event_loop().run_until_complete(
                self.sample_model_view.delete(
                    FakeRequest(url_params={'id': sample_model.id})))
            response_data = json.loads(response.body.decode())

            assert response_data == {'deleted': True}
            assert asyncio.get_event_loop().run_until_complete(
                SampleModel.filter(id=sample_model.id)) == []
Ejemplo n.º 12
0
    def test_get_correct_instance(self):
        with DBHandler():
            sample_model = asyncio.get_event_loop().run_until_complete(
                SampleModel.first())
            response = asyncio.get_event_loop().run_until_complete(
                self.sample_model_view.instance(
                    FakeRequest(url_params={'id': sample_model.id})))
            response_data = json.loads(response.body.decode())

            assert {
                'id': sample_model.id,
                'name': sample_model.name
            } == response_data
Ejemplo n.º 13
0
    def test_get_list(self):
        with DBHandler():
            response = asyncio.get_event_loop().run_until_complete(
                self.sample_model_view.list(FakeRequest()))
            response_data = json.loads(response.body.decode())
            sample_models = asyncio.get_event_loop().run_until_complete(
                SampleModel.all())
            models_dicts = [{
                'id': sample_model.id,
                'name': sample_model.name
            } for sample_model in sample_models]

            assert models_dicts == response_data
Ejemplo n.º 14
0
    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
Ejemplo n.º 15
0
    def test_create(self):
        with DBHandler():
            create_data = {'name': 'correct name'}
            response = asyncio.get_event_loop().run_until_complete(
                self.sample_model_view.create(
                    FakeRequest(data={'name': 'correct name'})))
            response_data = json.loads(response.body.decode())

            new_instance = asyncio.get_event_loop().run_until_complete(
                SampleModel.get(**create_data))

            assert {
                'id': new_instance.id,
                'name': new_instance.name
            } == response_data
Ejemplo n.º 16
0
    def test_incorrect_value_for_datetime_field(self):
        with DBHandler():
            input_data = {
                'name': 'test_name',
                'number': 434,
                'data': 'another data to store',
                'sample_model': 'model_1',
                'created': "not valid datetime value"
            }

            serializer = CorrectSerializerFive(data=input_data)
            is_valid = asyncio.get_event_loop().run_until_complete(
                serializer.is_valid())

            assert is_valid is False
            assert 'created' in serializer.errors
Ejemplo n.º 17
0
    def test_save_serializer_slug_related_field_many(self):
        with DBHandler():
            input_data = {
                'name': 'group_3',
                'sample_models': ['model_2', 'model_3'],
            }
            serializer = CorrectSerializerFour(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
Ejemplo n.º 18
0
    def test_incorrect_value_for_binary_field(self):
        with DBHandler():
            input_data = {
                'name': 'test_name',
                'number': 434,
                'data': 1.0,
                'sample_model': 'model_1',
                'created': '1990-01-01 11:01:01',
            }

            serializer = CorrectSerializerFive(data=input_data)
            is_valid = asyncio.get_event_loop().run_until_complete(
                serializer.is_valid())

            assert is_valid is False
            assert 'data' in serializer.errors
Ejemplo n.º 19
0
    def test_update(self):
        with DBHandler():
            update_data = {'name': 'updated name'}
            sample_model = asyncio.get_event_loop().run_until_complete(
                SampleModel.first())
            response = asyncio.get_event_loop().run_until_complete(
                self.sample_model_view.update(
                    FakeRequest(url_params={'id': sample_model.id},
                                data=update_data)))
            response_data = json.loads(response.body.decode())
            sample_model = asyncio.get_event_loop().run_until_complete(
                SampleModel.get(**update_data))

            assert response_data == {
                'id': sample_model.id,
                'name': sample_model.name
            }
Ejemplo n.º 20
0
    def test_get_serializer_slug_related_field_many(self):
        with DBHandler():
            sample_model_groups = asyncio.get_event_loop().run_until_complete(
                SampleModelGroups.all())

            for sample_model_group in sample_model_groups:
                serializer = CorrectSerializerFour(instance=sample_model_group)
                serialized_object = asyncio.get_event_loop(
                ).run_until_complete(serializer.to_dict())

                assert serialized_object['id'] == sample_model_group.id
                assert serialized_object['name'] == sample_model_group.name

                sample_models = asyncio.get_event_loop().run_until_complete(
                    sample_model_group.sample_models.all().values_list(
                        'name', flat=True))
                assert serialized_object['sample_models'] == sample_models