def test_register_ok(self):
     registry = CacheRegistry()
     registry.register(TestSerializer)
     expected_registry = {
         TestModel: [TestSerializer],
     }
     self.assertEqual(registry._registry, expected_registry)
 def test_register_ok(self):
     registry = CacheRegistry()
     registry.register(TestSerializer)
     expected_registry = {
         TestModel: [TestSerializer],
     }
     self.assertEqual(registry._registry, expected_registry)
 def test_no_collisions(self):
     self.assertNotEqual(
         CacheRegistry.get_cache_key(TestModel(id=1), TestSerializer),
         CacheRegistry.get_cache_key(TestModel(id=2), TestSerializer))
     self.assertNotEqual(
         CacheRegistry.get_cache_key(TestModel(id=1), TestSerializer),
         CacheRegistry.get_cache_key(TestModel(id=1), TestSerializer2))
 def test_ok(self):
     instance = TestModel()
     instance.id = 1000
     serializer = TestSerializer()
     key = CacheRegistry.get_cache_key(instance, serializer.__class__)
     instance2 = TestModel(id=1000)
     serializer2 = TestSerializer()
     k2 = CacheRegistry.get_cache_key(instance2, serializer2.__class__)
     self.assertEqual(key, k2)
 def test_ok(self):
     instance = TestModel(id=1000)
     keys = self.registry.get_all_cache_keys(instance)
     self.assertEqual(len(keys), 2)
     self.assertIn(
         CacheRegistry.get_cache_key(TestModel(id=1000), TestSerializer),
         keys)
     self.assertIn(
         CacheRegistry.get_cache_key(TestModel(id=1000), TestSerializer2),
         keys)
Ejemplo n.º 6
0
    def setUp(self):
        self.registry = CacheRegistry()
        self.registry.register(TestCachedSerializer)
        self.registry.register(TestCachedSerializer2)

        self.instance = instance = TestModel()
        instance.id = 1000
        instance.save()
        self.serializer = TestCachedSerializer(instance)
        self.key = self.registry.get_cache_key(self.instance, TestCachedSerializer)
        self.expected_data = {"id": 1000, "name": ""}
class GetAllCacheKeyTestCase(unittest.TestCase):
    def setUp(self):
        self.registry = CacheRegistry()
        self.registry.register(TestSerializer)
        self.registry.register(TestSerializer2)

    def test_ok(self):
        instance = TestModel(id=1000)
        keys = self.registry.get_all_cache_keys(instance)
        self.assertEqual(len(keys), 2)
        self.assertIn(
            CacheRegistry.get_cache_key(TestModel(id=1000), TestSerializer),
            keys)
        self.assertIn(
            CacheRegistry.get_cache_key(TestModel(id=1000), TestSerializer2),
            keys)
Ejemplo n.º 8
0
class GetCacheKeyTestCase(TestCase):

    def setUp(self):
        self.registry = CacheRegistry()
        self.registry.register(TestCachedSerializer)
        self.registry.register(TestCachedSerializer2)

        self.instance = instance = TestModel()
        instance.id = 1000
        instance.save()
        self.serializer = TestCachedSerializer(instance)
        self.key = self.registry.get_cache_key(self.instance, TestCachedSerializer)
        self.expected_data = {"id": 1000, "name": ""}

    def test_cache_miss(self):
        with mock.patch.object(get_cache(), 'get', return_value=None) as get:
            with mock.patch.object(get_cache(), 'set') as set:
                with self.settings(REST_FRAMEWORK_CACHE={
                    'DEFAULT_CACHE_TIMEOUT': 5,
                }):
                    data = self.serializer.data

        self.assertEqual(data, self.expected_data)
        self.assertTrue(get.called)
        set.assert_called_with(self.key, self.expected_data, 5)

    def test_cache_hit(self):
        with mock.patch.object(get_cache(), 'get', return_value=self.expected_data) as get:
            with mock.patch.object(get_cache(), 'set') as set:
                data = self.serializer.data

        self.assertEqual(data, self.expected_data)
        self.assertTrue(get.called)
        self.assertFalse(set.called)

    def test_cache_clear(self):
        with mock.patch.object(get_cache(), 'get') as get,\
                mock.patch.object(get_cache(), 'set') as set, \
                mock.patch.object(get_cache(), 'delete') as delete:
            data = self.serializer.data

            self.assertEqual(delete.call_count, 0)
            self.instance.delete()
            self.assertEqual(delete.call_count, 2)  # 2 serializers
 def test_alread_registered(self):
     registry = CacheRegistry()
     registry.register(TestSerializer)
     with self.assertRaises(AlreadyRegistered):
         registry.register(TestSerializer)
Ejemplo n.º 10
0
 def _get_cache_key(self, instance):
     return CacheRegistry.get_cache_key(instance, self.__class__)
 def setUp(self):
     self.registry = CacheRegistry()
     self.registry.register(TestSerializer)
     self.registry.register(TestSerializer2)
 def test_alread_registered(self):
     registry = CacheRegistry()
     registry.register(TestSerializer)
     with self.assertRaises(AlreadyRegistered):
         registry.register(TestSerializer)
 def test_empty_registry(self):
     registry = CacheRegistry()
     self.assertEqual(registry._registry, {})