Create and use a temporary folder as the application directory when running tests #323

Fixes #322
This commit is contained in:
Jaisen Mathai 2019-07-12 21:31:53 -07:00 committed by GitHub
parent 12c17c9cac
commit 50c6e3597f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 47 additions and 29 deletions

View File

@ -26,6 +26,9 @@ def test_debug():
assert constants.debug == constants.debug, constants.debug
def test_application_directory_default():
if('ELODIE_APPLICATION_DIRECTORY' in os.environ):
del os.environ['ELODIE_APPLICATION_DIRECTORY']
reload(constants)
expected_path = '{}/.elodie'.format(os.path.expanduser('~'))
assert constants.application_directory == expected_path, constants.application_directory
@ -35,7 +38,8 @@ def test_application_directory_override_invalid():
directory_to_check = constants.application_directory
# reset
os.environ['ELODIE_APPLICATION_DIRECTORY'] = ''
if('ELODIE_APPLICATION_DIRECTORY' in os.environ):
del os.environ['ELODIE_APPLICATION_DIRECTORY']
reload(constants)
expected_path = '{}/.elodie'.format(os.path.expanduser('~'))
@ -49,7 +53,8 @@ def test_application_directory_override_valid():
hash_db_to_check = constants.hash_db
# reset
os.environ['ELODIE_APPLICATION_DIRECTORY'] = ''
if('ELODIE_APPLICATION_DIRECTORY' in os.environ):
del os.environ['ELODIE_APPLICATION_DIRECTORY']
reload(constants)
assert directory_to_check == cwd, constants.application_directory
@ -78,12 +83,16 @@ def test_mapquest_base_url_override():
url_to_check = constants.mapquest_base_url
# reset
os.environ['ELODIE_MAPQUEST_BASE_URL'] = ''
if('ELODIE_MAPQUEST_BASE_URL' in os.environ):
del os.environ['ELODIE_MAPQUEST_BASE_URL']
reload(constants)
assert url_to_check == 'foobar', constants.mapquest_base_url
def test_mapquest_Key():
def test_mapquest_key_default():
if('ELODIE_MAPQUEST_KEY' in os.environ):
os.environ['ELODIE_MAPQUEST_KEY'] = None
reload(constants)
assert constants.mapquest_key == None, constants.mapquest_key
def test_mapquest_key_override():
@ -92,10 +101,11 @@ def test_mapquest_key_override():
key_to_check = constants.mapquest_key
# reset
os.environ['ELODIE_MAPQUEST_KEY'] = ''
if('ELODIE_MAPQUEST_KEY' in os.environ):
del os.environ['ELODIE_MAPQUEST_KEY']
reload(constants)
assert key_to_check == 'foobar', constants.mapquest_key
assert key_to_check == 'foobar', key_to_check
def test_accepted_language():
assert constants.accepted_language == 'en', constants.accepted_language

View File

@ -150,29 +150,12 @@ def isclose(a, b, rel_tol = 1e-8):
def reset_dbs():
""" Back up hash_db and location_db """
hash_db = '{}-test'.format(constants.hash_db)
if not os.path.isfile(hash_db):
hash_db = constants.hash_db
if os.path.isfile(hash_db):
_rename(hash_db, '{}-test'.format(hash_db))
#else restore_dbs wasn't called by a previous test, keep the
#existing hash_db backup
location_db = '{}-test'.format(constants.location_db)
if not os.path.isfile(location_db):
location_db = constants.location_db
if os.path.isfile(location_db):
_rename(location_db, '{}-test'.format(location_db))
#else restore_dbs wasn't called by a previous test, keep the
#existing location_db backup
# This is no longer needed. See gh-322
# https://github.com/jmathai/elodie/issues/322
pass
def restore_dbs():
""" Restore back ups of hash_db and location_db """
hash_db = '{}-test'.format(constants.hash_db)
if os.path.isfile(hash_db):
_rename(hash_db, hash_db.replace('-test', ''))
location_db = '{}-test'.format(constants.location_db)
if os.path.isfile(location_db):
_rename(location_db, location_db.replace('-test', ''))
# This is no longer needed. See gh-322
# https://github.com/jmathai/elodie/issues/322
pass

View File

@ -2,10 +2,35 @@
import nose
import os
import shutil
import sys
import tempfile
if __name__ == "__main__":
# test_directory is what we pass nose.run for where to find tests
test_directory = os.path.dirname(os.path.abspath(__file__))
# create a temporary directory to use for the application directory while running tests
temporary_application_directory = tempfile.mkdtemp('-elodie-tests')
os.environ['ELODIE_APPLICATION_DIRECTORY'] = temporary_application_directory
# copy config.ini-sample over to the test application directory
temporary_config_file_sample = '{}/config.ini-sample'.format(os.path.dirname(os.path.dirname(test_directory)))
temporary_config_file = '{}/config.ini'.format(temporary_application_directory)
shutil.copy2(
temporary_config_file_sample,
temporary_config_file,
)
# read the sample config file and store contents to be replaced
with open(temporary_config_file_sample, 'r') as f:
config_contents = f.read()
# set the mapquest key in the temporary config file and write it to the temporary application directory
config_contents = config_contents.replace('your-api-key-goes-here', 'x8wQLqGhW7qK3sFpjYtVTogVtoMK0S8s')
with open(temporary_config_file, 'w+') as f:
f.write(config_contents)
test_argv = sys.argv
test_argv.append('--verbosity=2')
result = nose.run(argv=test_argv)