1
|
|
|
from __future__ import print_function |
2
|
|
|
from __future__ import absolute_import |
3
|
|
|
# Project imports |
4
|
|
|
import os |
5
|
|
|
import sys |
6
|
|
|
|
7
|
|
|
sys.path.insert(0, os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__)))))) |
8
|
|
|
|
9
|
|
|
from . import helper |
10
|
|
|
from elodie.localstorage import Db |
11
|
|
|
from elodie import constants |
12
|
|
|
|
13
|
|
|
os.environ['TZ'] = 'GMT' |
14
|
|
|
|
15
|
|
|
def test_init_writes_files(): |
16
|
|
|
db = Db() |
17
|
|
|
|
18
|
|
|
assert os.path.isfile(constants.hash_db) == True |
19
|
|
|
assert os.path.isfile(constants.location_db) == True |
20
|
|
|
|
21
|
|
|
def test_add_hash_default_do_not_write(): |
22
|
|
|
db = Db() |
23
|
|
|
|
24
|
|
|
random_key = helper.random_string(10) |
25
|
|
|
random_value = helper.random_string(12) |
26
|
|
|
|
27
|
|
|
# Test with default False value as 3rd param |
28
|
|
|
db.add_hash(random_key, random_value) |
29
|
|
|
|
30
|
|
|
assert db.check_hash(random_key) == True, 'Lookup for hash did not return True' |
31
|
|
|
|
32
|
|
|
# Instnatiate new db class to confirm random_key does not exist |
33
|
|
|
db2 = Db() |
34
|
|
|
assert db2.check_hash(random_key) == False |
35
|
|
|
|
36
|
|
|
def test_add_hash_explicit_do_not_write(): |
37
|
|
|
db = Db() |
38
|
|
|
|
39
|
|
|
random_key = helper.random_string(10) |
40
|
|
|
random_value = helper.random_string(12) |
41
|
|
|
|
42
|
|
|
# Test with explicit False value as 3rd param |
43
|
|
|
db.add_hash(random_key, random_value, False) |
44
|
|
|
|
45
|
|
|
assert db.check_hash(random_key) == True, 'Lookup for hash did not return True' |
46
|
|
|
|
47
|
|
|
# Instnatiate new db class to confirm random_key does not exist |
48
|
|
|
db2 = Db() |
49
|
|
|
assert db2.check_hash(random_key) == False |
50
|
|
|
|
51
|
|
|
def test_add_hash_explicit_write(): |
52
|
|
|
db = Db() |
53
|
|
|
|
54
|
|
|
random_key = helper.random_string(10) |
55
|
|
|
random_value = helper.random_string(12) |
56
|
|
|
|
57
|
|
|
# Test with explicit True value as 3rd param |
58
|
|
|
db.add_hash(random_key, random_value, True) |
59
|
|
|
|
60
|
|
|
assert db.check_hash(random_key) == True, 'Lookup for hash did not return True' |
61
|
|
|
|
62
|
|
|
# Instnatiate new db class to confirm random_key exists |
63
|
|
|
db2 = Db() |
64
|
|
|
assert db2.check_hash(random_key) == True |
65
|
|
|
|
66
|
|
|
def test_backup_hash_db(): |
67
|
|
|
db = Db() |
68
|
|
|
backup_file_name = db.backup_hash_db() |
69
|
|
|
file_exists = os.path.isfile(backup_file_name) |
70
|
|
|
os.remove(backup_file_name) |
71
|
|
|
|
72
|
|
|
assert file_exists, backup_file_name |
73
|
|
|
|
74
|
|
|
def test_check_hash_exists(): |
75
|
|
|
db = Db() |
76
|
|
|
|
77
|
|
|
random_key = helper.random_string(10) |
78
|
|
|
random_value = helper.random_string(12) |
79
|
|
|
|
80
|
|
|
# Test with explicit False value as 3rd param |
81
|
|
|
db.add_hash(random_key, random_value, False) |
82
|
|
|
|
83
|
|
|
assert db.check_hash(random_key) == True, 'Lookup for hash did not return True' |
84
|
|
|
|
85
|
|
|
def test_check_hash_does_not_exist(): |
86
|
|
|
db = Db() |
87
|
|
|
|
88
|
|
|
random_key = helper.random_string(10) |
89
|
|
|
|
90
|
|
|
assert db.check_hash(random_key) == False, 'Lookup for hash that should not exist returned True' |
91
|
|
|
|
92
|
|
|
def test_get_hash_exists(): |
93
|
|
|
db = Db() |
94
|
|
|
|
95
|
|
|
random_key = helper.random_string(10) |
96
|
|
|
random_value = helper.random_string(12) |
97
|
|
|
|
98
|
|
|
# Test with explicit False value as 3rd param |
99
|
|
|
db.add_hash(random_key, random_value, False) |
100
|
|
|
|
101
|
|
|
assert db.get_hash(random_key) == random_value, 'Lookup for hash that exists did not return value' |
102
|
|
|
|
103
|
|
|
def test_get_hash_does_not_exist(): |
104
|
|
|
db = Db() |
105
|
|
|
|
106
|
|
|
random_key = helper.random_string(10) |
107
|
|
|
|
108
|
|
|
assert db.get_hash(random_key) is None, 'Lookup for hash that should not exist did not return None' |
109
|
|
|
|
110
|
|
|
def test_get_all(): |
111
|
|
|
db = Db() |
112
|
|
|
db.reset_hash_db() |
113
|
|
|
|
114
|
|
|
random_keys = [] |
115
|
|
|
random_values = [] |
116
|
|
|
for _ in range(10): |
117
|
|
|
random_keys.append(helper.random_string(10)) |
118
|
|
|
random_values.append(helper.random_string(12)) |
119
|
|
|
db.add_hash(random_keys[-1:][0], random_values[-1:][0], False) |
120
|
|
|
|
121
|
|
|
counter = 0 |
122
|
|
|
for key, value in db.all(): |
123
|
|
|
assert key in random_keys, key |
124
|
|
|
assert value in random_values, value |
125
|
|
|
counter += 1 |
126
|
|
|
|
127
|
|
|
assert counter == 10, counter |
128
|
|
|
|
129
|
|
|
def test_get_all_empty(): |
130
|
|
|
db = Db() |
131
|
|
|
db.reset_hash_db() |
132
|
|
|
|
133
|
|
|
counter = 0 |
134
|
|
|
for key, value in db.all(): |
135
|
|
|
counter += 1 |
136
|
|
|
|
137
|
|
|
# there's a final iteration because of the generator |
138
|
|
|
assert counter == 0, counter |
139
|
|
|
|
140
|
|
|
def test_reset_hash_db(): |
141
|
|
|
db = Db() |
142
|
|
|
|
143
|
|
|
random_key = helper.random_string(10) |
144
|
|
|
random_value = helper.random_string(12) |
145
|
|
|
|
146
|
|
|
# Test with explicit False value as 3rd param |
147
|
|
|
db.add_hash(random_key, random_value, False) |
148
|
|
|
|
149
|
|
|
assert random_key in db.hash_db, random_key |
150
|
|
|
db.reset_hash_db() |
151
|
|
|
assert random_key not in db.hash_db, random_key |
152
|
|
|
|
153
|
|
|
|
154
|
|
|
def test_update_hash_db(): |
155
|
|
|
db = Db() |
156
|
|
|
|
157
|
|
|
random_key = helper.random_string(10) |
158
|
|
|
random_value = helper.random_string(12) |
159
|
|
|
|
160
|
|
|
# Test with default False value as 3rd param |
161
|
|
|
db.add_hash(random_key, random_value) |
162
|
|
|
|
163
|
|
|
assert db.check_hash(random_key) == True, 'Lookup for hash did not return True' |
164
|
|
|
|
165
|
|
|
# Instnatiate new db class to confirm random_key does not exist |
166
|
|
|
db2 = Db() |
167
|
|
|
assert db2.check_hash(random_key) == False |
168
|
|
|
|
169
|
|
|
db.update_hash_db() |
170
|
|
|
|
171
|
|
|
# Instnatiate new db class to confirm random_key exists |
172
|
|
|
db3 = Db() |
173
|
|
|
assert db3.check_hash(random_key) == True |
174
|
|
|
|
175
|
|
|
def test_checksum(): |
176
|
|
|
db = Db() |
177
|
|
|
|
178
|
|
|
src = helper.get_file('plain.jpg') |
179
|
|
|
checksum = db.checksum(src) |
180
|
|
|
|
181
|
|
|
assert checksum == 'd5eb755569ddbc8a664712d2d7d6e0fa1ddfcdb378475e4a6758dc38d5ea9a16', 'Checksum for plain.jpg did not match' |
182
|
|
|
|
183
|
|
|
def test_add_location(): |
184
|
|
|
db = Db() |
185
|
|
|
|
186
|
|
|
latitude, longitude, name = helper.get_test_location() |
187
|
|
|
|
188
|
|
|
db.add_location(latitude, longitude, name) |
189
|
|
|
retrieved_name = db.get_location_name(latitude, longitude, 5) |
190
|
|
|
|
191
|
|
|
assert name == retrieved_name |
192
|
|
|
|
193
|
|
|
def test_get_location_name(): |
194
|
|
|
db = Db() |
195
|
|
|
|
196
|
|
|
latitude, longitude, name = helper.get_test_location() |
197
|
|
|
db.add_location(latitude, longitude, name) |
198
|
|
|
|
199
|
|
|
|
200
|
|
|
# 1 meter |
201
|
|
|
retrieved_name = db.get_location_name(latitude, longitude, 1) |
202
|
|
|
|
203
|
|
|
assert name == retrieved_name |
204
|
|
|
|
205
|
|
|
def test_get_location_name_within_threshold(): |
206
|
|
|
db = Db() |
207
|
|
|
|
208
|
|
|
latitude, longitude, name = helper.get_test_location() |
209
|
|
|
db.add_location(latitude, longitude, name) |
210
|
|
|
|
211
|
|
|
print(latitude) |
212
|
|
|
new_latitude = helper.random_coordinate(latitude, 4) |
213
|
|
|
new_longitude = helper.random_coordinate(longitude, 4) |
214
|
|
|
print(new_latitude) |
215
|
|
|
|
216
|
|
|
# 10 miles |
217
|
|
|
retrieved_name = db.get_location_name(new_latitude, new_longitude, 1600*10) |
218
|
|
|
|
219
|
|
|
assert name == retrieved_name, 'Name (%r) did not match retrieved name (%r)' % (name, retrieved_name) |
220
|
|
|
|
221
|
|
|
def test_get_location_name_outside_threshold(): |
222
|
|
|
db = Db() |
223
|
|
|
|
224
|
|
|
latitude, longitude, name = helper.get_test_location() |
225
|
|
|
db.add_location(latitude, longitude, name) |
226
|
|
|
|
227
|
|
|
new_latitude = helper.random_coordinate(latitude, 1) |
228
|
|
|
new_longitude = helper.random_coordinate(longitude, 1) |
229
|
|
|
|
230
|
|
|
# 800 meters |
231
|
|
|
retrieved_name = db.get_location_name(new_latitude, new_longitude, 800) |
232
|
|
|
|
233
|
|
|
assert retrieved_name is None |
234
|
|
|
|
235
|
|
|
def test_get_location_coordinates_exists(): |
236
|
|
|
db = Db() |
237
|
|
|
|
238
|
|
|
latitude, longitude, name = helper.get_test_location() |
239
|
|
|
|
240
|
|
|
name = '%s-%s' % (name, helper.random_string(10)) |
241
|
|
|
latitude = helper.random_coordinate(latitude, 1) |
242
|
|
|
longitude = helper.random_coordinate(longitude, 1) |
243
|
|
|
|
244
|
|
|
db.add_location(latitude, longitude, name) |
245
|
|
|
|
246
|
|
|
location = db.get_location_coordinates(name) |
247
|
|
|
|
248
|
|
|
assert location is not None |
249
|
|
|
assert location[0] == latitude |
250
|
|
|
assert location[1] == longitude |
251
|
|
|
|
252
|
|
|
def test_get_location_coordinates_does_not_exists(): |
253
|
|
|
db = Db() |
254
|
|
|
|
255
|
|
|
latitude, longitude, name = helper.get_test_location() |
256
|
|
|
|
257
|
|
|
name = '%s-%s' % (name, helper.random_string(10)) |
258
|
|
|
latitude = helper.random_coordinate(latitude, 1) |
259
|
|
|
longitude = helper.random_coordinate(longitude, 1) |
260
|
|
|
|
261
|
|
|
location = db.get_location_coordinates(name) |
262
|
|
|
|
263
|
|
|
assert location is None |
264
|
|
|
|