|
1 | 1 | import os |
| 2 | +import re |
| 3 | +import json |
2 | 4 | import flowcraft.templates.pATLAS_consensus_json as pATLAS_consensus_json |
3 | 5 |
|
4 | 6 |
|
@@ -33,3 +35,74 @@ def test_generate_file(tmpdir): |
33 | 35 | assert not assert_message, "errors occurred:\n{}".format( |
34 | 36 | "\n".join(assert_message) |
35 | 37 | ) |
| 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