Exemplo n.º 1
0
def test_unlink_subtree_parents():
    assets = [
        Asset(id=1),
        Asset(id=2, parent_id=1),
        Asset(external_id="ext_1"),
        Asset(external_id="ext_2", parent_external_id="ext_1", metadata={}),
    ]
    original_parent_ids = [asset.parent_id for asset in assets]
    original_parent_external_ids = [
        asset.parent_external_id for asset in assets
    ]
    subtree_root_ids = [2]
    subtree_root_external_ids = ["ext_2"]
    unlink_subtree_parents(assets,
                           subtree_ids=subtree_root_ids,
                           subtree_external_ids=subtree_root_external_ids)
    for i, asset in enumerate(assets):
        if asset.id in subtree_root_ids or asset.external_id in subtree_root_external_ids:
            assert asset.parent_id is None
            assert asset.parent_external_id is None
            assert asset.metadata.get(
                "_replicatedOriginalParentId") == original_parent_ids[i]
            assert asset.metadata.get("_replicatedOriginalParentExternalId"
                                      ) == original_parent_external_ids[i]
        else:
            assert asset.parent_id == original_parent_ids[i]
            assert asset.parent_external_id == original_parent_external_ids[i]
Exemplo n.º 2
0
def test_create_hierarchy_without_dst_list():
    with monkeypatch_cognite_client() as client:
        runtime = time.time() * 1000
        assets_src = [
            Asset(
                id=3, name="Queen", external_id="Queen in the Kingdom", metadata={}, description="Married to the King"
            ),
            Asset(id=7, name="Prince", parent_id=3, external_id="Future King", metadata={}),
            Asset(id=5, name="Princess", parent_id=3, metadata={}),
        ]
Exemplo n.º 3
0
def test_find_children():
    assets = [
        Asset(id=3, name="holy grenade", metadata={}),
        Asset(id=7, name="not holy grenade", parent_id=3, metadata={}),
        Asset(id=5, name="in-holy grenade", parent_id=7, metadata={"source": "None"}),
    ]
    parents = find_children(assets, [None])
    children1 = find_children(assets, parents)
    children2 = find_children(assets, children1)
    assert parents[0].id == 3
    assert children1[0].id == 7
    assert children2[0].id == 5
    assert children1[0].parent_id == 3
    assert children2[0].parent_id == 7
Exemplo n.º 4
0
def test_build_asset_create():
    asset = Asset(id=3, name="holy grenade", metadata={})
    runtime = time.time() * 1000
    created = build_asset_create(asset, {}, "source_tenant", runtime, 0)
    assert "holy grenade" == created.name
    assert created.parent_id is None
    assert created.metadata
    assert "source_tenant" == created.metadata["_replicatedSource"]
    assert 3 == created.metadata["_replicatedInternalId"]

    asset = Asset(id=4, parent_id=2, name="holy grail", metadata={"_replicatedInternalId": 55})
    src_id_dst_map = {2: 5}
    second = build_asset_create(asset, src_id_dst_map, "source_tenant", runtime, 2)
    assert 5 == second.parent_id
Exemplo n.º 5
0
def build_asset_create(src_asset: Asset, src_id_dst_map: Dict[int, int],
                       project_src: str, runtime: int, depth: int) -> Asset:
    """
    Makes a new copy of the asset to be replicated based on the source asset.

    Args:
        src_asset: The asset from the source to be replicated to destination.
        src_id_dst_map: A dictionary of all the mappings of source asset id to destination asset id.
        project_src: The name of the project the object is being replicated from.
        runtime: The timestamp to be used in the new replicated metadata.
        depth: The depth of the asset within the asset hierarchy.

    Returns:
        The replicated asset to be created in the destination.

    """
    logging.debug(
        f"Creating a new asset based on source event id {src_asset.id}")

    return Asset(
        external_id=src_asset.external_id,
        name=src_asset.name,
        description=src_asset.description,
        metadata=replication.new_metadata(src_asset, project_src, runtime),
        source=src_asset.source,
        parent_id=src_id_dst_map[src_asset.parent_id] if depth > 0 else None,
    )
