|
| 1 | +import pytest |
| 2 | +import sys |
| 3 | +import importlib |
| 4 | +from unittest.mock import MagicMock, patch |
| 5 | + |
| 6 | +# ============================================================================ |
| 7 | +# 1. Test Lakeshore 350 Driver Class |
| 8 | +# Target: pica/keithley/k6517b/High_Resistance/Instrument_Control/IV_K6517B_L350_T_Control_Instrument_Control.py |
| 9 | +# ============================================================================ |
| 10 | +def test_lakeshore_driver_logic(): |
| 11 | + """ |
| 12 | + Tests the Lakeshore350 class logic without connecting to a real instrument. |
| 13 | + """ |
| 14 | + module_name = "pica.keithley.k6517b.High_Resistance.Instrument_Control.IV_K6517B_L350_T_Control_Instrument_Control" |
| 15 | + |
| 16 | + # Import the module |
| 17 | + module = importlib.import_module(module_name) |
| 18 | + |
| 19 | + # Mock the pyvisa resource manager so we don't need a real GPIB cable |
| 20 | + with patch("pyvisa.ResourceManager") as MockRM: |
| 21 | + mock_instr = MagicMock() |
| 22 | + MockRM.return_value.open_resource.return_value = mock_instr |
| 23 | + mock_instr.query.return_value = "LSCI,MODEL350,12345,1.0" # Fake IDN response |
| 24 | + |
| 25 | + # Instantiate the class |
| 26 | + lakeshore = module.Lakeshore350("GPIB::12") |
| 27 | + |
| 28 | + lakeshore.reset_and_clear() |
| 29 | + mock_instr.write.assert_any_call('*RST') |
| 30 | + |
| 31 | + lakeshore.set_setpoint(1, 300) |
| 32 | + mock_instr.write.assert_any_call('SETP 1,300') |
| 33 | + |
| 34 | + lakeshore.setup_heater(1, 1, 2) |
| 35 | + lakeshore.setup_ramp(1, 10, True) |
| 36 | + |
| 37 | + # Test getters |
| 38 | + mock_instr.query.return_value = "295.5" |
| 39 | + temp = lakeshore.get_temperature('A') |
| 40 | + assert temp == 295.5 |
| 41 | + |
| 42 | + lakeshore.close() |
| 43 | + |
| 44 | +# ============================================================================ |
| 45 | +# 2. Test Pyroelectric Measurement Script |
| 46 | +# Target: pica/keithley/k6517b/Pyroelectricity/Instrument_Control/Pyroelectric_K6517B_Working_Instrument_Control.py |
| 47 | +# ============================================================================ |
| 48 | +def test_pyroelectric_script_import(): |
| 49 | + """ |
| 50 | + Tests that the pyroelectric control script can be imported and main() exists. |
| 51 | + We don't run main() fully because it contains an infinite loop. |
| 52 | + """ |
| 53 | + module_name = "pica.keithley.k6517b.Pyroelectricity.Instrument_Control.Pyroelectric_K6517B_Working_Instrument_Control" |
| 54 | + |
| 55 | + # Just importing it covers the definitions |
| 56 | + module = importlib.import_module(module_name) |
| 57 | + assert hasattr(module, 'main') |
| 58 | + |
| 59 | +# ============================================================================ |
| 60 | +# 3. Test Visualization Script (Tricky: Runs on Import) |
| 61 | +# Target: pica/keithley/k6517b/Pyroelectricity/Instrument_Control/PyroDataVisualization_Simple.py |
| 62 | +# ============================================================================ |
| 63 | +def test_visualization_script_safe_run(): |
| 64 | + """ |
| 65 | + This script runs code immediately upon import (it has no 'if __name__ == main' guard). |
| 66 | + We must mock tkinter and file dialogs BEFORE importing to prevent the test from hanging. |
| 67 | + """ |
| 68 | + module_name = "pica.keithley.k6517b.Pyroelectricity.Instrument_Control.PyroDataVisualization_Simple" |
| 69 | + |
| 70 | + with patch("tkinter.Tk"), \ |
| 71 | + patch("tkinter.filedialog.askopenfilename", return_value="dummy_data.csv"), \ |
| 72 | + patch("pandas.read_csv") as mock_read, \ |
| 73 | + patch("matplotlib.pyplot.show"), \ |
| 74 | + patch("matplotlib.pyplot.subplots", return_value=(MagicMock(), [MagicMock(), MagicMock(), MagicMock()])): |
| 75 | + |
| 76 | + # Mock pandas data so the plotting logic doesn't crash |
| 77 | + mock_df = MagicMock() |
| 78 | + mock_df.__getitem__.return_value = [1, 2, 3] # Fake columns |
| 79 | + mock_read.return_value = mock_df |
| 80 | + |
| 81 | + # If the module was already imported by another test, reload it to trigger execution |
| 82 | + if module_name in sys.modules: |
| 83 | + importlib.reload(sys.modules[module_name]) |
| 84 | + else: |
| 85 | + importlib.import_module(module_name) |
| 86 | + |
| 87 | +# ============================================================================ |
| 88 | +# 4. Test GPIB Scanner Script (Runs on Import) |
| 89 | +# Target: pica/utils/GPIB_VISA_InterfaceTest_Simple_Instrument_Control.py |
| 90 | +# ============================================================================ |
| 91 | +def test_gpib_scanner_script(): |
| 92 | + """ |
| 93 | + This script also runs immediately on import. We mock PyVISA to prevent errors. |
| 94 | + """ |
| 95 | + module_name = "pica.utils.GPIB_VISA_InterfaceTest_Simple_Instrument_Control" |
| 96 | + |
| 97 | + with patch("pyvisa.ResourceManager") as MockRM: |
| 98 | + # Simulate finding one instrument |
| 99 | + mock_instr = MagicMock() |
| 100 | + mock_instr.query.return_value = "Keithley Instruments, Model 2400" |
| 101 | + |
| 102 | + instance = MockRM.return_value |
| 103 | + instance.list_resources.return_value = ["GPIB0::24::INSTR"] |
| 104 | + instance.open_resource.return_value.__enter__.return_value = mock_instr |
| 105 | + |
| 106 | + if module_name in sys.modules: |
| 107 | + importlib.reload(sys.modules[module_name]) |
| 108 | + else: |
| 109 | + importlib.import_module(module_name) |
0 commit comments