Merge lp:~raoul-snyman/openlp/bug-1451237 into lp:openlp

Proposed by Raoul Snyman
Status: Merged
Merged at revision: 2536
Proposed branch: lp:~raoul-snyman/openlp/bug-1451237
Merge into: lp:openlp
Diff against target: 368 lines (+158/-122)
2 files modified
openlp/plugins/images/lib/mediaitem.py (+13/-0)
tests/functional/openlp_plugins/images/test_lib.py (+145/-122)
To merge this branch: bzr merge lp:~raoul-snyman/openlp/bug-1451237
Reviewer Review Type Date Requested Status
Tim Bentley Approve
Review via email: mp+259336@code.launchpad.net

Description of the change

Fix bug#1451237 by overriding the create_item_from_id() method in the Images MediaItem.

Add this to your merge proposal:
--------------------------------
lp:~raoul-snyman/openlp/bug-1451237 (revision 2537)
[SUCCESS] https//ci.openlp.io/job/Branch-01-Pull/1019/
[SUCCESS] https//ci.openlp.io/job/Branch-02-Functional-Tests/942/
[SUCCESS] https//ci.openlp.io/job/Branch-03-Interface-Tests/884/
[SUCCESS] https//ci.openlp.io/job/Branch-04a-Windows_Functional_Tests/771/
[SUCCESS] https//ci.openlp.io/job/Branch-04b-Windows_Interface_Tests/369/
[SUCCESS] https//ci.openlp.io/job/Branch-05a-Code_Analysis/501/
[SUCCESS] https//ci.openlp.io/job/Branch-05b-Test_Coverage/372/

