Merge lp:~mfisch/ubuntu-accomplishments-daemon/ubuntu-accomplishments-daemon-unittest-accomplish into lp:ubuntu-accomplishments-daemon

Proposed by Matt Fischer
Status: Merged
Merged at revision: 110
Proposed branch: lp:~mfisch/ubuntu-accomplishments-daemon/ubuntu-accomplishments-daemon-unittest-accomplish
Merge into: lp:ubuntu-accomplishments-daemon
Diff against target: 309 lines (+157/-42)
2 files modified
accomplishments/daemon/api.py (+31/-24)
accomplishments/daemon/tests/tests.py (+126/-18)
To merge this branch: bzr merge lp:~mfisch/ubuntu-accomplishments-daemon/ubuntu-accomplishments-daemon-unittest-accomplish
Reviewer Review Type Date Requested Status
Ubuntu Accomplishments Daemon Developers Pending
Review via email: mp+115393@code.launchpad.net

Description of the change

More tests. For api.py, I cleaned up more whitespace. I also moved the trophy creation code to a separate function as it seemed to be an independent piece of code.

To post a comment you must log in.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'accomplishments/daemon/api.py'
2--- accomplishments/daemon/api.py 2012-07-17 17:00:06 +0000
3+++ accomplishments/daemon/api.py 2012-07-17 17:07:34 +0000
4@@ -259,7 +259,8 @@
5
6 log.msg("--- Starting Running Scripts - %d items on the queue ---" % (queuesize))
7 timestart = time.time()
8- self.parent.service.scriptrunner_start()
9+ if not self.parent.test_mode:
10+ self.parent.service.scriptrunner_start()
11
12 while queuesize > 0:
13 accomID = self.parent.scripts_queue.popleft()
14@@ -309,7 +310,8 @@
15
16 log.msg(
17 "--- Emptied the scripts queue in %.2f seconds---" % timefinal)
18- self.parent.service.scriptrunner_finish()
19+ if not self.parent.test_mode:
20+ self.parent.service.scriptrunner_finish()
21
22 self.scripts_state = NOT_RUNNING
23
24@@ -1246,7 +1248,7 @@
25 {'needs-signing': 'true', 'date-accomplished': '1990-04-12 02:22', 'needs-information': 'launchpad-email', 'version': '0.2', '__name__': 'trophy', 'launchpad-email': 'launchpaduser@ubuntu.com', 'id': 'ubuntu-community/registered-on-launchpad'}
26 """
27 if not self.get_acc_is_completed(accomID):
28- return
29+ return None
30 else:
31 cfg = ConfigParser.RawConfigParser()
32 cfg.read(self.get_trophy_path(accomID))
33@@ -1411,38 +1413,54 @@
34 def get_published_status(self):
35 """Detect if we are currently publishing online or not. Returns
36 True if we are or False if we are not. """
37-
38+
39 trophydir = self.get_config_value("config", "trophypath")
40 print trophydir
41 if os.path.exists(os.path.join(trophydir, "WEBVIEW")):
42 return True
43 else:
44 return False
45-
46+
47 def accomplish(self,accomID):
48 log.msg("Accomplishing: %s" % accomID)
49 if not self.get_acc_exists(accomID):
50 log.msg("There is no such accomplishment.")
51 return False #failure
52-
53+
54 # Check if is hasn't been already completed
55 if self.get_acc_is_completed(accomID):
56 log.msg("Not accomplishing " + accomID + ", it has already been completed.")
57 return True #success
58-
59+
60 # Check if this accomplishment is unlocked
61 if not self.get_acc_is_unlocked(accomID):
62 log.msg("This accomplishment cannot be completed; it's locked.")
63 return False
64-
65+
66 coll = self._coll_from_accomID(accomID)
67 accdata = self.get_acc_data(accomID)
68-
69+
70 # Prepare extra-info
71 needsinformation = self.get_acc_needs_info(accomID)
72 for i in needsinformation:
73 accdata[i] = self.get_extra_information(coll,i)[0][i]
74-
75+
76+ # Create .trophy file
77+ self._create_trophy_file(accdata, accomID)
78+
79+ if not self.get_acc_needs_signing(accomID):
80+ # The accomplishment does not need signing!
81+ if not self.test_mode:
82+ self.service.trophy_received(accomID)
83+ self._display_accomplished_bubble(accomID)
84+ self._display_unlocked_bubble(accomID)
85+ # Mark as completed and get list of new opportunities
86+ just_unlocked = self._mark_as_completed(accomID)
87+ self.run_scripts(just_unlocked)
88+
89+ return True
90+
91+ def _create_trophy_file(self, accdata, accomID):
92 # Create .trophy file
93 cp = ConfigParser.RawConfigParser()
94 cp.add_section("trophy")
95@@ -1465,17 +1483,6 @@
96 fp = open(trophypath, "w")
97 cp.write(fp)
98 fp.close()
99-
100- if not self.get_acc_needs_signing(accomID):
101- # The accomplishment does not need signing!
102- self.service.trophy_received(accomID)
103- self._display_accomplished_bubble(accomID)
104- self._display_unlocked_bubble(accomID)
105- # Mark as completed and get list of new opportunities
106- just_unlocked = self._mark_as_completed(accomID)
107- self.run_scripts(just_unlocked)
108-
109- return True
110
111 def set_daemon_session_start(self,value):
112 log.msg(value)
113@@ -1545,10 +1552,10 @@
114 return False
115 else:
116 return False
117-
118+
119 def _coll_from_accomID(self,accomID):
120 return accomID.split("/")[0]
121-
122+
123 def _display_accomplished_bubble(self,accomID):
124 if self.show_notifications == True and pynotify and (
125 pynotify.is_initted() or pynotify.init("icon-summary-body")):
126@@ -1558,7 +1565,7 @@
127 self.get_acc_icon_path(accomID) )
128 n.set_hint_string('append', 'allowed')
129 n.show()
130-
131+
132 def _display_unlocked_bubble(self,accomID):
133 unlocked = len(self.list_depending_on(accomID))
134 if unlocked is not 0:
135
136=== modified file 'accomplishments/daemon/tests/tests.py'
137--- accomplishments/daemon/tests/tests.py 2012-07-16 18:34:08 +0000
138+++ accomplishments/daemon/tests/tests.py 2012-07-17 17:07:34 +0000
139@@ -7,19 +7,13 @@
140 import shutil
141 import subprocess
142 import ConfigParser
143+import datetime
144
145 sys.path.insert(0, os.path.join(os.path.split(__file__)[0], "../../.."))
146 from accomplishments.daemon import app, api
147
148 # future tests:
149 # create extra information files - marked for removal in the code
150-# get_acc_date_completed - needs accomplish() to work to be useful
151-# get trophy data
152-# list trophies
153-# list opportunities
154-# list depending on
155-# list unlocked
156-# list unlocked not completed
157 # run scripts/runscript
158 # get published status
159 # invalidate extra information
160@@ -140,8 +134,131 @@
161 del os.environ['ACCOMPLISHMENTS_ROOT_DIR']
162 shutil.rmtree(self.td)
163
164- def test_handle_duplicate_accomplishments(self):
165- return
166+ def test_get_acc_date_completed(self):
167+ self.util_remove_all_accomps(self.accomp_dir)
168+ self.util_copy_accomp(self.accomp_dir, "first")
169+ self.util_copy_accomp(self.accomp_dir, "second")
170+ self.util_copy_accomp(self.accomp_dir, "third")
171+ self.util_copy_extrainfo(self.extrainfo_dir, "info")
172+ self.util_copy_extrainfo(self.extrainfo_dir, "info2")
173+ a = api.Accomplishments(None, None, True)
174+ a.write_extra_information_file("info", "whatever")
175+ a.write_extra_information_file("info2", "whatever2")
176+
177+ self.assertTrue(a.accomplish("%s/first" % self.ACCOMP_SET))
178+ self.assertTrue(a.accomplish("%s/second" % self.ACCOMP_SET))
179+ self.assertTrue(a.accomplish("%s/third" % self.ACCOMP_SET))
180+
181+ trophies = a.list_trophies()
182+ # since "second" requires signing, it shouldn't be listed
183+ self.assertEqual(len(trophies), 2)
184+
185+ d1 = a.get_acc_date_completed("%s/first" % self.ACCOMP_SET)
186+ self.assertTrue(isinstance(d1, basestring))
187+ dt1 = datetime.datetime.strptime(d1, "%Y-%m-%d %H:%M")
188+ self.assertTrue(dt1 is not None)
189+
190+ d3 = a.get_acc_date_completed("%s/third" % self.ACCOMP_SET)
191+ self.assertTrue(isinstance(d3, basestring))
192+ dt3 = datetime.datetime.strptime(d3, "%Y-%m-%d %H:%M")
193+ self.assertTrue(dt3 is not None)
194+
195+ # this tests:
196+ # accomplish()
197+ # list_opportunities
198+ # list_trophies
199+ # list_unlocked
200+ # list_unlocked_not_completed
201+ # get_trophy_data
202+ def test_accomplish(self):
203+ self.util_remove_all_accomps(self.accomp_dir)
204+ self.util_copy_accomp(self.accomp_dir, "first")
205+ self.util_copy_accomp(self.accomp_dir, "second")
206+ self.util_copy_accomp(self.accomp_dir, "third")
207+ a = api.Accomplishments(None, None, True)
208+
209+ # before accomplishing
210+ opps = a.list_opportunitues()
211+ self.assertEqual(len(opps), 3)
212+ for accom in opps:
213+ self.assertTrue(accom in ["%s/first" % self.ACCOMP_SET,
214+ "%s/second" % self.ACCOMP_SET, "%s/third" % self.ACCOMP_SET])
215+
216+ unlocked = a.list_unlocked()
217+ self.assertEqual(len(unlocked), 2)
218+ for accom in unlocked:
219+ self.assertTrue(accom in ["%s/first" % self.ACCOMP_SET,
220+ "%s/third" % self.ACCOMP_SET])
221+
222+ unlocked_nc = a.list_unlocked_not_completed()
223+ self.assertEqual(len(unlocked_nc), 2)
224+ for accom in unlocked_nc:
225+ self.assertTrue(accom in ["%s/first" % self.ACCOMP_SET,
226+ "%s/third" % self.ACCOMP_SET])
227+
228+ trophies = a.list_trophies()
229+ self.assertEqual(len(trophies), 0)
230+
231+ self.assertTrue(a.get_trophy_data("%s/first" % self.ACCOMP_SET) is None)
232+ self.assertTrue(a.get_trophy_data("%s/second" % self.ACCOMP_SET)
233+ is None)
234+ self.assertTrue(a.get_trophy_data("%s/third" % self.ACCOMP_SET) is None)
235+
236+ # now let's accomplish something, it should fail without extra info
237+ self.assertRaises(KeyError, a.accomplish, "%s/first" % self.ACCOMP_SET)
238+
239+ # this time it will work
240+ self.util_copy_extrainfo(self.extrainfo_dir, "info")
241+ self.util_copy_extrainfo(self.extrainfo_dir, "info2")
242+ a.reload_accom_database()
243+ self.assertTrue(a.accomplish("%s/first" % self.ACCOMP_SET))
244+
245+ opps = a.list_opportunitues()
246+ self.assertEqual(len(opps), 2)
247+ for accom in opps:
248+ self.assertTrue(accom in ["%s/second" % self.ACCOMP_SET,
249+ "%s/third" % self.ACCOMP_SET])
250+
251+ unlocked = a.list_unlocked()
252+ self.assertEqual(len(unlocked), 3)
253+ for accom in unlocked:
254+ self.assertTrue(accom in ["%s/first" % self.ACCOMP_SET,
255+ "%s/second" % self.ACCOMP_SET,
256+ "%s/third" % self.ACCOMP_SET])
257+
258+ unlocked_nc = a.list_unlocked_not_completed()
259+ self.assertEqual(len(unlocked_nc), 2)
260+ for accom in unlocked_nc:
261+ self.assertTrue(accom in ["%s/second" % self.ACCOMP_SET,
262+ "%s/third" % self.ACCOMP_SET])
263+
264+ trophies = a.list_trophies()
265+ self.assertEqual(len(trophies), 1)
266+ for accom in trophies:
267+ self.assertTrue(accom in ["%s/first" % self.ACCOMP_SET])
268+
269+ td = a.get_trophy_data("%s/first" % self.ACCOMP_SET)
270+ self.assertTrue(isinstance(td, dict))
271+
272+ self.assertTrue(td['date-accomplished'] is not None)
273+ self.assertTrue(td['version'] is not None)
274+ self.assertTrue(td['__name__'] == "trophy")
275+ self.assertTrue(td['id'] == "%s/first" % self.ACCOMP_SET)
276+ self.assertTrue(td['needs-information'] is not None)
277+
278+ def test_list_depending_on(self):
279+ self.util_remove_all_accomps(self.accomp_dir)
280+ self.util_copy_accomp(self.accomp_dir, "first")
281+ self.util_copy_accomp(self.accomp_dir, "second")
282+ self.util_copy_accomp(self.accomp_dir, "third")
283+ a = api.Accomplishments(None, None, True)
284+
285+ self.assertEquals(len(a.list_depending_on("%s/first" %
286+ self.ACCOMP_SET)), 1)
287+ self.assertEquals(len(a.list_depending_on("%s/second" %
288+ self.ACCOMP_SET)), 0)
289+ self.assertEquals(len(a.list_depending_on("%s/third" %
290+ self.ACCOMP_SET)), 0)
291
292 # tests all the get_acc_* functions, except for:
293 # get_acc_icon
294@@ -358,15 +475,6 @@
295
296 self.util_remove_all_accomps(self.accomp_dir)
297
298- @unittest.skip("need the daemon running")
299- def test_accomplish(self):
300- a = api.Accomplishments(None, None, True)
301- self.assertEqual(len(a.list_trophies()), 0)
302- a.accomplish("%s/first" % self.ACCOMP_SET)
303- trophies = a.list_trophies()
304- self.assertEqual(len(trophies), 1)
305- self.assertEqual(trophies[0]["title"], "My First Accomplishment")
306-
307 def test_missing_about_file(self):
308 os.remove(os.path.join(self.accomp_root, "ABOUT"))
309 self.assertRaises(LookupError, api.Accomplishments, None, None, True)

Subscribers

People subscribed via source and target branches