Passed
Pull Request — master (#319)
by Jaisen
01:52
created

googlephotos.googlephotos_test   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 80
Duplicated Lines 40 %

Importance

Changes 0
Metric Value
eloc 53
dl 32
loc 80
rs 10
c 0
b 0
f 0
wmc 12

3 Functions

Rating   Name   Duplication   Size   Complexity  
A test_googlephotos_set_session() 0 15 4
A test_googlephotos_upload() 16 16 4
A test_googlephotos_upload_invalid_empty() 16 16 4

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
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