def test_validate_asset_hierarchy_duplicate_ref_ids(self): assets = [ Asset(external_id="1"), Asset(parent_external_id="1", external_id="1") ] with pytest.raises(AssertionError, match="Duplicate"): _AssetPoster(assets, ASSETS_API)
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_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_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_validate_asset_hierarchy_asset_has_parent_id_and_parent_ref_id( self): assets = [ Asset(external_id="1"), Asset(parent_external_id="1", parent_id=1, external_id="2") ] with pytest.raises(AssertionError, match="has both"): _AssetPoster(assets, ASSETS_API)
def test_run(self, mock_assets_response): q_req = queue.Queue() q_res = queue.Queue() w = _AssetPosterWorker(request_queue=q_req, response_queue=q_res, client=ASSETS_API) w.start() q_req.put([Asset()]) time.sleep(0.1) w.stop = True assert [Asset._load(mock_assets_response.calls[0].response.json()["items"][0])] == q_res.get() assert 1 == len(mock_assets_response.calls)
def generate_asset_tree(root_external_id: str, depth: int, children_per_node: int, current_depth=1): assert 1 <= children_per_node <= 10, "children_per_node must be between 1 and 10" assets = [] if current_depth == 1: assets = [Asset(external_id=root_external_id)] if depth > current_depth: for i in range(children_per_node): asset = Asset(parent_external_id=root_external_id, external_id="{}{}".format(root_external_id, i)) assets.append(asset) if depth > current_depth + 1: assets.extend( generate_asset_tree(root_external_id + str(i), depth, children_per_node, current_depth + 1) ) return assets
def test_initialize(self): assets = [ Asset(external_id="1"), Asset(external_id="3", parent_external_id="1"), Asset(external_id="2", parent_external_id="1"), Asset(external_id="4", parent_external_id="2"), ] ap = _AssetPoster(assets, ASSETS_API) assert OrderedDict({str(i): None for i in range(1, 5)}) == ap.remaining_external_ids assert { "1": { Asset(external_id="2", parent_external_id="1"), Asset(external_id="3", parent_external_id="1") }, "2": {Asset(external_id="4", parent_external_id="2")}, "3": set(), "4": set(), } == ap.external_id_to_children assert { "1": 3, "2": 1, "3": 0, "4": 0 } == ap.external_id_to_descendent_count assert ap.assets_remaining() is True assert 0 == len(ap.posted_assets) assert ap.request_queue.empty() assert ap.response_queue.empty() assert {"1", "2", "3", "4"} == ap.remaining_external_ids_set
def test_post_with_failures(self, mock_post_assets_failures): assets = [ Asset(name="200", external_id="0"), Asset(name="200", parent_external_id="0", external_id="01"), Asset(name="400", parent_external_id="0", external_id="02"), Asset(name="200", parent_external_id="02", external_id="021"), Asset(name="200", parent_external_id="021", external_id="0211"), Asset(name="500", parent_external_id="0", external_id="03"), Asset(name="200", parent_external_id="03", external_id="031"), ] with pytest.raises(CogniteAPIError) as e: ASSETS_API.create_hierarchy(assets) assert {a.external_id for a in e.value.unknown} == {"03"} assert {a.external_id for a in e.value.failed} == {"02", "021", "0211", "031"} assert {a.external_id for a in e.value.successful} == {"0", "01"}
def test_get_unblocked_assets_parent_ref_null_pointer(self): assets = [Asset(parent_external_id="1", external_id="2")] ap = _AssetPoster(assets, ASSETS_API) asset_list = ap._get_unblocked_assets() assert len(asset_list) == 1
def test_update_with_resource_class(self, mock_assets_response): res = ASSETS_API.update(Asset(id=1)) assert isinstance(res, Asset) assert mock_assets_response.calls[0].response.json( )["items"][0] == res.dump(camel_case=True)
def test_create_multiple(self, mock_assets_response): res = ASSETS_API.create([Asset(external_id="1", name="blabla")]) assert isinstance(res, AssetList) assert mock_assets_response.calls[0].response.json( )["items"] == res.dump(camel_case=True)
def test_ignore_labels_resource_class(self, mock_assets_response): ASSETS_API.update(Asset(id=1, labels=[{"external_id": "PUMP"}], name="Abc")) request_body = json.loads(mock_assets_response.calls[0].request.body)["items"][0]["update"] expected = {"name": {"set": "Abc"}} assert request_body == expected