|
| 1 | +import vimms.MzmlWriter as mzml_writer_module |
| 2 | +from vimms.MzmlWriter import MzmlWriter |
| 3 | + |
| 4 | + |
| 5 | +class _ContextManager: |
| 6 | + def __enter__(self): |
| 7 | + return self |
| 8 | + |
| 9 | + def __exit__(self, exc_type, exc_value, traceback): |
| 10 | + return False |
| 11 | + |
| 12 | + |
| 13 | +class _FakePsimsMzMLWriter: |
| 14 | + opened_handles = [] |
| 15 | + |
| 16 | + def __init__(self, out_handle): |
| 17 | + self.opened_handles.append(out_handle) |
| 18 | + |
| 19 | + def __enter__(self): |
| 20 | + return self |
| 21 | + |
| 22 | + def __exit__(self, exc_type, exc_value, traceback): |
| 23 | + return False |
| 24 | + |
| 25 | + def controlled_vocabularies(self): |
| 26 | + pass |
| 27 | + |
| 28 | + def file_description(self, *args, **kwargs): |
| 29 | + pass |
| 30 | + |
| 31 | + def sample_list(self, *args, **kwargs): |
| 32 | + pass |
| 33 | + |
| 34 | + def software_list(self, *args, **kwargs): |
| 35 | + pass |
| 36 | + |
| 37 | + def scan_settings_list(self, *args, **kwargs): |
| 38 | + pass |
| 39 | + |
| 40 | + def instrument_configuration_list(self, *args, **kwargs): |
| 41 | + pass |
| 42 | + |
| 43 | + def data_processing_list(self, *args, **kwargs): |
| 44 | + pass |
| 45 | + |
| 46 | + def run(self, *args, **kwargs): |
| 47 | + return _ContextManager() |
| 48 | + |
| 49 | + def spectrum_list(self, *args, **kwargs): |
| 50 | + return _ContextManager() |
| 51 | + |
| 52 | + def chromatogram_list(self, *args, **kwargs): |
| 53 | + return _ContextManager() |
| 54 | + |
| 55 | + def write_chromatogram(self, *args, **kwargs): |
| 56 | + pass |
| 57 | + |
| 58 | + def close(self): |
| 59 | + pass |
| 60 | + |
| 61 | + |
| 62 | +def test_mzml_writer_closes_output_handle(monkeypatch, tmp_path): |
| 63 | + _FakePsimsMzMLWriter.opened_handles = [] |
| 64 | + monkeypatch.setattr(mzml_writer_module, "PsimsMzMLWriter", _FakePsimsMzMLWriter) |
| 65 | + |
| 66 | + MzmlWriter("test_analysis", {1: []}).write_mzML(tmp_path / "test.mzML") |
| 67 | + |
| 68 | + assert len(_FakePsimsMzMLWriter.opened_handles) == 1 |
| 69 | + assert _FakePsimsMzMLWriter.opened_handles[0].closed |
0 commit comments