def test_parent_external_id_list(self, new_asset_hierarchy): prefix, ext_ids = new_asset_hierarchy with set_request_limit(COGNITE_CLIENT.assets_playground, 10): res = COGNITE_CLIENT.assets_playground.list( limit=20, parent_external_ids=[ext_ids[0]]) assert 20 == len(res)
def test_validate_asset_hierarchy__more_than_limit_only_resolved_assets( self): with set_request_limit(ASSETS_API, 1): _AssetPoster([ Asset(external_id="a1", parent_id=1), Asset(external_id="a2", parent_id=2) ], ASSETS_API)
def test_over_limit_concurrent(self, rsps): rsps.add(rsps.POST, BASE_URL + URL_PATH + "/delete", status=200, json={}) rsps.add(rsps.POST, BASE_URL + URL_PATH + "/delete", status=200, json={}) with set_request_limit(API_CLIENT_WITH_API_KEY, 2): API_CLIENT_WITH_API_KEY._delete_multiple(resource_path=URL_PATH, ids=[1, 2, 3, 4], wrap_ids=False) assert {"items": [1, 2]} == jsgz_load(rsps.calls[0].request.body) assert {"items": [3, 4]} == jsgz_load(rsps.calls[1].request.body)
def test_validate_asset_hierarchy_self_dependency(self): assets = [ Asset(external_id="1"), Asset(external_id="2", parent_external_id="2") ] with set_request_limit(ASSETS_API, 1): with pytest.raises(AssertionError, match="circular dependencies"): _AssetPoster(assets, ASSETS_API)
def test_post_assets_over_limit_only_resolved(self, mock_post_asset_hierarchy): with set_request_limit(ASSETS_API, 1): _AssetPoster([ Asset(external_id="a1", parent_id=1), Asset(external_id="a2", parent_id=2) ], ASSETS_API).post() assert 2 == len(mock_post_asset_hierarchy.calls)
def test_insert_pandas_dataframe(self, new_ts, post_spy): start = datetime(2018, 1, 1) x = pandas.DatetimeIndex( [start + timedelta(days=d) for d in range(100)]) y = numpy.random.normal(0, 1, 100) df = pandas.DataFrame({new_ts.id: y}, index=x) with set_request_limit(COGNITE_CLIENT.datapoints, 50): COGNITE_CLIENT.datapoints.insert_dataframe(df) assert 2 == COGNITE_CLIENT.datapoints._post.call_count
def test_get_unblocked_assets__assets_unblocked_by_default_more_than_limit(self): assets = [] for i in range(4): assets.extend(generate_asset_tree(root_external_id=str(i), depth=2, children_per_node=2)) with set_request_limit(ASSETS_API, 3): ap = _AssetPoster(assets=assets, client=ASSETS_API) unblocked_assets_lists = ap._get_unblocked_assets() assert 4 == len(unblocked_assets_lists) for li in unblocked_assets_lists: assert 3 == len(li)
def test_download_ids_over_limit(self, mock_file_download_response): with set_request_limit(FILES_API, 1): with TemporaryDirectory() as dir: res = FILES_API.download(directory=dir, id=[1], external_id=["2"]) bodies = [jsgz_load(mock_file_download_response.calls[i].request.body) for i in range(2)] assert {"items": [{"id": 1}]} in bodies assert {"items": [{"externalId": "2"}]} in bodies assert res is None assert os.path.isfile(os.path.join(dir, "file1")) assert os.path.isfile(os.path.join(dir, "file2"))
def test_over_limit_concurrent(self, rsps): rsps.add(rsps.POST, BASE_URL + URL_PATH + "/update", status=200, json={"items": [{"x": 1, "y": 2}]}) rsps.add(rsps.POST, BASE_URL + URL_PATH + "/update", status=200, json={"items": [{"x": 3, "y": 4}]}) with set_request_limit(API_CLIENT_WITH_API_KEY, 1): API_CLIENT_WITH_API_KEY._update_multiple( cls=SomeResourceList, resource_path=URL_PATH, items=[SomeResource(1, 2, id=1), SomeResource(3, 4, id=2)] ) assert {"items": [{"id": 1, "update": {"y": {"set": 2}}}]} == jsgz_load(rsps.calls[0].request.body) assert {"items": [{"id": 2, "update": {"y": {"set": 4}}}]} == jsgz_load(rsps.calls[1].request.body)
def test_over_limit_concurrent(self, rsps): rsps.add(rsps.POST, BASE_URL + URL_PATH + "/byids", status=200, json={"items": [{"x": 1, "y": 2}]}) rsps.add(rsps.POST, BASE_URL + URL_PATH + "/byids", status=200, json={"items": [{"x": 3, "y": 4}]}) with set_request_limit(API_CLIENT_WITH_API_KEY, 1): API_CLIENT_WITH_API_KEY._retrieve_multiple( cls=SomeResourceList, resource_path=URL_PATH, ids=[1, 2], wrap_ids=False ) assert {"items": [1]} == jsgz_load(rsps.calls[0].request.body) assert {"items": [2]} == jsgz_load(rsps.calls[1].request.body)
def test_post_hierarchy(self, limit, depth, children_per_node, expected_num_calls, mock_post_asset_hierarchy): assets = generate_asset_tree(root_external_id="0", depth=depth, children_per_node=children_per_node) with set_request_limit(ASSETS_API, limit): created_assets = ASSETS_API.create_hierarchy(assets) assert len(assets) == len(created_assets) assert expected_num_calls - 1 <= len(mock_post_asset_hierarchy.calls) <= expected_num_calls + 1 for asset in created_assets: if asset.id == "0id": assert asset.parent_id is None else: assert asset.id[:-3] == asset.parent_id[:-2]
def new_asset_hierarchy(post_spy): random_prefix = "test_{}_".format(utils._auxiliary.random_string(10)) assets = generate_asset_tree(random_prefix + "0", depth=5, children_per_node=5) with set_request_limit(COGNITE_CLIENT.assets, 50): COGNITE_CLIENT.assets.create_hierarchy(assets) assert 20 < COGNITE_CLIENT.assets._post.call_count < 30 ext_ids = [a.external_id for a in assets] yield random_prefix, ext_ids COGNITE_CLIENT.assets.delete(external_id=random_prefix + "0", recursive=True)
def mock_post_assets_failures(self, rsps): def request_callback(request): items = jsgz_load(request.body)["items"] response_assets = [] item = items[0] parent_id = None if "parentId" in item: parent_id = item["parentId"] if "parentExternalId" in item: parent_id = item["parentExternalId"] + "id" id = item.get("refId", "root_") + "id" response_assets.append({ "id": id, "parentId": parent_id, "externalId": item["externalId"], "parentExternalId": item.get("parentExternalId"), }) if item["name"] == "400": return 400, {}, json.dumps( {"error": { "message": "user error", "code": 400 }}) if item["name"] == "500": return 500, {}, json.dumps({ "error": { "message": "internal server error", "code": 500 } }) return 200, {}, json.dumps({"items": response_assets}) rsps.add_callback( rsps.POST, ASSETS_API._get_base_url_with_base_path() + "/assets", callback=request_callback, content_type="application/json", ) with set_request_limit(ASSETS_API, 1): yield rsps
def test_standard_update_fail_missing_and_5xx(self, rsps): rsps.add( rsps.POST, BASE_URL + URL_PATH + "/update", status=400, json={"error": {"message": "Missing ids", "missing": [{"id": 0}]}}, ) rsps.add(rsps.POST, BASE_URL + URL_PATH + "/update", status=500, json={"error": {"message": "Server Error"}}) with set_request_limit(API_CLIENT_WITH_API_KEY, 1): with pytest.raises(CogniteAPIError) as e: API_CLIENT_WITH_API_KEY._update_multiple( cls=SomeResourceList, resource_path=URL_PATH, items=[SomeResource(id=0), SomeResource(external_id="abc")], ) assert ["abc"] == e.value.unknown assert [0] == e.value.failed assert [{"id": 0}] == e.value.missing
def test_multiple_ids_not_found(self, rsps): rsps.add( rsps.POST, BASE_URL + URL_PATH + "/byids", status=400, json={"error": {"message": "Not Found", "missing": [{"id": 1}]}}, ) rsps.add( rsps.POST, BASE_URL + URL_PATH + "/byids", status=400, json={"error": {"message": "Not Found", "missing": [{"id": 2}]}}, ) with set_request_limit(API_CLIENT_WITH_API_KEY, 1): with pytest.raises(CogniteNotFoundError) as e: API_CLIENT_WITH_API_KEY._retrieve_multiple( cls=SomeResourceList, resource_path=URL_PATH, wrap_ids=True, ids=[1, 2] ) assert [{"id": 1}, {"id": 2}] == e.value.not_found
def test_standard_delete_multiple_fail_missing_ids(self, rsps): rsps.add( rsps.POST, BASE_URL + URL_PATH + "/delete", status=400, json={"error": {"message": "Missing ids", "missing": [{"id": 1}]}}, ) rsps.add( rsps.POST, BASE_URL + URL_PATH + "/delete", status=400, json={"error": {"message": "Missing ids", "missing": [{"id": 3}]}}, ) with set_request_limit(API_CLIENT_WITH_API_KEY, 2): with pytest.raises(CogniteNotFoundError) as e: API_CLIENT_WITH_API_KEY._delete_multiple(resource_path=URL_PATH, wrap_ids=False, ids=[1, 2, 3]) assert [{"id": 1}, {"id": 3}] == e.value.not_found assert [1, 2, 3] == e.value.failed
def test_standard_create_fail(self, rsps): def callback(request): item = jsgz_load(request.body)["items"][0] return int(item["externalId"]), {}, json.dumps({}) rsps.add_callback(rsps.POST, BASE_URL + URL_PATH, callback=callback, content_type="application/json") with set_request_limit(API_CLIENT_WITH_API_KEY, 1): with pytest.raises(CogniteAPIError) as e: API_CLIENT_WITH_API_KEY._create_multiple( cls=SomeResourceList, resource_path=URL_PATH, items=[ SomeResource(1, external_id="400"), SomeResource(external_id="500"), SomeResource(1, 1, external_id="200"), ], ) assert 500 == e.value.code assert [SomeResource(1, external_id="400")] == e.value.failed assert [SomeResource(1, 1, external_id="200")] == e.value.successful assert [SomeResource(external_id="500")] == e.value.unknown
def test_list(self, post_spy): with set_request_limit(COGNITE_CLIENT.assets, 10): res = COGNITE_CLIENT.assets.list(limit=20) assert 20 == len(res) assert 2 == COGNITE_CLIENT.assets._post.call_count
def test_list(self, post_spy): with set_request_limit(DS_API, 1): res = DS_API.list(limit=2) assert 2 == len(res) assert 2 == COGNITE_CLIENT.data_sets._post.call_count
def test_list(self, get_spy): with set_request_limit(COGNITE_CLIENT.time_series, 10): res = COGNITE_CLIENT.time_series.list(limit=20) assert 20 == len(res) assert 2 == COGNITE_CLIENT.time_series._get.call_count
def test_call(self, post_spy): with set_request_limit(COGNITE_CLIENT.sequences, 10): res = [s for s in COGNITE_CLIENT.sequences(limit=20)] assert 20 == len(res) assert 2 == COGNITE_CLIENT.sequences._post.call_count
def test_insert(self, new_ts, post_spy): datapoints = [(datetime(year=2018, month=1, day=1, hour=1, minute=i), i) for i in range(60)] with set_request_limit(COGNITE_CLIENT.datapoints, 30): COGNITE_CLIENT.datapoints.insert(datapoints, id=new_ts.id) assert 2 == COGNITE_CLIENT.datapoints._post.call_count