|
| 1 | +import json |
| 2 | + |
| 3 | +import requests |
| 4 | + |
| 5 | +from transcriptic.cli import cli |
| 6 | + |
| 7 | +from ..helpers.fixtures import * |
| 8 | +from ..helpers.mockAPI import MockResponse |
| 9 | + |
| 10 | + |
| 11 | +# Structure of the response object from SCLE |
| 12 | +def bool_success_res(): |
| 13 | + return {"success": True} |
| 14 | + |
| 15 | + |
| 16 | +def mock_api_endpoint(): |
| 17 | + return "foo.bar.baz" |
| 18 | + |
| 19 | + |
| 20 | +@pytest.fixture |
| 21 | +def ap_file(tmpdir_factory): |
| 22 | + """Make a temp autoprotocol file""" |
| 23 | + path = tmpdir_factory.mktemp("foo").join("ap.json") |
| 24 | + with open(str(path), "w") as f: |
| 25 | + f.write("{}") # any valid json works |
| 26 | + return path |
| 27 | + |
| 28 | + |
| 29 | +def test_good_autoprotocol(cli_test_runner, monkeypatch, ap_file): |
| 30 | + def mockpost(*args, **kwargs): |
| 31 | + return MockResponse(0, bool_success_res(), json.dumps(bool_success_res())) |
| 32 | + |
| 33 | + monkeypatch.setattr(requests, "post", mockpost) |
| 34 | + result = cli_test_runner.invoke( |
| 35 | + cli, ["exec", str(ap_file), "-a", mock_api_endpoint()] |
| 36 | + ) |
| 37 | + assert result.exit_code == 0 |
| 38 | + assert ( |
| 39 | + f"Success. View {mock_api_endpoint()} to see the scheduling outcome." |
| 40 | + in result.output |
| 41 | + ) |
| 42 | + |
| 43 | + |
| 44 | +def test_bad_autoprotocol(cli_test_runner): |
| 45 | + result = cli_test_runner.invoke( |
| 46 | + cli, ["exec", "bad-file-handle", "-a", mock_api_endpoint()] |
| 47 | + ) |
| 48 | + assert result.exit_code != 0 |
| 49 | + assert "Invalid value for '[AUTOPROTOCOL]': Could not open file" in result.stderr |
| 50 | + |
| 51 | + |
| 52 | +def test_bad_deviceset(cli_test_runner, ap_file): |
| 53 | + result = cli_test_runner.invoke( |
| 54 | + cli, |
| 55 | + [ |
| 56 | + "exec", |
| 57 | + str(ap_file), |
| 58 | + "--device-set", |
| 59 | + "bad-file-handle", |
| 60 | + "-a", |
| 61 | + mock_api_endpoint(), |
| 62 | + ], |
| 63 | + ) |
| 64 | + assert result.exit_code != 0 |
| 65 | + assert ( |
| 66 | + "Invalid value for '--device-set' / '-d': Could not open file: bad-file-handle:" |
| 67 | + in result.stderr |
| 68 | + ) |
| 69 | + |
| 70 | + |
| 71 | +def test_bad_api_response(cli_test_runner, monkeypatch, ap_file): |
| 72 | + def mockpost(*args, **kwargs): |
| 73 | + return MockResponse(0, "not-json", "not-json") |
| 74 | + |
| 75 | + monkeypatch.setattr(requests, "post", mockpost) |
| 76 | + result = cli_test_runner.invoke( |
| 77 | + cli, ["exec", str(ap_file), "-a", mock_api_endpoint()] |
| 78 | + ) |
| 79 | + assert result.exit_code == 0 |
| 80 | + assert "Error: " in result.stderr |
| 81 | + |
| 82 | + |
| 83 | +def test_good_workcell(cli_test_runner, monkeypatch, ap_file): |
| 84 | + def mockpost(*args, **kwargs): |
| 85 | + return MockResponse(0, bool_success_res(), json.dumps(bool_success_res())) |
| 86 | + |
| 87 | + monkeypatch.setattr(requests, "post", mockpost) |
| 88 | + result = cli_test_runner.invoke( |
| 89 | + cli, ["exec", str(ap_file), "-a", mock_api_endpoint(), "-w", "wc3"] |
| 90 | + ) |
| 91 | + assert result.exit_code == 0 |
| 92 | + assert ( |
| 93 | + f"Success. View {mock_api_endpoint()} to see the scheduling outcome." |
| 94 | + in result.output |
| 95 | + ) |
| 96 | + |
| 97 | + |
| 98 | +def test_bad_workcell(cli_test_runner, ap_file): |
| 99 | + result = cli_test_runner.invoke( |
| 100 | + cli, ["exec", str(ap_file), "-a", mock_api_endpoint(), "-w", "bad-workcell-id"] |
| 101 | + ) |
| 102 | + assert result.exit_code != 0 |
| 103 | + assert "Workcell id must be like wcN but was bad-workcell-id" in result.stderr |
0 commit comments