Skip to content

Commit

Permalink
Optimise tests (#480)
Browse files Browse the repository at this point in the history
* Optimise some TimeExhausted tests.
* Reuse fixtures in compute flow.
* Use samples in test_utilitary_functions_for_trusted_algorithms
* Optimise publisher trusted tests.
* Asset downloader test opti.
* More removals of wait_for_ddo.
* Change function names for sample_algorithm.
  • Loading branch information
calina-c authored Sep 10, 2021
1 parent 1d6ef84 commit ba90550
Show file tree
Hide file tree
Showing 14 changed files with 271 additions and 285 deletions.
6 changes: 4 additions & 2 deletions ocean_lib/assets/asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ def values(self) -> dict:

@enforce_types
def get_trusted_algorithms(self) -> list:
return self.get_compute_privacy_attributes().get("publisherTrustedAlgorithms")
return self.get_compute_privacy_attributes().get(
"publisherTrustedAlgorithms", []
)

@enforce_types
def get_trusted_algorithm_publishers(self) -> list:
return self.get_compute_privacy_attributes().get(
"publisherTrustedAlgorithmPublishers"
"publisherTrustedAlgorithmPublishers", []
)

@enforce_types
Expand Down
46 changes: 14 additions & 32 deletions ocean_lib/assets/test/test_asset_downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,16 @@
from ocean_lib.common.agreements.service_agreement import ServiceAgreement
from ocean_lib.common.agreements.service_types import ServiceTypes
from ocean_lib.data_provider.data_service_provider import DataServiceProvider
from tests.resources.ddo_helpers import wait_for_ddo
from tests.resources.ddo_helpers import get_sample_ddo
from tests.resources.helper_functions import get_publisher_wallet


def test_ocean_assets_download_failure(publisher_ocean_instance, metadata):
def test_ocean_assets_download_failure():
"""Tests that downloading from an empty service raises an AssertionError."""
publisher = get_publisher_wallet()
metadata_copy = metadata.copy()
data_provider = DataServiceProvider

ddo = publisher_ocean_instance.assets.create(metadata_copy, publisher)
wait_for_ddo(publisher_ocean_instance, ddo.did)
ddo = get_sample_ddo()
sa = ServiceAgreement.from_ddo(ServiceTypes.ASSET_ACCESS, ddo)
sa.service_endpoint = None
ddo.services[1] = sa
Expand All @@ -31,20 +29,18 @@ def test_ocean_assets_download_failure(publisher_ocean_instance, metadata):
ddo,
publisher,
"test_destination",
ddo.data_token_address,
"",
"test_order_tx_id",
data_provider,
)


def test_ocean_assets_download_indexes(publisher_ocean_instance, metadata):
def test_ocean_assets_download_indexes():
"""Tests different values of indexes that raise AssertionError."""
publisher = get_publisher_wallet()
metadata_copy = metadata.copy()
data_provider = DataServiceProvider

ddo = publisher_ocean_instance.assets.create(metadata_copy, publisher)
wait_for_ddo(publisher_ocean_instance, ddo.did)
ddo = get_sample_ddo()
sa = ServiceAgreement.from_ddo(ServiceTypes.ASSET_ACCESS, ddo)

index = range(3)
Expand All @@ -54,7 +50,7 @@ def test_ocean_assets_download_indexes(publisher_ocean_instance, metadata):
ddo,
publisher,
"test_destination",
ddo.data_token_address,
"",
"test_order_tx_id",
data_provider,
index,
Expand All @@ -67,7 +63,7 @@ def test_ocean_assets_download_indexes(publisher_ocean_instance, metadata):
ddo,
publisher,
"test_destination",
ddo.data_token_address,
"",
"test_order_tx_id",
data_provider,
index,
Expand All @@ -79,41 +75,27 @@ def test_ocean_assets_download_indexes(publisher_ocean_instance, metadata):
ddo,
publisher,
"test_destination",
ddo.data_token_address,
"",
"test_order_tx_id",
data_provider,
index,
)


def test_ocean_assets_download_destination_file(
publisher_ocean_instance, metadata, tmpdir
):
def test_ocean_assets_download_destination_file(tmpdir):
"""Convert tmpdir: py._path.local.LocalPath to str, satisfy enforce-typing."""
ocean_assets_download_destination_file_helper(
publisher_ocean_instance, metadata, str(tmpdir)
)
ocean_assets_download_destination_file_helper(str(tmpdir))


