Passed
Pull Request — master (#319)
by Jaisen
02:25 queued 12s
created

test_googlephotos_upload()   A

Complexity

Conditions 4

Size

Total Lines 16
Code Lines 12

Duplication

Lines 16
Ratio 100 %

Importance

Changes 0
Metric Value
cc 4
eloc 12
nop 0
dl 16
loc 16
rs 9.8
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
15
# Globals to simplify mocking configs
16
auth_file = helper.get_file('plugins/googlephotos/auth_file.json')
17
secrets_file = helper.get_file('plugins/googlephotos/secrets_file.json')
18
config_string = """
19
[Plugins]
20
plugins=GooglePhotos
21
22
[PluginGooglePhotos]
23
auth_file={}
24
secrets_file={}
25
        """
26
config_string_fmt = config_string.format(
27
    auth_file,
28
    secrets_file
29
)
30
31
@mock.patch('elodie.config.config_file', '%s/config.ini-googlephotos-set-session' % gettempdir())
32
def test_googlephotos_set_session():
33
    with open('%s/config.ini-googlephotos-set-session' % gettempdir(), 'w') as f:
34
        f.write(config_string_fmt)
35
    if hasattr(load_config, 'config'):
36
        del load_config.config
37
38
    gp = GooglePhotos()
39
40
    if hasattr(load_config, 'config'):
41
        del load_config.config
42
43
    assert gp.session is None, gp.session
44
    gp.set_session()
45
    assert gp.session is not None, gp.session
46
47 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...
48
def test_googlephotos_upload():
49
    with open('%s/config.ini-googlephotos-upload' % gettempdir(), 'w') as f:
50
        f.write(config_string_fmt)
51
    if hasattr(load_config, 'config'):
52
        del load_config.config
53
54
    gp = GooglePhotos()
55
56
    if hasattr(load_config, 'config'):
57
        del load_config.config
58
59
    gp.set_session()
60
    status = gp.upload(helper.get_file('plain.jpg'))
61
    
62
    assert status is not None, status
63
64 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...
65
def test_googlephotos_upload_invalid_empty():
66
    with open('%s/config.ini-googlephotos-upload-invalid-empty' % gettempdir(), 'w') as f:
67
        f.write(config_string_fmt)
68
    if hasattr(load_config, 'config'):
69
        del load_config.config
70
71
    gp = GooglePhotos()
72
73
    if hasattr(load_config, 'config'):
74
        del load_config.config
75
76
    gp.set_session()
77
    status = gp.upload(helper.get_file('invalid.jpg'))
78
    
79
    assert status is None, status
80
81
@mock.patch('elodie.config.config_file', '%s/config.ini-googlephotos-batch' % gettempdir())
82
def test_googlephotos_batch():
83
    with open('%s/config.ini-googlephotos-batch' % gettempdir(), 'w') as f:
84
        f.write(config_string_fmt)
85
    if hasattr(load_config, 'config'):
86
        del load_config.config
87
88
    final_file_path = helper.get_file('plain.jpg')
89
    gp = GooglePhotos()
90
    gp.after('', '', final_file_path, {'foo': 'bar'})
91
    db_row = gp.db.get(final_file_path)
92
    assert db_row == {'foo': 'bar'}, db_row
93
94
    status, count = gp.batch()
95
    db_row_after = gp.db.get(final_file_path)
96
    assert status == True, status
97
    assert count == 1, count
98
    assert db_row_after is None, db_row_after
99
100
101
    if hasattr(load_config, 'config'):
102
        del load_config.config
103
104
        
105
    gp.set_session()
106
    status = gp.upload(helper.get_file('invalid.jpg'))
107
    
108
    assert status is None, status
109