Merge lp:~mfisch/ubuntu-accomplishments-daemon/ubuntu-accomplishments-daemon-more-unittests into lp:ubuntu-accomplishments-daemon

Proposed by Matt Fischer
Status: Merged
Merged at revision: 103
Proposed branch: lp:~mfisch/ubuntu-accomplishments-daemon/ubuntu-accomplishments-daemon-more-unittests
Merge into: lp:ubuntu-accomplishments-daemon
Diff against target: 518 lines (+232/-104)
9 files modified
accomplishments/daemon/api.py (+1/-1)
accomplishments/daemon/tests/accomps/first.accomplishment (+2/-1)
accomplishments/daemon/tests/accomps/second.accomplishment (+1/-0)
accomplishments/daemon/tests/accomps/third.accomplishment (+1/-0)
accomplishments/daemon/tests/extrainformation/info (+9/-0)
accomplishments/daemon/tests/extrainformation/info2 (+23/-0)
accomplishments/daemon/tests/tests.py (+188/-101)
debian/control (+1/-0)
debian/rules (+6/-1)
To merge this branch: bzr merge lp:~mfisch/ubuntu-accomplishments-daemon/ubuntu-accomplishments-daemon-more-unittests
Reviewer Review Type Date Requested Status
Rafał Cieślak Pending
Review via email: mp+114960@code.launchpad.net

Description of the change

This has more unit tests, some clean-up of how I was testing exceptions and fixes a bug in the API that cielak found:

Basically if the value is not set (is ''), when you call get_extra_information() it returns an empty label too.

<cielak> which causes that if - in get_extra_info - value is '', then label will be empty too
<cielak> api.py, line 837
<mfisch> whats the fix?
<cielak> api.py, line 837 is now:
<cielak> final = [{item : "", "label" : ""}]
<cielak> should be:
<cielak> final = [{item : "", "label" : label}]

To post a comment you must log in.
104. By Matt Fischer

