Merge lp:~mvo/software-center/test-catview-cleanup into lp:software-center/5.2

Proposed by Michael Vogt on 2012-05-22
Status: Merged
Merged at revision: 3033
Proposed branch: lp:~mvo/software-center/test-catview-cleanup
Merge into: lp:software-center/5.2
Diff against target: 371 lines (+97/-125)
3 files modified
softwarecenter/testutils.py (+6/-0)
softwarecenter/ui/gtk3/views/catview_gtk.py (+12/-9)
test/gtk3/test_catview.py (+79/-116)
To merge this branch: bzr merge lp:~mvo/software-center/test-catview-cleanup
Reviewer Review Type Date Requested Status
Gary Lasker (community) 2012-05-22 Approve on 2012-05-23
Review via email: mp+106831@code.launchpad.net

This proposal supersedes a proposal from 2012-05-22.

Description of the change

This branch refactors the test_catview.py code:
- split the TestCases for the Recommender and the Top Rated/Whats New
- remove duplicated code in the tests by using setUp()/tearDown() more efficiently
- use the patch decorator more
- avoid re-creating the DB in each test via setUpClass, this could be optimized further by just having one global instance for the entire test

To post a comment you must log in.
3035. By Michael Vogt on 2012-05-22

merge 5.2 and resolve conflicts

Gary Lasker (gary-lasker) wrote :

