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(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_run_delete(monkeypatch): _was_called = False def _execute_delete(url): nonlocal _was_called assert url == "/livequery/v1/orgs/Z100/runs/abcdefg" if _was_called: pytest.fail("_execute_delete should not be called twice!") _was_called = True return StubResponse(None) api = CbLiveQueryAPI(url="https://example.com", token="ABCD/1234", org_key="Z100", ssl_verify=True) patch_cbapi(monkeypatch, api, DELETE=_execute_delete) run = Run( api, "abcdefg", { "org_key": "Z100", "name": "FoobieBletch", "id": "abcdefg", "status": "ACTIVE" }) rc = run.delete() assert _was_called assert rc assert run._is_deleted # Now ensure that certain operations that don't make sense on a deleted object raise ApiError with pytest.raises(ApiError): run.refresh() with pytest.raises(ApiError): run.stop() # And make sure that deleting a deleted object returns True immediately rc = run.delete() assert rc
def test_run_delete_failed(monkeypatch): _was_called = False def _execute_delete(url): nonlocal _was_called assert url == "/livequery/v1/orgs/Z100/runs/abcdefg" _was_called = True return StubResponse(None, 403) api = CbLiveQueryAPI(url="https://example.com", token="ABCD/1234", org_key="Z100", ssl_verify=True) patch_cbapi(monkeypatch, api, DELETE=_execute_delete) run = Run( api, "abcdefg", { "org_key": "Z100", "name": "FoobieBletch", "id": "abcdefg", "status": "ACTIVE" }) rc = run.delete() assert _was_called assert not rc assert not run._is_deleted
def test_run_delete(monkeypatch): _was_called = False def mock_delete_object(url): nonlocal _was_called if _was_called: pytest.fail("delete should not be called twice!") assert url == "/livequery/v1/orgs/Z100/runs/abcdefg" _was_called = True return MockResponse(None) api = CbLiveQueryAPI(url="https://example.com", token="ABCD/1234", org_key="Z100", ssl_verify=True) run = Run(api, "abcdefg", {"org_key":"Z100", "name":"FoobieBletch", "id":"abcdefg", "status":"ACTIVE"}) monkeypatch.setattr(api, "get_object", ConnectionMocks.get("GET")) monkeypatch.setattr(api, "post_object", ConnectionMocks.get("POST")) monkeypatch.setattr(api, "put_object", ConnectionMocks.get("PUT")) monkeypatch.setattr(api, "delete_object", mock_delete_object) rc = run.delete() assert _was_called assert rc == True assert run._is_deleted # Now ensure that certain operations that don't make sense on a deleted object raise ApiError with pytest.raises(ApiError): run.refresh() with pytest.raises(ApiError): run.stop() # And make sure that deleting a deleted object returns True immediately rc = run.delete() assert rc == True
def test_simple_get(monkeypatch): _was_called = False def _get_run(url, parms=None, default=None): nonlocal _was_called assert url == "/livequery/v1/orgs/Z100/runs/abcdefg" _was_called = True return {"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, GET=_get_run) run = api.select(Run, "abcdefg") assert _was_called assert run.org_key == "Z100" assert run.name == "FoobieBletch" assert run.id == "abcdefg"
def test_run_stop(monkeypatch): _was_called = False def mock_put_object(url, body, **kwargs): nonlocal _was_called assert url == "/livequery/v1/orgs/Z100/runs/abcdefg/status" assert body["status"] == "CANCELLED" _was_called = True return MockResponse({"org_key":"Z100", "name":"FoobieBletch", "id":"abcdefg", "status":"CANCELLED"}) api = CbLiveQueryAPI(url="https://example.com", token="ABCD/1234", org_key="Z100", ssl_verify=True) run = Run(api, "abcdefg", {"org_key":"Z100", "name":"FoobieBletch", "id":"abcdefg", "status":"ACTIVE"}) monkeypatch.setattr(api, "get_object", ConnectionMocks.get("GET")) monkeypatch.setattr(api, "post_object", ConnectionMocks.get("POST")) monkeypatch.setattr(api, "put_object", mock_put_object) monkeypatch.setattr(api, "delete_object", ConnectionMocks.get("DELETE")) rc = run.stop() assert _was_called assert rc == True assert run.org_key == "Z100" assert run.name == "FoobieBletch" assert run.id == "abcdefg" assert run.status == "CANCELLED"
def test_run_refresh(monkeypatch): _was_called = False def mock_get_object(url, parms=None, default=None): nonlocal _was_called assert url == "/livequery/v1/orgs/Z100/runs/abcdefg" assert parms is None assert default is None _was_called = True return {"org_key":"Z100", "name":"FoobieBletch", "id":"abcdefg", "status":"COMPLETE"} api = CbLiveQueryAPI(url="https://example.com", token="ABCD/1234", org_key="Z100", ssl_verify=True) run = Run(api, "abcdefg", {"org_key":"Z100", "name":"FoobieBletch", "id":"abcdefg", "status":"ACTIVE"}) monkeypatch.setattr(api, "get_object", mock_get_object) monkeypatch.setattr(api, "post_object", ConnectionMocks.get("POST")) monkeypatch.setattr(api, "put_object", ConnectionMocks.get("PUT")) monkeypatch.setattr(api, "delete_object", ConnectionMocks.get("DELETE")) rc = run.refresh() assert _was_called assert rc == True assert run.org_key == "Z100" assert run.name == "FoobieBletch" assert run.id == "abcdefg" assert run.status == "COMPLETE"
def test_run_refresh(monkeypatch): _was_called = False def _get_run(url, parms=None, default=None): nonlocal _was_called assert url == "/livequery/v1/orgs/Z100/runs/abcdefg" _was_called = True return { "org_key": "Z100", "name": "FoobieBletch", "id": "abcdefg", "status": "COMPLETE" } api = CbLiveQueryAPI(url="https://example.com", token="ABCD/1234", org_key="Z100", ssl_verify=True) patch_cbapi(monkeypatch, api, GET=_get_run) run = Run( api, "abcdefg", { "org_key": "Z100", "name": "FoobieBletch", "id": "abcdefg", "status": "ACTIVE" }) rc = run.refresh() assert _was_called assert rc assert run.org_key == "Z100" assert run.name == "FoobieBletch" assert run.id == "abcdefg" assert run.status == "COMPLETE"
def test_run_stop_failed(monkeypatch): _was_called = False def _execute_stop(url, body, **kwargs): nonlocal _was_called assert url == "/livequery/v1/orgs/Z100/runs/abcdefg/status" assert body == {"status": "CANCELLED"} _was_called = True return StubResponse( {"error_message": "The query is not presently running."}, 409) api = CbLiveQueryAPI(url="https://example.com", token="ABCD/1234", org_key="Z100", ssl_verify=True) patch_cbapi(monkeypatch, api, PUT=_execute_stop) run = Run( api, "abcdefg", { "org_key": "Z100", "name": "FoobieBletch", "id": "abcdefg", "status": "CANCELLED" }) rc = run.stop() assert _was_called assert not rc
def test_run_stop(monkeypatch): _was_called = False def _execute_stop(url, body, **kwargs): nonlocal _was_called assert url == "/livequery/v1/orgs/Z100/runs/abcdefg/status" assert body == {"status": "CANCELLED"} _was_called = True return StubResponse({ "org_key": "Z100", "name": "FoobieBletch", "id": "abcdefg", "status": "CANCELLED" }) api = CbLiveQueryAPI(url="https://example.com", token="ABCD/1234", org_key="Z100", ssl_verify=True) patch_cbapi(monkeypatch, api, PUT=_execute_stop) run = Run( api, "abcdefg", { "org_key": "Z100", "name": "FoobieBletch", "id": "abcdefg", "status": "ACTIVE" }) rc = run.stop() assert _was_called assert rc assert run.org_key == "Z100" assert run.name == "FoobieBletch" assert run.id == "abcdefg" assert run.status == "CANCELLED"
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
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_result_query_device_summary_facets(monkeypatch): _was_called = False def mock_post_object(url, body, **kwargs): nonlocal _was_called assert url == "/livequery/v1/orgs/Z100/runs/abcdefg/results/device_summaries/_facet" assert body["query"] == "xyzzy" t = body["criteria"] assert t["device_name"] == ["AxCx", "A7X"] t = body["terms"] assert t["fields"] == ["alpha", "bravo", "charlie"] _was_called = True v1 = {"total":1, "id":"alpha1", "name":"alpha1"} v2 = {"total":2, "id":"alpha2", "name":"alpha2"} term1 = {"field":"alpha", "values":[v1, v2]} v1 = {"total":1, "id":"bravo1", "name":"bravo1"} v2 = {"total":2, "id":"bravo2", "name":"bravo2"} term2 = {"field":"bravo", "values":[v1, v2]} v1 = {"total":1, "id":"charlie1", "name":"charlie1"} v2 = {"total":2, "id":"charlie2", "name":"charlie2"} term3 = {"field":"charlie", "values":[v1, v2]} return MockResponse({"terms":[term1, term2, term3]}) api = CbLiveQueryAPI(url="https://example.com", token="ABCD/1234", org_key="Z100", ssl_verify=True) tmp_id = {"id":"abcdefg"} result = Result(api, {"id": "abcdefg", "device":tmp_id, "fields":{}, "metrics":{}}) 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 = result.query_device_summary_facets().where("xyzzy") query = query.facet_field("alpha").facet_field(["bravo", "charlie"]) query = query.criteria(device_name=["AxCx", "A7X"]) assert isinstance(query, FacetQuery) count = 0 for item in query.all(): vals = item.values if item.field == "alpha": assert vals[0]["id"] == "alpha1" assert vals[1]["id"] == "alpha2" elif item.field == "bravo": assert vals[0]["id"] == "bravo1" assert vals[1]["id"] == "bravo2" elif item.field == "charlie": assert vals[0]["id"] == "charlie1" assert vals[1]["id"] == "charlie2" else: pytest.fail("Unknown field name %s seen" % item.field) count = count + 1 assert _was_called assert count == 3
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"
def test_result_device_summaries(monkeypatch): _was_called = False def mock_post_object(url, body, **kwargs): nonlocal _was_called assert url == "/livequery/v1/orgs/Z100/runs/abcdefg/results/device_summaries/_search" assert body["query"] == "foo" t = body["criteria"] assert t["device_name"] == ["AxCx", "A7X"] t = body["sort"][0] assert t["field"] == "device_name" assert t["order"] == "ASC" _was_called = True metrics = [{"key":"aaa", "value":0.0}, {"key":"bbb", "value":0.0}] res1 = {"id":"ghijklm", "total_results":2, "device_id":314159, "metrics":metrics} res2 = {"id":"mnopqrs", "total_results":3, "device_id":271828, "metrics":metrics} return MockResponse({"org_key":"Z100", "num_found":2, "results":[res1, res2]}) api = CbLiveQueryAPI(url="https://example.com", token="ABCD/1234", org_key="Z100", ssl_verify=True) tmp_id = {"id":"abcdefg"} result = Result(api, {"id": "abcdefg", "device":tmp_id, "fields":{}, "metrics":{}}) 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 = result.query_device_summaries().where("foo").criteria(device_name=["AxCx", "A7X"]) query = query.sort_by("device_name") assert isinstance(query, ResultQuery) count = 0 for item in query.all(): if item.id == "ghijklm": assert item.total_results == 2 assert item.device_id == 314159 elif item.id == "mnopqrs": assert item.total_results == 3 assert item.device_id == 271828 else: pytest.fail("Invalid object with ID %s seen" % item.id) count = count + 1 assert _was_called assert count == 2
def test_run_delete_failed(monkeypatch): _was_called = False def mock_delete_object(url): nonlocal _was_called assert url == "/livequery/v1/orgs/Z100/runs/abcdefg" _was_called = True return MockResponse(None, 403) api = CbLiveQueryAPI(url="https://example.com", token="ABCD/1234", org_key="Z100", ssl_verify=True) run = Run(api, "abcdefg", {"org_key":"Z100", "name":"FoobieBletch", "id":"abcdefg", "status":"ACTIVE"}) monkeypatch.setattr(api, "get_object", ConnectionMocks.get("GET")) monkeypatch.setattr(api, "post_object", ConnectionMocks.get("POST")) monkeypatch.setattr(api, "put_object", ConnectionMocks.get("PUT")) monkeypatch.setattr(api, "delete_object", mock_delete_object) rc = run.delete() assert _was_called assert rc == False assert not run._is_deleted
def test_no_org_key(): with pytest.raises(CredentialError): CbLiveQueryAPI(url="https://example.com", token="ABCD/1234", ssl_verify=True) # note: no org_key
def test_result_query_device_summary_facets(monkeypatch): _was_called = False def _run_facets(url, body, **kwargs): nonlocal _was_called assert url == "/livequery/v1/orgs/Z100/runs/abcdefg/results/device_summaries/_facet" assert body == { "query": "xyzzy", "criteria": { "device_name": ["AxCx", "A7X"] }, "terms": { "fields": ["alpha", "bravo", "charlie"] } } _was_called = True return StubResponse({ "terms": [{ "field": "alpha", "values": [{ "total": 1, "id": "alpha1", "name": "alpha1" }, { "total": 2, "id": "alpha2", "name": "alpha2" }] }, { "field": "bravo", "values": [{ "total": 1, "id": "bravo1", "name": "bravo1" }, { "total": 2, "id": "bravo2", "name": "bravo2" }] }, { "field": "charlie", "values": [{ "total": 1, "id": "charlie1", "name": "charlie1" }, { "total": 2, "id": "charlie2", "name": "charlie2" }] }] }) api = CbLiveQueryAPI(url="https://example.com", token="ABCD/1234", org_key="Z100", ssl_verify=True) patch_cbapi(monkeypatch, api, POST=_run_facets) result = Result(api, { "id": "abcdefg", "device": { "id": "abcdefg" }, "fields": {}, "metrics": {} }) query = result.query_device_summary_facets().where("xyzzy").facet_field("alpha") \ .facet_field(["bravo", "charlie"]).criteria(device_name=["AxCx", "A7X"]) assert isinstance(query, FacetQuery) count = 0 for item in query.all(): vals = item.values if item.field == "alpha": assert vals[0]["id"] == "alpha1" assert vals[1]["id"] == "alpha2" elif item.field == "bravo": assert vals[0]["id"] == "bravo1" assert vals[1]["id"] == "bravo2" elif item.field == "charlie": assert vals[0]["id"] == "charlie1" assert vals[1]["id"] == "charlie2" else: pytest.fail("Unknown field name %s seen" % item.field) count = count + 1 assert _was_called assert count == 3
def test_result_device_summaries(monkeypatch): _was_called = False def _run_summaries(url, body, **kwargs): nonlocal _was_called assert url == "/livequery/v1/orgs/Z100/runs/abcdefg/results/device_summaries/_search" assert body == { "query": "foo", "criteria": { "device_name": ["AxCx", "A7X"] }, "sort": [{ "field": "device_name", "order": "ASC" }], "start": 0 } _was_called = True return StubResponse({ "org_key": "Z100", "num_found": 2, "results": [{ "id": "ghijklm", "total_results": 2, "device_id": 314159, "metrics": [{ "key": "aaa", "value": 0.0 }, { "key": "bbb", "value": 0.0 }] }, { "id": "mnopqrs", "total_results": 3, "device_id": 271828, "metrics": [{ "key": "aaa", "value": 0.0 }, { "key": "bbb", "value": 0.0 }] }] }) api = CbLiveQueryAPI(url="https://example.com", token="ABCD/1234", org_key="Z100", ssl_verify=True) patch_cbapi(monkeypatch, api, POST=_run_summaries) result = Result(api, { "id": "abcdefg", "device": { "id": "abcdefg" }, "fields": {}, "metrics": {} }) query = result.query_device_summaries().where("foo").criteria( device_name=["AxCx", "A7X"]).sort_by("device_name") assert isinstance(query, ResultQuery) count = 0 for item in query.all(): if item.id == "ghijklm": assert item.total_results == 2 assert item.device_id == 314159 elif item.id == "mnopqrs": assert item.total_results == 3 assert item.device_id == 271828 else: pytest.fail("Invalid object with ID %s seen" % item.id) count = count + 1 assert _was_called assert count == 2