To post a comment you must log in.
Revision history for this message
Tim Bentley (trb143) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'openlp/plugins/images/lib/mediaitem.py'
2--- openlp/plugins/images/lib/mediaitem.py 2015-04-22 19:37:32 +0000
3+++ openlp/plugins/images/lib/mediaitem.py 2015-05-17 21:27:07 +0000
4@@ -551,6 +551,7 @@
5 service_item.title = items[0].text(0)
6 else:
7 service_item.title = str(self.plugin.name_strings['plural'])
8+
9 service_item.add_capability(ItemCapabilities.CanMaintain)
10 service_item.add_capability(ItemCapabilities.CanPreview)
11 service_item.add_capability(ItemCapabilities.CanLoop)
12@@ -697,3 +698,15 @@
13 filename = os.path.split(str(file_object.filename))[1]
14 results.append([file_object.filename, filename])
15 return results
16+
17+ def create_item_from_id(self, item_id):
18+ """
19+ Create a media item from an item id. Overridden from the parent method to change the item type.
20+
21+ :param item_id: Id to make live
22+ """
23+ item = QtGui.QTreeWidgetItem()
24+ item_data = self.manager.get_object_filtered(ImageFilenames, ImageFilenames.filename == item_id)
25+ item.setText(0, os.path.basename(item_data.filename))
26+ item.setData(0, QtCore.Qt.UserRole, item_data)
27+ return item
28
29=== modified file 'tests/functional/openlp_plugins/images/test_lib.py'
30--- tests/functional/openlp_plugins/images/test_lib.py 2015-04-22 21:05:46 +0000
31+++ tests/functional/openlp_plugins/images/test_lib.py 2015-05-17 21:27:07 +0000
32@@ -24,6 +24,8 @@
33 """
34 from unittest import TestCase
35
36+from PyQt4 import QtCore, QtGui
37+
38 from openlp.core.common import Registry
39 from openlp.plugins.images.lib.db import ImageFilenames, ImageGroups
40 from openlp.plugins.images.lib.mediaitem import ImageMediaItem
41@@ -48,123 +50,121 @@
42 self.media_item = ImageMediaItem(None, mocked_plugin)
43 self.media_item.settings_section = 'images'
44
45- def validate_and_load_test(self):
46+ @patch('openlp.plugins.images.lib.mediaitem.ImageMediaItem.load_list')
47+ @patch('openlp.plugins.images.lib.mediaitem.Settings')
48+ def validate_and_load_test(self, mocked_settings, mocked_load_list):
49 """
50 Test that the validate_and_load_test() method when called without a group
51 """
52 # GIVEN: A list of files
53 file_list = ['/path1/image1.jpg', '/path2/image2.jpg']
54
55- with patch('openlp.plugins.images.lib.mediaitem.ImageMediaItem.load_list') as mocked_load_list, \
56- patch('openlp.plugins.images.lib.mediaitem.Settings') as mocked_settings:
57-
58- # WHEN: Calling validate_and_load with the list of files
59- self.media_item.validate_and_load(file_list)
60-
61- # THEN: load_list should have been called with the file list and None,
62- # the directory should have been saved to the settings
63- mocked_load_list.assert_called_once_with(file_list, None)
64- mocked_settings().setValue.assert_called_once_with(ANY, '/path1')
65-
66- def validate_and_load_group_test(self):
67+ # WHEN: Calling validate_and_load with the list of files
68+ self.media_item.validate_and_load(file_list)
69+
70+ # THEN: load_list should have been called with the file list and None,
71+ # the directory should have been saved to the settings
72+ mocked_load_list.assert_called_once_with(file_list, None)
73+ mocked_settings().setValue.assert_called_once_with(ANY, '/path1')
74+
75+ @patch('openlp.plugins.images.lib.mediaitem.ImageMediaItem.load_list')
76+ @patch('openlp.plugins.images.lib.mediaitem.Settings')
77+ def validate_and_load_group_test(self, mocked_settings, mocked_load_list):
78 """
79 Test that the validate_and_load_test() method when called with a group
80 """
81 # GIVEN: A list of files
82 file_list = ['/path1/image1.jpg', '/path2/image2.jpg']
83
84- with patch('openlp.plugins.images.lib.mediaitem.ImageMediaItem.load_list') as mocked_load_list, \
85- patch('openlp.plugins.images.lib.mediaitem.Settings') as mocked_settings:
86-
87- # WHEN: Calling validate_and_load with the list of files and a group
88- self.media_item.validate_and_load(file_list, 'group')
89-
90- # THEN: load_list should have been called with the file list and the group name,
91- # the directory should have been saved to the settings
92- mocked_load_list.assert_called_once_with(file_list, 'group')
93- mocked_settings().setValue.assert_called_once_with(ANY, '/path1')
94-
95- def save_new_images_list_empty_list_test(self):
96+ # WHEN: Calling validate_and_load with the list of files and a group
97+ self.media_item.validate_and_load(file_list, 'group')
98+
99+ # THEN: load_list should have been called with the file list and the group name,
100+ # the directory should have been saved to the settings
101+ mocked_load_list.assert_called_once_with(file_list, 'group')
102+ mocked_settings().setValue.assert_called_once_with(ANY, '/path1')
103+
104+ @patch('openlp.plugins.images.lib.mediaitem.ImageMediaItem.load_full_list')
105+ def save_new_images_list_empty_list_test(self, mocked_load_full_list):
106 """
107 Test that the save_new_images_list() method handles empty lists gracefully
108 """
109 # GIVEN: An empty image_list
110 image_list = []
111- with patch('openlp.plugins.images.lib.mediaitem.ImageMediaItem.load_full_list'):
112- self.media_item.manager = MagicMock()
113-
114- # WHEN: We run save_new_images_list with the empty list
115- self.media_item.save_new_images_list(image_list)
116-
117- # THEN: The save_object() method should not have been called
118- self.assertEquals(self.media_item.manager.save_object.call_count, 0,
119- 'The save_object() method should not have been called')
120-
121- def save_new_images_list_single_image_with_reload_test(self):
122+ self.media_item.manager = MagicMock()
123+
124+ # WHEN: We run save_new_images_list with the empty list
125+ self.media_item.save_new_images_list(image_list)
126+
127+ # THEN: The save_object() method should not have been called
128+ self.assertEquals(self.media_item.manager.save_object.call_count, 0,
129+ 'The save_object() method should not have been called')
130+
131+ @patch('openlp.plugins.images.lib.mediaitem.ImageMediaItem.load_full_list')
132+ def save_new_images_list_single_image_with_reload_test(self, mocked_load_full_list):
133 """
134 Test that the save_new_images_list() calls load_full_list() when reload_list is set to True
135 """
136 # GIVEN: A list with 1 image and a mocked out manager
137 image_list = ['test_image.jpg']
138- with patch('openlp.plugins.images.lib.mediaitem.ImageMediaItem.load_full_list') as mocked_load_full_list:
139- ImageFilenames.filename = ''
140- self.media_item.manager = MagicMock()
141-
142- # WHEN: We run save_new_images_list with reload_list=True
143- self.media_item.save_new_images_list(image_list, reload_list=True)
144-
145- # THEN: load_full_list() should have been called
146- self.assertEquals(mocked_load_full_list.call_count, 1, 'load_full_list() should have been called')
147-
148- # CLEANUP: Remove added attribute from ImageFilenames
149- delattr(ImageFilenames, 'filename')
150-
151- def save_new_images_list_single_image_without_reload_test(self):
152+ ImageFilenames.filename = ''
153+ self.media_item.manager = MagicMock()
154+
155+ # WHEN: We run save_new_images_list with reload_list=True
156+ self.media_item.save_new_images_list(image_list, reload_list=True)
157+
158+ # THEN: load_full_list() should have been called
159+ self.assertEquals(mocked_load_full_list.call_count, 1, 'load_full_list() should have been called')
160+
161+ # CLEANUP: Remove added attribute from ImageFilenames
162+ delattr(ImageFilenames, 'filename')
163+
164+ @patch('openlp.plugins.images.lib.mediaitem.ImageMediaItem.load_full_list')
165+ def save_new_images_list_single_image_without_reload_test(self, mocked_load_full_list):
166 """
167 Test that the save_new_images_list() doesn't call load_full_list() when reload_list is set to False
168 """
169 # GIVEN: A list with 1 image and a mocked out manager
170 image_list = ['test_image.jpg']
171- with patch('openlp.plugins.images.lib.mediaitem.ImageMediaItem.load_full_list') as mocked_load_full_list:
172- self.media_item.manager = MagicMock()
173-
174- # WHEN: We run save_new_images_list with reload_list=False
175- self.media_item.save_new_images_list(image_list, reload_list=False)
176-
177- # THEN: load_full_list() should not have been called
178- self.assertEquals(mocked_load_full_list.call_count, 0, 'load_full_list() should not have been called')
179-
180- def save_new_images_list_multiple_images_test(self):
181+ self.media_item.manager = MagicMock()
182+
183+ # WHEN: We run save_new_images_list with reload_list=False
184+ self.media_item.save_new_images_list(image_list, reload_list=False)
185+
186+ # THEN: load_full_list() should not have been called
187+ self.assertEquals(mocked_load_full_list.call_count, 0, 'load_full_list() should not have been called')
188+
189+ @patch('openlp.plugins.images.lib.mediaitem.ImageMediaItem.load_full_list')
190+ def save_new_images_list_multiple_images_test(self, mocked_load_full_list):
191 """
192 Test that the save_new_images_list() saves all images in the list
193 """
194 # GIVEN: A list with 3 images
195 image_list = ['test_image_1.jpg', 'test_image_2.jpg', 'test_image_3.jpg']
196- with patch('openlp.plugins.images.lib.mediaitem.ImageMediaItem.load_full_list') as mocked_load_full_list:
197- self.media_item.manager = MagicMock()
198-
199- # WHEN: We run save_new_images_list with the list of 3 images
200- self.media_item.save_new_images_list(image_list, reload_list=False)
201-
202- # THEN: load_full_list() should not have been called
203- self.assertEquals(self.media_item.manager.save_object.call_count, 3,
204- 'load_full_list() should have been called three times')
205-
206- def save_new_images_list_other_objects_in_list_test(self):
207+ self.media_item.manager = MagicMock()
208+
209+ # WHEN: We run save_new_images_list with the list of 3 images
210+ self.media_item.save_new_images_list(image_list, reload_list=False)
211+
212+ # THEN: load_full_list() should not have been called
213+ self.assertEquals(self.media_item.manager.save_object.call_count, 3,
214+ 'load_full_list() should have been called three times')
215+
216+ @patch('openlp.plugins.images.lib.mediaitem.ImageMediaItem.load_full_list')
217+ def save_new_images_list_other_objects_in_list_test(self, mocked_load_full_list):
218 """
219 Test that the save_new_images_list() ignores everything in the provided list except strings
220 """
221 # GIVEN: A list with images and objects
222 image_list = ['test_image_1.jpg', None, True, ImageFilenames(), 'test_image_2.jpg']
223- with patch('openlp.plugins.images.lib.mediaitem.ImageMediaItem.load_full_list') as mocked_load_full_list:
224- self.media_item.manager = MagicMock()
225-
226- # WHEN: We run save_new_images_list with the list of images and objects
227- self.media_item.save_new_images_list(image_list, reload_list=False)
228-
229- # THEN: load_full_list() should not have been called
230- self.assertEquals(self.media_item.manager.save_object.call_count, 2,
231- 'load_full_list() should have been called only once')
232+ self.media_item.manager = MagicMock()
233+
234+ # WHEN: We run save_new_images_list with the list of images and objects
235+ self.media_item.save_new_images_list(image_list, reload_list=False)
236+
237+ # THEN: load_full_list() should not have been called
238+ self.assertEquals(self.media_item.manager.save_object.call_count, 2,
239+ 'load_full_list() should have been called only once')
240
241 def on_reset_click_test(self):
242 """
243@@ -180,31 +180,31 @@
244 self.media_item.reset_action.setVisible.assert_called_with(False)
245 self.media_item.live_controller.display.reset_image.assert_called_with()
246
247- def recursively_delete_group_test(self):
248+ @patch('openlp.plugins.images.lib.mediaitem.delete_file')
249+ def recursively_delete_group_test(self, mocked_delete_file):
250 """
251 Test that recursively_delete_group() works
252 """
253 # GIVEN: An ImageGroups object and mocked functions
254- with patch('openlp.plugins.images.lib.mediaitem.delete_file') as mocked_delete_file:
255- ImageFilenames.group_id = 1
256- ImageGroups.parent_id = 1
257- self.media_item.manager = MagicMock()
258- self.media_item.manager.get_all_objects.side_effect = self._recursively_delete_group_side_effect
259- self.media_item.service_path = ''
260- test_group = ImageGroups()
261- test_group.id = 1
262-
263- # WHEN: recursively_delete_group() is called
264- self.media_item.recursively_delete_group(test_group)
265-
266- # THEN: delete_file() should have been called 12 times and manager.delete_object() 7 times.
267- self.assertEquals(mocked_delete_file.call_count, 12, 'delete_file() should have been called 12 times')
268- self.assertEquals(self.media_item.manager.delete_object.call_count, 7,
269- 'manager.delete_object() should be called exactly 7 times')
270-
271- # CLEANUP: Remove added attribute from Image Filenames and ImageGroups
272- delattr(ImageFilenames, 'group_id')
273- delattr(ImageGroups, 'parent_id')
274+ ImageFilenames.group_id = 1
275+ ImageGroups.parent_id = 1
276+ self.media_item.manager = MagicMock()
277+ self.media_item.manager.get_all_objects.side_effect = self._recursively_delete_group_side_effect
278+ self.media_item.service_path = ''
279+ test_group = ImageGroups()
280+ test_group.id = 1
281+
282+ # WHEN: recursively_delete_group() is called
283+ self.media_item.recursively_delete_group(test_group)
284+
285+ # THEN: delete_file() should have been called 12 times and manager.delete_object() 7 times.
286+ self.assertEquals(mocked_delete_file.call_count, 12, 'delete_file() should have been called 12 times')
287+ self.assertEquals(self.media_item.manager.delete_object.call_count, 7,
288+ 'manager.delete_object() should be called exactly 7 times')
289+
290+ # CLEANUP: Remove added attribute from Image Filenames and ImageGroups
291+ delattr(ImageFilenames, 'group_id')
292+ delattr(ImageGroups, 'parent_id')
293
294 def _recursively_delete_group_side_effect(*args, **kwargs):
295 """
296@@ -231,28 +231,51 @@
297 return [returned_object1]
298 return []
299
300- def on_delete_click_test(self):
301+ @patch('openlp.plugins.images.lib.mediaitem.delete_file')
302+ @patch('openlp.plugins.images.lib.mediaitem.check_item_selected')
303+ def on_delete_click_test(self, mocked_check_item_selected, mocked_delete_file):
304 """
305 Test that on_delete_click() works
306 """
307 # GIVEN: An ImageGroups object and mocked functions
308- with patch('openlp.plugins.images.lib.mediaitem.delete_file') as mocked_delete_file, \
309- patch('openlp.plugins.images.lib.mediaitem.check_item_selected') as mocked_check_item_selected:
310- mocked_check_item_selected.return_value = True
311- test_image = ImageFilenames()
312- test_image.id = 1
313- test_image.group_id = 1
314- test_image.filename = 'imagefile.png'
315- self.media_item.manager = MagicMock()
316- self.media_item.service_path = ''
317- self.media_item.list_view = MagicMock()
318- mocked_row_item = MagicMock()
319- mocked_row_item.data.return_value = test_image
320- mocked_row_item.text.return_value = ''
321- self.media_item.list_view.selectedItems.return_value = [mocked_row_item]
322-
323- # WHEN: Calling on_delete_click
324- self.media_item.on_delete_click()
325-
326- # THEN: delete_file should have been called twice
327- self.assertEquals(mocked_delete_file.call_count, 2, 'delete_file() should have been called twice')
328+ mocked_check_item_selected.return_value = True
329+ test_image = ImageFilenames()
330+ test_image.id = 1
331+ test_image.group_id = 1
332+ test_image.filename = 'imagefile.png'
333+ self.media_item.manager = MagicMock()
334+ self.media_item.service_path = ''
335+ self.media_item.list_view = MagicMock()
336+ mocked_row_item = MagicMock()
337+ mocked_row_item.data.return_value = test_image
338+ mocked_row_item.text.return_value = ''
339+ self.media_item.list_view.selectedItems.return_value = [mocked_row_item]
340+
341+ # WHEN: Calling on_delete_click
342+ self.media_item.on_delete_click()
343+
344+ # THEN: delete_file should have been called twice
345+ self.assertEquals(mocked_delete_file.call_count, 2, 'delete_file() should have been called twice')
346+
347+ def create_item_from_id_test(self):
348+ """
349+ Test that the create_item_from_id() method returns a valid QTreeWidgetItem with a pre-created ImageFilenames
350+ """
351+ # GIVEN: An ImageFilenames that already exists in the database
352+ image_file = ImageFilenames()
353+ image_file.id = 1
354+ image_file.filename = '/tmp/test_file_1.jpg'
355+ self.media_item.manager = MagicMock()
356+ self.media_item.manager.get_object_filtered.return_value = image_file
357+ ImageFilenames.filename = ''
358+
359+ # WHEN: create_item_from_id() is called
360+ item = self.media_item.create_item_from_id(1)
361+
362+ # THEN: A QTreeWidgetItem should be created with the above model object as it's data
363+ self.assertIsInstance(item, QtGui.QTreeWidgetItem)
364+ self.assertEqual('test_file_1.jpg', item.text(0))
365+ item_data = item.data(0, QtCore.Qt.UserRole)
366+ self.assertIsInstance(item_data, ImageFilenames)
367+ self.assertEqual(1, item_data.id)
368+ self.assertEqual('/tmp/test_file_1.jpg', item_data.filename)