Passed
Push — master ( 9ddb09...7fc5b5 )
by Jaisen
01:14
created

test_get_class_by_file_ds_store()   A

Complexity

Conditions 1

Size

Total Lines 4
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
# Project imports
2
import os
3
import sys
4
5
import hashlib
6
import random
7
import re
8
import shutil
9
import string
10
import tempfile
11
import time
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.audio import Audio
18
from elodie.media.media import Media
19
from elodie.media.photo import Photo
20
from elodie.media.video import Video
21
22
os.environ['TZ'] = 'GMT'
23
24
25
def test_get_file_path():
26
    media = Media(helper.get_file('plain.jpg'))
27
    path = media.get_file_path()
28
29
    assert 'plain.jpg' in path, path
30
31
def test_get_class_by_file_photo():
32
    media = Media.get_class_by_file(helper.get_file('plain.jpg'), [Photo, Video])
33
34
    assert media.__name__ == 'Photo'
35
36
def test_get_class_by_file_video():
37
    media = Media.get_class_by_file(helper.get_file('video.mov'), [Photo, Video])
38
39
    assert media.__name__ == 'Video'
40
41
def test_get_class_by_file_unsupported():
42
    media = Media.get_class_by_file(helper.get_file('text.txt'), [Photo, Video])
43
44
    assert media is None
45
46
def test_get_class_by_file_ds_store():
47
    media = Media.get_class_by_file(helper.get_file('.DS_Store'),
48
                                    [Photo, Video, Audio])
49
    assert media is None
50
51
def test_get_class_by_file_invalid_type():
52
    media = Media.get_class_by_file(None,
53
                                    [Photo, Video, Audio])
54
    assert media is None
55
56
    media = Media.get_class_by_file(False,
57
                                    [Photo, Video, Audio])
58
    assert media is None
59
60
    media = Media.get_class_by_file(True,
61
                                    [Photo, Video, Audio])
62
    assert media is None
63
64 View Code Duplication
def test_get_original_name():
0 ignored issues
show
Duplication introduced by Jaisen Mathai
This code seems to be duplicated in your project.
Loading history...
65
    temporary_folder, folder = helper.create_working_folder()
66
67
    origin = '%s/%s' % (folder, 'with-original-name.jpg')
68
    file = helper.get_file('with-original-name.jpg')
69
    
70
    shutil.copyfile(file, origin)
71
72
    media = Media.get_class_by_file(origin, [Photo])
73
    original_name = media.get_original_name()
74
75
    assert original_name == 'originalfilename.jpg', original_name
76
77 View Code Duplication
def test_get_original_name_invalid_file():
0 ignored issues
show
Duplication introduced by Jaisen Mathai
This code seems to be duplicated in your project.
Loading history...
78
    temporary_folder, folder = helper.create_working_folder()
79
80
    origin = '%s/%s' % (folder, 'invalid.jpg')
81
    file = helper.get_file('invalid.jpg')
82
    
83
    shutil.copyfile(file, origin)
84
85
    media = Media.get_class_by_file(origin, [Photo])
86
    original_name = media.get_original_name()
87
88
    assert original_name is None, original_name
89
    
90
def test_set_original_name_when_exists():
91
    temporary_folder, folder = helper.create_working_folder()
92
93
    origin = '%s/%s' % (folder, 'with-original-name.jpg')
94
    file = helper.get_file('with-original-name.jpg')
95
    
96
    shutil.copyfile(file, origin)
97
98
    media = Media.get_class_by_file(origin, [Photo])
99
    result = media.set_original_name()
100
101
    assert result is None, result
102
103
def test_set_original_name_when_does_not_exist():
104
    temporary_folder, folder = helper.create_working_folder()
105
106
    origin = '%s/%s' % (folder, 'plain.jpg')
107
    file = helper.get_file('plain.jpg')
108
    
109
    shutil.copyfile(file, origin)
110
111
    media = Media.get_class_by_file(origin, [Photo])
112
    metadata_before = media.get_metadata()
113
    result = media.set_original_name()
114
    metadata_after = media.get_metadata()
115
116
    assert metadata_before['original_name'] is None, metadata_before
117
    assert metadata_after['original_name'] == 'plain.jpg', metadata_after
118
    assert result is True, result
119
120
def test_set_original_name_with_arg():
121
    temporary_folder, folder = helper.create_working_folder()
122
123
    origin = '%s/%s' % (folder, 'plain.jpg')
124
    file = helper.get_file('plain.jpg')
125
    
126
    shutil.copyfile(file, origin)
127
128
    new_name = helper.random_string(15)
129
130
    media = Media.get_class_by_file(origin, [Photo])
131
    metadata_before = media.get_metadata()
132
    result = media.set_original_name(new_name)
133
    metadata_after = media.get_metadata()
134
135
    assert metadata_before['original_name'] is None, metadata_before
136
    assert metadata_after['original_name'] == new_name, metadata_after
137
    assert result is True, result
138
139
def test_set_original_name():
140
    files = ['plain.jpg', 'audio.m4a', 'photo.nef', 'video.mov']
141
142
    for file in files:
143
        ext = os.path.splitext(file)[1]
144
145
        temporary_folder, folder = helper.create_working_folder()
146
147
        random_file_name = '%s%s' % (helper.random_string(10), ext)
148
        origin = '%s/%s' % (folder, random_file_name)
149
        file_path = helper.get_file(file)
150
        if file_path is False:
151
            file_path = helper.download_file(file, folder)
152
153
        shutil.copyfile(file_path, origin)
154
155
        media = Media.get_class_by_file(origin, [Audio, Media, Photo, Video])
156
        metadata = media.get_metadata()
157
        media.set_original_name()
158
        metadata_updated = media.get_metadata()
159
160
        shutil.rmtree(folder)
161
162
        assert metadata['original_name'] is None, metadata['original_name']
163
        assert metadata_updated['original_name'] == random_file_name, metadata_updated['original_name']
164
165
def is_valid():
166
    media = Media()
167
168
    assert not media.is_valid()
169