Very nice, thank you, mvo!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'softwarecenter/testutils.py'
2--- softwarecenter/testutils.py 2012-05-21 21:33:20 +0000
3+++ softwarecenter/testutils.py 2012-05-22 15:42:19 +0000
4@@ -144,6 +144,12 @@
5 main_loop.iteration()
6
7
8+def do_events_with_sleep(iterations=5, sleep=0.1):
9+ for i in range(iterations):
10+ do_events()
11+ time.sleep(sleep)
12+
13+
14 def get_mock_app_from_real_app(real_app):
15 """ take a application and return a app where the details are a mock
16 of the real details so they can easily be modified
17
18=== modified file 'softwarecenter/ui/gtk3/views/catview_gtk.py'
19--- softwarecenter/ui/gtk3/views/catview_gtk.py 2012-05-22 14:36:03 +0000
20+++ softwarecenter/ui/gtk3/views/catview_gtk.py 2012-05-22 15:42:19 +0000
21@@ -728,21 +728,24 @@
22 #return
23
24
25-def get_test_window_catview():
26+def get_test_window_catview(db=None):
27
28 def on_category_selected(view, cat):
29 print "on_category_selected view: ", view
30 print "on_category_selected cat: ", cat
31
32- from softwarecenter.db.pkginfo import get_pkg_info
33- cache = get_pkg_info()
34- cache.open()
35+ if db is None:
36+ from softwarecenter.db.pkginfo import get_pkg_info
37+ cache = get_pkg_info()
38+ cache.open()
39
40- from softwarecenter.db.database import StoreDatabase
41- xapian_base_path = "/var/cache/software-center"
42- pathname = os.path.join(xapian_base_path, "xapian")
43- db = StoreDatabase(pathname, cache)
44- db.open()
45+ from softwarecenter.db.database import StoreDatabase
46+ xapian_base_path = "/var/cache/software-center"
47+ pathname = os.path.join(xapian_base_path, "xapian")
48+ db = StoreDatabase(pathname, cache)
49+ db.open()
50+ else:
51+ cache = db._aptcache
52
53 import softwarecenter.paths
54 datadir = softwarecenter.paths.datadir
55
56=== modified file 'test/gtk3/test_catview.py'
57--- test/gtk3/test_catview.py 2012-05-22 14:18:49 +0000
58+++ test/gtk3/test_catview.py 2012-05-22 15:42:19 +0000
59@@ -1,4 +1,3 @@
60-import time
61 import unittest
62
63 from gi.repository import Gtk
64@@ -13,6 +12,8 @@
65 from softwarecenter.db.database import StoreDatabase
66 from softwarecenter.enums import SortMethods
67 from softwarecenter.testutils import (
68+ do_events_with_sleep,
69+ do_events,
70 FakedCache,
71 get_test_db,
72 make_recommender_agent_recommend_me_dict,
73@@ -24,49 +25,51 @@
74 from softwarecenter.ui.gtk3.widgets.spinner import SpinnerNotebook
75
76
77-class TestCatView(unittest.TestCase):
78+class CatViewBaseTestCase(unittest.TestCase):
79+
80+ @classmethod
81+ def setUpClass(cls):
82+ cls.db = get_test_db()
83
84 def setUp(self):
85 self._cat = None
86- self.db = get_test_db()
87+ self.win = get_test_window_catview(self.db)
88+ self.addCleanup(self.win.destroy)
89+ self.notebook = self.win.get_child()
90+ self.lobby = self.win.get_data("lobby")
91+ self.subcat_view = self.win.get_data("subcat")
92+ self.rec_panel = self.lobby.recommended_for_you_panel
93
94 def _on_category_selected(self, subcatview, category):
95 self._cat = category
96
97+
98+class TopAndWhatsNewTestCase(CatViewBaseTestCase):
99+
100 def test_top_rated(self):
101- # get the widgets we need
102- win = get_test_window_catview()
103- self.addCleanup(win.destroy)
104- lobby = win.get_data("lobby")
105-
106 # simulate review-stats refresh
107- lobby._update_top_rated_content = Mock()
108- lobby.reviews_loader.emit("refresh-review-stats-finished", [])
109- self.assertTrue(lobby._update_top_rated_content.called)
110+ self.lobby._update_top_rated_content = Mock()
111+ self.lobby.reviews_loader.emit("refresh-review-stats-finished", [])
112+ self.assertTrue(self.lobby._update_top_rated_content.called)
113
114 # test clicking top_rated
115- lobby.connect("category-selected", self._on_category_selected)
116- lobby.top_rated_frame.more.clicked()
117- self._p()
118+ self.lobby.connect("category-selected", self._on_category_selected)
119+ self.lobby.top_rated_frame.more.clicked()
120+ do_events()
121 self.assertNotEqual(self._cat, None)
122 self.assertEqual(self._cat.name, "Top Rated")
123 self.assertEqual(self._cat.sortmode, SortMethods.BY_TOP_RATED)
124
125 def test_new(self):
126- # get the widgets we need
127- win = get_test_window_catview()
128- self.addCleanup(win.destroy)
129- lobby = win.get_data("lobby")
130-
131 # test db reopen triggers whats-new update
132- lobby._update_whats_new_content = Mock()
133- lobby.db.emit("reopen")
134- self.assertTrue(lobby._update_whats_new_content.called)
135+ self.lobby._update_whats_new_content = Mock()
136+ self.lobby.db.emit("reopen")
137+ self.assertTrue(self.lobby._update_whats_new_content.called)
138
139 # test clicking new
140- lobby.connect("category-selected", self._on_category_selected)
141- lobby.whats_new_frame.more.clicked()
142- self._p()
143+ self.lobby.connect("category-selected", self._on_category_selected)
144+ self.lobby.whats_new_frame.more.clicked()
145+ do_events()
146 self.assertNotEqual(self._cat, None)
147 # encoding is utf-8 (since r2218, see category.py)
148 self.assertEqual(self._cat.name, 'What\xe2\x80\x99s New')
149@@ -105,145 +108,109 @@
150 win.add(scroll)
151 win.show()
152 # test visibility
153- self._p()
154+ do_events()
155 self.assertFalse(view.whats_new_frame.get_property("visible"))
156- self._p()
157-
158- def test_recommended_for_you_opt_in_display(self):
159+
160+
161+class RecommendationsTestCase(CatViewBaseTestCase):
162+ """The test suite for the recommendations ."""
163+
164+ @patch('softwarecenter.backend.recagent.RecommenderAgent.is_opted_in')
165+ def test_recommended_for_you_opt_in_display(
166+ self, mock_get_recommender_opted_in):
167 # patch the recommender UUID value to ensure that we are not opted-in
168 # for this test
169- recommender_opted_in_patcher = patch(
170- 'softwarecenter.backend.recagent.RecommenderAgent.is_opted_in')
171- self.addCleanup(recommender_opted_in_patcher.stop)
172- mock_get_recommender_opted_in = recommender_opted_in_patcher.start()
173 mock_get_recommender_opted_in.return_value = False
174
175- # get the widgets we need
176- win = get_test_window_catview()
177- self.addCleanup(win.destroy)
178-
179- lobby = win.get_data("lobby")
180- rec_panel = lobby.recommended_for_you_panel
181- self._p()
182- self.assertEqual(rec_panel.spinner_notebook.get_current_page(),
183+ do_events()
184+ self.assertEqual(self.rec_panel.spinner_notebook.get_current_page(),
185 FramedHeaderBox.CONTENT)
186- self.assertTrue(rec_panel.opt_in_vbox.get_property("visible"))
187+ self.assertTrue(self.rec_panel.opt_in_vbox.get_property("visible"))
188
189 # patch out the agent query method to avoid making the actual server call
190+ @patch('softwarecenter.backend.recagent.RecommenderAgent.is_opted_in')
191 @patch('softwarecenter.backend.recagent.RecommenderAgent'
192 '.post_submit_profile')
193- def test_recommended_for_you_spinner_display(self, mock_query):
194+ def test_recommended_for_you_spinner_display(
195+ self, mock_query, mock_get_recommender_opted_in):
196 # patch the recommender UUID value to insure that we are not opted-in
197 # for this test
198- recommender_opted_in_patcher = patch(
199- 'softwarecenter.backend.recagent.RecommenderAgent.is_opted_in')
200- self.addCleanup(recommender_opted_in_patcher.stop)
201- mock_get_recommender_opted_in = recommender_opted_in_patcher.start()
202 mock_get_recommender_opted_in.return_value = False
203
204- # get the widgets we need
205- win = get_test_window_catview()
206- self.addCleanup(win.destroy)
207- lobby = win.get_data("lobby")
208- rec_panel = lobby.recommended_for_you_panel
209- self._p()
210 # click the opt-in button to initiate the process,
211 # this will show the spinner
212- rec_panel.opt_in_button.emit('clicked')
213- self._p()
214- self.assertEqual(rec_panel.spinner_notebook.get_current_page(),
215+ self.rec_panel.opt_in_button.emit('clicked')
216+ do_events()
217+ self.assertEqual(self.rec_panel.spinner_notebook.get_current_page(),
218 SpinnerNotebook.SPINNER_PAGE)
219- self.assertTrue(rec_panel.opt_in_vbox.get_property("visible"))
220+ self.assertTrue(self.rec_panel.opt_in_vbox.get_property("visible"))
221
222 # patch out the agent query method to avoid making the actual server call
223+ @patch('softwarecenter.backend.recagent.RecommenderAgent.is_opted_in')
224 @patch('softwarecenter.backend.recagent.RecommenderAgent'
225 '.post_submit_profile')
226 def test_recommended_for_you_display_recommendations(self,
227- mock_query):
228+ mock_query, mock_get_recommender_opted_in):
229 # patch the recommender UUID value to insure that we are not opted-in
230 # for this test
231- recommender_opted_in_patcher = patch(
232- 'softwarecenter.backend.recagent.RecommenderAgent.is_opted_in')
233- self.addCleanup(recommender_opted_in_patcher.stop)
234- mock_get_recommender_opted_in = recommender_opted_in_patcher.start()
235 mock_get_recommender_opted_in.return_value = False
236
237- # get the widgets we need
238- win = get_test_window_catview()
239- self.addCleanup(win.destroy)
240- lobby = win.get_data("lobby")
241- rec_panel = lobby.recommended_for_you_panel
242- self._p()
243 # click the opt-in button to initiate the process,
244 # this will show the spinner
245- rec_panel.opt_in_button.emit('clicked')
246- self._p()
247- rec_panel._update_recommended_for_you_content()
248- self._p()
249+ self.rec_panel.opt_in_button.emit('clicked')
250+ do_events()
251+ self.rec_panel._update_recommended_for_you_content()
252+ do_events()
253 # we fake the callback from the agent here
254- for_you = lobby.recommended_for_you_panel.recommended_for_you_cat
255+ for_you = self.lobby.recommended_for_you_panel.recommended_for_you_cat
256 for_you._recommend_me_result(None,
257 make_recommender_agent_recommend_me_dict())
258 self.assertNotEqual(for_you.get_documents(self.db), [])
259- self.assertEqual(rec_panel.spinner_notebook.get_current_page(),
260+ self.assertEqual(self.rec_panel.spinner_notebook.get_current_page(),
261 SpinnerNotebook.CONTENT_PAGE)
262- self._p()
263+ do_events()
264 # test clicking recommended_for_you More button
265- lobby.connect("category-selected", self._on_category_selected)
266- lobby.recommended_for_you_panel.more.clicked()
267- self._p()
268+ self.lobby.connect("category-selected", self._on_category_selected)
269+ self.lobby.recommended_for_you_panel.more.clicked()
270+ # this is delayed for some reason so we need to sleep here
271+ do_events_with_sleep()
272 self.assertNotEqual(self._cat, None)
273 self.assertEqual(self._cat.name, "Recommended For You")
274
275 # patch out the agent query method to avoid making the actual server call
276+ @patch('softwarecenter.backend.recagent.RecommenderAgent.is_opted_in')
277 @patch('softwarecenter.backend.recagent.RecommenderAgent'
278 '.query_recommend_me')
279 def test_recommended_for_you_display_recommendations_not_opted_in(self,
280- mock_query):
281+ mock_query, mock_get_recommender_opted_in):
282 # patch the recommender UUID value to insure that we are not opted-in
283 # for this test
284- recommender_opted_in_patcher = patch(
285- 'softwarecenter.backend.recagent.RecommenderAgent.is_opted_in')
286- self.addCleanup(recommender_opted_in_patcher.stop)
287- mock_get_recommender_opted_in = recommender_opted_in_patcher.start()
288 mock_get_recommender_opted_in.return_value = False
289
290- # get the widgets we need
291- win = get_test_window_catview()
292- self.addCleanup(win.destroy)
293 # we want to work in the "subcat" view
294- notebook = win.get_child()
295- notebook.next_page()
296+ self.notebook.next_page()
297
298- subcat_view = win.get_data("subcat")
299- self._p()
300- self.assertFalse(subcat_view.recommended_for_you_in_cat.get_property(
301- "visible"))
302+ do_events()
303+ visible = self.subcat_view.recommended_for_you_in_cat.get_property(
304+ "visible")
305+ self.assertFalse(visible)
306
307 # patch out the agent query method to avoid making the actual server call
308+ @patch('softwarecenter.backend.recagent.RecommenderAgent.is_opted_in')
309 @patch('softwarecenter.backend.recagent.RecommenderAgent'
310 '.query_recommend_me')
311- def test_recommended_for_you_display_recommendations_opted_in(self, mock_query):
312+ def test_recommended_for_you_display_recommendations_opted_in(
313+ self, mock_query, mock_get_recommender_opted_in):
314 # patch the recommender UUID value to insure that we are not opted-in
315 # for this test
316- recommender_opted_in_patcher = patch(
317- 'softwarecenter.backend.recagent.RecommenderAgent.is_opted_in')
318- self.addCleanup(recommender_opted_in_patcher.stop)
319- mock_get_recommender_opted_in = recommender_opted_in_patcher.start()
320 mock_get_recommender_opted_in.return_value = True
321
322- # get the widgets we need
323- win = get_test_window_catview()
324- self.addCleanup(win.destroy)
325 # we want to work in the "subcat" view
326- notebook = win.get_child()
327- notebook.next_page()
328+ self.notebook.next_page()
329
330- subcat_view = win.get_data("subcat")
331- rec_cat_panel = subcat_view.recommended_for_you_in_cat
332- self._p()
333+ rec_cat_panel = self.subcat_view.recommended_for_you_in_cat
334 rec_cat_panel._update_recommended_for_you_content()
335- self._p()
336+ do_events()
337 # we fake the callback from the agent here
338 rec_cat_panel.recommended_for_you_cat._recommend_me_result(
339 None,
340@@ -258,25 +225,21 @@
341 self.assertEqual(rec_cat_panel.spinner_notebook.get_current_page(),
342 SpinnerNotebook.CONTENT_PAGE)
343 # check that the tiles themselves are visible
344- self._p()
345+ do_events()
346 self.assertTrue(rec_cat_panel.recommended_for_you_content.get_property(
347 "visible"))
348 self.assertTrue(rec_cat_panel.recommended_for_you_content.get_children(
349 )[0].title.get_property("visible"))
350- self._p()
351+ do_events()
352 # test clicking recommended_for_you More button
353- subcat_view.connect("category-selected", self._on_category_selected)
354+ self.subcat_view.connect(
355+ "category-selected", self._on_category_selected)
356 rec_cat_panel.more.clicked()
357- self._p()
358+ # this is delayed for some reason so we need to sleep here
359+ do_events_with_sleep()
360 self.assertNotEqual(self._cat, None)
361 self.assertEqual(self._cat.name, "Recommended For You in Internet")
362
363- def _p(self):
364- for i in range(5):
365- time.sleep(0.1)
366- while Gtk.events_pending():
367- Gtk.main_iteration()
368-
369
370 class ExhibitsTestCase(unittest.TestCase):
371 """The test suite for the exhibits carousel."""

Subscribers

People subscribed via source and target branches