def test_result_fields(cbcsdk_mock): """Testing Result.fields_ property.""" api = cbcsdk_mock.api result = Result(api, initial_data=GET_RUN_RESULTS_RESP) fields = result.fields_ assert fields._info == {"status", "device.id"} metrics = result.metrics_ assert isinstance(metrics, Result.Metrics) assert metrics._info == {}
def test_result_device(cbcsdk_mock): """Testing Result.device_ property.""" api = cbcsdk_mock.api result = Result(api, initial_data=GET_RUN_RESULTS_RESP) device = result.device_ assert isinstance(device, Result.Device) assert device._info == GET_RUN_RESULTS_RESP["device"] assert device.id == 1234567 assert device.name == "WIN-A1B2C3D4"
def test_result_fields_with_metrics(cbcsdk_mock): """Testing Result.metrics_ property with Device Summary results.""" api = cbcsdk_mock.api result = Result(api, initial_data=GET_RUN_RESULTS_RESP_1) metrics = result.metrics_ assert metrics._info == {"cpu": 24.3, "memory": 8.0}
def test_result_query_implementation(cbcsdk_mock): """Testing Run._query_implementation().""" api = cbcsdk_mock.api result = Result(api, initial_data=GET_RUN_RESULTS_RESP) assert isinstance(result._query_implementation(api), ResultQuery)
def test_result_query_device_summary_facets(monkeypatch): """Test a facet query on device summary.""" _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 = CBCloudAPI(url="https://example.com", token="ABCD/1234", org_key="Z100", ssl_verify=True) patch_cbc_sdk_api(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"]).set_device_names(["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): """Test result of a device summary query.""" _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, "name": "device1" }, "metrics": [{ "key": "aaa", "value": 0.0 }, { "key": "bbb", "value": 0.0 }] }, { "id": "mnopqrs", "total_results": 3, "device": { "id": 271828, "name": "device2" }, "metrics": [{ "key": "aaa", "value": 0.0 }, { "key": "bbb", "value": 0.0 }] }] }) api = CBCloudAPI(url="https://example.com", token="ABCD/1234", org_key="Z100", ssl_verify=True) patch_cbc_sdk_api(monkeypatch, api, POST=_run_summaries) result = Result(api, { "id": "abcdefg", "device": { "id": "abcdefg" }, "fields": {}, "metrics": {} }) query = result.query_device_summaries().where("foo").set_device_names( ["AxCx", "A7X"]).sort_by("device_name") assert isinstance(query, ResultQuery) count = 0 for item in query.all(): if item.id == 314159: # assert item.total_results == 2 assert item.device_id == 314159 assert item.name == "device1" elif item.id == 271828: # assert item.total_results == 3 assert item.device_id == 271828 assert item.name == "device2" else: pytest.fail("Invalid object with ID %s seen" % item.id) count = count + 1 assert _was_called assert count == 2