|
| 1 | +"""Test the general loader module.""" |
| 2 | + |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | +import pytest |
| 6 | +import numpy as np |
| 7 | +from AFMReader import general_loader |
| 8 | + |
| 9 | + |
| 10 | +BASE_DIR = Path.cwd() |
| 11 | +RESOURCES = BASE_DIR / "tests" / "resources" |
| 12 | + |
| 13 | + |
| 14 | +@pytest.mark.parametrize( |
| 15 | + ("filepath", "channel", "error", "message"), |
| 16 | + [ |
| 17 | + pytest.param( |
| 18 | + RESOURCES / "sample_0.asd", |
| 19 | + "TP", |
| 20 | + False, |
| 21 | + "Extracted image", |
| 22 | + id="'.asd' success.", |
| 23 | + ), |
| 24 | + pytest.param( |
| 25 | + RESOURCES / "sample_0.asd", |
| 26 | + "notherelol", |
| 27 | + True, |
| 28 | + "'notherelol' not found .asd channel list: TP, PH", |
| 29 | + id="'asd' channel not found.", |
| 30 | + ), |
| 31 | + pytest.param( |
| 32 | + RESOURCES / "sample_0.gwy", |
| 33 | + "ZSensor", |
| 34 | + False, |
| 35 | + "Extracted image", |
| 36 | + id="'.gwy' success.", |
| 37 | + ), |
| 38 | + pytest.param( |
| 39 | + RESOURCES / "sample_0.gwy", |
| 40 | + "SenZor", |
| 41 | + True, |
| 42 | + "'SenZor' not found in .gwy channel list: {'ZSensor': '0', 'Peak Force Error': '1', 'Stiffness': '2', " |
| 43 | + "'LogStiffness': '3', 'Adhesion': '4', 'Deformation': '5', 'Dissipation': '6', 'Height': '7'}", |
| 44 | + id="'.gwy' channel not found.", |
| 45 | + ), |
| 46 | + pytest.param( |
| 47 | + RESOURCES / "sample_0.ibw", |
| 48 | + "HeightTracee", |
| 49 | + False, |
| 50 | + "Extracted image", |
| 51 | + id="'.ibw' success.", |
| 52 | + ), |
| 53 | + pytest.param( |
| 54 | + RESOURCES / "sample_0.ibw", |
| 55 | + "Hight", |
| 56 | + True, |
| 57 | + "'Hight' not in .ibw channel list: ['HeightTracee', 'HeightRetrace', 'ZSensorTrace', 'ZSensorRetrace', " |
| 58 | + "'UserIn0Trace', 'UserIn0Retrace', 'UserIn1Trace', 'UserIn1Retrace']", |
| 59 | + id="'.ibw' channel not found.", |
| 60 | + ), |
| 61 | + pytest.param( |
| 62 | + RESOURCES / "sample_0.jpk", |
| 63 | + "height_trace", |
| 64 | + False, |
| 65 | + "Extracted image", |
| 66 | + id="'.jpk' success.", |
| 67 | + ), |
| 68 | + pytest.param( |
| 69 | + RESOURCES / "sample_0.jpk", |
| 70 | + "might_base", |
| 71 | + True, |
| 72 | + "'might_base' not in .jpk channel list: {'height_retrace': 1, 'measuredHeight_retrace': 2, " |
| 73 | + "'amplitude_retrace': 3, 'phase_retrace': 4, 'error_retrace': 5, 'height_trace': 6, " |
| 74 | + "'measuredHeight_trace': 7, 'amplitude_trace': 8, 'phase_trace': 9, 'error_trace': 10}", |
| 75 | + id="'.jpk' channel not found.", |
| 76 | + ), |
| 77 | + pytest.param( |
| 78 | + RESOURCES / "sample_0.spm", |
| 79 | + "Height", |
| 80 | + False, |
| 81 | + "Extracted channel Height", |
| 82 | + id="'.spm' success.", |
| 83 | + ), |
| 84 | + pytest.param( |
| 85 | + RESOURCES / "sample_0.spm", |
| 86 | + "Force", |
| 87 | + True, |
| 88 | + "'Force' not in .spm channel list: ['Height Sensor', 'Peak Force Error', 'DMTModulus', 'LogDMTModulus', " |
| 89 | + "'Adhesion', 'Deformation', 'Dissipation', 'Height']", |
| 90 | + id="'.spm' channel not found.", |
| 91 | + ), |
| 92 | + pytest.param( |
| 93 | + RESOURCES / "sample_0.stp", |
| 94 | + "", |
| 95 | + False, |
| 96 | + "Extracted image", |
| 97 | + id="'.stp' success.", |
| 98 | + ), |
| 99 | + pytest.param( |
| 100 | + RESOURCES / "sample_0.top", |
| 101 | + "", |
| 102 | + False, |
| 103 | + "Extracted image", |
| 104 | + id="'.top' success.", |
| 105 | + ), |
| 106 | + pytest.param( |
| 107 | + RESOURCES / "sample_0_1.topostats", |
| 108 | + "image", |
| 109 | + False, |
| 110 | + "Extracted .topostats dictionary.", |
| 111 | + id="'.topostats' success.", |
| 112 | + ), |
| 113 | + pytest.param( |
| 114 | + RESOURCES / "sample_0_1.topostats", |
| 115 | + "hgjswbweongp", |
| 116 | + True, |
| 117 | + "'hgjswbweongp' not in available image keys: ['image']", |
| 118 | + id="'.topostats' channel not found.", |
| 119 | + ), |
| 120 | + pytest.param( |
| 121 | + RESOURCES / "sample_0.xxx", |
| 122 | + "NotAChannel", |
| 123 | + True, |
| 124 | + "File type '.xxx' is not currently handled by AFMReader.", |
| 125 | + id="'.xxx' unsupported filetype.", |
| 126 | + ), |
| 127 | + ], |
| 128 | +) |
| 129 | +def test_load(caplog: pytest.LogCaptureFixture, filepath: Path, channel: str, error: bool, message: str) -> None: |
| 130 | + """Test loading of all (asd, gwy, ibw, jpk, spm, stp, top, topostats) filetypes.""" |
| 131 | + loader = general_loader.LoadFile(filepath, channel) |
| 132 | + |
| 133 | + image, px2nm = loader.load() |
| 134 | + |
| 135 | + if not error: |
| 136 | + # check array and px2nm returned |
| 137 | + assert isinstance(image, np.ndarray) |
| 138 | + assert isinstance(px2nm, float) |
| 139 | + else: |
| 140 | + # check when channel wrong |
| 141 | + assert isinstance(image, ValueError) |
| 142 | + assert px2nm is None |
| 143 | + |
| 144 | + # check output logs |
| 145 | + assert message in caplog.text |
| 146 | + |
| 147 | + |
| 148 | +@pytest.mark.parametrize( |
| 149 | + ("filepath"), |
| 150 | + [ |
| 151 | + pytest.param( |
| 152 | + RESOURCES / "not_a_real_file.spm", |
| 153 | + id="File not found error raised.", |
| 154 | + ), |
| 155 | + ], |
| 156 | +) |
| 157 | +def test_load_filenotfounderror(filepath: Path) -> None: |
| 158 | + """Test that a file not found error is raise when filepath is wrong.""" |
| 159 | + loader = general_loader.LoadFile(filepath, "channel") |
| 160 | + |
| 161 | + with pytest.raises(FileNotFoundError) as execinfo: # noqa: PT012 |
| 162 | + _, _ = loader.load() |
| 163 | + assert "[not_a_real_file] FileNotFoundError" in execinfo.value |
0 commit comments