Пример #1
0
 def test_related_field_with_no_pk(self):
     """Test that a related field receiving a model object
     with no primary key returns None.
     """
     rel_field = RelatedField(())
     answer = rel_field.to_native(test_models.ChildModel())
     self.assertEqual(answer, None)
Пример #2
0
 def test_related_field_with_no_pk(self):
     """Test that a related field receiving a model object
     with no primary key returns None.
     """
     rel_field = RelatedField(())
     answer = rel_field.to_native(test_models.ChildModel())
     self.assertEqual(answer, None)
Пример #3
0
    def setUp(self):
        # Save my fake models to my test class.
        NormalModel = test_models.NormalModel
        self.nm = test_models.NormalModel
        self.cm = test_models.ChildModel

        # Set up related fields and things.
        self.rel_field = RelatedField(())
        self.rel_field.context = {}
        if hasattr(test_models.NormalModel.objects, 'get_queryset'):
            self.rel_field.queryset = NormalModel.objects.get_queryset()
        else:
            self.rel_field.queryset = NormalModel.objects.get_query_set()
Пример #4
0
    def setUp(self):
        # Save my fake models to my test class.
        NormalModel = test_models.NormalModel
        self.nm = test_models.NormalModel
        self.cm = test_models.ChildModel

        # Set up related fields and things.
        self.rel_field = RelatedField(())
        self.rel_field.context = {}
        if hasattr(test_models.NormalModel.objects, 'get_queryset'):
            self.rel_field.queryset = NormalModel.objects.get_queryset()
        else:
            self.rel_field.queryset = NormalModel.objects.get_query_set()
Пример #5
0
class RelatedFieldTests(unittest.TestCase):
    def setUp(self):
        # Save my fake models to my test class.
        NormalModel = test_models.NormalModel
        self.nm = test_models.NormalModel
        self.cm = test_models.ChildModel

        # Set up related fields and things.
        self.rel_field = RelatedField(())
        self.rel_field.context = {}
        if hasattr(test_models.NormalModel.objects, 'get_queryset'):
            self.rel_field.queryset = NormalModel.objects.get_queryset()
        else:
            self.rel_field.queryset = NormalModel.objects.get_query_set()

    def test_related_field_from_id_dict(self):
        """Test that a related field's `from_native` method, when
        sent a dictionary with an `id` key, returns that ID.
        """
        # Test the case where we get a valid value back.
        with mock.patch.object(self.rel_field.queryset, 'get') as qs:
            qs.return_value = test_models.NormalModel(id=42)
            answer = self.rel_field.from_native({'id': 42})
            qs.assert_called_with(id=42)
        self.assertEqual(answer, qs.return_value)

    def test_related_field_from_with_no_unique(self):
        """Test that a related field's `from_native` method, when
        no unique values are sent, raises ValidationError.
        """
        # Test the case where we get a valid value back.
        with self.assertRaises(ValidationError):
            answer = self.rel_field.from_native({'foo': 3})

    def test_related_field_from_pk_noexist(self):
        """Test that a related field's `from_native` method processes
        a plain ID correctly, and processes DoesNotExist correctly.
        """
        # Test processing when DoesNotExist is raised.
        with mock.patch.object(self.rel_field.queryset, 'get') as m:
            m.side_effect = test_models.NormalModel.DoesNotExist
            with self.assertRaises(ValidationError):
                answer = self.rel_field.from_native(42)

    def test_related_field_from_pk_valueerror(self):
        """Test that a related field's `from_native` method processes
        a plain ID correctly, and processes ValueError correctly.
        """
        # Test processing when DoesNotExist is raised.
        with mock.patch.object(self.rel_field.queryset, 'get') as m:
            m.side_effect = ValueError
            with self.assertRaises(ValidationError):
                answer = self.rel_field.from_native(42)

    def test_related_field_from_unique_key(self):
        """Establish that we can retrieve a relation by a unique key within
        that model.
        """
        with mock.patch.object(self.rel_field.queryset, 'get') as m:
            answer = self.rel_field.from_native({'bacon': 42})
            m.assert_called_once_with(bacon=42)

    def test_related_field_from_composite_unique_keys(self):
        """Establish that we can retrieve a relation by a composite-unique
        set of keys within that model.
        """
        with mock.patch.object(self.rel_field.queryset, 'get') as m:
            answer = self.rel_field.from_native({'bar': 1, 'baz': 2})
            m.assert_called_once_with(bar=1, baz=2)

    def test_related_field_from_no_unique_keys(self):
        """Establish that if we attempt a lookup with no unique keys,
        that the system doesn't even try and raises an error.
        """
        with self.assertRaises(ValidationError):
            answer = self.rel_field.from_native({'foo': []})

    def test_related_field_from_bogus_field(self):
        """Establish that if I attempt to retrieve a related instance based on
        a field that does not exist on the related model, that ValidationError
        is raised.
        """
        with self.assertRaises(ValidationError):
            answer = self.rel_field.from_native({'bogus': None})

    def test_related_field_ignores_api_endpoint(self):
        """Establish that a `from_native` call will ignore serializer fields
        that do not correspond to model fields, such as `api_endpoint`.
        """
        with mock.patch.object(self.rel_field.queryset, 'get') as get:
            answer = self.rel_field.from_native({'api_endpoint': 1, 'baz': 2})
            get.assert_called_once_with(baz=2)

    def test_related_field_multiple_objects(self):
        """Establish that if I send criteria that don't narrow down to
        a single model instance, that ValidationError is raised.
        """
        with mock.patch.object(self.rel_field.queryset, 'get') as m:
            m.side_effect = test_models.NormalModel.MultipleObjectsReturned
            with self.assertRaises(ValidationError):
                answer = self.rel_field.from_native({'bar': 3})
