forked from SteveMarshall/mp4-quicktime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
atomtest.py
executable file
·911 lines (664 loc) · 29 KB
/
atomtest.py
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
#!/usr/bin/env python
# encoding: utf-8
"""Unit tests for atom.py
"""
__author__ = "Steve Marshall (steve@nascentguruism.com)"
__copyright__ = "Copyright (c) 2008 Steve Marshall"
__license__ = "Python"
import atom
import os
import signal
import StringIO
import struct
import unittest
# TODO: Data atom equality based on content? Currently based on tempfile ref.
class SimpleContainerAtom(unittest.TestCase):
type='moov'
def setUp(self):
self.atom = atom.Atom(type=self.type)
def tearDown(self):
del self.atom
def testHasCorrectType(self):
self.assertEqual(self.type, self.atom.type)
def testReprIsTypeAndList(self):
list_repr = repr([])
expected_repr = '%s: %s' % (self.type, list_repr)
self.assertEqual(expected_repr, repr(self.atom))
# Containers are sequences
def testIsContainer(self):
self.assertEqual(True, self.atom.is_container())
def testContainerIsEqual(self):
equal_atom = atom.Atom(type=self.type)
self.assertEqual(equal_atom, self.atom)
def testContainerIsNotEqual(self):
inequal_atom = atom.Atom(type='ftyp')
self.assertNotEqual(inequal_atom, self.atom)
def testLengthIsZero(self):
self.assertEqual(0, len(self.atom))
class ContainerAtomChildManipulation(unittest.TestCase):
type='moov'
child_type = 'free'
second_child_type = 'ftyp'
def setUp(self):
self.atom = atom.Atom(type=self.type)
self.child_atom = atom.Atom(type=self.child_type)
self.second_child_atom = atom.Atom(type=self.second_child_type)
def tearDown(self):
del self.atom
self.child_atom = None
self.second_child_atom = None
def testAppendsFirstChild(self):
self.atom.append(self.child_atom)
self.assertEqual(1, len(self.atom))
self.assertEqual(self.child_type, self.atom[0].type)
def testInsertsOnlyChild(self):
self.atom.insert(0, self.child_atom)
self.assertEqual(1, len(self.atom))
self.assertEqual(self.child_type, self.atom[0].type)
def testInsertsBeforeAppendedChild(self):
self.atom.append(self.second_child_atom)
self.atom.insert(0, self.child_atom)
self.assertEqual(2, len(self.atom))
self.assertEqual(self.child_type, self.atom[0].type)
self.assertEqual(self.second_child_type, self.atom[1].type)
def testSetFirstItem(self):
self.atom.append(self.child_atom)
self.atom[0] = self.second_child_atom
self.assertEqual(1, len(self.atom))
self.assertEqual(self.second_child_type, self.atom[0].type)
def testSetFirstItemSlice(self):
self.atom[0:] = [self.child_atom]
self.assertEqual(1, len(self.atom))
self.assertEqual(self.child_type, self.atom[0].type)
def testGetItemIndex(self):
self.atom[0:] = [self.child_atom, self.second_child_atom]
self.assertEqual(0, self.atom.index(self.child_atom))
self.assertEqual(1, self.atom.index(self.second_child_atom))
def testRemoveSoleItem(self):
self.atom.append(self.child_atom)
self.atom.remove(self.child_atom)
self.assertEqual(0, len(self.atom))
def testRemoveFirstItem(self):
self.atom[0:] = [self.child_atom, self.second_child_atom]
self.atom.remove(self.child_atom)
self.assertEqual(1, len(self.atom))
self.assertEqual(0, self.atom.index(self.second_child_atom))
def testRemoveSlice(self):
self.atom[0:] = [self.child_atom, self.second_child_atom]
del self.atom[0:]
self.assertEqual(0, len(self.atom))
def testIterable(self):
child_atoms = [self.child_atom, self.second_child_atom]
self.atom[0:] = child_atoms
[self.assertEqual(True, atom in child_atoms) for atom in self.atom]
def testContainerWithChildrenIsEqual(self):
child_atoms = [self.child_atom, self.second_child_atom]
self.atom[0:] = child_atoms
other = atom.Atom(type=self.type)
other[0:] = child_atoms
self.assertEqual(other, self.atom)
def testContainerIsNotEqual(self):
child_atoms = [self.child_atom, self.second_child_atom]
self.atom[0:] = child_atoms
other = atom.Atom(type=self.type)
self.assertNotEqual(other, self.atom)
class ContainerAtomInvalidChildManipulation(unittest.TestCase):
type = 'moov'
def setUp(self):
self.atom = atom.Atom(type=self.type)
def tearDown(self):
del self.atom
# NOTE: Only testing inputs to container, as methods like index()
# won't find items if we only let Atoms in
def testAppendOnlyAcceptsAtoms(self):
self.assertRaises(TypeError, self.atom.append, None)
def testInsertOnlyAcceptsAtoms(self):
self.assertRaises(TypeError, self.atom.insert, 0, None)
def testSetItemOnlyAcceptsAtoms(self):
self.atom.append(atom.Atom(type='free'))
self.assertRaises(TypeError, self.atom.__setitem__, 0, None)
def testSetSliceOnlyAcceptsAtoms(self):
self.assertRaises(TypeError, self.atom.__setslice__,
0, 0, [None])
class ContainerAtomInvalidDataManipulation(unittest.TestCase):
type = 'moov'
content = 'content'
def setUp(self):
self.atom = atom.Atom(type=self.type)
def tearDown(self):
del self.atom
def testCannotWrite(self):
self.assertRaises(ValueError, self.atom.write, self.content)
def testCannotWrite(self):
self.assertRaises(ValueError, self.atom.writelines, self.content)
class StoreContainerAtom(unittest.TestCase):
type = 'moov'
child_type = 'free'
child_content = 'line 1\nline 2'
def setUp(self):
self.atom = atom.Atom(type=self.type)
self.child_atom = atom.Atom(type=self.child_type)
def tearDown(self):
del self.atom
del self.child_atom
def testSaveEmpty(self):
save_stream = StringIO.StringIO()
self.atom.save(save_stream)
save_stream.seek(0)
rendered_atom = atom.render_atom_header(self.type, 0)
self.assertEqual(rendered_atom, save_stream.read())
def testSaveWithChild(self):
self.atom.append(self.child_atom)
save_stream = StringIO.StringIO()
self.atom.save(save_stream)
save_stream.seek(0)
rendered_child = atom.render_atom_header(self.child_type, 0)
rendered_atom = atom.render_atom_header(self.type, len(rendered_child))
rendered_atom += rendered_child
self.assertEqual(rendered_atom, save_stream.read())
class LoadContainerAtom(unittest.TestCase):
type = 'moov'
child_type = 'free'
child_content = 'line 1'
def setUp(self):
child_atom = atom.render_atom_header(self.child_type, \
len(self.child_content))
child_atom += self.child_content
self.rendered_atom = atom.render_atom_header(self.type, len(child_atom))
self.rendered_atom += child_atom
self.atom_stream = StringIO.StringIO()
self.atom_stream.write(self.rendered_atom)
self.atom_stream.seek(0)
def tearDown(self):
del self.rendered_atom
del self.atom_stream
def testAtomCanLoad(self):
loaded_atom = atom.Atom(self.atom_stream)
def testAtomHasChild(self):
loaded_atom = atom.Atom(self.atom_stream)
self.assertEqual(1, len(loaded_atom))
def testAtomChildIsCorrect(self):
loaded_atom = atom.Atom(self.atom_stream)
child_atom = loaded_atom[0]
self.assertEqual(self.child_type, child_atom.type)
child_atom.seek(0)
self.assertEqual(self.child_content, child_atom.read())
class LoadPaddedContainerAtoms(unittest.TestCase):
type = 'moov'
child_type = 'free'
child_content = 'line 1'
def setUp(self):
self.padded_atom = atom.render_atom_header(self.type, struct.calcsize('xxxx'))
self.padded_atom += struct.pack('xxxx')
self.filled_atom = atom.render_atom_header(self.child_type, \
len(self.child_content))
self.filled_atom += self.child_content
def tearDown(self):
del self.padded_atom
del self.filled_atom
def testOversizeContainerAtom(self):
rendered_atom = atom.render_atom_header(self.type, \
len(self.filled_atom) + struct.calcsize('xxxx'))
rendered_atom += self.filled_atom + struct.pack('xxxx')
atom_stream = StringIO.StringIO()
atom_stream.write(rendered_atom)
atom_stream.seek(0)
loaded_atom = atom.Atom(atom_stream)
self.assertEqual(self.type, loaded_atom.type)
self.assertEqual(self.child_type, loaded_atom[0].type)
def timeout_handler(self, signum, frame):
raise Exception("Timed out!")
def testOversizeInternalAtom(self):
rendered_atom = atom.render_atom_header(self.type, \
len(self.padded_atom) + len(self.filled_atom))
rendered_atom += self.padded_atom + self.filled_atom
atom_stream = StringIO.StringIO()
atom_stream.write(rendered_atom)
atom_stream.seek(0)
# Exception if this takes more than 2 seconds
signal.signal(signal.SIGALRM, self.timeout_handler)
signal.alarm(2)
loaded_atom = atom.Atom(atom_stream)
self.assertEqual(self.type, loaded_atom.type)
self.assertEqual(self.type, loaded_atom[0].type)
self.assertEqual(self.child_type, loaded_atom[1].type)
class LoadComplexContainerAtom(unittest.TestCase):
root_type = 'moov'
child_1_type = 'moov'
child_1_1_type = 'free'
child_1_1_data = '1.1'
child_1_2_type = 'free'
child_1_2_data = '1.2'
child_2_type = 'free'
child_2_data = '2'
def setUp(self):
# TODO: Create some kind of atom-tree builder
child_1_1 = atom.render_atom_header(self.child_1_1_type, \
len(self.child_1_1_data))
child_1_1 += self.child_1_1_data
child_1_2 = atom.render_atom_header(self.child_1_2_type, \
len(self.child_1_2_data))
child_1_2 += self.child_1_2_data
child_1 = atom.render_atom_header(self.child_1_type, \
len(child_1_1 + child_1_2))
child_1 += child_1_1 + child_1_2
child_2 = atom.render_atom_header(self.child_2_type, \
len(self.child_2_data))
child_2 += self.child_2_data
self.rendered_atom = atom.render_atom_header(self.root_type, len(child_1 + child_2))
self.rendered_atom += child_1 + child_2
self.atom_stream = StringIO.StringIO()
self.atom_stream.write(self.rendered_atom)
self.atom_stream.seek(0)
def tearDown(self):
del self.rendered_atom
del self.atom_stream
def testLoadedAtomIsCorrectlyStructured(self):
loaded_atom = atom.Atom(self.atom_stream)
self.assertEqual(2, len(loaded_atom))
self.assertEqual(2, len(loaded_atom[0]))
self.assertEqual(0, len(loaded_atom[1]))
def testReadDataAtom(self):
loaded_atom = atom.Atom(self.atom_stream)
loaded_atom[0][0].seek(0)
self.assertEqual(self.child_1_1_data, loaded_atom[0][0].read())
def testIterateOverDataAtom(self):
loaded_atom = atom.Atom(self.atom_stream)
loaded_atom[0][0].seek(0)
lines = 0
for line in loaded_atom[0][0]:
loaded_atom[0][0].seek(0)
self.assertEqual(loaded_atom[0][0].read(), line)
lines += 1
self.assertEqual(1, lines)
def testGetAllDescendants(self):
loaded_atom = atom.Atom(self.atom_stream)
descendants = loaded_atom.get_all_descendants()
self.assertEqual(
len(loaded_atom) + len(loaded_atom[0]) + len(loaded_atom[1]),
len(descendants)
)
self.assertEqual(loaded_atom[0], descendants[0])
self.assertEqual(loaded_atom[0][0], descendants[1])
self.assertEqual(loaded_atom[0][1], descendants[2])
self.assertEqual(loaded_atom[1], descendants[3])
def testGetChildrenOfType(self):
loaded_atom = atom.Atom(self.atom_stream)
children_of_type = loaded_atom.get_children_of_type(self.child_1_type)
self.assertEqual(1, len(children_of_type))
self.assertEqual(loaded_atom[0], children_of_type[0])
def testGetDescendantsOfType(self):
loaded_atom = atom.Atom(self.atom_stream)
descendants_of_type = loaded_atom.get_descendants_of_type(self.child_1_1_type)
self.assertEqual(3, len(descendants_of_type))
self.assertEqual(loaded_atom[0][0], descendants_of_type[0])
self.assertEqual(loaded_atom[0][1], descendants_of_type[1])
self.assertEqual(loaded_atom[1], descendants_of_type[2])
class LoadedContainerAtomChildManipulation(unittest.TestCase):
type = 'moov'
initial_child_type = 'free'
initial_child_content = 'line 1'
new_child_type = 'free'
new_child_content = 'line 2'
ad_hoc_type = 'free'
ad_hoc_content = 'ad hoc'
def setUp(self):
child_atom = atom.render_atom_header(self.initial_child_type, \
len(self.initial_child_content))
child_atom += self.initial_child_content
rendered_new_child = atom.render_atom_header(self.new_child_type, \
len(self.new_child_content)) + self.new_child_content
new_child_stream = StringIO.StringIO()
new_child_stream.write(rendered_new_child)
new_child_stream.seek(0)
self.new_child_atom = atom.Atom(new_child_stream)
rendered_atom = atom.render_atom_header(self.type, len(child_atom))
rendered_atom += child_atom
atom_stream = StringIO.StringIO()
atom_stream.write(rendered_atom)
atom_stream.seek(0)
self.atom = atom.Atom(stream=atom_stream)
def tearDown(self):
del self.atom
def testAppendAtom(self):
self.atom.append(self.new_child_atom)
self.assertEqual(2, len(self.atom))
self.atom[0].seek(0)
self.assertEqual(self.initial_child_content, self.atom[0].read())
self.atom[1].seek(0)
self.assertEqual(self.new_child_content, self.atom[1].read())
class StoreLoadedContainerAtom(unittest.TestCase):
type = 'moov'
child_type = 'free'
child_initial_content = 'line 1'
child_new_content = 'line 2'
def setUp(self):
child_atom = atom.render_atom_header(self.child_type, \
len(self.child_initial_content))
child_atom += self.child_initial_content
self.rendered_atom = atom.render_atom_header(self.type, len(child_atom))
self.rendered_atom += child_atom
init_stream = StringIO.StringIO()
init_stream.write(self.rendered_atom)
init_stream.seek(0)
self.atom = atom.Atom(init_stream)
def tearDown(self):
del self.atom
del self.rendered_atom
def testCanSaveLoadedAtom(self):
save_stream = StringIO.StringIO()
self.atom.save(save_stream)
def testSavedUnchangedAtomHasCorrectContent(self):
save_stream = StringIO.StringIO()
self.atom.save(save_stream)
save_stream.seek(0)
self.assertEqual(self.rendered_atom, save_stream.read())
def testSavesChangesToChildAtom(self):
self.atom[0].seek(0, os.SEEK_END)
self.atom[0].write(self.child_new_content)
self.atom.seek(0)
save_stream = StringIO.StringIO()
self.atom.save(save_stream)
save_stream.seek(0)
saved_atom = atom.Atom(save_stream)
# Saved types/counts are correct
self.assertEqual(self.atom.type, saved_atom.type)
self.assertEqual(len(self.atom), len(saved_atom))
self.assertEqual(self.atom[0].type, saved_atom[0].type)
# Saved child doesn't have initial content of old child
saved_atom[0].seek(0)
self.assertNotEqual(self.child_initial_content, saved_atom[0].read())
# Saved child has same content as old child
self.atom[0].seek(0)
saved_atom[0].seek(0)
self.assertEqual(self.atom[0].read(), saved_atom[0].read())
class LoadSpecialContainerAtom(unittest.TestCase):
atom_type = 'stsd'
child_type = 'mp4a'
def setUp(self):
child_padding = atom.ATOM_SPECIAL_CONTAINER_TYPES[self.child_type]['padding']
child_padding = ''.join(['x' for i in range(child_padding)])
child_atom = atom.render_atom_header(self.child_type, len(child_padding))
child_atom += child_padding
atom_padding = atom.ATOM_SPECIAL_CONTAINER_TYPES[self.atom_type]['padding']
atom_padding = ''.join(['x' for i in range(atom_padding)])
self.rendered_atom = atom.render_atom_header(self.atom_type, len(atom_padding + child_atom))
self.rendered_atom += atom_padding + child_atom
init_stream = StringIO.StringIO()
init_stream.write(self.rendered_atom)
init_stream.seek(0)
self.atom = atom.Atom(init_stream)
def tearDown(self):
del self.atom
def testIsSpecialAtom(self):
self.assertTrue(self.atom.is_special_container())
def tesCorrectStructure(self):
self.assertEqual(1, len(self.atom))
self.assertEqual(self.child_type, self.atom[0].type)
self.assertEqual(0, len(self.atom[0]))
class SimpleDataAtom(unittest.TestCase):
type = 'free'
def setUp(self):
self.atom = atom.Atom(type=self.type)
def tearDown(self):
del self.atom
def testHasCorrectType(self):
self.assertEqual(self.type, self.atom.type)
def testReprIsAtomType(self):
self.assertEqual(self.type, repr(self.atom))
def testIsNotContainer(self):
self.assertEqual(False, self.atom.is_container())
def testDataIsEqual(self):
equal_atom = atom.Atom(type=self.type)
self.assertEqual(equal_atom, self.atom)
def testDataIsNotEqual(self):
inequal_atom = atom.Atom(type='ftyp')
self.assertNotEqual(inequal_atom, self.atom)
def testCanTell(self):
self.assertEqual(0, self.atom.tell())
def testCanRead(self):
self.atom.seek(0)
self.assertEqual('', self.atom.read())
def testLengthIsZero(self):
self.assertEqual(0, len(self.atom))
class DataAtomInvalidChildManipluation(unittest.TestCase):
type='free'
child_type = 'free'
def setUp(self):
self.atom = atom.Atom(type=self.type)
self.child_atom = atom.Atom(type=self.child_type)
def tearDown(self):
del self.atom
self.child_atom = None
# NOTE: Only testing inputs to container, as methods like index()
# won't find items if we only let Atoms in
def testCannotAppend(self):
self.assertRaises(ValueError, self.atom.append, self.child_atom)
def testCannotInsert(self):
self.assertRaises(ValueError, self.atom.insert, 0, self.child_atom)
def testCannotSetSlice(self):
self.assertRaises(ValueError, self.atom.__setslice__,
0, 0, [self.child_atom])
class DataAtomManipulation(unittest.TestCase):
type = 'free'
content = 'content'
def setUp(self):
self.atom = atom.Atom(type=self.type)
def tearDown(self):
del self.atom
def testCanWrite(self):
self.atom.write(self.content)
def testTellIsCorrectAfterWrite(self):
self.atom.write(self.content)
self.assertEqual(len(self.content), self.atom.tell())
def testTellIsCorrectAfterSeek(self):
self.atom.write(self.content)
self.atom.seek(2)
self.assertEqual(2, self.atom.tell())
def testCanSeekRelativeToEnd(self):
self.atom.write(self.content)
self.atom.seek(-1, os.SEEK_END)
self.assertEqual(len(self.content) - 1, self.atom.tell())
def testCanSeekRelativeToCurrent(self):
self.atom.write(self.content)
self.atom.seek(2)
self.atom.seek(2, os.SEEK_CUR)
self.assertEqual(4, self.atom.tell())
def testCanWriteAndReadBack(self):
self.atom.write(self.content)
self.atom.seek(0)
self.assertEqual(self.content, self.atom.read())
def testCanReadSegment(self):
self.atom.write(self.content)
self.atom.seek(0)
self.assertEqual(self.content[:1], self.atom.read(1))
def testCanReadAfterSeek(self):
self.atom.write(self.content)
self.atom.seek(4)
self.assertEqual(self.content[4:6], self.atom.read(2))
def testCanTruncate(self):
self.atom.write(self.content)
self.atom.seek(4)
self.atom.truncate()
self.atom.seek(0)
self.assertEqual(self.content[:4], self.atom.read())
def testCanTruncateToSize(self):
self.atom.write(self.content)
self.atom.truncate(4)
self.atom.seek(0)
self.assertEqual(self.content[:4], self.atom.read())
def testDataIsEqual(self):
self.atom.write(self.content)
other = self.atom
self.assertEqual(other, self.atom)
def testDataIsNotEqual(self):
self.atom.write(self.content)
other = atom.Atom(type=self.type)
self.assertNotEqual(other, self.atom)
class DataAtomExtendedManipulation(unittest.TestCase):
type = 'free'
content = 'line 1 line 1 line 1 line 1 line 1 \n' \
+ 'line 2 line 2 line 2 line 2 line 2 line 2 line 2 '
def setUp(self):
self.atom = atom.Atom(type=self.type)
def tearDown(self):
del self.atom
def testCanWriteLines(self):
self.atom.writelines(self.content)
def testTellIsCorrectAfterWriteLines(self):
self.atom.writelines(self.content)
self.assertEqual(len(self.content), self.atom.tell())
def testCanWriteLinesAndReadBack(self):
self.atom.writelines(self.content)
self.atom.seek(0)
self.assertEqual(self.content, self.atom.read())
def testCanReadLine(self):
self.atom.write(self.content)
self.atom.seek(0)
first_line = self.content.index('\n')
self.assertEqual(self.content[:first_line + 1], self.atom.readline())
self.assertEqual(self.content[first_line + 1:], self.atom.readline())
def testCanReadLineWithSize(self):
self.atom.write(self.content)
self.atom.seek(0)
self.assertEqual(self.content[:4], self.atom.readline(4))
def testCanReadLines(self):
self.atom.write(self.content)
self.atom.seek(0)
self.assertEqual(self.content.splitlines(True), self.atom.readlines())
def testCanReadLinesWithSize(self):
self.atom.write(self.content)
self.atom.seek(0)
self.assertEqual(self.content.splitlines(True), self.atom.readlines(50))
def testCanIterateOverLines(self):
self.atom.write(self.content)
self.atom.seek(0)
self.assertEqual(
[len(line) for line in self.content.splitlines(True)],
[len(line) for line in self.atom])
def testCanGetNextLine(self):
self.atom.write(self.content)
self.atom.seek(0)
self.assertEqual(self.content.splitlines(True)[0], self.atom.next())
class StoreDataAtom(unittest.TestCase):
type = 'free'
content = 'line 1\nline 2'
def setUp(self):
self.atom = atom.Atom(type=self.type)
def tearDown(self):
del self.atom
def testSaveEmpty(self):
save_stream = StringIO.StringIO()
self.atom.save(save_stream)
save_stream.seek(0)
rendered_atom = atom.render_atom_header(self.type, 0)
self.assertEqual(rendered_atom, save_stream.read())
def testSaveWithContent(self):
self.atom.write(self.content)
save_stream = StringIO.StringIO()
self.atom.save(save_stream)
save_stream.seek(0)
rendered_atom = atom.render_atom_header(self.type, len(self.content))
rendered_atom += self.content
self.assertEqual(rendered_atom, save_stream.read())
class LoadSimpleDataAtom(unittest.TestCase):
type = 'free'
content = 'line 1\nline 2'
def setUp(self):
empty_atom = atom.render_atom_header(self.type, 0)
self.atom_stream = StringIO.StringIO()
self.atom_stream.write(empty_atom)
content_atom = atom.render_atom_header(self.type, len(self.content))
self.atom_stream_with_content = StringIO.StringIO()
self.atom_stream_with_content.write(content_atom + self.content)
def tearDown(self):
del self.atom_stream
del self.atom_stream_with_content
def testHasCorrectType(self):
data_atom = atom.Atom(self.atom_stream)
self.assertEqual(self.type, data_atom.type)
def testReprIsAtomType(self):
data_atom = atom.Atom(self.atom_stream)
self.assertEqual(self.type, repr(data_atom))
def testIsNotContainer(self):
data_atom = atom.Atom(self.atom_stream)
self.assertEqual(False, data_atom.is_container())
def testDataIsEqual(self):
data_atom = atom.Atom(self.atom_stream)
equal_atom = atom.Atom(type=self.type)
self.assertEqual(equal_atom, data_atom)
def testDataIsNotEqual(self):
data_atom = atom.Atom(self.atom_stream)
inequal_atom = atom.Atom(type='ftyp')
self.assertNotEqual(inequal_atom, data_atom)
def testCanTell(self):
data_atom = atom.Atom(self.atom_stream)
self.assertEqual(0, data_atom.tell())
def testCanTellAfterSeek(self):
data_atom = atom.Atom(self.atom_stream_with_content)
data_atom.seek(2)
self.assertEqual(2, data_atom.tell())
def testCanTellAfterSeekRelativeToCurrent(self):
data_atom = atom.Atom(self.atom_stream_with_content)
data_atom.seek(3)
data_atom.seek(-3, os.SEEK_CUR)
self.assertEqual(0, data_atom.tell())
def testCanTellAfterSeekRelativeToEnd(self):
data_atom = atom.Atom(self.atom_stream_with_content)
data_atom.seek(-1, os.SEEK_END)
self.assertEqual(len(self.content) - 1, data_atom.tell())
def testCanRead(self):
data_atom = atom.Atom(self.atom_stream)
data_atom.seek(0)
self.assertEqual('', data_atom.read())
def testLengthIsZero(self):
data_atom = atom.Atom(self.atom_stream)
self.assertEqual(0, len(data_atom))
def testCanReadWithContent(self):
data_atom = atom.Atom(self.atom_stream_with_content)
data_atom.seek(0)
self.assertEqual(self.content, data_atom.read())
def testCanReadAfterSeek(self):
data_atom = atom.Atom(self.atom_stream_with_content)
data_atom.seek(7)
self.assertEqual(self.content[7:], data_atom.read())
class ManipulateLoadedDataAtom(unittest.TestCase):
type = 'free'
initial_content = 'line 1'
new_content = 'line 2'
def setUp(self):
content_atom = atom.render_atom_header(self.type, len(self.initial_content))
self.atom_stream = StringIO.StringIO()
self.atom_stream.write(content_atom + self.initial_content)
self.atom = atom.Atom(self.atom_stream)
def tearDown(self):
del self.atom_stream
del self.atom
def testCanWrite(self):
self.atom.seek(0)
self.atom.write(self.new_content)
def testReadAfterWriteIsCorrect(self):
self.atom.seek(0)
self.atom.write(self.new_content)
self.atom.seek(0)
self.assertEqual(self.new_content, self.atom.read())
def testCanAppend(self):
self.atom.seek(0, os.SEEK_END)
self.atom.write(self.new_content)
def testReadAfterAppendIsCorrect(self):
self.atom.seek(0, os.SEEK_END)
self.atom.write(self.new_content)
self.atom.seek(0)
self.assertEqual(self.initial_content + self.new_content, \
self.atom.read())
def testCanSaveAfterAppend(self):
self.atom.seek(0, os.SEEK_END)
self.atom.write(self.new_content)
save_stream = StringIO.StringIO()
self.atom.save(save_stream)
save_stream.seek(0)
rendered_atom = atom.render_atom_header(self.type, len(self.initial_content) + len(self.new_content))
rendered_atom += self.initial_content + self.new_content
self.assertEqual(rendered_atom, save_stream.read())
if __name__ == "__main__":
unittest.main()