Skip to content

Commit

Permalink
added custom fixtures to plasmid templates tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tiagofilipe12 committed Nov 6, 2018
1 parent 3d6b664 commit 0271f13
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 241 deletions.
104 changes: 40 additions & 64 deletions flowcraft/tests/template_tests/test_mapping2json.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import json
import pytest
import flowcraft.templates.mapping2json as mapping2json

depth_dict_coverage = {
Expand Down Expand Up @@ -34,6 +35,26 @@
}


@pytest.fixture
def fetch_file(tmpdir, request):
# create a temporary file depth txt file
depth_file = tmpdir.join("test_depth_file.txt")
depth_file.write("ACC1\t1\t30")
# creates a temporary file with the json_dict
json_dict = tmpdir.join("test_json_dict.json")
json_dict.write('{"ACC1": 2000}')
# executes the function to be tested
mapping2json.main(str(depth_file), str(json_dict), "0", "test")
result_dict = json.load(open("{}_mapping.json".format(str(depth_file))))

# finalizer statement that removes .report.json
def remove_test_files():
os.remove(".report.json")
request.addfinalizer(remove_test_files)

return result_dict, str(depth_file)


def test_depth_file_reader(tmpdir):
"""
test the output of depth_file_reader_function to be a given dict
Expand Down Expand Up @@ -67,78 +88,33 @@ def test_generate_jsons():
)


def test_generate_file(tmpdir):
def test_generate_file(fetch_file):
"""
This function tests if the files are generated by the main function.
.report.json and the file to import in pATLAS.
This function tests if the output json file is generated
"""
_, depth_file = fetch_file
assert os.path.isfile("{}_mapping.json".format(depth_file))

# create a temporary file depth txt file
depth_file = tmpdir.join("test_depth_file.txt")
depth_file.write("seq1\t1\t30")

# creates a temporary file with the json_dict
json_dict = tmpdir.join("test_json_dict.json")
json_dict.write('{"seq1": 2000}')

# executes the function to be tested
mapping2json.main(str(depth_file), str(json_dict), "0", "test")

assert_message = []
# conditions to check that files are generated
if not os.path.isfile("{}_mapping.json".format(str(depth_file))):
assert_message.append("_mapping.json not created")
if not os.path.isfile(".report.json"):
assert_message.append(".report.json file was not created")

# remove the .report.json file
os.remove(".report.json")

assert not assert_message, "errors occurred:\n{}".format(
"\n".join(assert_message)
)
def test_generate_report(fetch_file):
"""
This tests if the report.json file is generated
"""
assert os.path.isfile(".report.json")


def test_generated_dict(tmpdir):
def test_generated_dict(fetch_file):
"""
This function tests if the files generated by this template script are
created
This function checks if the file contains a dict
"""
result_dict, _ = fetch_file
assert isinstance(result_dict, dict)

# create a temporary file depth txt file
depth_file = tmpdir.join("test_depth_file.txt")
depth_file.write("ACC1\t1\t30")

# creates a temporary file with the json_dict
json_dict = tmpdir.join("test_json_dict.json")
json_dict.write('{"ACC1": 2000}')

def test_generated_dict_contents(fetch_file):
"""
This function tests if the dictionary in the file has the required fields
"""
expected_dict = {"ACC1": 0.0}

# executes the function to be tested
mapping2json.main(str(depth_file), str(json_dict), "0", "test")

result_dict = json.load(open("{}_mapping.json".format(str(depth_file))))

assert_message = []

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

if not result_dict == expected_dict:
assert_message.append("The expected dictionary must be equal to the "
"one present in the generated file.\n"
"Expected: {}\n"
"Result: {}". format(
json.dumps(expected_dict),
json.dumps(result_dict)
))

# remove the .report.json file
os.remove(".report.json")

assert not assert_message, "Errors occurred:\n{}".format(
"\n".join(assert_message)
)
result_dict, _ = fetch_file
assert result_dict == expected_dict
78 changes: 29 additions & 49 deletions flowcraft/tests/template_tests/test_mashdist2json.py
Original file line number Diff line number Diff line change
@@ -1,73 +1,53 @@
import os
import json
import pytest
import flowcraft.templates.mashdist2json as mashdist2json


def test_generate_file(tmpdir):
"""
This function tests if the files generated by this template script are
created
"""

@pytest.fixture
def fetch_file(tmpdir, request):
# create a temporary file mash screen txt file
mash_file = tmpdir.join("test_depth_file.txt")
mash_file.write("ACC1\tseq1\t0\t900/1000")

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

assert_message = []
# conditions to check that files are generated
if not os.path.isfile("{}.json".format(
str(mash_file).split(".")[0])):
assert_message.append("mash dist json not created")
if not os.path.isfile(".report.json"):
assert_message.append(".report.json file was not created")

# remove the .report.json file
os.remove(".report.json")
# finalizer statement that removes .report.json
def remove_test_files():
os.remove(".report.json")
request.addfinalizer(remove_test_files)

assert not assert_message, "errors occurred:\n{}".format(
"\n".join(assert_message)
)
return result_dict, str(mash_file)


def test_generated_dict(tmpdir):
def test_generate_file(fetch_file):
"""
This function tests if the files generated by this template script are
created
"""
_, mash_file = fetch_file
assert os.path.isfile("{}.json".format(mash_file.split(".")[0]))

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

# the expected result from loading the dictionary in the generated file
expected_dict = {"ACC1": [1.0, 0.9, "seq1"]}

mashdist2json.main(str(mash_file), "0.5", "test", "assembly_file")

result_dict = json.load(open("{}.json".format(
str(mash_file).split(".")[0])))

assert_message = []
def test_generate_report(fetch_file):
"""
This tests if the report.json file is generated
"""
assert os.path.isfile(".report.json")

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

if not result_dict == expected_dict:
assert_message.append("The expected dictionary must be equal to the "
"one present in the generated file.\n"
"Expected: {}\n"
"Result: {}". format(
json.dumps(expected_dict),
json.dumps(result_dict)
))
def test_generated_dict(fetch_file):
"""
This function checks if the file contains a dict
"""
result_dict, _ = fetch_file
assert isinstance(result_dict, dict)

# remove the .report.json file
os.remove(".report.json")

assert not assert_message, "Errors occurred:\n{}".format(
"\n".join(assert_message)
)
def test_generated_dict_contents(fetch_file):
# the expected result from loading the dictionary in the generated file
expected_dict = {"ACC1": [1.0, 0.9, "seq1"]}
result_dict, _ = fetch_file
assert result_dict == expected_dict
79 changes: 29 additions & 50 deletions flowcraft/tests/template_tests/test_mashscreen2json.py
Original file line number Diff line number Diff line change
@@ -1,74 +1,53 @@
import os
import json
import pytest
import flowcraft.templates.mashscreen2json as mashscreen2json


def test_generate_file(tmpdir):
"""
This function tests if the files generated by this template script are
created
"""

@pytest.fixture
def fetch_file(tmpdir, request):
# create a temporary file mash screen txt file
mash_file = tmpdir.join("test_depth_file.txt")
mash_file.write("100\tNA\t30\tNA\tACC1")

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

assert_message = []
# conditions to check that files are generated
if not os.path.isfile("{}.json".format(
str(mash_file).split(".")[0])):
assert_message.append("mash screen json not created")
if not os.path.isfile(".report.json"):
assert_message.append(".report.json file was not created")

# remove the .report.json file
os.remove(".report.json")
# finalizer statement that removes .report.json
def remove_test_files():
os.remove(".report.json")
request.addfinalizer(remove_test_files)

assert not assert_message, "errors occurred:\n{}".format(
"\n".join(assert_message)
)
return result_dict, str(mash_file)


def test_generated_dict(tmpdir):
def test_generate_file(fetch_file):
"""
This function tests if the files generated by this template script are
created
"""
_, mash_file = fetch_file
assert os.path.isfile("{}.json".format(mash_file.split(".")[0]))

# create a temporary file mash screen txt file
mash_file = tmpdir.join("test_depth_file.txt")
mash_file.write("100\tNA\t30\tNA\tACC1")

# the expected result from loading the dictionary in the generated file
expected_dict = {"ACC1": [100.0, 1]}

mashscreen2json.main(str(mash_file), "test")

result_dict = json.load(open("{}.json".format(
str(mash_file).split(".")[0])))

assert_message = []

# checks if result_dict is indeed a dictionary
if not isinstance(result_dict, dict):
assert_message.append("Contents of the generated file must be a "
"dictionary.")
def test_generate_report(fetch_file):
"""
This tests if the report.json file is generated
"""
assert os.path.isfile(".report.json")

if not result_dict == expected_dict:
assert_message.append("The expected dictionary must be equal to the "
"one present in the generated file.\n"
"Expected: {}\n"
"Result: {}". format(
json.dumps(expected_dict),
json.dumps(result_dict)
))

# remove the .report.json file
os.remove(".report.json")
def test_generated_dict(fetch_file):
"""
This function checks if the file contains a dict
"""
result_dict, _ = fetch_file
assert isinstance(result_dict, dict)

assert not assert_message, "Errors occurred:\n{}".format(
"\n".join(assert_message)
)

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

0 comments on commit 0271f13

Please sign in to comment.