|
1 | 1 | """Test the controls module.""" |
2 | 2 |
|
| 3 | +import contextlib |
| 4 | +import os |
| 5 | +import tempfile |
3 | 6 | from typing import Any, Union |
4 | 7 |
|
5 | 8 | import pydantic |
|
9 | 12 | from RATapi.utils.enums import BoundHandling, Display, Parallel, Procedures, Strategies |
10 | 13 |
|
11 | 14 |
|
| 15 | +@pytest.fixture |
| 16 | +def IPC_controls(): |
| 17 | + """A controls object with a temporary file set as the IPC file.""" |
| 18 | + IPC_controls = Controls() |
| 19 | + IPC_obj, IPC_controls._IPCFilePath = tempfile.mkstemp() |
| 20 | + os.close(IPC_obj) |
| 21 | + yield IPC_controls |
| 22 | + with contextlib.suppress(FileNotFoundError): |
| 23 | + os.remove(IPC_controls._IPCFilePath) |
| 24 | + |
| 25 | + |
12 | 26 | def test_initialise_procedure_error() -> None: |
13 | 27 | """Tests for a ValidationError if the procedure property of the Controls class is initialised with an invalid |
14 | 28 | value. |
@@ -851,3 +865,35 @@ def test_dream_nChains_error(self, value: int) -> None: |
851 | 865 | def test_control_class_dream_str(self, table_str) -> None: |
852 | 866 | """Tests the Dream model __str__.""" |
853 | 867 | assert self.dream.__str__() == table_str |
| 868 | + |
| 869 | + |
| 870 | +def test_initialise_IPC() -> None: |
| 871 | + """Tests that an Inter-Process Communication File can be set up.""" |
| 872 | + test_controls = Controls() |
| 873 | + test_controls.initialise_IPC() |
| 874 | + assert test_controls._IPCFilePath != "" |
| 875 | + with open(test_controls._IPCFilePath, "rb") as f: |
| 876 | + file_content = f.read() |
| 877 | + assert file_content == b"0" |
| 878 | + os.remove(test_controls._IPCFilePath) |
| 879 | + |
| 880 | + |
| 881 | +def test_sendStopEvent(IPC_controls) -> None: |
| 882 | + """Tests that an Inter-Process Communication File can be modified.""" |
| 883 | + IPC_controls.sendStopEvent() |
| 884 | + with open(IPC_controls._IPCFilePath, "rb") as f: |
| 885 | + file_content = f.read() |
| 886 | + assert file_content == b"1" |
| 887 | + |
| 888 | + |
| 889 | +def test_sendStopEvent_empty_file() -> None: |
| 890 | + """Tests that we do not write to a non-existent Inter-Process Communication File.""" |
| 891 | + test_controls = Controls() |
| 892 | + with pytest.warns(UserWarning, match="An IPC file was not initialised."): |
| 893 | + test_controls.sendStopEvent() |
| 894 | + |
| 895 | + |
| 896 | +def test_delete_IPC(IPC_controls) -> None: |
| 897 | + """Tests that an Inter-Process Communication File can be safely removed.""" |
| 898 | + IPC_controls.delete_IPC() |
| 899 | + assert not os.path.isfile(IPC_controls._IPCFilePath) |
0 commit comments