|
| 1 | +import os |
| 2 | +from datetime import datetime, UTC |
| 3 | +import pytest |
| 4 | + |
| 5 | +import utils |
| 6 | +import messages |
| 7 | + |
| 8 | + |
| 9 | +def decode_fix(msg: bytes) -> dict[str, str]: |
| 10 | + string = msg.decode('utf-8').replace('\x01', '|') |
| 11 | + return utils.get_attrs(string) |
| 12 | + |
| 13 | + |
| 14 | +def test_get_attrs_and_get_attr(): |
| 15 | + msg = "8=FIX.4.4|35=D|49=CLIENT|56=PT-OE|11=123|" |
| 16 | + attrs = utils.get_attrs(msg) |
| 17 | + assert attrs["8"] == "FIX.4.4" |
| 18 | + assert utils.get_attr(msg, "35") == "D" |
| 19 | + assert utils.get_attr(msg, "99") is None |
| 20 | + |
| 21 | + |
| 22 | +def test_format_epoch_time(): |
| 23 | + assert utils.format_epoch_time(0) == "19700101-00:00:00.000" |
| 24 | + |
| 25 | + |
| 26 | +def test_get_log_filename(monkeypatch): |
| 27 | + fixed = datetime(2024, 1, 2, 3, 4, 5, tzinfo=UTC) |
| 28 | + |
| 29 | + class DummyDatetime: |
| 30 | + @classmethod |
| 31 | + def utcnow(cls): |
| 32 | + return fixed |
| 33 | + |
| 34 | + monkeypatch.setattr(utils, "datetime", DummyDatetime) |
| 35 | + filename = utils.get_log_filename("prefix") |
| 36 | + assert filename == "prefix_20240102_030405.log" |
| 37 | + |
| 38 | + |
| 39 | +def test_generate_jwt_success(monkeypatch): |
| 40 | + os.environ["API_SECRET"] = "secret" |
| 41 | + os.environ["API_URI"] = "uri" |
| 42 | + monkeypatch.setattr(utils.jwt, "encode", lambda p, k, algorithm=None: "TOKEN") |
| 43 | + assert utils.generateJWT("api", 123) == "TOKEN" |
| 44 | + |
| 45 | + |
| 46 | +def test_getMsgHeartbeat_and_errors(): |
| 47 | + with pytest.raises(AssertionError): |
| 48 | + messages.getMsgHeartbeat(None, 3) |
| 49 | + with pytest.raises(AssertionError): |
| 50 | + messages.getMsgHeartbeat("api", 2) |
| 51 | + |
| 52 | + msg = messages.getMsgHeartbeat("api", 3) |
| 53 | + attrs = decode_fix(msg) |
| 54 | + assert attrs["35"] == "0" |
| 55 | + assert attrs["34"] == "3" |
| 56 | + assert attrs["49"] == "api" |
| 57 | + |
| 58 | + |
| 59 | +def test_getMsgLogon(monkeypatch): |
| 60 | + monkeypatch.setattr(messages, "generateJWT", lambda a, n: "TOK") |
| 61 | + msg = messages.getMsgLogon("api") |
| 62 | + attrs = decode_fix(msg) |
| 63 | + assert attrs["35"] == "A" |
| 64 | + assert attrs["49"] == "api" |
| 65 | + assert attrs["554"] == "TOK" |
| 66 | + |
| 67 | + |
| 68 | +def test_translateFix_cases(): |
| 69 | + assert messages.translateFix("35", "D") == "New Order - Single" |
| 70 | + assert messages.translateFix("39", "4") == "Cancelled" |
| 71 | + assert "UNKNOWN Fix key" in messages.translateFix("35", "Z") |
| 72 | + assert "UNKNOWN Fix field" in messages.translateFix("99", "1") |
0 commit comments