|
1
|
|
|
from __future__ import absolute_import |
|
2
|
|
|
# Project imports |
|
3
|
|
|
import mock |
|
4
|
|
|
import os |
|
5
|
|
|
import sys |
|
6
|
|
|
import time |
|
7
|
|
|
from tempfile import gettempdir |
|
8
|
|
|
|
|
9
|
|
|
sys.path.insert(0, os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__)))))) |
|
10
|
|
|
sys.path.insert(0, os.path.abspath(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))) |
|
11
|
|
|
|
|
12
|
|
|
import helper |
|
13
|
|
|
from elodie.config import load_config |
|
14
|
|
|
from elodie.localstorage import Db |
|
15
|
|
|
from elodie.media.text import Text |
|
16
|
|
|
from elodie.plugins.sqlite.sqlite import SQLite |
|
17
|
|
|
|
|
18
|
|
|
# Globals to simplify mocking configs |
|
19
|
|
|
db_schema = helper.get_file('plugins/sqlite/schema.sql') |
|
20
|
|
|
config_string = """ |
|
21
|
|
|
[Plugins] |
|
22
|
|
|
plugins=SQLite |
|
23
|
|
|
|
|
24
|
|
|
[PluginSQLite] |
|
25
|
|
|
database_file={} |
|
26
|
|
|
""" |
|
27
|
|
|
config_string_fmt = config_string.format( |
|
28
|
|
|
':memory:' |
|
29
|
|
|
) |
|
30
|
|
|
|
|
31
|
|
|
mock_metadata = { |
|
32
|
|
|
'checksum': 'checksum-val', |
|
33
|
|
|
'date_taken': time.localtime(), |
|
34
|
|
|
'camera_make': 'camera_make-val', |
|
35
|
|
|
'camera_model': 'camera_model-val', |
|
36
|
|
|
'latitude': 0.1, |
|
37
|
|
|
'longitude': 0.2, |
|
38
|
|
|
'album': 'album-val', |
|
39
|
|
|
'title': 'title-val', |
|
40
|
|
|
'mime_type': 'mime_type-val', |
|
41
|
|
|
'original_name': 'original_name-val', |
|
42
|
|
|
'base_name': 'base_name-val', |
|
43
|
|
|
'extension': 'extension-val', |
|
44
|
|
|
'directory_path': 'directory_path-val' |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
setup_module = helper.setup_module |
|
48
|
|
|
teardown_module = helper.teardown_module |
|
49
|
|
|
|
|
50
|
|
|
@mock.patch('elodie.config.config_file', '%s/config.ini-sqlite-insert' % gettempdir()) |
|
51
|
|
|
def test_sqlite_insert(): |
|
52
|
|
|
with open('%s/config.ini-sqlite-insert' % gettempdir(), 'w') as f: |
|
53
|
|
|
f.write(config_string_fmt) |
|
54
|
|
|
if hasattr(load_config, 'config'): |
|
55
|
|
|
del load_config.config |
|
56
|
|
|
|
|
57
|
|
|
sqlite_plugin = SQLite() |
|
58
|
|
|
sqlite_plugin.after('/some/source/path.jpg', '/folder/path', '/file/path.jpg', mock_metadata) |
|
59
|
|
|
results = sqlite_plugin._run_query( |
|
60
|
|
|
'SELECT * FROM `metadata` WHERE `path`=:path', |
|
61
|
|
|
{'path': '/folder/path/file/path.jpg'} |
|
62
|
|
|
); |
|
63
|
|
|
|
|
64
|
|
|
if hasattr(load_config, 'config'): |
|
65
|
|
|
del load_config.config |
|
66
|
|
|
|
|
67
|
|
|
assert len(results) == 1, results |
|
68
|
|
|
assert results[0]['path'] == '/folder/path/file/path.jpg', results[0] |
|
69
|
|
|
|
|
70
|
|
View Code Duplication |
@mock.patch('elodie.config.config_file', '%s/config.ini-sqlite-insert-multiple' % gettempdir()) |
|
|
|
|
|
|
71
|
|
|
def test_sqlite_insert_multiple(): |
|
72
|
|
|
with open('%s/config.ini-sqlite-insert-multiple' % gettempdir(), 'w') as f: |
|
73
|
|
|
f.write(config_string_fmt) |
|
74
|
|
|
if hasattr(load_config, 'config'): |
|
75
|
|
|
del load_config.config |
|
76
|
|
|
|
|
77
|
|
|
sqlite_plugin = SQLite() |
|
78
|
|
|
sqlite_plugin.after('/some/source/path.jpg', '/folder/path', '/file/path.jpg', mock_metadata) |
|
79
|
|
|
mock_metadata_2 = {**mock_metadata, **{'checksum': 'new-hash'}} |
|
80
|
|
|
sqlite_plugin.after('/some/source/path.jpg', '/folder/path', '/file/path2.jpg', mock_metadata_2) |
|
81
|
|
|
results = sqlite_plugin._run_query( |
|
82
|
|
|
'SELECT * FROM `metadata`', |
|
83
|
|
|
{} |
|
84
|
|
|
); |
|
85
|
|
|
|
|
86
|
|
|
if hasattr(load_config, 'config'): |
|
87
|
|
|
del load_config.config |
|
88
|
|
|
|
|
89
|
|
|
assert len(results) == 2, results |
|
90
|
|
|
assert results[0]['path'] == '/folder/path/file/path.jpg', results[0] |
|
91
|
|
|
assert results[1]['path'] == '/folder/path/file/path2.jpg', results[1] |
|
92
|
|
|
|
|
93
|
|
View Code Duplication |
@mock.patch('elodie.config.config_file', '%s/config.ini-sqlite-update' % gettempdir()) |
|
|
|
|
|
|
94
|
|
|
def test_sqlite_update(): |
|
95
|
|
|
with open('%s/config.ini-sqlite-update' % gettempdir(), 'w') as f: |
|
96
|
|
|
f.write(config_string_fmt) |
|
97
|
|
|
if hasattr(load_config, 'config'): |
|
98
|
|
|
del load_config.config |
|
99
|
|
|
|
|
100
|
|
|
sqlite_plugin = SQLite() |
|
101
|
|
|
# write to /folder/path/file/path.jpg and then update it |
|
102
|
|
|
sqlite_plugin.after('/some/source/path.jpg', '/folder/path', '/file/path.jpg', mock_metadata) |
|
103
|
|
|
mock_metadata_2 = {**mock_metadata, **{'title': 'title-val-new'}} |
|
104
|
|
|
sqlite_plugin.after('/folder/path/file/path.jpg', '/new-folder/path', '/new-file/path.jpg', mock_metadata_2) |
|
105
|
|
|
results = sqlite_plugin._run_query( |
|
106
|
|
|
'SELECT * FROM `metadata`', |
|
107
|
|
|
{} |
|
108
|
|
|
); |
|
109
|
|
|
|
|
110
|
|
|
if hasattr(load_config, 'config'): |
|
111
|
|
|
del load_config.config |
|
112
|
|
|
|
|
113
|
|
|
assert len(results) == 1, results |
|
114
|
|
|
assert results[0]['path'] == '/new-folder/path/new-file/path.jpg', results |
|
115
|
|
|
assert results[0]['title'] == 'title-val-new', results |
|
116
|
|
|
|
|
117
|
|
|
@mock.patch('elodie.config.config_file', '%s/config.ini-sqlite-update-multiple' % gettempdir()) |
|
118
|
|
|
def test_sqlite_update_multiple(): |
|
119
|
|
|
with open('%s/config.ini-sqlite-update-multiple' % gettempdir(), 'w') as f: |
|
120
|
|
|
f.write(config_string_fmt) |
|
121
|
|
|
if hasattr(load_config, 'config'): |
|
122
|
|
|
del load_config.config |
|
123
|
|
|
|
|
124
|
|
|
sqlite_plugin = SQLite() |
|
125
|
|
|
mock_metadata_1 = mock_metadata |
|
126
|
|
|
mock_metadata_2 = {**mock_metadata, **{'checksum': 'checksum-val-2', 'title': 'title-val-2'}} |
|
127
|
|
|
sqlite_plugin.after('/some/source/path.jpg', '/folder/path', '/file/path.jpg', mock_metadata) |
|
128
|
|
|
sqlite_plugin.after('/some/source/path2.jpg', '/folder/path', '/file/path2.jpg', mock_metadata_2) |
|
129
|
|
|
|
|
130
|
|
|
mock_metadata_1_upd = {**mock_metadata_1, **{'title': 'title-val-upd'}} |
|
131
|
|
|
mock_metadata_2_upd = {**mock_metadata_2, **{'title': 'title-val-2-upd'}} |
|
132
|
|
|
sqlite_plugin.after('/folder/path/file/path.jpg', '/new-folder/path', '/new-file/path.jpg', mock_metadata_1_upd) |
|
133
|
|
|
sqlite_plugin.after('/folder/path/file/path2.jpg', '/new-folder/path', '/new-file/path2.jpg', mock_metadata_2_upd) |
|
134
|
|
|
results = sqlite_plugin._run_query( |
|
135
|
|
|
'SELECT * FROM `metadata`', |
|
136
|
|
|
{} |
|
137
|
|
|
); |
|
138
|
|
|
|
|
139
|
|
|
if hasattr(load_config, 'config'): |
|
140
|
|
|
del load_config.config |
|
141
|
|
|
|
|
142
|
|
|
assert len(results) == 2, results |
|
143
|
|
|
assert results[0]['path'] == '/new-folder/path/new-file/path.jpg', results[0] |
|
144
|
|
|
assert results[0]['title'] == 'title-val-upd', results[0] |
|
145
|
|
|
assert results[1]['path'] == '/new-folder/path/new-file/path2.jpg', results[1] |
|
146
|
|
|
assert results[1]['title'] == 'title-val-2-upd', results[1] |
|
147
|
|
|
|
|
148
|
|
|
@mock.patch('elodie.config.config_file', '%s/config.ini-sqlite-regenerate-db' % gettempdir()) |
|
149
|
|
|
def test_sqlite_regenerate_db(): |
|
150
|
|
|
with open('%s/config.ini-sqlite-regenerate-db' % gettempdir(), 'w') as f: |
|
151
|
|
|
f.write(config_string_fmt) |
|
152
|
|
|
if hasattr(load_config, 'config'): |
|
153
|
|
|
del load_config.config |
|
154
|
|
|
|
|
155
|
|
|
sqlite_plugin = SQLite() |
|
156
|
|
|
db = Db() |
|
157
|
|
|
file_path_1 = helper.get_file('with-original-name.txt') |
|
158
|
|
|
file_path_2 = helper.get_file('valid.txt') |
|
159
|
|
|
db.add_hash('1', file_path_1, True) |
|
160
|
|
|
db.add_hash('2', file_path_2, True) |
|
161
|
|
|
|
|
162
|
|
|
sqlite_plugin.generate_db() |
|
163
|
|
|
|
|
164
|
|
|
results = sqlite_plugin._run_query( |
|
165
|
|
|
'SELECT * FROM `metadata`', |
|
166
|
|
|
{} |
|
167
|
|
|
); |
|
168
|
|
|
|
|
169
|
|
|
assert len(results) == 2, results |
|
170
|
|
|
assert results[0]['hash'] == 'e2275f3d95c4b55e35bd279bec3f86fcf76b3f3cc0abbf4183725c89a72f94c4', results[0]['hash'] |
|
171
|
|
|
assert results[0]['path'] == file_path_1, results[0]['path'] |
|
172
|
|
|
assert results[1]['hash'] == '3c19a5d751cf19e093b7447297731124d9cc987d3f91a9d1872c3b1c1b15639a', results[1]['hash'] |
|
173
|
|
|
assert results[1]['path'] == file_path_2, results[1]['path'] |
|
174
|
|
|
|