|
1 | 1 | """Test write invalid reasons module""" |
2 | | - |
| 2 | +import pandas as pd |
| 3 | +import pytest |
| 4 | +import synapseclient |
3 | 5 | from unittest import mock |
4 | 6 | from unittest.mock import patch |
5 | 7 |
|
6 | 8 | from genie import write_invalid_reasons |
7 | | -import pandas as pd |
8 | | -import synapseclient |
| 9 | + |
| 10 | + |
| 11 | +@pytest.fixture |
| 12 | +def mock_centers_mapping(): |
| 13 | + df = pd.DataFrame( |
| 14 | + { |
| 15 | + "center": ["A", "B"], |
| 16 | + "errorsSynId": ["synErrA", "synErrB"], |
| 17 | + "stagingSynId": ["synStageA", "synStageB"], |
| 18 | + "inputSynId": ["synInputA", "synInputB"], |
| 19 | + }, |
| 20 | + ) |
| 21 | + df.index = ["0_1", "1_1"] |
| 22 | + return df |
| 23 | + |
9 | 24 |
|
10 | 25 | CENTER_ERRORSDF = pd.DataFrame( |
11 | 26 | { |
@@ -52,3 +67,62 @@ def test_get_center_invalid_errors(syn): |
52 | 67 | assert center_invalid == {"SAGE": "errors", "TEST": "errors"} |
53 | 68 | patch_query.assert_called_once_with("SELECT * FROM syn3333") |
54 | 69 | assert patch_combine.call_count == 2 |
| 70 | + |
| 71 | + |
| 72 | +def test_write_writes_no_errors_and_correct_errors_and_uses_correct_parent_ids( |
| 73 | + mock_centers_mapping, |
| 74 | +): |
| 75 | + syn = mock.Mock() |
| 76 | + center_errors = {"A": "A had errors"} # no errors for center B |
| 77 | + |
| 78 | + with mock.patch.object( |
| 79 | + write_invalid_reasons.extract, |
| 80 | + "get_syntabledf", |
| 81 | + return_value=mock_centers_mapping, |
| 82 | + ), mock.patch.object( |
| 83 | + write_invalid_reasons, "get_center_invalid_errors", return_value=center_errors |
| 84 | + ), mock.patch.object( |
| 85 | + write_invalid_reasons.synapseclient, "File" |
| 86 | + ) as m_file_cls, mock.patch.object( |
| 87 | + write_invalid_reasons.os, "remove" |
| 88 | + ) as m_remove: |
| 89 | + # Make open() return a different handle per file so we can assert per-center writes |
| 90 | + file_handles = {} |
| 91 | + |
| 92 | + def _open_side_effect(filename, mode): |
| 93 | + fh = mock.Mock(name=f"handle_{filename}") |
| 94 | + ctx = mock.Mock() |
| 95 | + ctx.__enter__ = mock.Mock(return_value=fh) |
| 96 | + ctx.__exit__ = mock.Mock(return_value=False) |
| 97 | + file_handles[filename] = fh |
| 98 | + return ctx |
| 99 | + |
| 100 | + with mock.patch("builtins.open", side_effect=_open_side_effect) as m_open: |
| 101 | + # Make File() return different objects so we can assert store calls too (optional) |
| 102 | + ent_a = mock.Mock(name="ent_a") |
| 103 | + ent_b = mock.Mock(name="ent_b") |
| 104 | + m_file_cls.side_effect = [ent_a, ent_b] |
| 105 | + |
| 106 | + write_invalid_reasons.write( |
| 107 | + syn=syn, |
| 108 | + center_mapping_synid="synCenterMap", |
| 109 | + error_tracker_synid="synErrTrack", |
| 110 | + ) |
| 111 | + |
| 112 | + # assertions: open + writes |
| 113 | + m_open.assert_any_call("A_validation_errors.txt", "w") |
| 114 | + m_open.assert_any_call("B_validation_errors.txt", "w") |
| 115 | + |
| 116 | + file_handles["A_validation_errors.txt"].write.assert_called_once_with( |
| 117 | + "A had errors" |
| 118 | + ) |
| 119 | + file_handles["B_validation_errors.txt"].write.assert_called_once_with("No errors!") |
| 120 | + |
| 121 | + # assertions: correct Synapse folder IDs used (parentId) |
| 122 | + m_file_cls.assert_any_call("A_validation_errors.txt", parentId="synErrA") |
| 123 | + m_file_cls.assert_any_call("B_validation_errors.txt", parentId="synErrB") |
| 124 | + |
| 125 | + # Synapse store + cleanup |
| 126 | + assert syn.store.call_args_list == [mock.call(ent_a), mock.call(ent_b)] |
| 127 | + m_remove.assert_any_call("A_validation_errors.txt") |
| 128 | + m_remove.assert_any_call("B_validation_errors.txt") |
0 commit comments