Skip to content

Commit e9f0d71

Browse files
committed
added sample test for the generated files
1 parent dd58ad5 commit e9f0d71

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

flowcraft/templates/pATLAS_consensus_json.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ def main(list_of_jsons):
8383

8484
json_dic = {
8585
"patlas_mashscreen": json_dict
86-
# TODO add information for report webapp
8786
}
8887

8988
with open(".report.json", "w") as json_report:

flowcraft/tests/template_tests/test_pATLAS_consensus_json.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import os
2+
import re
3+
import json
24
import flowcraft.templates.pATLAS_consensus_json as pATLAS_consensus_json
35

46

@@ -33,3 +35,74 @@ def test_generate_file(tmpdir):
3335
assert not assert_message, "errors occurred:\n{}".format(
3436
"\n".join(assert_message)
3537
)
38+
39+
40+
def test_generated_dict(tmpdir):
41+
"""
42+
Test the dictionary generated by the script. However, since the dict is
43+
only outputted to a file it is needed that we read the generated file
44+
in order to check the result.
45+
"""
46+
47+
# create a temporary file mash screen txt file
48+
mash_file = tmpdir.join("mash_file.txt")
49+
depth_file = tmpdir.join("test_depth_file.txt")
50+
mash_file.write('{"ACC1": ["0.9", "1"]}')
51+
depth_file.write('{"ACC1": 0.9}')
52+
53+
list_of_files = [str(mash_file), str(depth_file)]
54+
55+
# the expected result from loading the dictionary in the generated file
56+
expected_dict = {"ACC1":
57+
{"/tmp/pytest-of-tiago/pytest-1/test_generated_dict0/"
58+
"mash_file.txt": ["0.9", "1"],
59+
"/tmp/pytest-of-tiago/pytest-1/test_generated_dict0/"
60+
"test_depth_file.txt": 0.9}
61+
}
62+
63+
pATLAS_consensus_json.main(list_of_files)
64+
65+
result_dict = json.load(open("consensus_file.json"))
66+
67+
assert_message = []
68+
69+
# checks if result_dict is indeed a dictionary
70+
if not isinstance(result_dict, dict):
71+
assert_message.append("Contents of the generated file must be a "
72+
"dictionary.")
73+
74+
# checks if the accession key matches between the expected result and the
75+
# actual result
76+
if not list(result_dict.keys())[0] == list(expected_dict.keys())[0]:
77+
assert_message.append("Expected key doesn't match the resulting key "
78+
"in dict from file.\nExpected value: {}\n"
79+
"Result value: {}".format(
80+
list(expected_dict.keys())[0],
81+
list(result_dict.keys())[0]
82+
))
83+
84+
# checks that the accession must have two keys
85+
if not len(list(result_dict[list(result_dict.keys())[0]].keys())) == 2:
86+
assert_message.append("This dictionary must contain only two keys"
87+
"per accession number. It had ".format(
88+
len(list(result_dict[list(result_dict.keys())[0]].keys()))
89+
))
90+
91+
# checks if the resulting values for each type of file are the proper type:
92+
first_file_values = list(result_dict[
93+
list(result_dict.keys())[0]].values())[0]
94+
second_file_values = list(result_dict[
95+
list(result_dict.keys())[0]].values())[1]
96+
list_of_checks = [isinstance(first_file_values, list),
97+
isinstance(second_file_values, float)]
98+
if not all(list_of_checks):
99+
assert_message.append("Value of the 1 element in each accession "
100+
"dictionary must be a list and the 2 element"
101+
" must be a str.\nOffending value: {} "
102+
"element".format(" ".join(
103+
[str(x + 1) for x, el in enumerate(list_of_checks) if not el]
104+
)))
105+
106+
assert not assert_message, "Errors occurred:\n{}".format(
107+
"\n".join(assert_message)
108+
)

0 commit comments

Comments
 (0)