Merge lp:~therve/storm/reference-invalidated-state into lp:storm

Proposed by Thomas Herve
Status: Merged
Approved by: Jamu Kakar
Approved revision: not available
Merged at revision: not available
Proposed branch: lp:~therve/storm/reference-invalidated-state
Merge into: lp:storm
Diff against target: 76 lines (+33/-4)
2 files modified
storm/references.py (+14/-2)
tests/store/base.py (+19/-2)
To merge this branch: bzr merge lp:~therve/storm/reference-invalidated-state
Reviewer Review Type Date Requested Status
Jamu Kakar (community) Approve
James Henstridge Approve
Review via email: mp+14463@code.launchpad.net
To post a comment you must log in.
Revision history for this message
James Henstridge (jamesh) wrote :

Looks good.

review: Approve
Revision history for this message
Jamu Kakar (jkakar) wrote :

[1]

+ def test_primary_key_reference(self):
+ """
+ When an object reference another one using its primary key, it
+ correctly checks for the invalidated state after the store has been
+ committed, detecting if the referenced object has been removed behind
+ its back.
+ """

s/object reference/object references/ please

[2]

Would you mind adding a docstring to get_remote, describing it's
behaviour, including mention of what happens when the remote object
disappears, please?

+1!

review: Approve
340. By Thomas Herve

Add a docstring to get_remote.

341. By Thomas Herve

Typo

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'storm/references.py'
2--- storm/references.py 2009-11-20 20:54:23 +0000
3+++ storm/references.py 2009-11-25 16:43:13 +0000
4@@ -21,7 +21,7 @@
5 import weakref
6
7 from storm.exceptions import WrongStoreError, NoStoreError, ClassInfoError
8-from storm.store import Store, get_where_for_args
9+from storm.store import Store, get_where_for_args, LostObjectError
10 from storm.variables import LazyValue
11 from storm.expr import (
12 Select, Column, Exists, ComparableExpr, LeftJoin, Not, SQLRaw,
13@@ -467,11 +467,23 @@
14 self._r_to_l = {}
15
16 def get_remote(self, local):
17+ """Return the remote object for this relation, using the local cache.
18+
19+ If the object in the cache is invalidated, we validate it again to
20+ check if it's still in the database.
21+ """
22 local_info = get_obj_info(local)
23 try:
24- return local_info[self]["remote"]
25+ obj = local_info[self]["remote"]
26 except KeyError:
27 return None
28+ remote_info = get_obj_info(obj)
29+ if remote_info.get("invalidated"):
30+ try:
31+ Store.of(obj)._validate_alive(remote_info)
32+ except LostObjectError:
33+ return None
34+ return obj
35
36 def get_where_for_remote(self, local):
37 """Generate a column comparison expression for reference properties.
38
39=== modified file 'tests/store/base.py'
40--- tests/store/base.py 2009-11-20 20:25:35 +0000
41+++ tests/store/base.py 2009-11-25 16:43:14 +0000
42@@ -30,8 +30,7 @@
43 from storm.properties import PropertyPublisherMeta, Decimal
44 from storm.variables import PickleVariable
45 from storm.expr import (
46- Asc, Desc, Select, Func, LeftJoin, SQL, Count, Sum, Avg, And, Or, Eq,
47- Lower)
48+ Asc, Desc, Select, LeftJoin, SQL, Count, Sum, Avg, And, Or, Eq, Lower)
49 from storm.variables import Variable, UnicodeVariable, IntVariable
50 from storm.info import get_obj_info, ClassAlias
51 from storm.exceptions import *
52@@ -5009,6 +5008,24 @@
53 obj_info = get_obj_info(foo)
54 self.assertEquals(obj_info.variables[Foo.title].get_lazy(), AutoReload)
55
56+ def test_primary_key_reference(self):
57+ """
58+ When an object references another one using its primary key, it
59+ correctly checks for the invalidated state after the store has been
60+ committed, detecting if the referenced object has been removed behind
61+ its back.
62+ """
63+ class BarOnRemote(object):
64+ __storm_table__ = "bar"
65+ foo_id = Int(primary=True)
66+ foo = Reference(foo_id, Foo.id, on_remote=True)
67+ foo = self.store.get(Foo, 10)
68+ bar = self.store.get(BarOnRemote, 10)
69+ self.assertEqual(bar.foo, foo)
70+ self.store.execute("DELETE FROM foo WHERE id = 10")
71+ self.store.commit()
72+ self.assertEqual(bar.foo, None)
73+
74 def test_invalidate_and_get_object(self):
75 foo = self.store.get(Foo, 20)
76 self.store.invalidate(foo)

Subscribers

People subscribed via source and target branches

to status/vote changes: