OpenShot Video Editor  2.0.0
query_tests.py
Go to the documentation of this file.
1 ##
2 #
3 # @file
4 # @brief This file contains unit tests for the Query class
5 # @author Jonathan Thomas <jonathan@openshot.org>
6 #
7 # @section LICENSE
8 #
9 # Copyright (c) 2008-2018 OpenShot Studios, LLC
10 # (http://www.openshotstudios.com). This file is part of
11 # OpenShot Video Editor (http://www.openshot.org), an open-source project
12 # dedicated to delivering high quality video editing and animation solutions
13 # to the world.
14 #
15 # OpenShot Video Editor is free software: you can redistribute it and/or modify
16 # it under the terms of the GNU General Public License as published by
17 # the Free Software Foundation, either version 3 of the License, or
18 # (at your option) any later version.
19 #
20 # OpenShot Video Editor is distributed in the hope that it will be useful,
21 # but WITHOUT ANY WARRANTY; without even the implied warranty of
22 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 # GNU General Public License for more details.
24 #
25 # You should have received a copy of the GNU General Public License
26 # along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
27 #
28 
29 import sys, os
30 # Import parent folder (so it can find other imports)
31 PATH = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
32 if not PATH in sys.path:
33  sys.path.append(PATH)
34 
35 import random
36 import unittest
37 import uuid
38 from classes.app import OpenShotApp
39 from classes import info
40 import openshot # Python module for libopenshot (required video editing module installed separately)
41 
42 try:
43  import json
44 except ImportError:
45  import simplejson as json
46 
47 
48 ##
49 # Unit test class for Query class
50 class TestQueryClass(unittest.TestCase):
51 
52  @classmethod
53  ##
54  # Init unit test data
55  def setUpClass(TestQueryClass):
56  # Create Qt application
57  TestQueryClass.app = OpenShotApp(sys.argv, mode="unittest")
58  TestQueryClass.clip_ids = []
59  TestQueryClass.file_ids = []
60  TestQueryClass.transition_ids = []
61 
62  # Import additional classes that need the app defined first
63  from classes.query import Clip, File, Transition
64 
65  # Insert some clips into the project data
66  for num in range(5):
67  # Create clip
68  c = openshot.Clip(os.path.join(info.IMAGES_PATH, "AboutLogo.png"))
69 
70  # Parse JSON
71  clip_data = json.loads(c.Json())
72 
73  # Insert into project data
74  query_clip = Clip()
75  query_clip.data = clip_data
76  query_clip.save()
77 
78  # Keep track of the ids
79  TestQueryClass.clip_ids.append(query_clip.id)
80 
81  # Insert some files into the project data
82  for num in range(5):
83  # Create file
84  r = openshot.DummyReader(openshot.Fraction(24, 1), 640, 480, 44100, 2, 30.0)
85 
86  # Parse JSON
87  file_data = json.loads(r.Json())
88 
89  # Insert into project data
90  query_file = File()
91  query_file.data = file_data
92  query_file.data["path"] = os.path.join(info.IMAGES_PATH, "AboutLogo.png")
93  query_file.data["media_type"] = "image"
94  query_file.save()
95 
96  # Keep track of the ids
97  TestQueryClass.file_ids.append(query_file.id)
98 
99  # Insert some transitions into the project data
100  for num in range(5):
101  # Create mask object
102  transition_object = openshot.Mask()
103  transitions_data = json.loads(transition_object.Json())
104 
105  # Insert into project data
106  query_transition = Transition()
107  query_transition.data = transitions_data
108  query_transition.save()
109 
110  # Keep track of the ids
111  TestQueryClass.transition_ids.append(query_transition.id)
112 
113  @classmethod
114  def tearDownClass(cls):
115  "Hook method for deconstructing the class fixture after running all tests in the class."
116  print ('Exiting Unittests: Quitting QApplication')
117  TestQueryClass.app.window.actionQuit.trigger()
118 
119  ##
120  # Test the Clip.save method by adding multiple clips
121  def test_add_clip(self):
122 
123  # Import additional classes that need the app defined first
124  from classes.query import Clip
125 
126  # Find number of clips in project
127  num_clips = len(Clip.filter())
128 
129  # Create clip
130  c = openshot.Clip(os.path.join(info.IMAGES_PATH, "AboutLogo.png"))
131 
132  # Parse JSON
133  clip_data = json.loads(c.Json())
134 
135  # Insert into project data
136  query_clip = Clip()
137  query_clip.data = clip_data
138  query_clip.save()
139 
140  self.assertTrue(query_clip)
141  self.assertEqual(len(Clip.filter()), num_clips + 1)
142 
143  # Save the clip again (which should not change the total # of clips)
144  query_clip.save()
145 
146  self.assertEqual(len(Clip.filter()), num_clips + 1)
147 
148  ##
149  # Test the Clip.save method
150  def test_update_clip(self):
151 
152  # Import additional classes that need the app defined first
153  from classes.query import Clip
154 
155  # Find a clip named file1
156  update_id = TestQueryClass.clip_ids[0]
157  clip = Clip.get(id=update_id)
158  self.assertTrue(clip)
159 
160  # Update clip
161  clip.data["layer"] = 2
162  clip.data["title"] = "My Title"
163  clip.save()
164 
165  # Verify updated data
166  # Get clip again
167  clip = Clip.get(id=update_id)
168  self.assertEqual(clip.data["layer"], 2)
169  self.assertEqual(clip.data["title"], "My Title")
170 
171  ##
172  # Test the Clip.delete method
173  def test_delete_clip(self):
174 
175  # Import additional classes that need the app defined first
176  from classes.query import Clip
177 
178  # Find a clip named file1
179  delete_id = TestQueryClass.clip_ids[4]
180  clip = Clip.get(id=delete_id)
181  self.assertTrue(clip)
182 
183  # Delete clip
184  clip.delete()
185 
186  # Verify deleted data
187  deleted_clip = Clip.get(id=delete_id)
188  self.assertFalse(deleted_clip)
189 
190  # Delete clip again (should do nothing)
191  clip.delete()
192 
193  # Verify deleted data
194  deleted_clip = Clip.get(id=delete_id)
195  self.assertFalse(deleted_clip)
196 
197  ##
198  # Test the Clip.filter method
199  def test_filter_clip(self):
200 
201  # Import additional classes that need the app defined first
202  from classes.query import Clip
203 
204  # Find all clips named file1
205  clips = Clip.filter(id=TestQueryClass.clip_ids[0])
206  self.assertTrue(clips)
207 
208  # Do not find a clip
209  clips = Clip.filter(id="invalidID")
210  self.assertEqual(len(clips), 0)
211 
212  ##
213  # Test the Clip.get method
214  def test_get_clip(self):
215 
216  # Import additional classes that need the app defined first
217  from classes.query import Clip
218 
219  # Find a clip named file1
220  clip = Clip.get(id=TestQueryClass.clip_ids[1])
221  self.assertTrue(clip)
222 
223  # Do not find a clip
224  clip = Clip.get(id="invalidID")
225  self.assertEqual(clip, None)
226 
227  ##
228  # Test the File.save method
229  def test_update_File(self):
230 
231  # Import additional classes that need the app defined first
232  from classes.query import File
233 
234  # Find a File named file1
235  update_id = TestQueryClass.file_ids[0]
236  file = File.get(id=update_id)
237  self.assertTrue(file)
238 
239  # Update File
240  file.data["height"] = 1080
241  file.data["width"] = 1920
242  file.save()
243 
244  # Verify updated data
245  # Get File again
246  file = File.get(id=update_id)
247  self.assertEqual(file.data["height"], 1080)
248  self.assertEqual(file.data["width"], 1920)
249 
250  ##
251  # Test the File.delete method
252  def test_delete_File(self):
253 
254  # Import additional classes that need the app defined first
255  from classes.query import File
256 
257  # Find a File named file1
258  delete_id = TestQueryClass.file_ids[4]
259  file = File.get(id=delete_id)
260  self.assertTrue(file)
261 
262  # Delete File
263  file.delete()
264 
265  # Verify deleted data
266  deleted_file = File.get(id=delete_id)
267  self.assertFalse(deleted_file)
268 
269  # Delete File again (should do nothing
270  file.delete()
271 
272  # Verify deleted data
273  deleted_file = File.get(id=delete_id)
274  self.assertFalse(deleted_file)
275 
276  ##
277  # Test the File.filter method
278  def test_filter_File(self):
279 
280  # Import additional classes that need the app defined first
281  from classes.query import File
282 
283  # Find all Files named file1
284  files = File.filter(id=TestQueryClass.file_ids[0])
285  self.assertTrue(files)
286 
287  # Do not find a File
288  files = File.filter(id="invalidID")
289  self.assertEqual(len(files), 0)
290 
291  ##
292  # Test the File.get method
293  def test_get_File(self):
294 
295  # Import additional classes that need the app defined first
296  from classes.query import File
297 
298  # Find a File named file1
299  file = File.get(id=TestQueryClass.file_ids[1])
300  self.assertTrue(file)
301 
302  # Do not find a File
303  file = File.get(id="invalidID")
304  self.assertEqual(file, None)
305 
306  ##
307  # Test the File.save method by adding multiple files
308  def test_add_file(self):
309 
310  # Import additional classes that need the app defined first
311  from classes.query import File
312 
313  # Find number of files in project
314  num_files = len(File.filter())
315 
316  # Create file
317  r = openshot.DummyReader(openshot.Fraction(24, 1), 640, 480, 44100, 2, 30.0)
318 
319  # Parse JSON
320  file_data = json.loads(r.Json())
321 
322  # Insert into project data
323  query_file = File()
324  query_file.data = file_data
325  query_file.data["path"] = os.path.join(PATH, "images", "openshot.png")
326  query_file.data["media_type"] = "image"
327  query_file.save()
328 
329  self.assertTrue(query_file)
330  self.assertEqual(len(File.filter()), num_files + 1)
331 
332  # Save the file again (which should not change the total # of files)
333  query_file.save()
334 
335  self.assertEqual(len(File.filter()), num_files + 1)
336 
337 
338 if __name__ == '__main__':
339  unittest.main()
def test_filter_clip(self)
Test the Clip.filter method.
Definition: query_tests.py:199
def test_delete_clip(self)
Test the Clip.delete method.
Definition: query_tests.py:173
def test_add_file(self)
Test the File.save method by adding multiple files.
Definition: query_tests.py:308
def test_update_clip(self)
Test the Clip.save method.
Definition: query_tests.py:150
Unit test class for Query class.
Definition: query_tests.py:50
def test_get_File(self)
Test the File.get method.
Definition: query_tests.py:293
def test_filter_File(self)
Test the File.filter method.
Definition: query_tests.py:278
def test_get_clip(self)
Test the Clip.get method.
Definition: query_tests.py:214
def setUpClass(TestQueryClass)
Init unit test data.
Definition: query_tests.py:55
def test_delete_File(self)
Test the File.delete method.
Definition: query_tests.py:252
def test_update_File(self)
Test the File.save method.
Definition: query_tests.py:229
def test_add_clip(self)
Test the Clip.save method by adding multiple clips.
Definition: query_tests.py:121