Passed
Push — master ( 9e03c4...3276cc )
by Jaisen
02:00
created

elodie.tests.media.text_test   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 319
Duplicated Lines 67.4 %

Importance

Changes 0
Metric Value
eloc 209
dl 215
loc 319
rs 10
c 0
b 0
f 0
wmc 30

18 Functions

Rating   Name   Duplication   Size   Complexity  
A test_text_extensions() 0 9 1
A test_get_date_taken() 0 7 1
A test_get_default_coordinate() 0 4 1
A test_set_original_name() 16 16 1
A test_get_coordinate_longitude() 0 4 1
A test_set_original_name_with_arg() 18 18 1
A test_get_original_name_when_does_not_exist() 0 5 1
A test_get_metadata_with_numeric_header() 0 9 1
A test_get_title() 0 4 1
A test_get_coordinate_latitude() 0 4 1
A test_set_location() 33 33 3
A test_set_date_taken_without_header() 28 28 3
A test_set_location_without_header() 32 32 3
A test_set_album_without_header() 29 29 3
A test_set_album() 30 30 3
A test_set_date_taken() 29 29 3
A test_get_original_name() 0 5 1
A test_get_date_taken_from_invalid() 0 14 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
# -*- coding: utf-8
2
# Project imports
3
import os
4
import sys
5
6
from datetime import datetime
7
import shutil
8
import tempfile
9
import time
10
11
from nose.plugins.skip import SkipTest
12
13
sys.path.insert(0, os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__)))))))
14
sys.path.insert(0, os.path.abspath(os.path.dirname(os.path.dirname(os.path.realpath(__file__)))))
15
16
import helper
17
from elodie.media.base import Base
18
from elodie.media.text import Text
19
20
os.environ['TZ'] = 'GMT'
21
22
def test_text_extensions():
23
    text = Text()
24
    extensions = text.extensions
25
26
    assert 'txt' in extensions
27
28
    valid_extensions = Text.get_valid_extensions()
29
30
    assert extensions == valid_extensions, valid_extensions
31
32
def test_get_original_name():
33
    media = Text(helper.get_file('with-original-name.txt'))
34
    original_name = media.get_original_name()
35
36
    assert original_name == 'originalname.txt', original_name
37
38
def test_get_original_name_when_does_not_exist():
39
    media = Text(helper.get_file('valid.txt'))
40
    original_name = media.get_original_name()
41
42
    assert original_name is None, original_name
43
44
def test_get_title():
45
    text = Text(helper.get_file('valid.txt'))
46
    text.get_metadata()
47
    assert text.get_title() == 'sample title', text.get_title()
48
49
def test_get_default_coordinate():
50
    text = Text(helper.get_file('valid.txt'))
51
    text.get_metadata()
52
    assert text.get_coordinate() == '51.521435', text.get_coordinate()
53
54
def test_get_coordinate_latitude():
55
    text = Text(helper.get_file('valid.txt'))
56
    text.get_metadata()
57
    assert text.get_coordinate('latitude') == '51.521435', text.get_coordinate('latitude')
58
59
def test_get_coordinate_longitude():
60
    text = Text(helper.get_file('valid.txt'))
61
    text.get_metadata()
62
    assert text.get_coordinate('longitude') == '0.162714', text.get_coordinate('longitude')
63
64
def test_get_date_taken():
65
    text = Text(helper.get_file('valid.txt'))
66
    text.get_metadata()
67
68
    date_taken = text.get_date_taken()
69
70
    assert date_taken == helper.time_convert((2016, 4, 7, 11, 15, 26, 3, 98, 0)), date_taken
71
72
def test_get_date_taken_from_invalid():
73
    origin = helper.get_file('valid-without-header.txt')
74
    text = Text(origin)
75
    text.get_metadata()
76
77
    date_taken = text.get_date_taken()
78
79
    seconds_since_epoch = min(
80
        os.path.getmtime(origin),
81
        os.path.getctime(origin)
82
    )
83
    expected_date_taken = time.gmtime(seconds_since_epoch)
84
85
    assert date_taken == expected_date_taken, date_taken
86
87
def test_get_metadata_with_numeric_header():
88
    # See gh-98 for details
89
    text = Text(helper.get_file('valid-with-numeric-header.txt'))
90
91
    # Should not throw error
92
    # TypeError: argument of type 'int' is not iterable
93
    metadata = text.get_metadata()
94
95
    assert metadata['mime_type'] == 'text/plain'