Пример #6
0
class RelatedFieldTests(unittest.TestCase):
    def setUp(self):
        # Save my fake models to my test class.
        NormalModel = test_models.NormalModel
        self.nm = test_models.NormalModel
        self.cm = test_models.ChildModel

        # Set up related fields and things.
        self.rel_field = RelatedField(())
        self.rel_field.context = {}
        if hasattr(test_models.NormalModel.objects, 'get_queryset'):
            self.rel_field.queryset = NormalModel.objects.get_queryset()
        else:
            self.rel_field.queryset = NormalModel.objects.get_query_set()

    def test_related_field_from_id_dict(self):
        """Test that a related field's `from_native` method, when
        sent a dictionary with an `id` key, returns that ID.
        """
        # Test the case where we get a valid value back.
        with mock.patch.object(self.rel_field.queryset, 'get') as qs:
            qs.return_value = test_models.NormalModel(id=42)
            answer = self.rel_field.from_native({'id': 42 })
            qs.assert_called_with(id=42)
        self.assertEqual(answer, qs.return_value)

    def test_related_field_from_with_no_unique(self):
        """Test that a related field's `from_native` method, when
        no unique values are sent, raises ValidationError.
        """
        # Test the case where we get a valid value back.
        with self.assertRaises(ValidationError):
            answer = self.rel_field.from_native({'foo': 3 })

    def test_related_field_from_pk_noexist(self):
        """Test that a related field's `from_native` method processes
        a plain ID correctly, and processes DoesNotExist correctly.
        """
        # Test processing when DoesNotExist is raised.
        with mock.patch.object(self.rel_field.queryset, 'get') as m:
            m.side_effect = test_models.NormalModel.DoesNotExist
            with self.assertRaises(ValidationError):
                answer = self.rel_field.from_native(42)

    def test_related_field_from_pk_valueerror(self):
        """Test that a related field's `from_native` method processes
        a plain ID correctly, and processes ValueError correctly.
        """
        # Test processing when DoesNotExist is raised.
        with mock.patch.object(self.rel_field.queryset, 'get') as m:
            m.side_effect = ValueError
            with self.assertRaises(ValidationError):
                answer = self.rel_field.from_native(42)

    def test_related_field_from_unique_key(self):
        """Establish that we can retrieve a relation by a unique key within
        that model.
        """
        with mock.patch.object(self.rel_field.queryset, 'get') as m:
            answer = self.rel_field.from_native({'bacon': 42})
            m.assert_called_once_with(bacon=42)

    def test_related_field_from_composite_unique_keys(self):
        """Establish that we can retrieve a relation by a composite-unique
        set of keys within that model.
        """
        with mock.patch.object(self.rel_field.queryset, 'get') as m:
            answer = self.rel_field.from_native({'bar': 1, 'baz': 2})
            m.assert_called_once_with(bar=1, baz=2)

    def test_related_field_from_no_unique_keys(self):
        """Establish that if we attempt a lookup with no unique keys,
        that the system doesn't even try and raises an error.
        """
        with self.assertRaises(ValidationError):
            answer = self.rel_field.from_native({'foo': []})

    def test_related_field_from_bogus_field(self):
        """Establish that if I attempt to retrieve a related instance based on
        a field that does not exist on the related model, that ValidationError
        is raised.
        """
        with self.assertRaises(ValidationError):
            answer = self.rel_field.from_native({'bogus': None})

    def test_related_field_ignores_api_endpoint(self):
        """Establish that a `from_native` call will ignore serializer fields
        that do not correspond to model fields, such as `api_endpoint`.
        """
        with mock.patch.object(self.rel_field.queryset, 'get') as get:
            answer = self.rel_field.from_native({'api_endpoint': 1, 'baz': 2})
            get.assert_called_once_with(baz=2)

    def test_related_field_multiple_objects(self):
        """Establish that if I send criteria that don't narrow down to
        a single model instance, that ValidationError is raised.
        """
        with mock.patch.object(self.rel_field.queryset, 'get') as m:
            m.side_effect = test_models.NormalModel.MultipleObjectsReturned
            with self.assertRaises(ValidationError):
                answer = self.rel_field.from_native({'bar': 3})