forked from james-see/iptcinfo3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iptcinfo_test.py
117 lines (81 loc) · 3.17 KB
/
iptcinfo_test.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
import random
import os
import pytest
from iptcinfo3 import (
EOFException,
IPTCData,
IPTCInfo,
file_is_jpeg,
hex_dump,
jpeg_collect_file_parts,
)
def test_EOFException_message():
exp = EOFException()
assert str(exp) == ''
exp = EOFException('ugh', 'well')
assert str(exp) == 'ugh\nwell'
def test_hex_dump():
out = hex_dump(b'ABCDEF')
assert out.strip() == '41 42 43 44 45 46 | ABCDEF'
def test_jpeg_collect_parts_works_with_many_jpegs():
with open('fixtures/Lenna.jpg', 'rb') as fh:
start, end, adobe = jpeg_collect_file_parts(fh)
assert len(start) == 356
assert len(end) == 42891
assert len(adobe) == 0
with open('fixtures/instagram.jpg', 'rb') as fh:
start, end, adobe = jpeg_collect_file_parts(fh)
assert len(start) == 20
assert len(end) == 73394
assert len(adobe) == 0
def test_IPTCData():
data = IPTCData({105: 'Audiobook Narrator Really Going For Broke With Cajun Accent'})
assert data['headline'].startswith('Audiobook')
assert data[105].startswith('Audiobook')
assert data['Headline'].startswith('Audiobook')
data['keywords'] = ['foo']
data['keywords'] = ['foo', 'bar']
with pytest.raises(ValueError):
data['keywords'] = 'foo'
with pytest.raises(KeyError):
IPTCData({'yobby': 'yoshi'})
with pytest.raises(KeyError):
data['yobby'] = 'yoshi'
data = IPTCData({'nonstandard_69': 'sanic'})
assert data[69] == 'sanic'
assert str(data) == "{'nonstandard_69': 'sanic'}"
def test_file_is_jpeg_detects_invalid_file():
with open('fixtures/Lenna.jpg', 'rb') as fh:
assert file_is_jpeg(fh)
with open('setup.cfg', 'rb') as fh:
assert not file_is_jpeg(fh)
def test_getitem_can_read_info():
info = IPTCInfo('fixtures/Lenna.jpg')
assert len(info) >= 4
assert info['keywords'] == [b'lenna', b'test']
assert info['supplemental category'] == [b'supplemental category']
assert info['caption/abstract'] == b'I am a caption'
def test_save_as_saves_as_new_file_with_info():
if os.path.isfile('fixtures/deleteme.jpg'): # pragma: no cover
os.unlink('fixtures/deleteme.jpg')
info = IPTCInfo('fixtures/Lenna.jpg')
info.save_as('fixtures/deleteme.jpg')
info2 = IPTCInfo('fixtures/deleteme.jpg')
# The files won't be byte for byte exact, so filecmp won't work
assert info._data == info2._data
with open('fixtures/Lenna.jpg', 'rb') as fh, open('fixtures/deleteme.jpg', 'rb') as fh2:
start, end, adobe = jpeg_collect_file_parts(fh)
start2, end2, adobe2 = jpeg_collect_file_parts(fh2)
# But we can compare each section
assert start == start2
assert end == end2
assert adobe == adobe2
def test_save_as_saves_as_new_file_with_new_info():
if os.path.isfile('fixtures/deleteme.jpg'): # pragma: no cover
os.unlink('fixtures/deleteme.jpg')
new_headline = b'test headline %d' % random.randint(0, 100)
info = IPTCInfo('fixtures/Lenna.jpg')
info['headline'] = new_headline
info.save_as('fixtures/deleteme.jpg')
info2 = IPTCInfo('fixtures/deleteme.jpg')
assert info2['headline'] == new_headline