Passed
Push — master ( 75e659...d8cee1 )
by Jaisen
01:58
created

test_googlephotos_batch()   A

Complexity

Conditions 4

Size

Total Lines 31
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 24
nop 0
dl 0
loc 31
rs 9.304
c 0
b 0
f 0
1
from __future__ import absolute_import
2
# Project imports
3
import mock
4
import os
5
import sys
6
from tempfile import gettempdir
7
8
sys.path.insert(0, os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))))
9
sys.path.insert(0, os.path.abspath(os.path.dirname(os.path.dirname(os.path.realpath(__file__)))))
10
11
import helper
12
from elodie.config import load_config
13
from elodie.plugins.googlephotos.googlephotos import GooglePhotos
14
from elodie.media.audio import Audio
15
from elodie.media.photo import Photo
16
17
# Globals to simplify mocking configs
18
auth_file = helper.get_file('plugins/googlephotos/auth_file.json')
19
secrets_file = helper.get_file('plugins/googlephotos/secrets_file.json')
20
config_string = """
21
[Plugins]
22
plugins=GooglePhotos
23
24
[PluginGooglePhotos]
25
auth_file={}
26
secrets_file={}
27
        """
28
config_string_fmt = config_string.format(
29
    auth_file,
30
    secrets_file
31
)
32
33
setup_module = helper.setup_module
34
teardown_module = helper.teardown_module
35
36
@mock.patch('elodie.config.config_file', '%s/config.ini-googlephotos-set-session' % gettempdir())
37
def test_googlephotos_set_session():
38
    with open('%s/config.ini-googlephotos-set-session' % gettempdir(), 'w') as f:
39
        f.write(config_string_fmt)
40
    if hasattr(load_config, 'config'):
41
        del load_config.config
42
43
    gp = GooglePhotos()
44
45
    if hasattr(load_config, 'config'):
46
        del load_config.config
47
48
    assert gp.session is None, gp.session
49
    gp.set_session()
50
    assert gp.session is not None, gp.session
51
52 View Code Duplication
@mock.patch('elodie.config.config_file', '%s/config.ini-googlephotos-after-supported' % gettempdir())
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
53
def test_googlephotos_after_supported():
54
    with open('%s/config.ini-googlephotos-after-supported' % gettempdir(), 'w') as f:
55
        f.write(config_string_fmt)
56
    if hasattr(load_config, 'config'):
57
        del load_config.config
58
59
    sample_photo = Photo(helper.get_file('plain.jpg'))
60
    sample_metadata = sample_photo.get_metadata()
61
    sample_metadata['original_name'] = 'foobar'
62
    final_file_path = helper.get_file('plain.jpg')
63
    gp = GooglePhotos()
64
    gp.after('', '', final_file_path, sample_metadata)
65
    db_row = gp.db.get(final_file_path)
66
67
    if hasattr(load_config, 'config'):
68
        del load_config.config
69
70
    assert db_row == 'foobar', db_row
71
72 View Code Duplication
@mock.patch('elodie.config.config_file', '%s/config.ini-googlephotos-after-unsupported' % gettempdir())
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
73
def test_googlephotos_after_unsupported():
74
    with open('%s/config.ini-googlephotos-after-unsupported' % gettempdir(), 'w') as f:
75
        f.write(config_string_fmt)
76
    if hasattr(load_config, 'config'):
77
        del load_config.config
78
79
    final_file_path = helper.get_file('audio.m4a')
80
    sample_photo = Audio(final_file_path)
81
    sample_metadata = sample_photo.get_metadata()
82
    sample_metadata['original_name'] = 'foobar'
83
    gp = GooglePhotos()
84
    gp.after('', '', final_file_path, sample_metadata)
85
    db_row = gp.db.get(final_file_path)
86
87
    if hasattr(load_config, 'config'):
88
        del load_config.config
89
90
    assert db_row == None, db_row
