Exemplo n.º 1
0
    def test_root_serializer_cycle_busting(self):
        s = CatSerializer(
            request_fields={'home': {}, 'backup_home': {}}
        )

        s.parent = s  # Create cycle.

        self.assertIsNone(s.fields['home'].root_serializer)
Exemplo n.º 2
0
    def test_root_serializer_cycle_busting(self):
        s = CatSerializer(
            request_fields={'home': {}, 'backup_home': {}}
        )

        s.parent = s  # Create cycle.

        self.assertIsNone(s.fields['home'].root_serializer)
Exemplo n.º 3
0
    def test_serializer_propagation_consistency(self):
        s = CatSerializer(request_fields={'home': True})

        # In version <= 1.3.7 these will have returned different values.
        r1 = s.get_all_fields()['home'].serializer.id_only()
        r2 = s.fields['home'].serializer.id_only()
        r3 = s.get_all_fields()['home'].serializer.id_only()
        self.assertEqual(r1, r2)
        self.assertEqual(r2, r3)
Exemplo n.º 4
0
    def test_serializer_propagation_consistency(self):
        s = CatSerializer(
            request_fields={'home': True}
        )

        # In version <= 1.3.7 these will have returned different values.
        r1 = s.get_all_fields()['home'].serializer.id_only()
        r2 = s.fields['home'].serializer.id_only()
        r3 = s.get_all_fields()['home'].serializer.id_only()
        self.assertEqual(r1, r2)
        self.assertEqual(r2, r3)
Exemplo n.º 5
0
    def test_different_roots(self):
        serializer2 = CatSerializer(request_fields={
            'home': {},
            'backup_home': {}
        })

        home1 = self.serializer.fields['home']
        home2 = serializer2.fields['home']

        self.assertIsNot(
            home1.serializer, home2.serializer,
            'Different root serializers should yield different instances.')
Exemplo n.º 6
0
    def test_root_serializer_trickledown_request_fields(self):
        s = CatSerializer(request_fields=True)

        self.assertIsNotNone(s.get_all_fields()['home'].serializer)
Exemplo n.º 7
0
 def setUp(self):
     self.serializer = CatSerializer(request_fields={
         'home': {},
         'backup_home': True
     })
Exemplo n.º 8
0
class TestSerializerCaching(TestCase):
    def setUp(self):
        self.serializer = CatSerializer(request_fields={
            'home': {},
            'backup_home': True
        })

    def test_get_all_fields(self):
        all_fields = self.serializer.get_all_fields()

        # These are two different instances of the field object
        # because get_all_fields() does a copy().
        home_field_1 = self.serializer.fields['home']
        home_field_2 = all_fields['home']
        '''
        # Expected with fields cache
        self.assertNotEqual(
            home_field_1,
            home_field_2,
            'Expected different field instances, got same.'
        )
        '''

        self.assertEqual(home_field_1.serializer, home_field_2.serializer,
                         'Expected same serializer instance, got different.')

    def test_serializer_args_busts_cache(self):
        home_field = self.serializer.fields['home']

        self.assertIsNot(home_field.get_serializer(),
                         home_field.get_serializer('foo'),
                         ('Passing arg to get_serializer should construct new'
                          ' serializer. Instead got same one.'))

    def test_same_serializer_class_different_fields(self):
        # These two use the same serializer class, but are different
        # fields, so they should use different serializer instances.
        home_field = self.serializer.fields['home']
        backup_home_field = self.serializer.fields['backup_home']

        self.assertIsNot(
            home_field.serializer, backup_home_field.serializer,
            ('Different fields that use same serializer should get',
             ' separate serializer instances.'))

    def test_different_roots(self):
        serializer2 = CatSerializer(request_fields={
            'home': {},
            'backup_home': {}
        })

        home1 = self.serializer.fields['home']
        home2 = serializer2.fields['home']

        self.assertIsNot(
            home1.serializer, home2.serializer,
            'Different root serializers should yield different instances.')

    @unittest.skip(
        "skipping because DRF's Field.root doesn't have cycle-detection.")
    def test_root_serializer_cycle_busting(self):
        s = CatSerializer(request_fields={'home': {}, 'backup_home': {}})

        s.parent = s  # Create cycle.

        self.assertIsNone(s.fields['home'].root_serializer)

    def test_root_serializer_trickledown_request_fields(self):
        s = CatSerializer(request_fields=True)

        self.assertIsNotNone(s.get_all_fields()['home'].serializer)

    def test_recursive_serializer(self):
        s = LocationSerializer(
            request_fields={'cats': {
                'parent': {
                    'parent': True
                }
            }})

        cats_field = s.get_all_fields()['cats']

        l1 = cats_field.serializer.child  # .child because list
        l2 = l1.get_all_fields()['parent'].serializer
        l3 = l2.get_all_fields()['parent'].serializer
        l4 = l3.get_all_fields()['parent'].serializer
        self.assertIsNot(l2, l3)

        # l3 and l4 should be same cached instance because both have
        # request_fields=True (l3 by inheritence, l4 by default)
        self.assertIs(l3, l4)