96
97 View Code Duplication
def test_set_album():
0 ignored issues
show
Duplication introduced by Jaisen Mathai
This code seems to be duplicated in your project.
Loading history...
98
    temporary_folder, folder = helper.create_working_folder()
99
100
    origin = '%s/text.txt' % folder
101
    shutil.copyfile(helper.get_file('valid.txt'), origin)
102
103
    text = Text(origin)
104
    metadata = text.get_metadata()
105
106
    with open(origin, 'r') as f:
107
        f.readline()
108
        contents = f.read()
109
110
    album_name = 'Test Album'
111
    assert album_name != metadata['album']
112
113
    status = text.set_album(album_name)
114
    assert status == True, status
115
116
    text_new = Text(origin)
117
    metadata_new = text_new.get_metadata()
118
119
    with open(origin, 'r') as f:
120
        f.readline()
121
        contents_new = f.read()
122
        assert contents == contents_new, contents_new
123
124
    shutil.rmtree(folder)
125
126
    assert album_name == metadata_new['album'], metadata_new
127
128 View Code Duplication
def test_set_date_taken():
0 ignored issues
show
Duplication introduced by Jaisen Mathai
This code seems to be duplicated in your project.
Loading history...
129
    temporary_folder, folder = helper.create_working_folder()
130
131
    origin = '%s/text.txt' % folder
132
    shutil.copyfile(helper.get_file('valid.txt'), origin)
133
134
    text = Text(origin)
135
    metadata = text.get_metadata()
136
137
    with open(origin, 'r') as f:
138
        f.readline()
139
        contents = f.read()
140
141
    assert helper.time_convert((2013, 9, 30, 7, 6, 5, 0, 273, 0)) != metadata['date_taken'], metadata['date_taken']
142
143
    status = text.set_date_taken(datetime(2013, 9, 30, 7, 6, 5))
144
    assert status == True, status
145
146
    text_new = Text(origin)
147
    metadata_new = text_new.get_metadata()
148
149
    with open(origin, 'r') as f:
150
        f.readline()
151
        contents_new = f.read()
152
        assert contents == contents_new, contents_new
153
154
    shutil.rmtree(folder)
155
156
    assert helper.time_convert((2013, 9, 30, 7, 6, 5, 0, 273, 0)) == metadata_new['date_taken'], metadata_new['date_taken']
157
158 View Code Duplication
def test_set_location():
0 ignored issues
show
Duplication introduced by Jaisen Mathai
This code seems to be duplicated in your project.
Loading history...
159
    temporary_folder, folder = helper.create_working_folder()
160
161
    origin = '%s/text.txt' % folder
162
    shutil.copyfile(helper.get_file('valid.txt'), origin)
163
164
    text = Text(origin)
165
    origin_metadata = text.get_metadata()
166
167
    with open(origin, 'r') as f:
168
        f.readline()
169
        contents = f.read()
170
171
    # Verify that original photo has different location info that what we
172
    #   will be setting and checking
173
    assert not helper.isclose(origin_metadata['latitude'], 11.1111111111), origin_metadata['latitude']
174
    assert not helper.isclose(origin_metadata['longitude'], 99.9999999999), origin_metadata['longitude']
175
176
    status = text.set_location(11.1111111111, 99.9999999999)
177
178
    assert status == True, status
179
180
    text_new = Text(origin)
181
    metadata = text_new.get_metadata()
182
183
    with open(origin, 'r') as f:
184
        f.readline()
185
        contents_new = f.read()
186
        assert contents == contents_new, contents_new
187
188
    shutil.rmtree(folder)
189
190
    assert helper.isclose(metadata['latitude'], 11.1111111111), metadata['latitude']
191
192 View Code Duplication
def test_set_album_without_header():
0 ignored issues
show
Duplication introduced by Jaisen Mathai
This code seems to be duplicated in your project.
Loading history...
193
    temporary_folder, folder = helper.create_working_folder()
194
195
    origin = '%s/text.txt' % folder
196
    shutil.copyfile(helper.get_file('valid-without-header.txt'), origin)
197
198
    text = Text(origin)
199
    metadata = text.get_metadata()
200
201
    with open(origin, 'r') as f:
202
        contents = f.read()
203
204
    album_name = 'Test Album'
205
    assert album_name != metadata['album']
206
207
    status = text.set_album(album_name)
208
    assert status == True, status
209
210
    text_new = Text(origin)
211
    metadata_new = text_new.get_metadata()
212
213
    with open(origin, 'r') as f:
214
        f.readline()
