Skip to content
Snippets Groups Projects
Commit b7e6f316 authored by Alex Diebold's avatar Alex Diebold
Browse files

finished create unit test

parent e6ad20ba
No related branches found
No related tags found
No related merge requests found
......@@ -31,11 +31,16 @@ class TestCreate(object):
def teardown_method(self, method):
pass
def test_create_basic(self):
"""Test the basic functionality of create.
def test_create_file_exists(self):
"""Test create functionality by checking if the database file exists."""
#simulate 'python db.py test.db create'
args = db.parse_args([DATABASE, 'create'])
args.func(args)
Check results by doing a blank query.
"""
assert os.path.exists(DATABASE)
def test_create_query(self):
"""Test create functionality by querying the entire database and comparing results."""
#simulate 'python db.py test.db create'
args = db.parse_args([DATABASE, 'create'])
args.func(args)
......@@ -58,6 +63,30 @@ class TestCreate(object):
'''
#remove indentation
test_input_str = test_input_str.replace(' ', '')
#put test input into a generator that returns the string line-by-line
test_input_generator = iter(test_input_str.split('\n'))
#replace default input function with generator reading line-by-line from string
with mock.patch.object(db, 'input', lambda x: next(test_input_generator)):
#simulate 'python db.py test.db query -i -s -e -t'
args = db.parse_args([DATABASE, 'query', '-i', '-s', '-e', '-t'])
queries = args.func(args)
#find how many query results are found total
num_queries = 0
for qry_list in queries.values():
for qry in qry_list:
num_queries += len(qry.all())
#3 Insrument, 3 Site, 3 Experiment, 4 FileType
expected_num_queries = 13
assert num_queries == expected_num_queries
class TestSync(object):
......@@ -167,32 +196,32 @@ class TestQuery(object):
args = db.parse_args([DATABASE, 'query', '-i', '-s', '-e', '-t', '-f'])
results = args.func(args)
#combine all query results into a single list
query_result = ''
for index in (db.C.INST, db.C.SITE, db.C.EXP, db.C.FILETYPE, db.C.FILE):
for qry in results[index]:
for val in qry:
query_result = query_result + repr(val) + '\n'
#make the expected query results in string format
query_expected = '''test_inst_1 - test_site_1 - test_site_1.test_inst_1.ascii~test_site_1.test_inst_1.qc~ -
test_inst_2 - test_site_1 - test_site_1.test_inst_2.nc~ -
test_site_1 - test_exp_1~test_exp_2~ -
test_site_2 - n/a~ -
test_site_3 - test_exp_2~ -
test_exp_1 - 2000-01-01 01:02:03 - 2000-01-02 01:02:03 - test_site_1~ -
test_site_2.test_inst_3.png - None - ??????.png - {start_time:%y%m%d}.png - None - Daily - test_inst_3~ -
test_inst_3 - n/a - test_site_2.test_inst_3.png - data/test_site_2/test_inst_3/991114.png - None - None
'''
#remove indendation from string
query_expected = query_expected.replace(' ', '')
if query_result != query_expected:
print('\n\nTEST FAILED -- RESULTS:\n')
print('actual - \n{}'.format(query_result))
print('expected - \n{}'.format(query_expected))
for i in range(len(query_result.split('\n'))):
print(query_result.split('\n')[i] == query_expected.split('\n')[i])
#test if actual results are the same as the expected
assert query_result == query_expected
#combine all query results into a single list
query_result = ''
for index in (db.C.INST, db.C.SITE, db.C.EXP, db.C.FILETYPE, db.C.FILE):
for qry in results[index]:
for val in qry:
query_result = query_result + repr(val) + '\n'
#make the expected query results in string format
query_expected = '''test_inst_1 - test_site_1 - test_site_1.test_inst_1.ascii~test_site_1.test_inst_1.qc~ -
test_inst_2 - test_site_1 - test_site_1.test_inst_2.nc~ -
test_site_1 - test_exp_1~test_exp_2~ -
test_site_2 - n/a~ -
test_site_3 - test_exp_2~ -
test_exp_1 - 2000-01-01 01:02:03 - 2000-01-02 01:02:03 - test_site_1~ -
test_site_2.test_inst_3.png - None - ??????.png - {start_time:%y%m%d}.png - None - Daily - test_inst_3~ -
test_inst_3 - n/a - test_site_2.test_inst_3.png - data/test_site_2/test_inst_3/991114.png - None - None
'''
#remove indendation from string
query_expected = query_expected.replace(' ', '')
if query_result != query_expected:
print('\n\nTEST FAILED -- RESULTS:\n')
print('actual - \n{}'.format(query_result))
print('expected - \n{}'.format(query_expected))
for i in range(len(query_result.split('\n'))):
print(query_result.split('\n')[i] == query_expected.split('\n')[i])
#test if actual results are the same as the expected
assert query_result == query_expected
class TestAdd(object):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment