Merge lp:~zeitgeist/zeitgeist/fix-673916-GetEvent-duplicate-id into lp:zeitgeist/0.1

Proposed by Markus Korn
Status: Merged
Merged at revision: 1632
Proposed branch: lp:~zeitgeist/zeitgeist/fix-673916-GetEvent-duplicate-id
Merge into: lp:zeitgeist/0.1
Diff against target: 45 lines (+15/-2)
2 files modified
_zeitgeist/engine/main.py (+8/-2)
test/engine-test.py (+7/-0)
To merge this branch: bzr merge lp:~zeitgeist/zeitgeist/fix-673916-GetEvent-duplicate-id
Reviewer Review Type Date Requested Status
Seif Lotfy Approve
Review via email: mp+40655@code.launchpad.net

Description of the change

ZeitgeistEngine.get_events() now maps event objects correctly to its ids if
the method was called with duplicate ids (LP: #673916)

To post a comment you must log in.
1634. By Markus Korn

added another test

Revision history for this message
Seif Lotfy (seif) wrote :

Good work! Works fine here. Very elegantly done

review: Approve
1635. By Markus Korn

added descriptive comments

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file '_zeitgeist/engine/main.py'
2--- _zeitgeist/engine/main.py 2010-11-11 08:16:53 +0000
3+++ _zeitgeist/engine/main.py 2010-11-12 11:41:42 +0000
4@@ -173,7 +173,11 @@
5 else:
6 ids = (row[0] for row in rows)
7
8- id_hash = dict((id, n) for n, id in enumerate(ids))
9+ id_hash = defaultdict(list)
10+ for n, id in enumerate(ids):
11+ # the same id can be at multible places (LP: #673916)
12+ # cache all of them
13+ id_hash[id].append(n)
14
15 # If we are not able to get an event by the given id
16 # append None instead of raising an Error. The client
17@@ -195,7 +199,9 @@
18 event.append_subject(self._get_subject_from_row(row))
19 event = self.extensions.apply_get_hooks(event, sender)
20 if event is not None:
21- sorted_events[id_hash[event.id]] = event
22+ for n in id_hash[event.id]:
23+ # insert the event into all necessary spots (LP: #673916)
24+ sorted_events[n] = event
25
26 log.debug("Got %d events in %fs" % (len(sorted_events), time.time()-t))
27 return sorted_events
28
29=== modified file 'test/engine-test.py'
30--- test/engine-test.py 2010-11-10 14:44:08 +0000
31+++ test/engine-test.py 2010-11-12 11:41:42 +0000
32@@ -212,6 +212,13 @@
33 self.assertEquals(3, len(events))
34 for ev in events : self.assertEquals(None, ev)
35
36+ def testGetDuplicateEventIds(self):
37+ import_events("test/data/five_events.js", self.engine)
38+ events = self.engine.get_events([1, 1])
39+ self.assertEqual(2, len(events))
40+ self.assertEqual(2, len(filter(None, events)))
41+ self.assertTrue(events[0].id == events[1].id == 1)
42+
43 def testFindEventsId(self):
44 global test_event_1
45 self.testSingleInsertGet()