def test_query(monkeypatch): _was_called = False def mock_post_object(url, body, **kwargs): nonlocal _was_called assert url == "/livequery/v1/orgs/Z100/runs" assert body["sql"] == "select * from whatever;" _was_called = True return MockResponse({ "org_key": "Z100", "name": "FoobieBletch", "id": "abcdefg" }) 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("select * from whatever;") assert isinstance(query, RunQuery) run = query.submit() assert _was_called assert run.org_key == "Z100" assert run.name == "FoobieBletch" assert run.id == "abcdefg"
def test_query_policy_ids_broken(): api = CbLiveQueryAPI(url="https://example.com", token="ABCD/1234", org_key="Z100", ssl_verify=True) query = api.query("select * from whatever;") with pytest.raises(ApiError): query = query.policy_ids(["Bogus"])
def test_query(monkeypatch): _was_called = False def _run_query(url, body, **kwargs): nonlocal _was_called assert url == "/livequery/v1/orgs/Z100/runs" assert body == {"sql": "select * from whatever;", "device_filter": {}} _was_called = True return StubResponse({"org_key": "Z100", "name": "FoobieBletch", "id": "abcdefg"}) 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("select * from whatever;") assert isinstance(query, RunQuery) run = query.submit() assert _was_called assert run.org_key == "Z100" assert run.name == "FoobieBletch" assert run.id == "abcdefg"
def test_query_with_everything(monkeypatch): _was_called = False def _run_query(url, body, **kwargs): nonlocal _was_called assert url == "/livequery/v1/orgs/Z100/runs" assert body == {"sql": "select * from whatever;", "name": "AmyWasHere", "notify_on_finish": True, "device_filter": {"device_ids": [1, 2, 3], "device_types": ["Alpha", "Bravo", "Charlie"], "policy_ids": [16, 27, 38]}} _was_called = True return StubResponse({"org_key": "Z100", "name": "FoobieBletch", "id": "abcdefg"}) 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("select * from whatever;").device_ids([1, 2, 3]).device_types(["Alpha", "Bravo", "Charlie"]) \ .policy_ids([16, 27, 38]).name("AmyWasHere").notify_on_finish() assert isinstance(query, RunQuery) run = query.submit() assert _was_called assert run.org_key == "Z100" assert run.name == "FoobieBletch" assert run.id == "abcdefg"
def test_query_with_everything(monkeypatch): _was_called = False def mock_post_object(url, body, **kwargs): nonlocal _was_called assert url == "/livequery/v1/orgs/Z100/runs" assert body["sql"] == "select * from whatever;" assert body["name"] == "AmyWasHere" assert body["notify_on_finish"] == True df = body["device_filter"] assert df["device_ids"] == [1, 2, 3] assert df["device_types"] == ["Alpha", "Bravo", "Charlie"] assert df["policy_ids"] == [16, 27, 38] _was_called = True return MockResponse({ "org_key": "Z100", "name": "FoobieBletch", "id": "abcdefg" }) 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("select * from whatever;").device_ids([1, 2, 3]) query = query.device_types(["Alpha", "Bravo", "Charlie"]) query = query.policy_ids([16, 27, 38]) query = query.name("AmyWasHere").notify_on_finish() assert isinstance(query, RunQuery) run = query.submit() assert _was_called assert run.org_key == "Z100" assert run.name == "FoobieBletch" assert run.id == "abcdefg"