Exemplo n.º 6
0
def test_build_asset_update():
    assets_src = [
        Asset(id=3, name="Dog", external_id="Woff Woff", metadata={}, description="Humans best friend"),
        Asset(id=7, name="Cat", parent_id=3, external_id="Miau Miau", metadata={}),
        Asset(id=5, name="Cow", parent_id=7, metadata={}),
    ]
    assets_dst = [
        Asset(id=333, name="Copy-Dog", metadata={"_replicatedInternalId": 3}),
        Asset(id=777, name="Copy-Cat", parent_id=333, metadata={"_replicatedInternalId": 7}),
        Asset(id=555, name="Copy-Cow", parent_id=777, metadata={"_replicatedInternalId": 5, "source": "None"}),
    ]
    runtime = time.time() * 1000
    id_mapping = {3: 333, 7: 777, 5: 555}
    dst_asset_0 = build_asset_update(assets_src[0], assets_dst[0], id_mapping, "Flying Circus", runtime, depth=0)
    dst_asset_1 = build_asset_update(assets_src[1], assets_dst[1], id_mapping, "Flying Circus", runtime, depth=1)
    dst_asset_2 = build_asset_update(assets_src[2], assets_dst[2], id_mapping, "Flying Circus", runtime, depth=1)
    assert dst_asset_0.metadata["_replicatedSource"] == "Flying Circus"
    assert dst_asset_1.metadata["_replicatedSource"] == "Flying Circus"
    assert dst_asset_2.metadata["_replicatedSource"] == "Flying Circus"
    assert dst_asset_0.metadata["_replicatedInternalId"] == assets_src[0].id
    assert dst_asset_1.metadata["_replicatedInternalId"] == assets_src[1].id
    assert dst_asset_2.metadata["_replicatedInternalId"] == assets_src[2].id
    assert dst_asset_0.description == assets_src[0].description
    assert dst_asset_1.parent_id == 333
    assert dst_asset_2.parent_id == 777

    assets_src[2].parent_id = 3
    dst_asset_changed_2 = build_asset_update(
        assets_src[2], assets_dst[2], id_mapping, "Flying Circus", runtime, depth=1
    )
    assert dst_asset_changed_2.parent_id == 333
Exemplo n.º 7
0
def build_asset_update(src_asset: Asset, dst_asset: Asset,
                       src_id_dst_map: Dict[int, int], project_src: str,
                       runtime: int, depth: int) -> Asset:
    """
    Makes an updated version of the destination asset based on the corresponding source asset.

    Args:
        src_asset: The asset from the source to be replicated to destination.
        dst_asset: The asset from the destination that needs to be updated to reflect changes made to its source asset.
        src_id_dst_map: \\*\\*A dictionary of all the mappings of source asset id to destination asset id.
        project_src: The name of the project the object is being replicated from.
        runtime: The timestamp to be used in the new replicated metadata.
        depth: \\*\\*The depth of the asset within the asset hierarchy.
    \\*\\* only needed when hierarchy becomes mutable

    Returns:
        The updated asset object for the replication destination.

    """
    logging.debug(
        f"Updating existing event {dst_asset.id} based on source event id {src_asset.id}"
    )

    dst_asset.external_id = src_asset.external_id
    dst_asset.name = src_asset.name
    dst_asset.description = src_asset.description
    dst_asset.metadata = replication.new_metadata(src_asset, project_src,
                                                  runtime)
    dst_asset.source = src_asset.source
    dst_asset.parent_id = src_id_dst_map[
        src_asset.
        parent_id] if depth > 0 else None  # when asset hierarchy is mutable
    return dst_asset
Exemplo n.º 8
0
def test_create_hierarchy_with_dst_list():
    assets_src = [
        Asset(id=3,
              name="Queen",
              external_id="Queen in the Kingdom",
              metadata={},
              description="Married to the King"),
        Asset(id=7,
              name="Prince",
              parent_id=3,
              external_id="Future King",
              metadata={}),
        Asset(id=5, name="Princess", parent_id=3, metadata={}),
    ]

    assets_dst = [
        Asset(
            id=333,
            name="Copy-Queen",
            external_id="Queen in the Kingdom",
            metadata={
                "_replicatedInternalId": 3,
                "_replicatedTime": 1
            },
            description="Married to the King",
        ),
        Asset(
            id=777,
            name="Copy-Prince",
            external_id="Future King",
            metadata={
                "_replicatedInternalId": 7,
                "_replicatedTime": 1
            },
        ),
        Asset(id=555,
              name="Copy-Princess",
              metadata={
                  "_replicatedInternalId": 5,
                  "_replicatedTime": 1
              }),
        Asset(id=101, name="Adopted", metadata={}),
    ]