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

completed sync and add tests

parent 728c4873
No related branches found
No related tags found
No related merge requests found
...@@ -13,15 +13,15 @@ DATABASE = 'test.db' ...@@ -13,15 +13,15 @@ DATABASE = 'test.db'
class TestCreate(object): class TestCreate(object):
"""Test create functionary.""" """Test create functionary."""
@pytest.fixture(autouse=True) # @pytest.fixture(autouse=True)
def setup(self, tmpdir): def setup(self):
self.tmpdir = tmpdir.strpath # self.tmpdir = tmpdir.strpath
self.database = self.tmpdir = '/' + DATABASE # self.database = self.tmpdir = '/' + DATABASE
if os.path.isfile(self.database): if os.path.isfile(DATABASE):
os.remove(self.database) os.remove(DATABASE)
def teardown(self): def teardown(self):
os.remove(self.database) os.remove(DATABASE)
def setup_class(cls): def setup_class(cls):
pass pass
...@@ -38,15 +38,15 @@ class TestCreate(object): ...@@ -38,15 +38,15 @@ class TestCreate(object):
def test_create_file_exists(self): def test_create_file_exists(self):
"""Test create functionality by checking if the database file exists.""" """Test create functionality by checking if the database file exists."""
#simulate 'python db.py test.db create' #simulate 'python db.py test.db create'
args = db.parse_args([self.database, 'create']) args = db.parse_args([DATABASE, 'create'])
args.func(args) args.func(args)
assert os.path.exists(self.database) assert os.path.exists(DATABASE)
def test_create_query(self): def test_create_query(self):
"""Test create functionality by querying the entire database and comparing results.""" """Test create functionality by querying the entire database and comparing results."""
#simulate 'python db.py test.db create' #simulate 'python db.py test.db create'
args = db.parse_args([self.database, 'create']) args = db.parse_args([DATABASE, 'create'])
args.func(args) args.func(args)
#test input for querying Instrument, Site, Experiment, and FileType #test input for querying Instrument, Site, Experiment, and FileType
...@@ -67,18 +67,15 @@ class TestCreate(object): ...@@ -67,18 +67,15 @@ class TestCreate(object):
''' '''
#remove indentation #remove indentation
test_input_str = test_input_str.replace(' ', '') test_input_str = test_input_str.replace(' ', '')
#put test input into a generator that returns the string line-by-line #put test input into a generator that returns the string line-by-line
test_input_generator = iter(test_input_str.split('\n')) test_input_generator = iter(test_input_str.split('\n'))
#replace default input function with generator reading line-by-line from string #replace default input function with generator reading line-by-line from string
with mock.patch.object(db, 'input', lambda x: next(test_input_generator)): with mock.patch.object(db, 'input', lambda x: next(test_input_generator)):
#simulate 'python db.py test.db query -i -s -e -t' #simulate 'python db.py test.db query -i -s -e -t'
args = db.parse_args([self.database, 'query', '-i', '-s', '-e', '-t']) args = db.parse_args([DATABASE, 'query', '-i', '-s', '-e', '-t'])
queries = args.func(args) queries = args.func(args)
#find how many query results are found total #find how many query results are found total
...@@ -93,12 +90,16 @@ class TestCreate(object): ...@@ -93,12 +90,16 @@ class TestCreate(object):
assert num_queries == expected_num_queries assert num_queries == expected_num_queries
class TestSync(object): class TestSync(object):
"""Test sync functionality."""
def setup(self): def setup(self):
pass if os.path.isfile(DATABASE):
os.remove(DATABASE)
#simulate 'python db.py test.db create'
args = db.parse_args([DATABASE, 'create'])
args.func(args)
def teardown(self): def teardown(self):
pass os.remove(DATABASE)
def setup_class(cls): def setup_class(cls):
pass pass
...@@ -111,6 +112,62 @@ class TestSync(object): ...@@ -111,6 +112,62 @@ class TestSync(object):
def teardown_method(self, method): def teardown_method(self, method):
pass pass
def test_sync_basic(self):
"""Test sync by running a query before and after to ensure files are properly added."""
#test input for query
test_input_str = '''
'''
#remove indentation from test input
test_input_str = test_input_str.replace(' ', '')
#create generator of the test input that reads it one line at a time
test_input_generator = iter(test_input_str.split('\n'))
#replace the default input with generator reading line-by-line from a string
with mock.patch.object(db, 'input', lambda x: next(test_input_generator)):
#simulate 'python db.py test.db query -f'
args = db.parse_args([DATABASE, 'query', '-f'])
queries = args.func(args)
#find the number of results from the query
num_results = 0
for col in queries.values():
for qry in col:
for val in qry:
num_results += 1
#expected number of results is 0
expected_num_results = 0
#ensure the num_results is the same as the expected
assert num_results == expected_num_results
#simulate 'python db.py test.db sync'
args = db.parse_args([DATABASE, 'sync'])
args.func(args)
#re-run the query and compare with an updated expected value
test_input_generator = iter(test_input_str.split('\n'))
#replace the default input with generator reading line-by-line from a string
with mock.patch.object(db, 'input', lambda x: next(test_input_generator)):
#simulate 'python db.py test.db query -f'
args = db.parse_args([DATABASE, 'query', '-f'])
queries = args.func(args)
#find the number of results from the query
num_results = 0
for col in queries.values():
for qry in col:
for val in qry:
num_results += 1
#expected number of results is 4
expected_num_results = 4
#ensure the num_results is the same as the expected
assert num_results == expected_num_results
class TestQuery(object): class TestQuery(object):
"""Test query functionality. """Test query functionality.
...@@ -195,7 +252,6 @@ class TestQuery(object): ...@@ -195,7 +252,6 @@ class TestQuery(object):
#replace default input function with generator reading line-by-line from string #replace default input function with generator reading line-by-line from string
with mock.patch.object(db, 'input', lambda x: next(test_input_generator)): with mock.patch.object(db, 'input', lambda x: next(test_input_generator)):
#simulate 'python db.py test.db query -i' #simulate 'python db.py test.db query -i'
args = db.parse_args([DATABASE, 'query', '-i', '-s', '-e', '-t', '-f']) args = db.parse_args([DATABASE, 'query', '-i', '-s', '-e', '-t', '-f'])
results = args.func(args) results = args.func(args)
...@@ -228,12 +284,16 @@ class TestQuery(object): ...@@ -228,12 +284,16 @@ class TestQuery(object):
assert query_result == query_expected assert query_result == query_expected
class TestAdd(object): class TestAdd(object):
"""Test add functionality."""
def setup(self): def setup(self):
pass if os.path.isfile(DATABASE):
os.remove(DATABASE)
#simulate 'python db.py test.db create'
args = db.parse_args([DATABASE, 'create'])
args.func(args)
def teardown(self): def teardown(self):
pass os.remove(DATABASE)
def setup_class(cls): def setup_class(cls):
pass pass
...@@ -246,3 +306,60 @@ class TestAdd(object): ...@@ -246,3 +306,60 @@ class TestAdd(object):
def teardown_method(self, method): def teardown_method(self, method):
pass pass
def test_add_basic(self):
"""Test add functionality by querying before and after adding two Files."""
#test input for query
test_input_str = '''
'''
#remove indentation from test input
test_input_str = test_input_str.replace(' ', '')
#create generator of the test input that reads it one line at a time
test_input_generator = iter(test_input_str.split('\n'))
#replace the default input with generator reading line-by-line from a string
with mock.patch.object(db, 'input', lambda x: next(test_input_generator)):
#simulate 'python db.py test.db query -f'
args = db.parse_args([DATABASE, 'query', '-f'])
queries = args.func(args)
#find the number of results from the query
num_results = 0
for col in queries.values():
for qry in col:
for val in qry:
num_results += 1
#expected number of results is 0
expected_num_results = 0
#ensure the num_results is the same as the expected
assert num_results == expected_num_results
#simulate 'python db.py test.db sync'
args = db.parse_args([DATABASE, 'add', '-i', 'test_inst_1', '-s', 'test_site_1', '-S'
'2000-01-01', '-f', '000101.ascii', '000101.qc'])
args.func(args)
#re-run the query and compare with an updated expected value
test_input_generator = iter(test_input_str.split('\n'))
#replace the default input with generator reading line-by-line from a string
with mock.patch.object(db, 'input', lambda x: next(test_input_generator)):
#simulate 'python db.py test.db query -f'
args = db.parse_args([DATABASE, 'query', '-f'])
queries = args.func(args)
#find the number of results from the query
num_results = 0
for col in queries.values():
for qry in col:
for val in qry:
num_results += 1
#expected number of results is 2
expected_num_results = 2
#ensure the num_results is the same as the expected
assert num_results == expected_num_results
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment