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-2016 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)
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 dummy transition
102  transitions_data = {
103  "id": str(num),
104  "layer": num,
105  "title": "Transition",
106  "position": 20.0 + num,
107  "duration": 30 + num
108  }
109 
110  # Insert into project data
111  query_transition = Transition()
112  query_transition.data = transitions_data
113  query_transition.save()
114 
115  # Keep track of the ids
116  TestQueryClass.transition_ids.append(query_transition.id)
117 
118  ##
119  # Test the Clip.save method by adding multiple clips
120  def test_add_clip(self):
121 
122  # Import additional classes that need the app defined first
123  from classes.query import Clip
124 
125  # Find number of clips in project
126  num_clips = len(Clip.filter())
127 
128  # Create clip
129  c = openshot.Clip(os.path.join(info.IMAGES_PATH, "AboutLogo.png"))
130 
131  # Parse JSON
132  clip_data = json.loads(c.Json())
133 
134  # Insert into project data
135  query_clip = Clip()
136  query_clip.data = clip_data
137  query_clip.save()
138 
139  self.assertTrue(query_clip)
140  self.assertEqual(len(Clip.filter()), num_clips + 1)
141 
142  # Save the clip again (which should not change the total # of clips)
143  query_clip.save()
144 
145  self.assertEqual(len(Clip.filter()), num_clips + 1)
146 
147  ##
148  # Test the Clip.save method
149  def test_update_clip(self):
150 
151  # Import additional classes that need the app defined first
152  from classes.query import Clip
153 
154  # Find a clip named file1
155  update_id = TestQueryClass.clip_ids[0]
156  clip = Clip.get(id=update_id)
157  self.assertTrue(clip)
158 
159  # Update clip
160  clip.data["layer"] = 2
161  clip.data["title"] = "My Title"
162  clip.save()
163 
164  # Verify updated data
165  # Get clip again
166  clip = Clip.get(id=update_id)
167  self.assertEqual(clip.data["layer"], 2)
168  self.assertEqual(clip.data["title"], "My Title")
169 
170  ##
171  # Test the Clip.delete method
172  def test_delete_clip(self):
173 
174  # Import additional classes that need the app defined first
175  from classes.query import Clip
176 
177  # Find a clip named file1
178  delete_id = TestQueryClass.clip_ids[4]
179  clip = Clip.get(id=delete_id)
180  self.assertTrue(clip)
181 
182  # Delete clip
183  clip.delete()
184 
185  # Verify deleted data
186  deleted_clip = Clip.get(id=delete_id)
187  self.assertFalse(deleted_clip)
188 
189  # Delete clip again (should do nothing)
190  clip.delete()
191 
192  # Verify deleted data
193  deleted_clip = Clip.get(id=delete_id)
194  self.assertFalse(deleted_clip)
195 
196  ##
197  # Test the Clip.filter method
198  def test_filter_clip(self):
199 
200  # Import additional classes that need the app defined first
201  from classes.query import Clip
202 
203  # Find all clips named file1
204  clips = Clip.filter(id=TestQueryClass.clip_ids[0])
205  self.assertTrue(clips)
206 
207  # Do not find a clip
208  clips = Clip.filter(id="invalidID")
209  self.assertEqual(len(clips), 0)
210 
211  ##
212  # Test the Clip.get method
213  def test_get_clip(self):
214 
215  # Import additional classes that need the app defined first
216  from classes.query import Clip
217 
218  # Find a clip named file1
219  clip = Clip.get(id=TestQueryClass.clip_ids[1])
220  self.assertTrue(clip)
221 
222  # Do not find a clip
223  clip = Clip.get(id="invalidID")
224  self.assertEqual(clip, None)
225 
226  ##
227  # Test the File.save method
228  def test_update_File(self):
229 
230  # Import additional classes that need the app defined first
231  from classes.query import File
232 
233  # Find a File named file1
234  update_id = TestQueryClass.file_ids[0]
235  file = File.get(id=update_id)
236  self.assertTrue(file)
237 
238  # Update File
239  file.data["height"] = 1080
240  file.data["width"] = 1920
241  file.save()
242 
243  # Verify updated data
244  # Get File again
245  file = File.get(id=update_id)
246  self.assertEqual(file.data["height"], 1080)
247  self.assertEqual(file.data["width"], 1920)
248 
249  ##
250  # Test the File.delete method
251  def test_delete_File(self):
252 
253  # Import additional classes that need the app defined first
254  from classes.query import File
255 
256  # Find a File named file1
257  delete_id = TestQueryClass.file_ids[4]
258  file = File.get(id=delete_id)
259  self.assertTrue(file)
260 
261  # Delete File
262  file.delete()
263 
264  # Verify deleted data
265  deleted_file = File.get(id=delete_id)
266  self.assertFalse(deleted_file)
267 
268  # Delete File again (should do nothing
269  file.delete()
270 
271  # Verify deleted data
272  deleted_file = File.get(id=delete_id)
273  self.assertFalse(deleted_file)
274 
275  ##
276  # Test the File.filter method
277  def test_filter_File(self):
278 
279  # Import additional classes that need the app defined first
280  from classes.query import File
281 
282  # Find all Files named file1
283  files = File.filter(id=TestQueryClass.file_ids[0])
284  self.assertTrue(files)
285 
286  # Do not find a File
287  files = File.filter(id="invalidID")
288  self.assertEqual(len(files), 0)
289 
290  ##
291  # Test the File.get method
292  def test_get_File(self):
293 
294  # Import additional classes that need the app defined first
295  from classes.query import File
296 
297  # Find a File named file1
298  file = File.get(id=TestQueryClass.file_ids[1])
299  self.assertTrue(file)
300 
301  # Do not find a File
302  file = File.get(id="invalidID")
303  self.assertEqual(file, None)
304 
305  ##
306  # Test the File.save method by adding multiple files
307  def test_add_file(self):
308 
309  # Import additional classes that need the app defined first
310  from classes.query import File
311 
312  # Find number of files in project
313  num_files = len(File.filter())
314 
315  # Create file
316  r = openshot.DummyReader(openshot.Fraction(24, 1), 640, 480, 44100, 2, 30.0)
317 
318  # Parse JSON
319  file_data = json.loads(r.Json())
320 
321  # Insert into project data
322  query_file = File()
323  query_file.data = file_data
324  query_file.data["path"] = os.path.join(PATH, "images", "openshot.png")
325  query_file.data["media_type"] = "image"
326  query_file.save()
327 
328  self.assertTrue(query_file)
329  self.assertEqual(len(File.filter()), num_files + 1)
330 
331  # Save the file again (which should not change the total # of files)
332  query_file.save()
333 
334  self.assertEqual(len(File.filter()), num_files + 1)
335 
336 
337 if __name__ == '__main__':
338  unittest.main()
def test_filter_clip(self)
Test the Clip.filter method.
Definition: query_tests.py:198
def test_delete_clip(self)
Test the Clip.delete method.
Definition: query_tests.py:172
def test_add_file(self)
Test the File.save method by adding multiple files.
Definition: query_tests.py:307
def test_update_clip(self)
Test the Clip.save method.
Definition: query_tests.py:149
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:292
def test_filter_File(self)
Test the File.filter method.
Definition: query_tests.py:277
def test_get_clip(self)
Test the Clip.get method.
Definition: query_tests.py:213
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:251
def test_update_File(self)
Test the File.save method.
Definition: query_tests.py:228
def test_add_clip(self)
Test the Clip.save method by adding multiple clips.
Definition: query_tests.py:120