run tests during build

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-13 20:01:07 +0000
3+++ accomplishments/daemon/api.py 2012-07-14 02:21:18 +0000
4@@ -834,7 +834,7 @@
5 final = [{item : data, "label" : label}]
6 except IOError as e:
7 #print "No data."
8- final = [{item : "", "label" : ""}]
9+ final = [{item : "", "label" : label}]
10 return final
11
12 # =================================================================
13
14=== modified file 'accomplishments/daemon/tests/accomps/first.accomplishment'
15--- accomplishments/daemon/tests/accomps/first.accomplishment 2012-07-12 21:10:53 +0000
16+++ accomplishments/daemon/tests/accomps/first.accomplishment 2012-07-14 02:21:18 +0000
17@@ -2,5 +2,6 @@
18 title=My First Accomplishment
19 icon=first.jpg
20 description=An example accomplishment for the test suite
21-needs-information=info,more info
22+needs-information=info,info2
23 collection = testaccomp
24+author = Tester <tester@tester>
25
26=== modified file 'accomplishments/daemon/tests/accomps/second.accomplishment'
27--- accomplishments/daemon/tests/accomps/second.accomplishment 2012-07-12 21:10:53 +0000
28+++ accomplishments/daemon/tests/accomps/second.accomplishment 2012-07-14 02:21:18 +0000
29@@ -5,3 +5,4 @@
30 depends=testaccomp/first
31 needs-signing=True
32 collection=testaccomp
33+author = Tester <tester@tester>
34
35=== modified file 'accomplishments/daemon/tests/accomps/third.accomplishment'
36--- accomplishments/daemon/tests/accomps/third.accomplishment 2012-07-12 21:10:53 +0000
37+++ accomplishments/daemon/tests/accomps/third.accomplishment 2012-07-14 02:21:18 +0000
38@@ -7,3 +7,4 @@
39 category=testing, unit test
40 # comment should be ignored
41 # comment
42+author = Someone
43
44=== added directory 'accomplishments/daemon/tests/extrainformation'
45=== added file 'accomplishments/daemon/tests/extrainformation/info'
46--- accomplishments/daemon/tests/extrainformation/info 1970-01-01 00:00:00 +0000
47+++ accomplishments/daemon/tests/extrainformation/info 2012-07-14 02:21:18 +0000
48@@ -0,0 +1,9 @@
49+[label]
50+en = Some info
51+
52+[description]
53+en = Some description
54+# a comment
55+
56+[example]
57+en = Some example
58
59=== added file 'accomplishments/daemon/tests/extrainformation/info2'
60--- accomplishments/daemon/tests/extrainformation/info2 1970-01-01 00:00:00 +0000
61+++ accomplishments/daemon/tests/extrainformation/info2 2012-07-14 02:21:18 +0000
62@@ -0,0 +1,23 @@
63+# a comment
64+
65+
66+
67+
68+
69+
70+
71+
72+
73+[label]
74+en = More info
75+
76+[description]
77+en = More description
78+
79+####
80+
81+#
82+##
83+
84+[example]
85+en = More example
86
87=== modified file 'accomplishments/daemon/tests/tests.py'
88--- accomplishments/daemon/tests/tests.py 2012-07-13 19:36:06 +0000
89+++ accomplishments/daemon/tests/tests.py 2012-07-14 02:21:18 +0000
90@@ -11,40 +11,41 @@
91 sys.path.insert(0, os.path.join(os.path.split(__file__)[0], "../../.."))
92 from accomplishments.daemon import app, api
93
94-# future tests
95+# future tests:
96+# create extra information files - marked for removal in the code
97 # get_acc_date_completed - needs accomplish() to work to be useful
98-# get all extra information
99-# get all extra information required
100-# create extra information files
101-# invalidate extra information
102-# get extra information
103-# get trophy path
104 # get trophy data
105-# get collection name
106-# get collection exists
107-# get collection authors
108-# get collection categories
109-# get collection data
110 # list trophies
111 # list opportunities
112 # list depending on
113 # list unlocked
114 # list unlocked not completed
115-# list collections
116 # run scripts/runscript
117-# build viewer database
118 # get published status
119+# invalidate extra information
120
121 # These tests will modify the user's envrionment, outside of the test
122 # dir and so are not written/skipped:
123 # - set daemon session start
124 # - set block u1 notification bubbles
125
126+# Debugging:
127+# To debug tests, the following changes are recommended:
128+# 1) comment out the shutil.rmtree in tearDown()
129+# 2) in setUp, set self.td to a known place, like /tmp/foo (you will need
130+# to create this directory as well)
131+
132 class TestDaemon(unittest.TestCase):
133
134 ACCOMP_SET = "testaccomp"
135 LANG = "en"
136
137+ def util_copy_extrainfo(self, extrainfo_dir, extrainfo_name):
138+ testdir = os.path.dirname(__file__)
139+ src = os.path.join(testdir, "extrainformation", extrainfo_name)
140+ dest = os.path.join(extrainfo_dir, extrainfo_name)
141+ shutil.copyfile(src, dest)
142+
143 def util_copy_accomp(self, accomp_dir, accomp_name):
144 testdir = os.path.dirname(__file__)
145 src = os.path.join(testdir, "accomps",
146@@ -52,6 +53,10 @@
147 dest = os.path.join(accomp_dir, "%s.accomplishment" % accomp_name)
148 shutil.copyfile(src, dest)
149
150+ # This function is not really needed because when tearDown runs it
151+ # removes the entire tree, but when debugging tests it's useful to comment
152+ # out the rmtree in tearDown, so then this is critical to make the tests
153+ # work.
154 def util_remove_all_accomps(self, accomp_dir):
155 for f in os.listdir(accomp_dir):
156 os.remove(os.path.join(self.accomp_dir, f))
157@@ -119,9 +124,9 @@
158 if not os.path.exists(self.script_root):
159 os.makedirs(self.script_root)
160
161- # XXX - not sure this is correct or needed
162- # /tmp/foo/accomplishments/accomplishments/testaccomp/trophies
163- self.trophy_dir = os.path.join(self.td, "trophies")
164+ # /tmp/foo/accomplishments/.local/share/accomplishments/trophies
165+ self.trophy_dir = os.path.join(self.td, "accomplishments", ".local",
166+ "share", "accomplishments", "trophies")
167 if not os.path.exists(self.trophy_dir):
168 os.makedirs(self.trophy_dir)
169
170@@ -171,14 +176,7 @@
171 "My Second Accomplishment")
172 self.assertEquals(a.get_acc_title("%s/third" % self.ACCOMP_SET),
173 "My Third Accomplishment")
174-
175- # XXX - assertRaises isn't working here the way I think it
176- # should, this is a substitute
177- try:
178- a.get_acc_title("wrong")
179- self.assertTrue(False)
180- except KeyError:
181- self.assertTrue(True)
182+ self.assertRaises(KeyError, a.get_acc_title, "wrong")
183
184 # get_acc_description
185 self.assertTrue("example" in a.get_acc_description("%s/first" %
186@@ -187,27 +185,13 @@
187 self.ACCOMP_SET))
188 self.assertTrue("example" in a.get_acc_description("%s/third" %
189 self.ACCOMP_SET))
190-
191- # XXX - assertRaises isn't working here the way I think it
192- # should, this is a substitute
193- try:
194- a.get_acc_description("wrong")
195- self.assertTrue(False)
196- except KeyError:
197- self.assertTrue(True)
198+ self.assertRaises(KeyError, a.get_acc_description, "wrong")
199
200 # get_acc_needs_signing
201 self.assertFalse(a.get_acc_needs_signing("%s/first" % self.ACCOMP_SET))
202 self.assertTrue(a.get_acc_needs_signing("%s/second" % self.ACCOMP_SET))
203 self.assertFalse(a.get_acc_needs_signing("%s/first" % self.ACCOMP_SET))
204-
205- # XXX - assertRaises isn't working here the way I think it
206- # should, this is a substitute
207- try:
208- a.get_acc_needs_signing("wrong")
209- self.assertTrue(False)
210- except KeyError:
211- self.assertTrue(True)
212+ self.assertRaises(KeyError, a.get_acc_needs_signing, "wrong")
213
214 # get_acc_depends
215 self.assertTrue(a.get_acc_depends("%s/first" % self.ACCOMP_SET) == [])
216@@ -215,27 +199,13 @@
217 self.assertEquals(len(deps), 1)
218 self.assertTrue(deps[0] == "%s/first" % self.ACCOMP_SET)
219 self.assertTrue(a.get_acc_depends("%s/third" % self.ACCOMP_SET) == [])
220-
221- # XXX - assertRaises isn't working here the way I think it
222- # should, this is a substitute
223- try:
224- a.get_acc_depends("wrong")
225- self.assertTrue(False)
226- except KeyError:
227- self.assertTrue(True)
228+ self.assertRaises(KeyError, a.get_acc_depends, "wrong")
229
230 # get_acc_is_unlocked
231 self.assertTrue(a.get_acc_is_unlocked("%s/first" % self.ACCOMP_SET))
232 self.assertFalse(a.get_acc_is_unlocked("%s/second" % self.ACCOMP_SET))
233 self.assertTrue(a.get_acc_is_unlocked("%s/third" % self.ACCOMP_SET))
234-
235- # XXX - assertRaises isn't working here the way I think it
236- # should, this is a substitute
237- try:
238- a.get_acc_is_unlocked("wrong")
239- self.assertTrue(False)
240- except KeyError:
241- self.assertTrue(True)
242+ self.assertRaises(KeyError, a.get_acc_is_unlocked, "wrong")
243
244 # get_acc_is_completed
245 # XXX - when we get the accomplish() code working, make some of these
246@@ -243,14 +213,7 @@
247 self.assertFalse(a.get_acc_is_completed("%s/first" % self.ACCOMP_SET))
248 self.assertFalse(a.get_acc_is_completed("%s/second" % self.ACCOMP_SET))
249 self.assertFalse(a.get_acc_is_completed("%s/third" % self.ACCOMP_SET))
250-
251- # XXX - assertRaises isn't working here the way I think it
252- # should, this is a substitute
253- try:
254- a.get_acc_is_completed("wrong")
255- self.assertTrue(False)
256- except KeyError:
257- self.assertTrue(True)
258+ self.assertRaises(KeyError, a.get_acc_is_completed, "wrong")
259
260 # get_acc_script_path
261 self.assertEqual(a.get_acc_script_path("%s/first" % self.ACCOMP_SET),
262@@ -260,30 +223,18 @@
263 self.util_write_file(self.script_root, "third.py", "print 'hello'")
264 sp = a.get_acc_script_path("%s/third" % self.ACCOMP_SET)
265 self.assertTrue(sp.endswith("third.py"))
266-
267- try:
268- a.get_acc_script_path("wrong")
269- self.assertTrue(False)
270- except KeyError:
271- self.assertTrue(True)
272+ self.assertRaises(KeyError, a.get_acc_script_path, "wrong")
273
274 # get_acc_needs_info
275 info = a.get_acc_needs_info("%s/first" % self.ACCOMP_SET)
276 self.assertEqual(len(info),2)
277- self.assertEqual(info[0], "info")
278- self.assertEqual(info[1], "more info")
279+ for i in info:
280+ self.assertTrue(i in ["info", "info2"])
281 self.assertEqual(a.get_acc_needs_info("%s/second" % self.ACCOMP_SET),
282 [])
283 self.assertEqual(a.get_acc_needs_info("%s/third" % self.ACCOMP_SET),
284 [])
285-
286- # XXX - assertRaises isn't working here the way I think it
287- # should, this is a substitute
288- try:
289- a.get_acc_needs_info("wrong")
290- self.assertTrue(False)
291- except KeyError:
292- self.assertTrue(True)
293+ self.assertRaises(KeyError, a.get_acc_needs_info, "wrong")
294
295 # get_acc_collection
296 self.assertEqual(a.get_acc_collection("%s/first" % self.ACCOMP_SET),
297@@ -292,32 +243,17 @@
298 self.ACCOMP_SET)
299 self.assertEqual(a.get_acc_collection("%s/third" % self.ACCOMP_SET),
300 self.ACCOMP_SET)
301-
302- # XXX - assertRaises isn't working here the way I think it
303- # should, this is a substitute
304- try:
305- a.get_acc_collection("wrong")
306- self.assertTrue(False)
307- except KeyError:
308- self.assertTrue(True)
309+ self.assertRaises(KeyError, a.get_acc_collection, "wrong")
310
311 # get_acc_categories
312 self.assertEqual(a.get_acc_categories("%s/first" % self.ACCOMP_SET), [])
313 self.assertEqual(a.get_acc_categories("%s/second" % self.ACCOMP_SET),
314 [])
315- info = a.get_acc_categories("%s/third" % self.ACCOMP_SET)
316+ categories = a.get_acc_categories("%s/third" % self.ACCOMP_SET)
317 self.assertEqual(len(info),2)
318- self.assertEqual(info[0], "testing")
319- self.assertEqual(info[1], "unit test")
320-
321- # XXX - assertRaises isn't working here the way I think it
322- # should, this is a substitute
323- try:
324- a.get_acc_categories("wrong")
325- self.assertTrue(False)
326- except KeyError:
327- self.assertTrue(True)
328-
329+ for category in categories:
330+ self.assertTrue(category in ["testing", "unit test"])
331+ self.assertRaises(KeyError, a.get_acc_categories, "wrong")
332
333 def test_get_block_ubuntuone_notification_bubbles(self):
334 a = api.Accomplishments(None)
335@@ -489,5 +425,156 @@
336 self.util_write_config_file(self.config_dir)
337 return
338
339+ # this tests the get_collection_* functions and list_collections():
340+ def test_get_collection_all_funcs(self):
341+ self.util_remove_all_accomps(self.accomp_dir)
342+ self.util_copy_accomp(self.accomp_dir, "first")
343+ self.util_copy_accomp(self.accomp_dir, "second")
344+ self.util_copy_accomp(self.accomp_dir, "third")
345+ a = api.Accomplishments(None)
346+
347+ # list_collections
348+ collections = a.list_collections()
349+ self.assertEqual(len(collections), 1)
350+ self.assertEqual(collections[0], self.ACCOMP_SET)
351+
352+ # get_collection_name
353+ self.assertEqual(a.get_collection_name(collections[0]),
354+ "Test Collection")
355+ self.assertRaises(KeyError, a.get_collection_name, "wrong")
356+
357+ # get_collection_exists
358+ self.assertTrue(a.get_collection_exists(collections[0]))
359+ self.assertFalse(a.get_collection_exists("wrong"))
360+ self.assertFalse(a.get_collection_exists(""))
361+ self.assertFalse(a.get_collection_exists(None))
362+
363+ # get_collection_authors
364+ authors = a.get_collection_authors(collections[0])
365+ # we have only 2 authors because dupes are removed
366+ self.assertEqual(len(authors), 2)
367+ for author in authors:
368+ self.assertTrue(author in ["Someone", "Tester <tester@tester>"])
369+ self.assertRaises(KeyError, a.get_collection_authors, "wrong")
370+
371+ # get_collection_categories
372+ categories = a.get_collection_categories(collections[0])
373+ self.assertEqual(len(categories), 2)
374+ for category in categories:
375+ self.assertTrue(category in ["testing", "unit test"])
376+ self.assertRaises(KeyError, a.get_collection_categories, "wrong")
377+
378+ # get_collection_data
379+ data = a.get_collection_data(collections[0])
380+ self.assertTrue(isinstance(data, dict))
381+ self.assertNotEquals(data['authors'], None)
382+ self.assertNotEquals(data['name'], None)
383+ self.assertNotEquals(data['categories'], None)
384+ self.assertRaises(KeyError, a.get_collection_data, "wrong")
385+
386+ # get trophy path
387+ def test_get_trophy_path(self):
388+ self.util_remove_all_accomps(self.accomp_dir)
389+ self.util_copy_accomp(self.accomp_dir, "first")
390+ self.util_copy_accomp(self.accomp_dir, "second")
391+ self.util_copy_accomp(self.accomp_dir, "third")
392+ a = api.Accomplishments(None)
393+
394+ self.assertTrue(a.get_trophy_path("%s/first" %
395+ self.ACCOMP_SET).endswith("first.trophy"))
396+ self.assertTrue(a.get_trophy_path("%s/second" %
397+ self.ACCOMP_SET).endswith("second.trophy"))
398+ self.assertTrue(a.get_trophy_path("%s/third" %
399+ self.ACCOMP_SET).endswith("third.trophy"))
400+
401+ def test_write_extra_information_file(self):
402+ a = api.Accomplishments(None)
403+
404+ # write extra information will make the directory for us if needed,
405+ # so lets remove it (if present and force it to)
406+ extrainfo_path = os.path.join(a.trophies_path, ".extrainformation")
407+ if os.path.exists(extrainfo_path):
408+ shutil.rmtree(extrainfo_path)
409+
410+ a.write_extra_information_file("whatever", "abcdefg")
411+ path = os.path.join(extrainfo_path, "whatever")
412+ self.assertTrue(os.path.exists(path))
413+
414+ # write extra info will remove a file if you don't pass in data
415+ a.write_extra_information_file("whatever", None)
416+ self.assertFalse(os.path.exists(path))
417+
418+ # tests:
419+ # get_extra_information()
420+ # get_all_extra_information()
421+ # get_all_extra_information_required()
422+ def test_get_extra_information_all_funcs(self):
423+ a = api.Accomplishments(None)
424+ self.util_copy_extrainfo(self.extrainfo_dir, "info")
425+ self.util_copy_extrainfo(self.extrainfo_dir, "info2")
426+ self.util_copy_accomp(self.accomp_dir, "first")
427+
428+ # get extra information
429+ # these won't show up until we reload
430+ self.assertRaises(KeyError, a.get_extra_information, self.ACCOMP_SET,
431+ "info")
432+
433+ # should return None when the collection doesn't exist
434+ self.assertEqual(a.get_extra_information("wrong", "info"), None)
435+
436+ # reloading should make them show up
437+ a.reload_accom_database()
438+
439+ # will throw a KeyError if collection is right, but extrainfo is
440+ # wrong
441+ self.assertRaises(KeyError, a.get_extra_information, self.ACCOMP_SET,
442+ "wrong")
443+
444+ ei = a.get_extra_information(self.ACCOMP_SET, "info")
445+ self.assertTrue(isinstance(ei, list))
446+ self.assertTrue(len(ei) == 1)
447+ self.assertEqual(ei[0]['info'], '')
448+ self.assertEqual(ei[0]['label'], 'Some info')
449+ ei = a.get_extra_information(self.ACCOMP_SET, "info2")
450+ self.assertTrue(isinstance(ei, list))
451+ self.assertTrue(len(ei) == 1)
452+ self.assertEqual(ei[0]['info2'], '')
453+ self.assertEqual(ei[0]['label'], 'More info')
454+
455+ # write some data out and reload the DB
456+ a.write_extra_information_file("info", "whatever")
457+ ei = a.get_extra_information(self.ACCOMP_SET, "info")
458+ self.assertEqual(ei[0]['info'], 'whatever')
459+ a.write_extra_information_file("info2", "whatever2")
460+ ei = a.get_extra_information(self.ACCOMP_SET, "info2")
461+ self.assertEqual(ei[0]['info2'], 'whatever2')
462+
463+ # get all extra information
464+ all_extra_info = a.get_all_extra_information()
465+ self.assertTrue(isinstance(all_extra_info, list))
466+ self.assertTrue(len(all_extra_info) == 2)
467+ for ei in all_extra_info:
468+ self.assertTrue(isinstance(ei, dict))
469+ self.assertEquals(ei['collection'], self.ACCOMP_SET)
470+ self.assertTrue(ei['description'] is not None)
471+ self.assertTrue(ei['example'] is not None)
472+ self.assertTrue(ei['needs-information'] is not None)
473+ self.assertTrue(ei['regex'] is '')
474+
475+ # get all extra information required
476+ # clear out the extra info files, so everything is required
477+ a.write_extra_information_file("info", None)
478+ a.write_extra_information_file("info2", None)
479+ all_extra_info_required = a.get_all_extra_information_required()
480+ self.assertTrue(isinstance(all_extra_info, list))
481+ self.assertTrue(len(all_extra_info) == 2)
482+
483+ # now mark fill them in with info
484+ a.write_extra_information_file("info", "whatever")
485+ a.write_extra_information_file("info2", "whatever2")
486+ all_extra_info_required = a.get_all_extra_information_required()
487+ self.assertTrue(isinstance(all_extra_info_required, list))
488+ self.assertTrue(len(all_extra_info_required) == 0)
489+
490 if __name__ == "__main__":
491 unittest.main()
492
493=== modified file 'debian/control'
494--- debian/control 2012-05-29 18:37:07 +0000
495+++ debian/control 2012-07-14 02:21:18 +0000
496@@ -4,6 +4,7 @@
497 Build-Depends: cdbs (>= 0.4.43),
498 debhelper (>= 6),
499 python (>= 2.6.6-3~),
500+ python-nose,
501 python-distutils-extra (>= 2.10)
502 Maintainer: Jono Bacon <jono@ubuntu.com>
503 Standards-Version: 3.9.3
504
505=== modified file 'debian/rules'
506--- debian/rules 2012-04-12 00:23:38 +0000
507+++ debian/rules 2012-07-14 02:21:18 +0000
508@@ -6,4 +6,9 @@
509 dh $@ --with python2
510 endif
511
512-
513+override_dh_auto_test:
514+ifeq ($(filter nocheck,$(DEB_BUILD_OPTIONS)),)
515+ set -ex; for python in $(shell pyversions -r) ; do \
516+ $$python /usr/bin/nosetests -v ; \
517+ done
518+endif

Subscribers

People subscribed via source and target branches