215
        contents_new = f.read()
216
        assert contents == contents_new, contents_new
217
218
    shutil.rmtree(folder)
219
220
    assert album_name == metadata_new['album'], metadata_new
221
222 View Code Duplication
def test_set_date_taken_without_header():
0 ignored issues
show
Duplication introduced by Jaisen Mathai
This code seems to be duplicated in your project.
Loading history...
223
    temporary_folder, folder = helper.create_working_folder()
224
225
    origin = '%s/text.txt' % folder
226
    shutil.copyfile(helper.get_file('valid-without-header.txt'), origin)
227
228
    text = Text(origin)
229
    metadata = text.get_metadata()
230
231
    with open(origin, 'r') as f:
232
        contents = f.read()
233
234
    assert helper.time_convert((2013, 9, 30, 7, 6, 5, 0, 273, 0)) != metadata['date_taken'], metadata['date_taken']
235
236
    status = text.set_date_taken(datetime(2013, 9, 30, 7, 6, 5))
237
    assert status == True, status
238
239
    text_new = Text(origin)
240
    metadata_new = text_new.get_metadata()
241
242
    with open(origin, 'r') as f:
243
        f.readline()
244
        contents_new = f.read()
245
        assert contents == contents_new, contents_new
246
247
    shutil.rmtree(folder)
248
249
    assert helper.time_convert((2013, 9, 30, 7, 6, 5, 0, 273, 0)) == metadata_new['date_taken'], metadata_new['date_taken']
250
251 View Code Duplication
def test_set_location_without_header():
0 ignored issues
show
Duplication introduced by Jaisen Mathai
This code seems to be duplicated in your project.
Loading history...
252
    temporary_folder, folder = helper.create_working_folder()
253
254
    origin = '%s/text.txt' % folder
255
    shutil.copyfile(helper.get_file('valid-without-header.txt'), origin)
256
257
    text = Text(origin)
258
    origin_metadata = text.get_metadata()
259
260
    with open(origin, 'r') as f:
261
        contents = f.read()
262
263
    # Verify that original photo has different location info that what we
264
    #   will be setting and checking
265
    assert not helper.isclose(origin_metadata['latitude'], 11.1111111111), origin_metadata['latitude']
266
    assert not helper.isclose(origin_metadata['longitude'], 99.9999999999), origin_metadata['longitude']
267
268
    status = text.set_location(11.1111111111, 99.9999999999)
269
270
    assert status == True, status
271
272
    text_new = Text(origin)
273
    metadata = text_new.get_metadata()
274
275
    with open(origin, 'r') as f:
276
        f.readline()
277
        contents_new = f.read()
278
        assert contents == contents_new, contents_new
279
280
    shutil.rmtree(folder)
281
282
    assert helper.isclose(metadata['latitude'], 11.1111111111), metadata['latitude']
283
284 View Code Duplication
def test_set_original_name():
0 ignored issues
show
Duplication introduced by Jaisen Mathai
This code seems to be duplicated in your project.
Loading history...
285
    temporary_folder, folder = helper.create_working_folder()
286
287
    random_file_name = '%s.txt' % helper.random_string(10)
288
    origin = '%s/%s' % (folder, random_file_name)
289
    shutil.copyfile(helper.get_file('valid.txt'), origin)
290
291
    text = Text(origin)
292
    metadata = text.get_metadata()
293
    text.set_original_name()
294
    metadata_updated = text.get_metadata()
295
296
    shutil.rmtree(folder)
297
298
    assert metadata['original_name'] is None, metadata['original_name']
299
    assert metadata_updated['original_name'] == random_file_name, metadata_updated['original_name']
300
301 View Code Duplication
def test_set_original_name_with_arg():
0 ignored issues
show
Duplication introduced by Jaisen Mathai
This code seems to be duplicated in your project.
Loading history...
302
    temporary_folder, folder = helper.create_working_folder()
303
304
    random_file_name = '%s.txt' % helper.random_string(10)
305
    origin = '%s/%s' % (folder, random_file_name)
306
    shutil.copyfile(helper.get_file('valid.txt'), origin)
307
308
    new_name = helper.random_string(15)
309
310
    text = Text(origin)
311
    metadata = text.get_metadata()
312
    text.set_original_name(new_name)
313
    metadata_updated = text.get_metadata()
314
315
    shutil.rmtree(folder)
316
317
    assert metadata['original_name'] is None, metadata['original_name']
318
    assert metadata_updated['original_name'] == new_name, metadata_updated['original_name']
319