91
92 View Code Duplication
@mock.patch('elodie.config.config_file', '%s/config.ini-googlephotos-upload' % gettempdir())
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
93
def test_googlephotos_upload():
94
    with open('%s/config.ini-googlephotos-upload' % gettempdir(), 'w') as f:
95
        f.write(config_string_fmt)
96
    if hasattr(load_config, 'config'):
97
        del load_config.config
98
99
    gp = GooglePhotos()
100
101
    if hasattr(load_config, 'config'):
102
        del load_config.config
103
104
    gp.set_session()
105
    status = gp.upload(helper.get_file('plain.jpg'))
106
    
107
    assert status is not None, status
108
109
@mock.patch('elodie.config.config_file', '%s/config.ini-googlephotos-upload-session-fail' % gettempdir())
110
def test_googlephotos_upload_session_fail():
111
    with open('%s/config.ini-googlephotos-upload-session-fail' % gettempdir(), 'w') as f:
112
        f.write(config_string)
113
    if hasattr(load_config, 'config'):
114
        del load_config.config
115
116
    gp = GooglePhotos()
117
118
    if hasattr(load_config, 'config'):
119
        del load_config.config
120
121
    gp.set_session()
122
    status = gp.upload(helper.get_file('plain.jpg'))
123
    
124
    assert status is None, status
125
126 View Code Duplication
@mock.patch('elodie.config.config_file', '%s/config.ini-googlephotos-upload-invalid-empty' % gettempdir())
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
127
def test_googlephotos_upload_invalid_empty():
128
    with open('%s/config.ini-googlephotos-upload-invalid-empty' % gettempdir(), 'w') as f:
129
        f.write(config_string_fmt)
130
    if hasattr(load_config, 'config'):
131
        del load_config.config
132
133
    gp = GooglePhotos()
134
135
    if hasattr(load_config, 'config'):
136
        del load_config.config
137
138
    gp.set_session()
139
    status = gp.upload(helper.get_file('invalid.jpg'))
140
    
141
    assert status is None, status
142
143
@mock.patch('elodie.config.config_file', '%s/config.ini-googlephotos-upload-dne' % gettempdir())
144
def test_googlephotos_upload_dne():
145
    with open('%s/config.ini-googlephotos-upload-dne' % gettempdir(), 'w') as f:
146
        f.write(config_string_fmt)
147
    if hasattr(load_config, 'config'):
148
        del load_config.config
149
150
    gp = GooglePhotos()
151
152
    if hasattr(load_config, 'config'):
153
        del load_config.config
154
155
    gp.set_session()
156
    status = gp.upload('/file/does/not/exist')
157
    
158
    assert status is None, status
159
160
@mock.patch('elodie.config.config_file', '%s/config.ini-googlephotos-batch' % gettempdir())
161
def test_googlephotos_batch():
162
    with open('%s/config.ini-googlephotos-batch' % gettempdir(), 'w') as f:
163
        f.write(config_string_fmt)
164
    if hasattr(load_config, 'config'):
165
        del load_config.config
166
167
    sample_photo = Photo(helper.get_file('plain.jpg'))
168
    sample_metadata = sample_photo.get_metadata()
169
    sample_metadata['original_name'] = 'foobar'
170
    final_file_path = helper.get_file('plain.jpg')
171
    gp = GooglePhotos()
172
    gp.after('', '', final_file_path, sample_metadata)
173
    db_row = gp.db.get(final_file_path)
174
    assert db_row == 'foobar', db_row
175
176
    status, count = gp.batch()
177
    db_row_after = gp.db.get(final_file_path)
178
    assert status == True, status
179
    assert count == 1, count
180
    assert db_row_after is None, db_row_after
181
182
183
    if hasattr(load_config, 'config'):
184
        del load_config.config
185
186
        
187
    gp.set_session()
188
    status = gp.upload(helper.get_file('invalid.jpg'))
189
    
190
    assert status is None, status
191