def ocean_assets_download_destination_file_helper(
publisher_ocean_instance, metadata, tmpdir
):
def ocean_assets_download_destination_file_helper(tmpdir):
"""Tests downloading to an existing directory."""
publisher = get_publisher_wallet()
metadata_copy = metadata.copy()
data_provider = DataServiceProvider

ddo = publisher_ocean_instance.assets.create(metadata_copy, publisher)
wait_for_ddo(publisher_ocean_instance, ddo.did)
ddo = get_sample_ddo()
sa = ServiceAgreement.from_ddo(ServiceTypes.ASSET_ACCESS, ddo)

written_path = download_asset_files(
sa.index,
ddo,
publisher,
tmpdir,
ddo.data_token_address,
"test_order_tx_id",
data_provider,
sa.index, ddo, publisher, tmpdir, "0x1", "test_order_tx_id", data_provider
)
assert os.path.exists(written_path)
11 changes: 2 additions & 9 deletions ocean_lib/assets/test/test_asset_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,9 @@ def test_resolve_asset(publisher_ocean_instance, metadata):
), "Resolve asset function call is unsuccessful."


def test_bad_resolved_asset(publisher_ocean_instance, metadata):
publisher = get_publisher_wallet()
metadata_copy = metadata.copy()

asset = publisher_ocean_instance.assets.create(metadata_copy, publisher)
wait_for_ddo(publisher_ocean_instance, asset.did)
assert asset is not None, "The asset is not cached."

def test_bad_resolved_asset():
with pytest.raises(AssertionError) as err:
resolve_asset(asset.did)
resolve_asset("0x1")
assert (
err.value.args[0]
== "Either metadata_cache_uri or (web3 and token_address) is required."
Expand Down
114 changes: 51 additions & 63 deletions ocean_lib/assets/test/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
# Copyright 2021 Ocean Protocol Foundation
# SPDX-License-Identifier: Apache-2.0
#
from unittest.mock import patch

import pytest
from ocean_lib.assets.utils import (
add_publisher_trusted_algorithm,
Expand All @@ -11,61 +13,61 @@
remove_publisher_trusted_algorithm,
remove_publisher_trusted_algorithm_publisher,
)
from ocean_lib.common.agreements.service_types import ServiceTypes
from ocean_lib.common.did import DID
from tests.resources.ddo_helpers import (
get_registered_algorithm_ddo,
get_registered_ddo_with_compute_service,
wait_for_ddo,
get_sample_algorithm_ddo,
get_sample_ddo,
get_sample_ddo_with_compute_service,
)
from tests.resources.helper_functions import get_publisher_wallet


def test_utilitary_functions_for_trusted_algorithms(publisher_ocean_instance):
"""Tests adding/removing trusted algorithms in the DDO metadata."""
publisher = get_publisher_wallet()

algorithm_ddo = get_registered_algorithm_ddo(publisher_ocean_instance, publisher)
wait_for_ddo(publisher_ocean_instance, algorithm_ddo.did)
assert algorithm_ddo is not None, "Algorithm DDO is not found in cache."

algorithm_ddo_v2 = get_registered_algorithm_ddo(publisher_ocean_instance, publisher)
wait_for_ddo(publisher_ocean_instance, algorithm_ddo_v2.did)
assert algorithm_ddo_v2 is not None, "Algorithm DDO is not found in cache."
algorithm_ddo = get_sample_algorithm_ddo()
algorithm_ddo.did = DID.did({"ab": "b"})
algorithm_ddo_v2 = get_sample_algorithm_ddo()
algorithm_ddo_v2.did = DID.did({"a": "b"})
algorithm_ddo_v3 = get_sample_algorithm_ddo()
algorithm_ddo_v3.did = DID.did({"A": "B"})

algorithm_ddo_v3 = get_registered_algorithm_ddo(publisher_ocean_instance, publisher)
wait_for_ddo(publisher_ocean_instance, algorithm_ddo_v3.did)
assert algorithm_ddo_v3 is not None, "Algorithm DDO is not found in cache."

ddo = get_registered_ddo_with_compute_service(
publisher_ocean_instance, publisher, trusted_algorithms=[algorithm_ddo.did]
)
wait_for_ddo(publisher_ocean_instance, ddo.did)
assert ddo is not None, "DDO is not found in cache."
ddo = get_sample_ddo_with_compute_service()

publisher_trusted_algorithms = create_publisher_trusted_algorithms(
[algorithm_ddo.did], publisher_ocean_instance.config.metadata_cache_uri
[algorithm_ddo], publisher_ocean_instance.config.metadata_cache_uri
)
assert len(publisher_trusted_algorithms) == 1
compute_service = ddo.get_service(ServiceTypes.CLOUD_COMPUTE)
compute_service.main["privacy"][
"publisherTrustedAlgorithms"
] = publisher_trusted_algorithms

with patch("ocean_lib.assets.utils.resolve_asset") as mock:
mock.return_value = algorithm_ddo_v2
# add a new trusted algorithm to the publisher_trusted_algorithms list
new_publisher_trusted_algorithms = add_publisher_trusted_algorithm(
ddo,
algorithm_ddo_v2.did,
publisher_ocean_instance.config.metadata_cache_uri,
)

# add a new trusted algorithm to the publisher_trusted_algorithms list
new_publisher_trusted_algorithms = add_publisher_trusted_algorithm(
ddo, algorithm_ddo_v2.did, publisher_ocean_instance.config.metadata_cache_uri
)

assert (
new_publisher_trusted_algorithms is not None
), "Added a new trusted algorithm failed. The list is empty."
assert len(new_publisher_trusted_algorithms) == 2

# add an existing algorithm to publisher_trusted_algorithms list
new_publisher_trusted_algorithms = add_publisher_trusted_algorithm(
ddo, algorithm_ddo.did, publisher_ocean_instance.config.metadata_cache_uri
)
assert new_publisher_trusted_algorithms is not None
for trusted_algorithm in publisher_trusted_algorithms:
assert (
trusted_algorithm["did"] == algorithm_ddo.did
), "Added a different algorithm besides the existing ones."
assert len(new_publisher_trusted_algorithms) == 2
new_publisher_trusted_algorithms is not None
), "Added a new trusted algorithm failed. The list is empty."
assert len(new_publisher_trusted_algorithms) == 2

