def test_subselect_support(self): """Test that subselects are handled properly.""" from django import db db.reset_queries() from testapp.models import Book, Person, PersonType author_types = PersonType.objects.filter(title='Author') author_people = Person.objects.filter(person_types__in=author_types) written_books = Book.objects.filter(authors__in=author_people) q = base.message_queue() self.failUnless(len(db.connection.queries) == 0) count = written_books.count() self.failUnless(q.get() == False) # execute the query again, this time it's cached self.failUnless(written_books.count() == count) self.failUnless(q.get() == True) # change the person type of 'Author' to something else pt = PersonType.objects.get(title='Author') pt.title = 'NonAuthor' pt.save() self.failUnless(PersonType.objects.filter(title='Author').count() == 0) q.clear() db.reset_queries() # now execute the same query; the result should be diff and it should be # a cache miss new_count = written_books.count() self.failUnless(new_count != count) self.failUnless(q.get() == False) PersonType.objects.filter(title='NonAuthor').order_by('-title')[:5]
def test_queries_from_templates(self): """Verify that doing the same request w/o a db write twice *does* populate the cache properly.""" connection.queries = [] q = base.message_queue() response = self.client.get("/test/template_queries") self.failUnless(q.get() is False) response = self.client.get("/test/template_queries") self.failUnless(q.get() is True)
def test_queries_from_templates(self): """Verify that doing the same request w/o a db write twice does not populate the cache properly.""" # it seems that django 1.3 doesn't exhibit this bug! if django.VERSION[:2] >= (1, 3): return connection.queries = [] q = base.message_queue() response = self.client.get("/test/template_queries") self.failUnless(q.get() is False) response = self.client.get("/test/template_queries") self.failUnless(q.get() is False)
def test_basic_blacklist(self): from johnny import cache, settings from testapp.models import Genre, Book q = base.message_queue() old = johnny_settings.BLACKLIST johnny_settings.BLACKLIST = set(['testapp_genre']) connection.queries = [] Book.objects.get(id=1) Book.objects.get(id=1) self.failUnless((False, True) == (q.get_nowait(), q.get_nowait())) list(Genre.objects.all()) list(Genre.objects.all()) self.failUnless(not any((q.get_nowait(), q.get_nowait()))) johnny_settings.BLACKLIST = old
def test_invalidate(self): """Test for the module-level invalidation function.""" from Queue import Queue as queue from testapp.models import Book, Genre, Publisher from johnny.cache import invalidate q = base.message_queue() b = Book.objects.get(id=1) invalidate(Book) b = Book.objects.get(id=1) first, second = q.get_nowait(), q.get_nowait() self.failUnless(first == second == False) g = Genre.objects.get(id=1) p = Publisher.objects.get(id=1) invalidate('testapp_genre', Publisher) g = Genre.objects.get(id=1) p = Publisher.objects.get(id=1) fg,fp,sg,sp = [q.get() for i in range(4)] self.failUnless(fg == fp == sg == sp == False)
def test_delete(self): """Test that a database delete clears a table cache.""" from testapp.models import Genre g1 = Genre.objects.get(pk=1) begin = Genre.objects.all().count() g1.delete() self.assertRaises(Genre.DoesNotExist, lambda: Genre.objects.get(pk=1)) connection.queries = [] self.failUnless(Genre.objects.all().count() == (begin -1)) self.failUnless(len(connection.queries) == 1) Genre(title='Science Fiction', slug='scifi').save() Genre(title='Fantasy', slug='rubbish').save() Genre(title='Science Fact', slug='scifact').save() count = Genre.objects.count() Genre.objects.get(title='Fantasy') q = base.message_queue() Genre.objects.filter(title__startswith='Science').delete() # this should not be cached Genre.objects.get(title='Fantasy') self.failUnless(not q.get_nowait())