Exemplo n.º 9
0
 def test_get_canonical_path_with_keyspace(self):
     rsrc_key = CatSerializer().get_resource_key()
     self.assertEqual('/v2/cats',
                      DynamicRouter.get_canonical_path(rsrc_key))
Exemplo n.º 10
0
    def test_root_serializer_trickledown_request_fields(self):
        s = CatSerializer(
            request_fields=True
        )

        self.assertIsNotNone(s.get_all_fields()['home'].serializer)
Exemplo n.º 11
0
 def setUp(self):
     self.serializer = CatSerializer(
         request_fields={'home': {}, 'backup_home': True}
     )
Exemplo n.º 12
0
class TestSerializerCaching(TestCase):

    def setUp(self):
        self.serializer = CatSerializer(
            request_fields={'home': {}, 'backup_home': True}
        )

    def test_get_all_fields(self):
        all_fields = self.serializer.get_all_fields()

        # These are two different instances of the field object
        # because get_all_fields() does a copy().
        home_field_1 = self.serializer.fields['home']
        home_field_2 = all_fields['home']

        self.assertNotEqual(
            home_field_1,
            home_field_2,
            'Expected different field instances, got same.'
        )

        self.assertEqual(
            home_field_1.serializer,
            home_field_2.serializer,
            'Expected same serializer instance, got different.'
        )

    def test_serializer_args_busts_cache(self):
        home_field = self.serializer.fields['home']

        self.assertIsNot(
            home_field.get_serializer(),
            home_field.get_serializer('foo'),
            (
                'Passing arg to get_serializer should construct new'
                ' serializer. Instead got same one.'
            )
        )

    def test_same_serializer_class_different_fields(self):
        # These two use the same serializer class, but are different
        # fields, so they should use different serializer instances.
        home_field = self.serializer.fields['home']
        backup_home_field = self.serializer.fields['backup_home']

        self.assertIsNot(
            home_field.serializer,
            backup_home_field.serializer,
            (
                'Different fields that use same serializer should get',
                ' separate serializer instances.'
            )
        )

    def test_different_roots(self):
        serializer2 = CatSerializer(
            request_fields={'home': {}, 'backup_home': {}}
        )

        home1 = self.serializer.fields['home']
        home2 = serializer2.fields['home']

        self.assertIsNot(
            home1.serializer,
            home2.serializer,
            'Different root serializers should yield different instances.'
        )

    def test_root_serializer_cycle_busting(self):
        s = CatSerializer(
            request_fields={'home': {}, 'backup_home': {}}
        )

        s.parent = s  # Create cycle.

        self.assertIsNone(s.fields['home'].root_serializer)

    def test_root_serializer_trickledown_request_fields(self):
        s = CatSerializer(
            request_fields=True
        )

        self.assertIsNotNone(s.get_all_fields()['home'].serializer)

    def test_recursive_serializer(self):
        s = LocationSerializer(
            request_fields={
                'cats': {
                    'parent': {
                        'parent': True
                    }
                }
            }
        )

        cats_field = s.get_all_fields()['cats']

        l1 = cats_field.serializer.child  # .child because list
        l2 = l1.get_all_fields()['parent'].serializer
        l3 = l2.get_all_fields()['parent'].serializer
        l4 = l3.get_all_fields()['parent'].serializer
        self.assertIsNot(l2, l3)

        # l3 and l4 should be same cached instance because both have
        # request_fields=True (l3 by inheritence, l4 by default)
        self.assertIs(l3, l4)