def test_query_history(monkeypatch): _was_called = False def _run_query(url, body, **kwargs): nonlocal _was_called assert url == "/livequery/v1/orgs/Z100/runs/_search" assert body == {"query": "xyzzy", "start": 0} _was_called = True return StubResponse({"org_key": "Z100", "num_found": 3, "results": [{"org_key": "Z100", "name": "FoobieBletch", "id": "abcdefg"}, {"org_key": "Z100", "name": "Aoxomoxoa", "id": "cdefghi"}, {"org_key": "Z100", "name": "Read_Me", "id": "efghijk"}]}) api = CbLiveQueryAPI(url="https://example.com", token="ABCD/1234", org_key="Z100", ssl_verify=True) patch_cbapi(monkeypatch, api, POST=_run_query) query = api.query_history("xyzzy") assert isinstance(query, RunHistoryQuery) count = 0 for item in query.all(): assert item.org_key == "Z100" if item.id == "abcdefg": assert item.name == "FoobieBletch" elif item.id == "cdefghi": assert item.name == "Aoxomoxoa" elif item.id == "efghijk": assert item.name == "Read_Me" else: pytest.fail("Unknown item ID: %s" % item.id) count = count + 1 assert _was_called assert count == 3
def test_query_history_with_everything(monkeypatch): _was_called = False def mock_post_object(url, body, **kwargs): nonlocal _was_called assert url == "/livequery/v1/orgs/Z100/runs/_search" assert body["query"] == "xyzzy" t = body["sort"][0] assert t["field"] == "id" assert t["order"] == "ASC" _was_called = True run1 = {"org_key": "Z100", "name": "FoobieBletch", "id": "abcdefg"} run2 = {"org_key": "Z100", "name": "Aoxomoxoa", "id": "cdefghi"} run3 = {"org_key": "Z100", "name": "Read_Me", "id": "efghijk"} return MockResponse({ "org_key": "Z100", "num_found": 3, "results": [run1, run2, run3] }) api = CbLiveQueryAPI(url="https://example.com", token="ABCD/1234", org_key="Z100", ssl_verify=True) monkeypatch.setattr(api, "get_object", ConnectionMocks.get("GET")) monkeypatch.setattr(api, "post_object", mock_post_object) monkeypatch.setattr(api, "put_object", ConnectionMocks.get("PUT")) monkeypatch.setattr(api, "delete_object", ConnectionMocks.get("DELETE")) query = api.query_history("xyzzy").sort_by("id") assert isinstance(query, RunHistoryQuery) count = 0 for item in query.all(): assert item.org_key == "Z100" if item.id == "abcdefg": assert item.name == "FoobieBletch" elif item.id == "cdefghi": assert item.name == "Aoxomoxoa" elif item.id == "efghijk": assert item.name == "Read_Me" else: pytest.fail("Unknown item ID: %s" % item.id) count = count + 1 assert _was_called assert count == 3