Merge lp:~jamesh/storm/resultset-find into lp:storm

Proposed by James Henstridge
Status: Merged
Approved by: Jamu Kakar
Approved revision: 308
Merged at revision: not available
Proposed branch: lp:~jamesh/storm/resultset-find
Merge into: lp:storm
Diff against target: 129 lines
To merge this branch: bzr merge lp:~jamesh/storm/resultset-find
Reviewer Review Type Date Requested Status
Jamu Kakar (community) Approve
Thomas Herve (community) Approve
Review via email: mp+5959@code.launchpad.net
To post a comment you must log in.
Revision history for this message
James Henstridge (jamesh) wrote :

Implement a ResultSet.find() method. In this early implementation, it only handles simple cases and raises FeatureError for other cases (sliced results, grouped result sets, set expressions, etc). We can look at extending the method to handle some of those later.

lp:~jamesh/storm/resultset-find updated
307. By James Henstridge

More test tweaks.

Revision history for this message
Thomas Herve (therve) wrote :

+ This is analogous to L{Store.find}, although it doesn't take a
+ C{cls_spec} argument, instead using the same tables as the
+ existing result set.

It looks slightly misleading, because it doesn't only take the same tables but also the current where arguments.

+1!

review: Approve
lp:~jamesh/storm/resultset-find updated
308. By James Henstridge

Fix up docstring, as per therve's review.

Revision history for this message
James Henstridge (jamesh) wrote :

> + This is analogous to L{Store.find}, although it doesn't take a
> + C{cls_spec} argument, instead using the same tables as the
> + existing result set.
>
> It looks slightly misleading, because it doesn't only take the same tables but
> also the current where arguments.

Thanks for the review. I've updated the docstring to state that it also restricts the results to those in the current set.

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

Nice and simple, thanks. +1!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'storm/store.py'
2--- storm/store.py 2009-05-19 15:17:29 +0000
3+++ storm/store.py 2009-07-28 05:20:38 +0000
4@@ -1363,6 +1363,37 @@
5 # in get_column().
6 return objects
7
8+ def find(self, *args, **kwargs):
9+ """Perform a query on objects within this result set.
10+
11+ This is analogous to L{Store.find}, although it doesn't take a
12+ C{cls_spec} argument, instead using the same tables as the
13+ existing result set, and restricts the results to those in
14+ this set.
15+
16+ @param args: Instances of L{Expr}.
17+ @param kwargs: Mapping of simple column names to values or
18+ expressions to query for.
19+
20+ @return: A L{ResultSet} of matching instances.
21+ """
22+ if self._select is not Undef:
23+ raise FeatureError("Can't query set expressions")
24+ if self._offset is not Undef or self._limit is not Undef:
25+ raise FeatureError("Can't query a sliced result set")
26+ if self._group_by is not Undef:
27+ raise FeatureError("Can't query grouped result sets")
28+
29+ result_set = self.copy()
30+ extra_where = get_where_for_args(
31+ args, kwargs, self._find_spec.default_cls)
32+ if extra_where is not Undef:
33+ if result_set._where is Undef:
34+ result_set._where = extra_where
35+ else:
36+ result_set._where = And(result_set._where, extra_where)
37+ return result_set
38+
39 def _set_expr(self, expr_cls, other, all=False):
40 if not self._find_spec.is_compatible(other._find_spec):
41 raise FeatureError("Incompatible results for set operation")
42@@ -1492,6 +1523,9 @@
43 def cached(self):
44 return []
45
46+ def find(self, *args, **kwargs):
47+ return self
48+
49 def union(self, other):
50 if isinstance(other, EmptyResultSet):
51 return self
52
53=== modified file 'tests/store/base.py'
54--- tests/store/base.py 2009-02-19 16:44:33 +0000
55+++ tests/store/base.py 2009-07-28 05:20:38 +0000
56@@ -5015,6 +5015,62 @@
57 self.store.reset()
58 self.assertIdentical(Store.of(foo1), None)
59
60+ def test_result_find(self):
61+ result1 = self.store.find(Foo, Foo.id <= 20)
62+ result2 = result1.find(Foo.id > 10)
63+ foo = result2.one()
64+ self.assertTrue(foo)
65+ self.assertEqual(foo.id, 20)
66+
67+ def test_result_find_kwargs(self):
68+ result1 = self.store.find(Foo, Foo.id <= 20)
69+ result2 = result1.find(id=20)
70+ foo = result2.one()
71+ self.assertTrue(foo)
72+ self.assertEqual(foo.id, 20)
73+
74+ def test_result_find_introduce_join(self):
75+ result1 = self.store.find(Foo, Foo.id <= 20)
76+ result2 = result1.find(Foo.id == Bar.foo_id,
77+ Bar.title == u"Title 300")
78+ foo = result2.one()
79+ self.assertTrue(foo)
80+ self.assertEqual(foo.id, 10)
81+
82+ def test_result_find_tuple(self):
83+ result1 = self.store.find((Foo, Bar), Foo.id == Bar.foo_id)
84+ result2 = result1.find(Bar.title == u"Title 100")
85+ foo_bar = result2.one()
86+ self.assertTrue(foo_bar)
87+ foo, bar = foo_bar
88+ self.assertEqual(foo.id, 30)
89+ self.assertEqual(bar.id, 300)
90+
91+ def test_result_find_undef_where(self):
92+ result = self.store.find(Foo, Foo.id == 20).find()
93+ foo = result.one()
94+ self.assertTrue(foo)
95+ self.assertEqual(foo.id, 20)
96+ result = self.store.find(Foo).find(Foo.id == 20)
97+ foo = result.one()
98+ self.assertTrue(foo)
99+ self.assertEqual(foo.id, 20)
100+
101+ def test_result_find_fails_on_set_expr(self):
102+ result1 = self.store.find(Foo)
103+ result2 = self.store.find(Foo)
104+ result = result1.union(result2)
105+ self.assertRaises(FeatureError, result.find, Foo.id == 20)
106+
107+ def test_result_find_fails_on_slice(self):
108+ result = self.store.find(Foo)[1:2]
109+ self.assertRaises(FeatureError, result.find, Foo.id == 20)
110+
111+ def test_result_find_fails_on_group_by(self):
112+ result = self.store.find(Foo)
113+ result.group_by(Foo)
114+ self.assertRaises(FeatureError, result.find, Foo.id == 20)
115+
116 def test_result_union(self):
117 result1 = self.store.find(Foo, id=30)
118 result2 = self.store.find(Foo, id=10)
119@@ -5641,6 +5697,10 @@
120 self.assertEquals(self.result.cached(), [])
121 self.assertEquals(self.empty.cached(), [])
122
123+ def test_find(self):
124+ self.assertEquals(list(self.result.find(Foo.title == u"foo")), [])
125+ self.assertEquals(list(self.empty.find(Foo.title == u"foo")), [])
126+
127 def test_union(self):
128 self.assertEquals(self.empty.union(self.empty), self.empty)
129 self.assertEquals(type(self.empty.union(self.result)),

Subscribers

People subscribed via source and target branches

to status/vote changes: