Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

params clash bug #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions johnny/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,15 @@ def sql_key(self, generation, sql, params, order, result_type,
pieces of the query and the generation key.
"""
# these keys will always look pretty opaque

# parameters have to be interleaved using simple separator
# for details, see ParamsClashBug test case
def expand_params(params):
for p in params:
yield p
yield "S"

params = list(expand_params(params))
suffix = self.keygen.gen_key(sql, params, order, result_type)
using = settings.DB_CACHE_KEYS[using]
return '%s_%s_query_%s.%s' % (self.prefix, using, generation, suffix)
Expand Down
29 changes: 28 additions & 1 deletion johnny/tests/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def any(iterable):
return False

# put tests in here to be included in the testing suite
__all__ = ['MultiDbTest', 'SingleModelTest', 'MultiModelTest', 'TransactionSupportTest', 'BlackListTest', 'TransactionManagerTestCase']
__all__ = ['MultiDbTest', 'SingleModelTest', 'MultiModelTest', 'TransactionSupportTest', 'BlackListTest', 'TransactionManagerTestCase', 'ParamsClashBug']

def _pre_setup(self):
self.saved_DISABLE_SETTING = getattr(johnny_settings, 'DISABLE_QUERYSET_CACHE', False)
Expand Down Expand Up @@ -964,3 +964,30 @@ def test_savepoint_localstore_flush(self):
tm._commit_all_savepoints()
# And this checks if it actually happened.
self.failUnless(table_key in tm.local)

class ParamsClashBug(QueryCacheBase):
fixtures = base.johnny_fixtures
def test_params(self):

from testapp.models import PersonType, Person
for i in range(2, 22):
pt, _ = PersonType.objects.get_or_create(title='title %s' % i, slug='slug_%s' % i)
p = Person()
p.first_name = 'first name'
p.last_name = 'last name'
p.slug = 'person_%s' % i
p.gender = 1
p.save()
p.person_types.add(pt)

person_A = Person.objects.get(id = 1)
person_B = Person.objects.get(id = 12)
person_A.person_types.add(PersonType.objects.get(id = 21))
person_B.person_types.add(PersonType.objects.get(id = 1))

qset = person_A.person_types.through._default_manager.values_list('persontype', flat=True)
qset_A = qset.filter(person = 1, persontype__in = [21])
qset_B = qset.filter(person = 12, persontype__in = [1])

self.assertTrue(list(qset_A) == [21])
self.assertTrue(list(qset_B) == [1])