-
Notifications
You must be signed in to change notification settings - Fork 334
Expand file tree
/
Copy pathtest_core_utils.py
More file actions
296 lines (214 loc) · 9.58 KB
/
Copy pathtest_core_utils.py
File metadata and controls
296 lines (214 loc) · 9.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# SPDX-License-Identifier: Apache-2.0
# Copyright Contributors to the OpenTimelineIO project
import copy
import json
import unittest
import opentimelineio._otio
import opentimelineio.core._core_utils
import opentimelineio.core
import opentimelineio.opentime
class AnyDictionaryTests(unittest.TestCase):
def test_main(self):
d = opentimelineio.core._core_utils.AnyDictionary()
d['a'] = 1
self.assertTrue('a' in d)
self.assertFalse('asdasdasd' in d)
self.assertEqual(len(d), 1)
self.assertEqual(d['a'], 1) # New key
with self.assertRaisesRegex(KeyError, "'non-existent'"):
d['non-existent']
# TODO: Test different type of values to exercise the any_to_py function?
d['a'] = 'newvalue'
self.assertEqual(d['a'], 'newvalue')
self.assertTrue('a' in d) # Test __contains__
self.assertFalse('b' in d)
with self.assertRaises(TypeError):
d[1] # AnyDictionary.__getitem__ only supports strings
del d['a']
self.assertEqual(dict(d), {})
with self.assertRaisesRegex(KeyError, "'non-existent'"):
del d['non-existent']
for key in iter(d): # Test AnyDictionaryProxy.Iterator.iter
self.assertTrue(key)
class CustomClass(object):
pass
with self.assertRaises(TypeError):
d['custom'] = CustomClass()
with self.assertRaises(ValueError):
# Integer bigger than C++ int64_t can accept.
d['super big int'] = 9223372036854775808
with self.assertRaises(ValueError):
# Integer smaller than C++ int64_t can accept.
d['super big int'] = -9223372036854775809
def test_raise_on_mutation_during_iter(self):
d = opentimelineio.core._core_utils.AnyDictionary()
d['a'] = 'test'
d['b'] = 'asdasda'
with self.assertRaisesRegex(ValueError, "container mutated during iteration"):
for key in d:
del d['b']
def test_raises_if_ref_destroyed(self):
d1 = opentimelineio.core._core_utils.AnyDictionary()
opentimelineio._otio._testing.test_AnyDictionary_destroy(d1)
with self.assertRaisesRegex(ValueError, r"Underlying C\+\+ AnyDictionary has been destroyed"): # noqa
d1['asd']
d2 = opentimelineio.core._core_utils.AnyDictionary()
opentimelineio._otio._testing.test_AnyDictionary_destroy(d2)
with self.assertRaisesRegex(ValueError, r"Underlying C\+\+ AnyDictionary has been destroyed"): # noqa
d2['asd'] = 'asd'
d3 = opentimelineio.core._core_utils.AnyDictionary()
opentimelineio._otio._testing.test_AnyDictionary_destroy(d3)
with self.assertRaisesRegex(ValueError, r"Underlying C\+\+ AnyDictionary has been destroyed"): # noqa
del d3['asd']
d4 = opentimelineio.core._core_utils.AnyDictionary()
d4['asd'] = 1
it = iter(d4)
opentimelineio._otio._testing.test_AnyDictionary_destroy(d4)
with self.assertRaisesRegex(ValueError, r"Underlying C\+\+ AnyDictionary has been destroyed"): # noqa
next(it)
class AnyVectorTests(unittest.TestCase):
def test_main(self):
v = opentimelineio.core._core_utils.AnyVector()
with self.assertRaises(IndexError):
del v[0] # There is a special case in the C++ code for empty vector
v.append(1)
self.assertEqual(len(v), 1)
v.append(2)
self.assertEqual(len(v), 2)
self.assertEqual([value for value in v], [1, 2])
v.insert(0, 5)
self.assertEqual([value for value in v], [5, 1, 2])
self.assertEqual(v[0], 5)
self.assertEqual(v[-3], 5)
with self.assertRaises(IndexError):
v[100]
with self.assertRaises(IndexError):
v[-100]
v[-1] = 100
self.assertEqual(v[2], 100)
with self.assertRaises(IndexError):
v[-4] = -1
with self.assertRaises(IndexError):
v[100] = 100
del v[0]
self.assertEqual(len(v), 2)
# Doesn't work...
# assert v == [1, 100]
self.assertEqual([value for value in v], [1, 100])
del v[1000] # This will surprisingly delete the last item...
self.assertEqual(len(v), 1)
self.assertEqual([value for value in v], [1])
# Will delete the last item even if the index doesn't match.
# It's a surprising behavior.
# This is caused by size_t(index)
del v[-1000]
v.extend([1, '234', {}])
items = []
for value in iter(v): # Test AnyVector.Iterator.iter
items.append(value)
self.assertEqual(items, [1, '234', {}])
self.assertFalse(v == [1, '234', {}]) # __eq__ is not implemented
self.assertTrue(1 in v) # Test __contains__
self.assertTrue('234' in v)
self.assertTrue({} in v)
self.assertFalse(5 in v)
self.assertEqual(list(reversed(v)), [{}, '234', 1])
self.assertEqual(v.index('234'), 1)
v += [1, 2]
self.assertEqual(v.count(1), 2)
self.assertEqual(v + ['new'], [1, '234', {}, 1, 2, 'new']) # __add__
self.assertEqual(['new'] + v, [1, '234', {}, 1, 2, 'new']) # __radd__
self.assertEqual(v + ('new',), [1, '234', {}, 1, 2, 'new']) # noqa __add__ with non list type
v2 = opentimelineio.core._core_utils.AnyVector()
v2.append('v2')
self.assertEqual(v + v2, [1, '234', {}, 1, 2, 'v2']) # __add__ with AnyVector
with self.assertRaises(TypeError):
v + 'asd' # __add__ invalid type
self.assertEqual(str(v), "[1, '234', {}, 1, 2]")
self.assertEqual(repr(v), "[1, '234', {}, 1, 2]")
v3 = opentimelineio.core._core_utils.AnyVector()
v3.extend(range(10))
self.assertEqual(v3[2:], [2, 3, 4, 5, 6, 7, 8, 9])
self.assertEqual(v3[4:8], [4, 5, 6, 7])
self.assertEqual(v3[1:7:2], [1, 3, 5])
del v3[2:7]
self.assertEqual(list(v3), [0, 1, 7, 8, 9])
v4 = opentimelineio.core._core_utils.AnyVector()
v4.extend(range(10))
del v4[::2]
self.assertEqual(list(v4), [1, 3, 5, 7, 9])
v5 = opentimelineio.core._core_utils.AnyVector()
tmplist = [1, 2]
v5.append(tmplist)
# If AnyVector was a pure list, this would fail. But it's not a real list.
# Appending copies data, completely removing references to it.
self.assertIsNot(v5[0], tmplist)
def test_raises_if_ref_destroyed(self):
v1 = opentimelineio.core._core_utils.AnyVector()
opentimelineio._otio._testing.test_AnyVector_destroy(v1)
with self.assertRaisesRegex(ValueError, r"Underlying C\+\+ AnyVector object has been destroyed"): # noqa
v1[0]
v2 = opentimelineio.core._core_utils.AnyVector()
opentimelineio._otio._testing.test_AnyVector_destroy(v2)
with self.assertRaisesRegex(ValueError, r"Underlying C\+\+ AnyVector object has been destroyed"): # noqa
v2[0] = 1
v3 = opentimelineio.core._core_utils.AnyVector()
opentimelineio._otio._testing.test_AnyVector_destroy(v3)
with self.assertRaisesRegex(ValueError, r"Underlying C\+\+ AnyVector object has been destroyed"): # noqa
del v3[0]
v4 = opentimelineio.core._core_utils.AnyVector()
v4.append(1)
it = iter(v4)
opentimelineio._otio._testing.test_AnyVector_destroy(v4)
with self.assertRaisesRegex(ValueError, r"Underlying C\+\+ AnyVector object has been destroyed"): # noqa
next(it)
def test_copy(self):
list1 = [1, 2, [3, 4], 5]
copied = copy.copy(list1)
self.assertEqual(list(list1), list(copied))
v = opentimelineio.core._core_utils.AnyVector()
v.extend([1, 2, [3, 4], 5])
copied = copy.copy(v)
self.assertIsNot(v, copied)
# AnyVector can only deep copy. So it's __copy__
# does a deepcopy.
self.assertIsNot(v[2], copied[2])
deepcopied = copy.deepcopy(v)
self.assertIsNot(v, deepcopied)
self.assertIsNot(v[2], deepcopied[2])
class ConvertToPython(unittest.TestCase):
def test_SerializableObject(self):
so = opentimelineio.core.SerializableObjectWithMetadata(name="asd")
so.metadata["key1"] = opentimelineio.core.Composition()
converted = so.to_dict()
self.assertTrue(isinstance(converted, dict))
json.dumps(converted)
def test_AnyDictionary(self):
ad = opentimelineio._otio.AnyDictionary()
ad["my key"] = opentimelineio.core.Composable()
converted = ad.to_dict()
self.assertTrue(isinstance(converted, dict))
json.dumps(converted)
def test_AnyVector(self):
av = opentimelineio._otio.AnyVector()
av.append(1)
av.append(opentimelineio._otio.AnyDictionary())
converted = av.to_list()
self.assertTrue(isinstance(converted, list))
self.assertEqual(converted, [1, {}])
json.dumps(converted)
def test_RationalTime(self):
rt = opentimelineio.opentime.RationalTime()
converted = rt.to_dict()
self.assertTrue(isinstance(converted, dict))
json.dumps(converted)
def test_TimeRange(self):
tr = opentimelineio.opentime.TimeRange()
converted = tr.to_dict()
self.assertTrue(isinstance(converted, dict))
json.dumps(converted)
def test_TimeTransform(self):
tt = opentimelineio.opentime.TimeTransform()
converted = tt.to_dict()
self.assertTrue(isinstance(converted, dict))
json.dumps(converted)