Skip to content

Commit

Permalink
Add replace() to relfield, call flush_query_cache instead of save
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Zimmermann committed Jan 29, 2012
1 parent 8bac847 commit 8ac59ce
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
9 changes: 7 additions & 2 deletions odis/__init__.py
Expand Up @@ -956,18 +956,23 @@ def add(self, *objs):
raise TypeError('invalid model')

ds.add(obj.pk)
obj.save()
obj.flush_query_cache()

def delete(self, *objs):
for obj in objs:
if not isinstance(obj, model):
raise TypeError('invalid model')

ds.delete(obj.pk)
obj.save()
obj.flush_query_cache()

self._flush_local_cache()

def replace(self, *objs):
'replace all members in a set with new members'
self.delete(*self.all())
self.add(*objs)

return RelQuerySet(model, key=key)

class BaseModel(type):
Expand Down
16 changes: 13 additions & 3 deletions tests/tests.py
Expand Up @@ -149,13 +149,23 @@ def test_relfield(self):
self.assertEqual(len(q.rel), count)

# test filters with different base key
self.assertEquals(q.rel[0], self.users[1])
self.assertEquals(q.rel.filter(username='bar')[0], self.users[1])
self.assertEqual(q.rel[0], self.users[1])
self.assertEqual(q.rel.filter(username='bar')[0], self.users[1])
self.assertRaises(EmptyError, q.rel.get, username='foo')

self.users.pop().delete()
count = count - 1
self.assertEquals(len(q.rel), count)
self.assertEqual(len(q.rel), count)

# test that we properly flush query cache for Qux when add() and delete()
q.rel.add(*self.users)
self.assertEqual(len(q.rel.all()), len(self.users))

q.rel.delete(*self.users)
self.assertEqual(len(q.rel.all()), 0)

q.rel.replace(*self.users)
self.assertEqual(len(q.rel.all()), len(self.users))

def test_foreignfield(self):
Baz._db.flushdb()
Expand Down

0 comments on commit 8ac59ce

Please sign in to comment.