예제 #1
0
    def test_cache(self):
        album = cache.get_model(Album,
                                artist="Taylor Swift",
                                title="Taylor Swift")
        self.assertEqual(album.artist, "Taylor Swift")

        try:
            album = cache.get_model(Album, artist="Tay-Tay")
        except Album.DoesNotExist:
            album = cache.get_model(Album, cache_exc=True, artist="Tay-Tay")

        self.assertEqual(album, None)

        Album(artist="Tay-Tay", title="1989").save()

        result_model = cache.get_model(Album, cache_exc=True, artist="Tay-Tay")
        self.assertEqual(result_model.artist, "Tay-Tay")

        albums = cache.get_model_list(Album, artist="Taylor Swift")
        self.assertEqual(len(albums), self.num_albums)

        albums = cache.get_models(Album, self.album_ids)
        self.assertEqual(len(albums), len(self.album_ids))

        album = Album.objects.create(artist="Tay-Tay", title="Red")
        cache.set_model(album)
        result_album = cache.get_model(Album, pk=album.pk)
        self.assertEqual(album.pk, result_album.pk)

        cache.set_model_list(Album, artist="Tay-Tay")
        num_albums = Album.objects.filter(artist="Tay-Tay").count()
        albums_tay = cache.get_model_list(Album, artist="Tay-Tay")
        self.assertEqual(len(albums_tay), num_albums)
        cache.clear_models(Album, 'artist', ["Tay-Tay"])
예제 #2
0
    def test_cache(self):
        album = cache.get_model(Album, artist="Taylor Swift",
                                title="Taylor Swift")
        self.assertEqual(album.artist, "Taylor Swift")

        try:
            album = cache.get_model(Album, artist="Tay-Tay")
        except Album.DoesNotExist:
            album = cache.get_model(Album, cache_exc=True, artist="Tay-Tay")

        self.assertEqual(album, None)

        Album(artist="Tay-Tay", title="1989").save()

        result_model = cache.get_model(Album, cache_exc=True, artist="Tay-Tay")
        self.assertEqual(result_model.artist, "Tay-Tay")

        albums = cache.get_model_list(Album, artist="Taylor Swift")
        self.assertEqual(len(albums), self.num_albums)

        albums = cache.get_models(Album, self.album_ids)
        self.assertEqual(len(albums), len(self.album_ids))

        album = Album.objects.create(artist="Tay-Tay", title="Red")
        cache.set_model(album)
        result_album = cache.get_model(Album, pk=album.pk)
        self.assertEqual(album.pk, result_album.pk)

        cache.set_model_list(Album, artist="Tay-Tay")
        num_albums = Album.objects.filter(artist="Tay-Tay").count()
        albums_tay = cache.get_model_list(Album, artist="Tay-Tay")
        self.assertEqual(len(albums_tay), num_albums)
        cache.clear_models(Album, 'artist', ["Tay-Tay"])
예제 #3
0
    def test_add_model_field(self):
        album = Album.objects.get(pk=1)
        result = {
            f.attname: getattr(album, f.attname) for f in album._meta.fields
        }
        result['another_field'] = '1'

        key = cache_keys.key_of_model(Album, pk=1)
        cache.set(key, result)
        key = cache_keys.key_of_model_list(Album, artist="Taylor Swift")
        cache.set(key, [result])

        album = cache.get_model(Album, pk=1)
        self.assertRaises(AttributeError, lambda: album.another_field)
예제 #4
0
    def test_add_model_field(self):
        album = Album.objects.get(pk=1)
        result = {
            f.attname: getattr(album, f.attname)
            for f in album._meta.fields
        }
        result['another_field'] = '1'

        key = cache_keys.key_of_model(Album, pk=1)
        cache.set(key, result)
        key = cache_keys.key_of_model_list(Album, artist="Taylor Swift")
        cache.set(key, [result])

        album = cache.get_model(Album, pk=1)
        self.assertRaises(AttributeError, lambda: album.another_field)
 def _cached_FIELD(self, field):
     value = getattr(self, field.attname)
     Model = field.related_model
     return cache.get_model(Model, pk=value)
 def _cached_FIELD(self, field, cache_exc):
     value = getattr(self, field.attname)
     Model = field.related_model
     return cache.get_model(Model, pk=value, cache_exc=cache_exc)