Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
MetObsCommon
Manage
Activity
Members
Plan
Wiki
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Analyze
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
MetObs
MetObsCommon
Commits
b7e6f316
Commit
b7e6f316
authored
7 years ago
by
Alex Diebold
Browse files
Options
Downloads
Patches
Plain Diff
finished create unit test
parent
e6ad20ba
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
metobscommon/tests/archive/unit_tests.py
+59
-30
59 additions, 30 deletions
metobscommon/tests/archive/unit_tests.py
with
59 additions
and
30 deletions
metobscommon/tests/archive/unit_tests.py
+
59
−
30
View file @
b7e6f316
...
...
@@ -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\n
TEST 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\n
TEST 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
):
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment