|
| 1 | +import pytest |
| 2 | +import inspect |
| 3 | +import socket |
| 4 | +from collections import namedtuple |
| 5 | + |
| 6 | +from cqc.pythonLib import CQCConnection |
| 7 | + |
| 8 | +Call = namedtuple("Call", ["name", "args", "kwargs"]) |
| 9 | + |
| 10 | + |
| 11 | +def _spy_wrapper(method): |
| 12 | + """Wraps a method to be able to spy on it""" |
| 13 | + def new_method(self, *args, **kwargs): |
| 14 | + if method.__name__ == '__init__': |
| 15 | + self.calls = [] |
| 16 | + call = Call(method.__name__, args, kwargs) |
| 17 | + self.calls.append(call) |
| 18 | + return method(self, *args, **kwargs) |
| 19 | + |
| 20 | + return new_method |
| 21 | + |
| 22 | + |
| 23 | +def spy_on_class(cls): |
| 24 | + """Spies on all calls to the methods of a class""" |
| 25 | + for method_name, method in inspect.getmembers(cls, predicate=inspect.isfunction): |
| 26 | + setattr(cls, method_name, _spy_wrapper(method)) |
| 27 | + return cls |
| 28 | + |
| 29 | + |
| 30 | +@spy_on_class |
| 31 | +class MockSocket: |
| 32 | + def __init__(self, *args, **kwargs): |
| 33 | + pass |
| 34 | + |
| 35 | + def connect(self, *args, **kwargs): |
| 36 | + pass |
| 37 | + |
| 38 | + def send(self, *args, **kwargs): |
| 39 | + pass |
| 40 | + |
| 41 | + def recv(self, *args, **kwargs): |
| 42 | + pass |
| 43 | + |
| 44 | + def close(self, *args, **kwargs): |
| 45 | + pass |
| 46 | + |
| 47 | + |
| 48 | +@pytest.fixture |
| 49 | +def mock_socket(monkeypatch): |
| 50 | + def get_mocked_socket(*args, **kwargs): |
| 51 | + mock_socket = MockSocket(*args, **kwargs) |
| 52 | + return mock_socket |
| 53 | + |
| 54 | + monkeypatch.setattr(socket, "socket", get_mocked_socket) |
| 55 | + |
| 56 | + |
| 57 | +class MockedFirstMessage: |
| 58 | + """Mocks the second header returned by CQCConnection.readMessage""" |
| 59 | + class MockedTypeEntry: |
| 60 | + def __eq__(self, other): |
| 61 | + """This type will be equal to any integer.""" |
| 62 | + return isinstance(other, int) |
| 63 | + |
| 64 | + @property |
| 65 | + def tp(self): |
| 66 | + return self.MockedTypeEntry() |
| 67 | + |
| 68 | + |
| 69 | +class MockedOtherMessage: |
| 70 | + """Mocks the second header returned by CQCConnection.readMessage""" |
| 71 | + next_qubit_id = 0 |
| 72 | + |
| 73 | + @property |
| 74 | + def qubit_id(self): |
| 75 | + qid = self.next_qubit_id |
| 76 | + self.next_qubit_id += 1 |
| 77 | + return qid |
| 78 | + |
| 79 | + @property |
| 80 | + def outcome(self): |
| 81 | + return 0 |
| 82 | + |
| 83 | + @property |
| 84 | + def datetime(self): |
| 85 | + return 0 |
| 86 | + |
| 87 | + |
| 88 | +@pytest.fixture |
| 89 | +def mock_read_message(monkeypatch): |
| 90 | + """Mock the readMessage, check_error and print_CQC_msg from CQCConnection when testing.""" |
| 91 | + def mocked_readMessage(self): |
| 92 | + return [MockedFirstMessage(), MockedOtherMessage()] |
| 93 | + |
| 94 | + def mocked_print_CQC_msg(self, message): |
| 95 | + pass |
| 96 | + |
| 97 | + def mocked_check_error(self, hdr): |
| 98 | + pass |
| 99 | + |
| 100 | + monkeypatch.setattr(CQCConnection, "readMessage", mocked_readMessage) |
| 101 | + monkeypatch.setattr(CQCConnection, "print_CQC_msg", mocked_print_CQC_msg) |
| 102 | + monkeypatch.setattr(CQCConnection, "check_error", mocked_check_error) |
0 commit comments