|
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 `pathOriginal`=:pathOriginal', |
|
45
|
|
|
{'pathOriginal': '/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
|
|
|
|
|
53
|
|
|
@mock.patch('elodie.config.config_file', '%s/config.ini-sqlite-insert-multiple' % gettempdir()) |
|
54
|
|
|
def test_sqlite_insert_multiple(): |
|
55
|
|
|
with open('%s/config.ini-sqlite-insert-multiple' % gettempdir(), 'w') as f: |
|
56
|
|
|
f.write(config_string_fmt) |
|
57
|
|
|
if hasattr(load_config, 'config'): |
|
58
|
|
|
del load_config.config |
|
59
|
|
|
|
|
60
|
|
|
sqlite_plugin = SQLite() |
|
61
|
|
|
sqlite_plugin.create_schema() |
|
62
|
|
|
sqlite_plugin.after('/some/source/path', '/folder/path', '/file/path.jpg', {}) |
|
63
|
|
|
sqlite_plugin.after('/some/source/path', '/folder/path', '/file/path2.jpg', {}) |
|
64
|
|
|
results = sqlite_plugin.run_query( |
|
65
|
|
|
'SELECT * FROM `metadata`', |
|
66
|
|
|
{} |
|
67
|
|
|
); |
|
68
|
|
|
|
|
69
|
|
|
if hasattr(load_config, 'config'): |
|
70
|
|
|
del load_config.config |
|
71
|
|
|
|
|
72
|
|
|
assert len(results) == 2, results |
|
73
|
|
|
|
|
74
|
|
|
@mock.patch('elodie.config.config_file', '%s/config.ini-sqlite-update' % gettempdir()) |
|
75
|
|
|
def test_sqlite_insert_multiple(): |
|
76
|
|
|
with open('%s/config.ini-sqlite-update' % gettempdir(), 'w') as f: |
|
77
|
|
|
f.write(config_string_fmt) |
|
78
|
|
|
if hasattr(load_config, 'config'): |
|
79
|
|
|
del load_config.config |
|
80
|
|
|
|
|
81
|
|
|
sqlite_plugin = SQLite() |
|
82
|
|
|
sqlite_plugin.create_schema() |
|
83
|
|
|
sqlite_plugin.after('/some/source/path', '/folder/path', '/file/path.jpg', {}) |
|
84
|
|
|
sqlite_plugin.after('/folder/path/file/path.jpg', '/new-folder/path', '/new-file/path.jpg', {}) |
|
85
|
|
|
results = sqlite_plugin.run_query( |
|
86
|
|
|
'SELECT * FROM `metadata`', |
|
87
|
|
|
{} |
|
88
|
|
|
); |
|
89
|
|
|
print(results) |
|
90
|
|
|
|
|
91
|
|
|
if hasattr(load_config, 'config'): |
|
92
|
|
|
del load_config.config |
|
93
|
|
|
|
|
94
|
|
|
assert len(results) == 1, results |
|
95
|
|
|
assert results[0]['pathOriginal'] == '/new-folder/path/new-file/path.jpg', results |
|
96
|
|
|
|