Skip to content

Commit 0271f13

Browse files
committed
added custom fixtures to plasmid templates tests
1 parent 3d6b664 commit 0271f13

File tree

4 files changed

+151
-241
lines changed

4 files changed

+151
-241
lines changed
Lines changed: 40 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
import json
3+
import pytest
34
import flowcraft.templates.mapping2json as mapping2json
45

56
depth_dict_coverage = {
@@ -34,6 +35,26 @@
3435
}
3536

3637

38+
@pytest.fixture
39+
def fetch_file(tmpdir, request):
40+
# create a temporary file depth txt file
41+
depth_file = tmpdir.join("test_depth_file.txt")
42+
depth_file.write("ACC1\t1\t30")
43+
# creates a temporary file with the json_dict
44+
json_dict = tmpdir.join("test_json_dict.json")
45+
json_dict.write('{"ACC1": 2000}')
46+
# executes the function to be tested
47+
mapping2json.main(str(depth_file), str(json_dict), "0", "test")
48+
result_dict = json.load(open("{}_mapping.json".format(str(depth_file))))
49+
50+
# finalizer statement that removes .report.json
51+
def remove_test_files():
52+
os.remove(".report.json")
53+
request.addfinalizer(remove_test_files)
54+
55+
return result_dict, str(depth_file)
56+
57+
3758
def test_depth_file_reader(tmpdir):
3859
"""
3960
test the output of depth_file_reader_function to be a given dict
@@ -67,78 +88,33 @@ def test_generate_jsons():
6788
)
6889

6990

70-
def test_generate_file(tmpdir):
91+
def test_generate_file(fetch_file):
7192
"""
72-
This function tests if the files are generated by the main function.
73-
.report.json and the file to import in pATLAS.
93+
This function tests if the output json file is generated
7494
"""
95+
_, depth_file = fetch_file
96+
assert os.path.isfile("{}_mapping.json".format(depth_file))
7597

76-
# create a temporary file depth txt file
77-
depth_file = tmpdir.join("test_depth_file.txt")
78-
depth_file.write("seq1\t1\t30")
79-
80-
# creates a temporary file with the json_dict
81-
json_dict = tmpdir.join("test_json_dict.json")
82-
json_dict.write('{"seq1": 2000}')
83-
84-
# executes the function to be tested
85-
mapping2json.main(str(depth_file), str(json_dict), "0", "test")
86-
87-
assert_message = []
88-
# conditions to check that files are generated
89-
if not os.path.isfile("{}_mapping.json".format(str(depth_file))):
90-
assert_message.append("_mapping.json not created")
91-
if not os.path.isfile(".report.json"):
92-
assert_message.append(".report.json file was not created")
93-
94-
# remove the .report.json file
95-
os.remove(".report.json")
9698

97-
assert not assert_message, "errors occurred:\n{}".format(
98-
"\n".join(assert_message)
99-
)
99+
def test_generate_report(fetch_file):
100+
"""
101+
This tests if the report.json file is generated
102+
"""
103+
assert os.path.isfile(".report.json")
100104

101105

102-
def test_generated_dict(tmpdir):
106+
def test_generated_dict(fetch_file):
103107
"""
104-
This function tests if the files generated by this template script are
105-
created
108+
This function checks if the file contains a dict
106109
"""
110+
result_dict, _ = fetch_file
111+
assert isinstance(result_dict, dict)
107112

108-
# create a temporary file depth txt file
109-
depth_file = tmpdir.join("test_depth_file.txt")
110-
depth_file.write("ACC1\t1\t30")
111-
112-
# creates a temporary file with the json_dict
113-
json_dict = tmpdir.join("test_json_dict.json")
114-
json_dict.write('{"ACC1": 2000}')
115113

114+
def test_generated_dict_contents(fetch_file):
115+
"""
116+
This function tests if the dictionary in the file has the required fields
117+
"""
116118
expected_dict = {"ACC1": 0.0}
117-
118-
# executes the function to be tested
119-
mapping2json.main(str(depth_file), str(json_dict), "0", "test")
120-
121-
result_dict = json.load(open("{}_mapping.json".format(str(depth_file))))
122-
123-
assert_message = []
124-
125-
# checks if result_dict is indeed a dictionary
126-
if not isinstance(result_dict, dict):
127-
assert_message.append("Contents of the generated file must be a "
128-
"dictionary.")
129-
130-
if not result_dict == expected_dict:
131-
assert_message.append("The expected dictionary must be equal to the "
132-
"one present in the generated file.\n"
133-
"Expected: {}\n"
134-
"Result: {}". format(
135-
json.dumps(expected_dict),
136-
json.dumps(result_dict)
137-
))
138-
139-
# remove the .report.json file
140-
os.remove(".report.json")
141-
142-
assert not assert_message, "Errors occurred:\n{}".format(
143-
"\n".join(assert_message)
144-
)
119+
result_dict, _ = fetch_file
120+
assert result_dict == expected_dict
Lines changed: 29 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,53 @@
11
import os
22
import json
3+
import pytest
34
import flowcraft.templates.mashdist2json as mashdist2json
45

56

6-
def test_generate_file(tmpdir):
7-
"""
8-
This function tests if the files generated by this template script are
9-
created
10-
"""
11-
7+
@pytest.fixture
8+
def fetch_file(tmpdir, request):
129
# create a temporary file mash screen txt file
1310
mash_file = tmpdir.join("test_depth_file.txt")
1411
mash_file.write("ACC1\tseq1\t0\t900/1000")
1512

1613
mashdist2json.main(str(mash_file), "0.5", "test", "assembly_file")
14+
result_dict = json.load(open("{}.json".format(
15+
str(mash_file).split(".")[0])))
1716

18-
assert_message = []
19-
# conditions to check that files are generated
20-
if not os.path.isfile("{}.json".format(
21-
str(mash_file).split(".")[0])):
22-
assert_message.append("mash dist json not created")
23-
if not os.path.isfile(".report.json"):
24-
assert_message.append(".report.json file was not created")
25-
26-
# remove the .report.json file
27-
os.remove(".report.json")
17+
# finalizer statement that removes .report.json
18+
def remove_test_files():
19+
os.remove(".report.json")
20+
request.addfinalizer(remove_test_files)
2821

29-
assert not assert_message, "errors occurred:\n{}".format(
30-
"\n".join(assert_message)
31-
)
22+
return result_dict, str(mash_file)
3223

3324

34-
def test_generated_dict(tmpdir):
25+
def test_generate_file(fetch_file):
3526
"""
3627
This function tests if the files generated by this template script are
3728
created
3829
"""
30+
_, mash_file = fetch_file
31+
assert os.path.isfile("{}.json".format(mash_file.split(".")[0]))
3932

40-
# create a temporary file mash screen txt file
41-
mash_file = tmpdir.join("test_depth_file.txt")
42-
mash_file.write("ACC1\tseq1\t0\t900/1000")
4333

44-
# the expected result from loading the dictionary in the generated file
45-
expected_dict = {"ACC1": [1.0, 0.9, "seq1"]}
46-
47-
mashdist2json.main(str(mash_file), "0.5", "test", "assembly_file")
48-
49-
result_dict = json.load(open("{}.json".format(
50-
str(mash_file).split(".")[0])))
51-
52-
assert_message = []
34+
def test_generate_report(fetch_file):
35+
"""
36+
This tests if the report.json file is generated
37+
"""
38+
assert os.path.isfile(".report.json")
5339

54-
# checks if result_dict is indeed a dictionary
55-
if not isinstance(result_dict, dict):
56-
assert_message.append("Contents of the generated file must be a "
57-
"dictionary.")
5840

59-
if not result_dict == expected_dict:
60-
assert_message.append("The expected dictionary must be equal to the "
61-
"one present in the generated file.\n"
62-
"Expected: {}\n"
63-
"Result: {}". format(
64-
json.dumps(expected_dict),
65-
json.dumps(result_dict)
66-
))
41+
def test_generated_dict(fetch_file):
42+
"""
43+
This function checks if the file contains a dict
44+
"""
45+
result_dict, _ = fetch_file
46+
assert isinstance(result_dict, dict)
6747

68-
# remove the .report.json file
69-
os.remove(".report.json")
7048

71-
assert not assert_message, "Errors occurred:\n{}".format(
72-
"\n".join(assert_message)
73-
)
49+
def test_generated_dict_contents(fetch_file):
50+
# the expected result from loading the dictionary in the generated file
51+
expected_dict = {"ACC1": [1.0, 0.9, "seq1"]}
52+
result_dict, _ = fetch_file
53+
assert result_dict == expected_dict
Lines changed: 29 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,53 @@
11
import os
22
import json
3+
import pytest
34
import flowcraft.templates.mashscreen2json as mashscreen2json
45

56

6-
def test_generate_file(tmpdir):
7-
"""
8-
This function tests if the files generated by this template script are
9-
created
10-
"""
11-
7+
@pytest.fixture
8+
def fetch_file(tmpdir, request):
129
# create a temporary file mash screen txt file
1310
mash_file = tmpdir.join("test_depth_file.txt")
1411
mash_file.write("100\tNA\t30\tNA\tACC1")
1512

1613
mashscreen2json.main(str(mash_file), "test")
14+
result_dict = json.load(open("{}.json".format(
15+
str(mash_file).split(".")[0])))
1716

18-
assert_message = []
19-
# conditions to check that files are generated
20-
if not os.path.isfile("{}.json".format(
21-
str(mash_file).split(".")[0])):
22-
assert_message.append("mash screen json not created")
23-
if not os.path.isfile(".report.json"):
24-
assert_message.append(".report.json file was not created")
25-
26-
# remove the .report.json file
27-
os.remove(".report.json")
17+
# finalizer statement that removes .report.json
18+
def remove_test_files():
19+
os.remove(".report.json")
20+
request.addfinalizer(remove_test_files)
2821

29-
assert not assert_message, "errors occurred:\n{}".format(
30-
"\n".join(assert_message)
31-
)
22+
return result_dict, str(mash_file)
3223

3324

34-
def test_generated_dict(tmpdir):
25+
def test_generate_file(fetch_file):
3526
"""
3627
This function tests if the files generated by this template script are
3728
created
3829
"""
30+
_, mash_file = fetch_file
31+
assert os.path.isfile("{}.json".format(mash_file.split(".")[0]))
3932

40-
# create a temporary file mash screen txt file
41-
mash_file = tmpdir.join("test_depth_file.txt")
42-
mash_file.write("100\tNA\t30\tNA\tACC1")
43-
44-
# the expected result from loading the dictionary in the generated file
45-
expected_dict = {"ACC1": [100.0, 1]}
46-
47-
mashscreen2json.main(str(mash_file), "test")
48-
49-
result_dict = json.load(open("{}.json".format(
50-
str(mash_file).split(".")[0])))
51-
52-
assert_message = []
5333

54-
# checks if result_dict is indeed a dictionary
55-
if not isinstance(result_dict, dict):
56-
assert_message.append("Contents of the generated file must be a "
57-
"dictionary.")
34+
def test_generate_report(fetch_file):
35+
"""
36+
This tests if the report.json file is generated
37+
"""
38+
assert os.path.isfile(".report.json")
5839

59-
if not result_dict == expected_dict:
60-
assert_message.append("The expected dictionary must be equal to the "
61-
"one present in the generated file.\n"
62-
"Expected: {}\n"
63-
"Result: {}". format(
64-
json.dumps(expected_dict),
65-
json.dumps(result_dict)
66-
))
6740

68-
# remove the .report.json file
69-
os.remove(".report.json")
41+
def test_generated_dict(fetch_file):
42+
"""
43+
This function checks if the file contains a dict
44+
"""
45+
result_dict, _ = fetch_file
46+
assert isinstance(result_dict, dict)
7047

71-
assert not assert_message, "Errors occurred:\n{}".format(
72-
"\n".join(assert_message)
73-
)
7448

49+
def test_generated_dict_contents(fetch_file):
50+
# the expected result from loading the dictionary in the generated file
51+
expected_dict = {"ACC1": [100.0, 1]}
52+
result_dict, _ = fetch_file
53+
assert result_dict == expected_dict

0 commit comments

Comments
 (0)