2015-12-28 09:42:18 +01:00
|
|
|
# Project imports
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
2021-06-27 07:00:04 +02:00
|
|
|
# sys.path.insert(0, os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))))
|
2015-12-28 09:42:18 +01:00
|
|
|
|
2016-03-12 20:09:28 +01:00
|
|
|
from . import helper
|
2015-12-28 09:42:18 +01:00
|
|
|
from elodie.localstorage import Db
|
|
|
|
from elodie import constants
|
|
|
|
|
|
|
|
os.environ['TZ'] = 'GMT'
|
|
|
|
|
2021-06-27 07:00:04 +02:00
|
|
|
PHOTO_PATH = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'files')
|
|
|
|
|
2015-12-28 09:42:18 +01:00
|
|
|
def test_init_writes_files():
|
2021-06-27 07:00:04 +02:00
|
|
|
db = Db(PHOTO_PATH)
|
2015-12-28 09:42:18 +01:00
|
|
|
|
2021-06-27 07:00:04 +02:00
|
|
|
assert os.path.isfile(helper.get_hash_db(PHOTO_PATH)) == True
|
|
|
|
assert os.path.isfile(helper.get_location_db(PHOTO_PATH)) == True
|
2015-12-28 09:42:18 +01:00
|
|
|
|
|
|
|
def test_add_hash_default_do_not_write():
|
2021-06-27 07:00:04 +02:00
|
|
|
db = Db(PHOTO_PATH)
|
2015-12-28 09:42:18 +01:00
|
|
|
|
|
|
|
random_key = helper.random_string(10)
|
|
|
|
random_value = helper.random_string(12)
|
|
|
|
|
|
|
|
# Test with default False value as 3rd param
|
|
|
|
db.add_hash(random_key, random_value)
|
|
|
|
|
|
|
|
assert db.check_hash(random_key) == True, 'Lookup for hash did not return True'
|
|
|
|
|
|
|
|
# Instnatiate new db class to confirm random_key does not exist
|
2021-06-27 07:00:04 +02:00
|
|
|
db2 = Db(PHOTO_PATH)
|
2015-12-28 09:42:18 +01:00
|
|
|
assert db2.check_hash(random_key) == False
|
2021-06-27 07:00:04 +02:00
|
|
|
|
2015-12-28 09:42:18 +01:00
|
|
|
def test_add_hash_explicit_do_not_write():
|
2021-06-27 07:00:04 +02:00
|
|
|
db = Db(PHOTO_PATH)
|
2015-12-28 09:42:18 +01:00
|
|
|
|
|
|
|
random_key = helper.random_string(10)
|
|
|
|
random_value = helper.random_string(12)
|
|
|
|
|
|
|
|
# Test with explicit False value as 3rd param
|
|
|
|
db.add_hash(random_key, random_value, False)
|
|
|
|
|
|
|
|
assert db.check_hash(random_key) == True, 'Lookup for hash did not return True'
|
|
|
|
|
|
|
|
# Instnatiate new db class to confirm random_key does not exist
|
2021-06-27 07:00:04 +02:00
|
|
|
db2 = Db(PHOTO_PATH)
|
2015-12-28 09:42:18 +01:00
|
|
|
assert db2.check_hash(random_key) == False
|
2021-06-27 07:00:04 +02:00
|
|
|
|
2015-12-28 09:42:18 +01:00
|
|
|
def test_add_hash_explicit_write():
|
2021-06-27 07:00:04 +02:00
|
|
|
db = Db(PHOTO_PATH)
|
2015-12-28 09:42:18 +01:00
|
|
|
|
|
|
|
random_key = helper.random_string(10)
|
|
|
|
random_value = helper.random_string(12)
|
|
|
|
|
|
|
|
# Test with explicit True value as 3rd param
|
|
|
|
db.add_hash(random_key, random_value, True)
|
|
|
|
|
|
|
|
assert db.check_hash(random_key) == True, 'Lookup for hash did not return True'
|
|
|
|
|
|
|
|
# Instnatiate new db class to confirm random_key exists
|
2021-06-27 07:00:04 +02:00
|
|
|
db2 = Db(PHOTO_PATH)
|
2015-12-28 09:42:18 +01:00
|
|
|
assert db2.check_hash(random_key) == True
|
2016-12-13 08:54:33 +01:00
|
|
|
|
|
|
|
def test_backup_hash_db():
|
2021-06-27 07:00:04 +02:00
|
|
|
db = Db(PHOTO_PATH)
|
2016-12-13 08:54:33 +01:00
|
|
|
backup_file_name = db.backup_hash_db()
|
|
|
|
file_exists = os.path.isfile(backup_file_name)
|
|
|
|
os.remove(backup_file_name)
|
2021-06-27 07:00:04 +02:00
|
|
|
|
2016-12-13 08:54:33 +01:00
|
|
|
assert file_exists, backup_file_name
|
2021-06-27 07:00:04 +02:00
|
|
|
|
2015-12-28 09:42:18 +01:00
|
|
|
def test_check_hash_exists():
|
2021-06-27 07:00:04 +02:00
|
|
|
db = Db(PHOTO_PATH)
|
2015-12-28 09:42:18 +01:00
|
|
|
|
|
|
|
random_key = helper.random_string(10)
|
|
|
|
random_value = helper.random_string(12)
|
|
|
|
|
|
|
|
# Test with explicit False value as 3rd param
|
|
|
|
db.add_hash(random_key, random_value, False)
|
|
|
|
|
|
|
|
assert db.check_hash(random_key) == True, 'Lookup for hash did not return True'
|
2021-06-27 07:00:04 +02:00
|
|
|
|
2015-12-28 09:42:18 +01:00
|
|
|
def test_check_hash_does_not_exist():
|
2021-06-27 07:00:04 +02:00
|
|
|
db = Db(PHOTO_PATH)
|
2015-12-28 09:42:18 +01:00
|
|
|
|
|
|
|
random_key = helper.random_string(10)
|
|
|
|
|
|
|
|
assert db.check_hash(random_key) == False, 'Lookup for hash that should not exist returned True'
|
|
|
|
|
|
|
|
def test_get_hash_exists():
|
2021-06-27 07:00:04 +02:00
|
|
|
db = Db(PHOTO_PATH)
|
2015-12-28 09:42:18 +01:00
|
|
|
|
|
|
|
random_key = helper.random_string(10)
|
|
|
|
random_value = helper.random_string(12)
|
|
|
|
|
|
|
|
# Test with explicit False value as 3rd param
|
|
|
|
db.add_hash(random_key, random_value, False)
|
|
|
|
|
|
|
|
assert db.get_hash(random_key) == random_value, 'Lookup for hash that exists did not return value'
|
2021-06-27 07:00:04 +02:00
|
|
|
|
2015-12-28 09:42:18 +01:00
|
|
|
def test_get_hash_does_not_exist():
|
2021-06-27 07:00:04 +02:00
|
|
|
db = Db(PHOTO_PATH)
|
2015-12-28 09:42:18 +01:00
|
|
|
|
|
|
|
random_key = helper.random_string(10)
|
|
|
|
|
|
|
|
assert db.get_hash(random_key) is None, 'Lookup for hash that should not exist did not return None'
|
|
|
|
|
2016-12-15 10:24:04 +01:00
|
|
|
def test_get_all():
|
2021-06-27 07:00:04 +02:00
|
|
|
db = Db(PHOTO_PATH)
|
2016-12-15 10:24:04 +01:00
|
|
|
db.reset_hash_db()
|
|
|
|
|
|
|
|
random_keys = []
|
|
|
|
random_values = []
|
|
|
|
for _ in range(10):
|
|
|
|
random_keys.append(helper.random_string(10))
|
|
|
|
random_values.append(helper.random_string(12))
|
|
|
|
db.add_hash(random_keys[-1:][0], random_values[-1:][0], False)
|
|
|
|
|
|
|
|
counter = 0
|
|
|
|
for key, value in db.all():
|
|
|
|
assert key in random_keys, key
|
|
|
|
assert value in random_values, value
|
|
|
|
counter += 1
|
|
|
|
|
|
|
|
assert counter == 10, counter
|
|
|
|
|
|
|
|
def test_get_all_empty():
|
2021-06-27 07:00:04 +02:00
|
|
|
db = Db(PHOTO_PATH)
|
2016-12-15 10:24:04 +01:00
|
|
|
db.reset_hash_db()
|
|
|
|
|
|
|
|
counter = 0
|
|
|
|
for key, value in db.all():
|
|
|
|
counter += 1
|
|
|
|
|
|
|
|
# there's a final iteration because of the generator
|
|
|
|
assert counter == 0, counter
|
|
|
|
|
|
|
|
def test_reset_hash_db():
|
2021-06-27 07:00:04 +02:00
|
|
|
db = Db(PHOTO_PATH)
|
2016-12-15 10:24:04 +01:00
|
|
|
|
|
|
|
random_key = helper.random_string(10)
|
|
|
|
random_value = helper.random_string(12)
|
|
|
|
|
|
|
|
# Test with explicit False value as 3rd param
|
|
|
|
db.add_hash(random_key, random_value, False)
|
2021-06-27 07:00:04 +02:00
|
|
|
|
2016-12-15 10:24:04 +01:00
|
|
|
assert random_key in db.hash_db, random_key
|
|
|
|
db.reset_hash_db()
|
|
|
|
assert random_key not in db.hash_db, random_key
|
|
|
|
|
|
|
|
|
2015-12-28 09:42:18 +01:00
|
|
|
def test_update_hash_db():
|
2021-06-27 07:00:04 +02:00
|
|
|
db = Db(PHOTO_PATH)
|
2015-12-28 09:42:18 +01:00
|
|
|
|
|
|
|
random_key = helper.random_string(10)
|
|
|
|
random_value = helper.random_string(12)
|
|
|
|
|
|
|
|
# Test with default False value as 3rd param
|
|
|
|
db.add_hash(random_key, random_value)
|
|
|
|
|
|
|
|
assert db.check_hash(random_key) == True, 'Lookup for hash did not return True'
|
|
|
|
|
|
|
|
# Instnatiate new db class to confirm random_key does not exist
|
2021-06-27 07:00:04 +02:00
|
|
|
db2 = Db(PHOTO_PATH)
|
2015-12-28 09:42:18 +01:00
|
|
|
assert db2.check_hash(random_key) == False
|
|
|
|
|
|
|
|
db.update_hash_db()
|
|
|
|
|
|
|
|
# Instnatiate new db class to confirm random_key exists
|
2021-06-27 07:00:04 +02:00
|
|
|
db3 = Db(PHOTO_PATH)
|
2015-12-28 09:42:18 +01:00
|
|
|
assert db3.check_hash(random_key) == True
|
|
|
|
|
|
|
|
def test_checksum():
|
2021-06-27 07:00:04 +02:00
|
|
|
db = Db(PHOTO_PATH)
|
2015-12-28 09:42:18 +01:00
|
|
|
|
|
|
|
src = helper.get_file('plain.jpg')
|
|
|
|
checksum = db.checksum(src)
|
|
|
|
|
|
|
|
assert checksum == 'd5eb755569ddbc8a664712d2d7d6e0fa1ddfcdb378475e4a6758dc38d5ea9a16', 'Checksum for plain.jpg did not match'
|
|
|
|
|
|
|
|
def test_add_location():
|
2021-06-27 07:00:04 +02:00
|
|
|
db = Db(PHOTO_PATH)
|
2015-12-28 09:42:18 +01:00
|
|
|
|
|
|
|
latitude, longitude, name = helper.get_test_location()
|
|
|
|
|
|
|
|
db.add_location(latitude, longitude, name)
|
|
|
|
retrieved_name = db.get_location_name(latitude, longitude, 5)
|
|
|
|
|
|
|
|
assert name == retrieved_name
|
|
|
|
|
|
|
|
def test_get_location_name():
|
2021-06-27 07:00:04 +02:00
|
|
|
db = Db(PHOTO_PATH)
|
2015-12-28 09:42:18 +01:00
|
|
|
|
|
|
|
latitude, longitude, name = helper.get_test_location()
|
|
|
|
db.add_location(latitude, longitude, name)
|
|
|
|
|
2021-06-27 07:00:04 +02:00
|
|
|
|
2015-12-28 09:42:18 +01:00
|
|
|
# 1 meter
|
|
|
|
retrieved_name = db.get_location_name(latitude, longitude, 1)
|
|
|
|
|
|
|
|
assert name == retrieved_name
|
|
|
|
|
|
|
|
def test_get_location_name_within_threshold():
|
2021-06-27 07:00:04 +02:00
|
|
|
db = Db(PHOTO_PATH)
|
2015-12-28 09:42:18 +01:00
|
|
|
|
|
|
|
latitude, longitude, name = helper.get_test_location()
|
|
|
|
db.add_location(latitude, longitude, name)
|
|
|
|
|
2016-03-12 20:09:28 +01:00
|
|
|
print(latitude)
|
2015-12-28 09:42:18 +01:00
|
|
|
new_latitude = helper.random_coordinate(latitude, 4)
|
|
|
|
new_longitude = helper.random_coordinate(longitude, 4)
|
2016-03-12 20:09:28 +01:00
|
|
|
print(new_latitude)
|
2015-12-28 09:42:18 +01:00
|
|
|
|
|
|
|
# 10 miles
|
|
|
|
retrieved_name = db.get_location_name(new_latitude, new_longitude, 1600*10)
|
|
|
|
|
|
|
|
assert name == retrieved_name, 'Name (%r) did not match retrieved name (%r)' % (name, retrieved_name)
|
|
|
|
|
|
|
|
def test_get_location_name_outside_threshold():
|
2021-06-27 07:00:04 +02:00
|
|
|
db = Db(PHOTO_PATH)
|
2015-12-28 09:42:18 +01:00
|
|
|
|
|
|
|
latitude, longitude, name = helper.get_test_location()
|
|
|
|
db.add_location(latitude, longitude, name)
|
|
|
|
|
|
|
|
new_latitude = helper.random_coordinate(latitude, 1)
|
|
|
|
new_longitude = helper.random_coordinate(longitude, 1)
|
|
|
|
|
|
|
|
# 800 meters
|
|
|
|
retrieved_name = db.get_location_name(new_latitude, new_longitude, 800)
|
|
|
|
|
|
|
|
assert retrieved_name is None
|
2015-12-29 09:07:50 +01:00
|
|
|
|
|
|
|
def test_get_location_coordinates_exists():
|
2021-06-27 07:00:04 +02:00
|
|
|
db = Db(PHOTO_PATH)
|
|
|
|
|
2015-12-29 09:07:50 +01:00
|
|
|
latitude, longitude, name = helper.get_test_location()
|
|
|
|
|
|
|
|
name = '%s-%s' % (name, helper.random_string(10))
|
|
|
|
latitude = helper.random_coordinate(latitude, 1)
|
|
|
|
longitude = helper.random_coordinate(longitude, 1)
|
|
|
|
|
|
|
|
db.add_location(latitude, longitude, name)
|
|
|
|
|
|
|
|
location = db.get_location_coordinates(name)
|
|
|
|
|
|
|
|
assert location is not None
|
|
|
|
assert location[0] == latitude
|
|
|
|
assert location[1] == longitude
|
|
|
|
|
|
|
|
def test_get_location_coordinates_does_not_exists():
|
2021-06-27 07:00:04 +02:00
|
|
|
db = Db(PHOTO_PATH)
|
|
|
|
|
2015-12-29 09:07:50 +01:00
|
|
|
latitude, longitude, name = helper.get_test_location()
|
|
|
|
|
|
|
|
name = '%s-%s' % (name, helper.random_string(10))
|
|
|
|
latitude = helper.random_coordinate(latitude, 1)
|
|
|
|
longitude = helper.random_coordinate(longitude, 1)
|
|
|
|
|
|
|
|
location = db.get_location_coordinates(name)
|
|
|
|
|
|
|
|
assert location is None
|