with patch("ocean_lib.assets.utils.resolve_asset") as mock:
mock.return_value = algorithm_ddo
# add an existing algorithm to publisher_trusted_algorithms list
new_publisher_trusted_algorithms = add_publisher_trusted_algorithm(
ddo, algorithm_ddo.did, publisher_ocean_instance.config.metadata_cache_uri
)
assert new_publisher_trusted_algorithms is not None
for trusted_algorithm in publisher_trusted_algorithms:
assert (
trusted_algorithm["did"] == algorithm_ddo.did
), "Added a different algorithm besides the existing ones."
assert len(new_publisher_trusted_algorithms) == 2

# remove an existing algorithm to publisher_trusted_algorithms list
new_publisher_trusted_algorithms = remove_publisher_trusted_algorithm(
Expand All @@ -84,23 +86,12 @@ def test_utilitary_functions_for_trusted_algorithms(publisher_ocean_instance):
assert len(new_publisher_trusted_algorithms) == 1


def test_add_trusted_algorithm_no_compute_service(publisher_ocean_instance, metadata):
def test_add_trusted_algorithm_no_compute_service(publisher_ocean_instance):
"""Tests if the DDO has or not a compute service."""
publisher = get_publisher_wallet()
metadata_copy = metadata.copy()

algorithm_ddo = get_registered_algorithm_ddo(publisher_ocean_instance, publisher)
wait_for_ddo(publisher_ocean_instance, algorithm_ddo.did)
assert algorithm_ddo is not None, "Algorithm DDO is not found in cache."

ddo = publisher_ocean_instance.assets.create(metadata_copy, publisher)
wait_for_ddo(publisher_ocean_instance, ddo.did)
assert ddo is not None, "DDO is not found in cache."

create_publisher_trusted_algorithms(
[algorithm_ddo.did], publisher_ocean_instance.config.metadata_cache_uri
)
algorithm_ddo = get_sample_algorithm_ddo()
algorithm_ddo.did = DID.did({"ab": "b"})

ddo = get_sample_ddo()
with pytest.raises(AssertionError):
add_publisher_trusted_algorithm(
ddo, algorithm_ddo.did, publisher_ocean_instance.config.metadata_cache_uri
Expand All @@ -115,12 +106,9 @@ def test_fail_generate_trusted_algo_dict():

def test_utilitary_functions_for_trusted_algorithm_publishers(publisher_ocean_instance):
"""Tests adding/removing trusted algorithms in the DDO metadata."""
publisher = get_publisher_wallet()
ddo = get_registered_ddo_with_compute_service(
publisher_ocean_instance, publisher, trusted_algorithm_publishers=["0xabc"]
)
wait_for_ddo(publisher_ocean_instance, ddo.did)
assert ddo is not None, "DDO is not found in cache."
ddo = get_sample_ddo_with_compute_service()
compute_service = ddo.get_service(ServiceTypes.CLOUD_COMPUTE)
compute_service.main["privacy"]["publisherTrustedAlgorithmPublishers"] = ["0xabc"]

# add a new trusted algorithm to the publisher_trusted_algorithms list
new_publisher_trusted_algo_publishers = add_publisher_trusted_algorithm_publisher(
Expand Down
11 changes: 7 additions & 4 deletions ocean_lib/assets/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def generate_trusted_algo_dict(
return {
"did": ddo.did,
"filesChecksum": create_checksum(
algo_metadata["encryptedFiles"]
algo_metadata.get("encryptedFiles", "")
+ json.dumps(algo_metadata["main"]["files"], separators=(",", ":"))
),
"containerSectionChecksum": create_checksum(
Expand All @@ -55,15 +55,17 @@ def generate_trusted_algo_dict(


@enforce_types
def create_publisher_trusted_algorithms(dids: list, metadata_cache_uri: str) -> list:
def create_publisher_trusted_algorithms(
ddos_or_dids: list, metadata_cache_uri: str
) -> list:
"""
:return: List of objects returned by `generate_trusted_algo_dict` method.
"""
return [
generate_trusted_algo_dict(
asset_or_did=did, metadata_cache_uri=metadata_cache_uri
asset_or_did=ddo_or_did, metadata_cache_uri=metadata_cache_uri
)
for did in dids
for ddo_or_did in ddos_or_dids
]


Expand Down Expand Up @@ -96,6 +98,7 @@ def add_publisher_trusted_algorithm(
# now add this algo_did as trusted algo
algo_ddo = resolve_asset(algo_did, metadata_cache_uri=metadata_cache_uri)
trusted_algos.append(generate_trusted_algo_dict(asset_or_did=algo_ddo))

# update with the new list
privacy_values["publisherTrustedAlgorithms"] = trusted_algos
assert (
Expand Down
13 changes: 8 additions & 5 deletions ocean_lib/models/test/test_datatoken.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import json
import os
import time
from unittest.mock import patch

import pytest
from ocean_lib.common.ddo.ddo import DDO
Expand Down Expand Up @@ -221,11 +222,13 @@ def test_verify_order_tx(alice_address, bob_address, alice_ocean, alice_wallet):
transfer_tx_id = token.transfer(bob_address, to_wei(5), from_wallet=alice_wallet)

with pytest.raises(TimeExhausted):
# dummy tx id which is not found in the chain
# need to catch TimeExhausted exception from web3
token.verify_order_tx(
"0x0", "some_did", "some_index", "some_amount", alice_address
)
with patch("ocean_lib.models.data_token.DataToken.get_tx_receipt") as mock:
# dummy tx id which is not found in the chain
# catches TimeExhausted exception from web3
mock.side_effect = TimeExhausted()
token.verify_order_tx(
"0x0", "some_did", "some_index", "some_amount", alice_address
)

transfer_tx_id = token.transfer(bob_address, to_wei(5), from_wallet=alice_wallet)
with pytest.raises(AssertionError):
Expand Down
8 changes: 7 additions & 1 deletion ocean_lib/models/test/test_dtfactory.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
# Copyright 2021 Ocean Protocol Foundation
# SPDX-License-Identifier: Apache-2.0
#
from unittest.mock import patch

import pytest
from ocean_lib.models.data_token import DataToken
from ocean_lib.models.dtfactory import DTFactory
Expand Down Expand Up @@ -47,7 +49,11 @@ def test_get_token_address_fails(web3, dtfactory_address):
dtfactory = DTFactory(web3, dtfactory_address)
# Transaction 0x is not in the chain
with pytest.raises(TimeExhausted):
dtfactory.get_token_address("")
with patch("ocean_lib.models.dtfactory.DTFactory.get_tx_receipt") as mock:
# throw the exception without acually waiting
mock.side_effect = TimeExhausted()
# we are checking that this exception bubbles up to get_token_address()
dtfactory.get_token_address("")


def test_get_token_minter(web3, alice_wallet, dtfactory_address, alice_address):
Expand Down
Loading

0 comments on commit ba90550

Please sign in to comment.