Passed
Pull Request — master (#443)
by Jaisen
05:39
created

test_sqlite_insert_multiple()   A

Complexity

Conditions 4

Size

Total Lines 22
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 18
nop 0
dl 0
loc 22
rs 9.5
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.sqlite.sqlite import SQLite
14
from elodie.media.audio import Audio
15
from elodie.media.photo import Photo
16
17
# Globals to simplify mocking configs
18
db_schema = helper.get_file('plugins/sqlite/schema.sql')
19
config_string = """
20
[Plugins]
21
plugins=SQLite
22
23
[PluginSQLite]
24
database_file={}
25
        """
26
config_string_fmt = config_string.format(
27
    ':memory:'
28
)
29
30
setup_module = helper.setup_module
31
teardown_module = helper.teardown_module
32
33
@mock.patch('elodie.config.config_file', '%s/config.ini-sqlite-insert' % gettempdir())
34
def test_sqlite_insert():
35
    with open('%s/config.ini-sqlite-insert' % gettempdir(), 'w') as f:
36
        f.write(config_string_fmt)
37
    if hasattr(load_config, 'config'):
38
        del load_config.config
39
40
    sqlite_plugin = SQLite()
41
    sqlite_plugin.create_schema()
42
    sqlite_plugin.after('/some/source/path', '/folder/path', '/file/path.jpg', {})
43
    results = sqlite_plugin.run_query(
44
        'SELECT * FROM `metadata` WHERE `path`=:path',
45
        {'path': '/folder/path/file/path.jpg'}
46
    );
47
48
    if hasattr(load_config, 'config'):
49
        del load_config.config
50
51
    assert len(results) == 1, results
52
    assert results[0]['path'] == '/folder/path/file/path.jpg', results[0]
53
54
@mock.patch('elodie.config.config_file', '%s/config.ini-sqlite-insert-multiple' % gettempdir())
55
def test_sqlite_insert_multiple():
56
    with open('%s/config.ini-sqlite-insert-multiple' % gettempdir(), 'w') as f:
57
        f.write(config_string_fmt)
58
    if hasattr(load_config, 'config'):
59
        del load_config.config
60
61
    sqlite_plugin = SQLite()
62
    sqlite_plugin.create_schema()
63
    sqlite_plugin.after('/some/source/path', '/folder/path', '/file/path.jpg', {})
64
    sqlite_plugin.after('/some/source/path', '/folder/path', '/file/path2.jpg', {})
65
    results = sqlite_plugin.run_query(
66
        'SELECT * FROM `metadata`',
67
        {}
68
    );
69
70
    if hasattr(load_config, 'config'):
71
        del load_config.config
72
73
    assert len(results) == 2, results
74
    assert results[0]['path'] == '/folder/path/file/path.jpg', results[0]
75
    assert results[1]['path'] == '/folder/path/file/path2.jpg', results[1]
76
77
@mock.patch('elodie.config.config_file', '%s/config.ini-sqlite-update' % gettempdir())
78
def test_sqlite_update():
79
    with open('%s/config.ini-sqlite-update' % gettempdir(), 'w') as f:
80
        f.write(config_string_fmt)
81
    if hasattr(load_config, 'config'):
82
        del load_config.config
83
84
    sqlite_plugin = SQLite()
85
    sqlite_plugin.create_schema()
86
    # write to /folder/path/file/path.jpg and then update it
87
    sqlite_plugin.after('/some/source/path', '/folder/path', '/file/path.jpg', {'foo':'bar'})
88
    sqlite_plugin.after('/folder/path/file/path.jpg', '/new-folder/path', '/new-file/path.jpg', {'foo':'updated'})
89
    results = sqlite_plugin.run_query(
90
        'SELECT * FROM `metadata`',
91
        {}
92
    );
93
94
    if hasattr(load_config, 'config'):
95
        del load_config.config
96
97
    assert len(results) == 1, results
98
    assert results[0]['path'] == '/new-folder/path/new-file/path.jpg', results
99
    assert results[0]['metadata'] == '{"foo":"updated"}', results
100
101
@mock.patch('elodie.config.config_file', '%s/config.ini-sqlite-update-multiple' % gettempdir())
102
def test_sqlite_update_multiple():
103
    with open('%s/config.ini-sqlite-update-multiple' % gettempdir(), 'w') as f:
104
        f.write(config_string_fmt)
105
    if hasattr(load_config, 'config'):
106
        del load_config.config
107
108
    sqlite_plugin = SQLite()
109
    sqlite_plugin.create_schema()
110
    sqlite_plugin.after('/some/source/path', '/folder/path', '/file/path.jpg', {'foo':'bar'})
111
    sqlite_plugin.after('/some/source/path', '/folder/path', '/file/path2.jpg', {'foo':'bar'})
112
    sqlite_plugin.after('/folder/path/file/path.jpg', '/new-folder/path', '/new-file/path.jpg', {'foo':'updated'})
113
    sqlite_plugin.after('/folder/path/file/path2.jpg', '/new-folder/path', '/new-file/path2.jpg', {'foo':'updated2'})
114
    results = sqlite_plugin.run_query(
115
        'SELECT * FROM `metadata`',
116
        {}
117
    );
118
119
    if hasattr(load_config, 'config'):
120
        del load_config.config
121
122
    assert len(results) == 2, results
123
    assert results[0]['path'] == '/new-folder/path/new-file/path.jpg', results[0]
124
    assert results[0]['metadata'] == '{"foo":"updated"}', results[0]
125
    assert results[1]['path'] == '/new-folder/path/new-file/path2.jpg', results[1]
126
    assert results[1]['metadata'] == '{"foo":"updated2"}', results[1]
127