Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 48 additions & 11 deletions packages/polywrap-client/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions packages/polywrap-client/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ polywrap-uri-resolvers = {path = "../polywrap-uri-resolvers", develop = true}
polywrap-manifest = {path = "../polywrap-manifest", develop = true}
polywrap-msgpack = {path = "../polywrap-msgpack", develop = true}
polywrap-core = {path = "../polywrap-core", develop = true}

[tool.poetry.dev-dependencies]
pytest = "^7.1.2"
pytest-asyncio = "^0.19.0"
polywrap-plugin = {path = "../polywrap-plugin", develop = true}
polywrap-client-config-builder = {path = "../polywrap-client-config-builder", develop = true}
pylint = "^2.15.4"
black = "^22.10.0"
bandit = { version = "^1.7.4", extras = ["toml"]}
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
59 changes: 53 additions & 6 deletions packages/polywrap-client/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,34 @@

from pathlib import Path
from polywrap_core import FileReader, ClientConfig
from polywrap_uri_resolvers import WRAP_MANIFEST_PATH, WRAP_MODULE_PATH, FsUriResolver, SimpleFileReader
from polywrap_core import FileReader, ClientConfig, Invoker, UriPackageOrWrapper, Env, Uri
from polywrap_uri_resolvers import (
RecursiveResolver,
UriResolverAggregator,
StaticResolver,
FsUriResolver,
SimpleFileReader,
WRAP_MANIFEST_PATH,
WRAP_MODULE_PATH
)
from polywrap_plugin import PluginModule, PluginPackage
from pytest import fixture
from typing import Dict, Any, Optional
from polywrap_client import PolywrapClient

import time

@fixture
def client():
def client(memory_storage_plugin: PluginPackage[None]):
memory_storage_uri = Uri.from_str("wrap://ens/memory-storage.polywrap.eth")
config = ClientConfig(
resolver=FsUriResolver(file_reader=SimpleFileReader())
resolver=RecursiveResolver(
UriResolverAggregator(
[
FsUriResolver(file_reader=SimpleFileReader()),
StaticResolver({ memory_storage_uri: memory_storage_plugin})
]
)
)
)
return PolywrapClient(config)

@fixture
Expand All @@ -36,4 +54,33 @@ async def read_file(self, file_path: str) -> bytes:
return simple_wrap_manifest
raise FileNotFoundError(f"FileNotFound: {file_path}")

yield SimpleFileReader()
yield SimpleFileReader()

class MemoryStorage(PluginModule[None]):
def __init__(self):
super().__init__(None)
self.value = 0

def getData(self, args: Dict[str, Any], client: Invoker[UriPackageOrWrapper], env: Optional[Env]) -> int:
time.sleep(0.05) # Sleep for 50 milliseconds
return self.value

def setData(self, args: Dict[str, Any], client: Invoker[UriPackageOrWrapper], env: Optional[Env]) -> bool:
time.sleep(0.05) # Sleep for 50 milliseconds
self.value = args["value"]
return True


@fixture
def memory_storage_plugin() -> PluginPackage[None]:
return PluginPackage(module=MemoryStorage(), manifest={}) # type: ignore


class Adder(PluginModule[None]):
def add(self, args: Dict[str, Any], client: Invoker[UriPackageOrWrapper], env: Optional[Env]) -> int:
return args["a"] + args["b"]


@fixture
def adder_plugin() -> PluginPackage[None]:
return PluginPackage(module=Adder(None), manifest={}) # type: ignore
111 changes: 111 additions & 0 deletions packages/polywrap-client/tests/test_asyncify.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
# Polywrap Python Client - https://polywrap.io
# BigNumber wrapper schema - https://wrappers.io/v/ipfs/Qme2YXThmsqtfpiUPHJUEzZSBiqX3woQxxdXbDJZvXrvAD

from pathlib import Path
from polywrap_client import PolywrapClient
from polywrap_core import Uri, InvokerOptions, UriPackageOrWrapper


async def test_asyncify(client: PolywrapClient):
uri = Uri.from_str(
f'fs/{Path(__file__).parent.joinpath("cases", "asyncify").absolute()}'
)
args = {
"numberOfTimes": 40
}
subsequent_invokes_options: InvokerOptions[UriPackageOrWrapper] = InvokerOptions(
uri=uri, method="subsequentInvokes", args=args
)
subsequent_invokes_result = await client.invoke(subsequent_invokes_options)
subsequent_invokes_expected = [str(i) for i in range(40)]

assert subsequent_invokes_result == subsequent_invokes_expected

local_var_method_options: InvokerOptions[UriPackageOrWrapper] = InvokerOptions(
uri=uri, method="localVarMethod", args=None
)

local_var_method_result = await client.invoke(local_var_method_options)

assert local_var_method_result == True

global_var_method_options: InvokerOptions[UriPackageOrWrapper] = InvokerOptions(
uri=uri, method="globalVarMethod", args=None
)

global_var_method_result = await client.invoke(global_var_method_options)
assert global_var_method_result == True


large_str = "polywrap" * 10000
set_data_with_large_args_options: InvokerOptions[UriPackageOrWrapper] = InvokerOptions(
uri=uri, method="setDataWithLargeArgs", args={"value":large_str}
)
set_data_with_large_args_result = await client.invoke(set_data_with_large_args_options)
assert set_data_with_large_args_result == large_str

large_str = "polywrap" * 10000
set_data_with_large_args_options: InvokerOptions[UriPackageOrWrapper] = InvokerOptions(
uri=uri, method="setDataWithLargeArgs", args={"value":large_str}
)
set_data_with_large_args_result = await client.invoke(set_data_with_large_args_options)
assert set_data_with_large_args_result == large_str

set_data_with_many_args_args = {
"valueA": "polywrap a",
"valueB": "polywrap b",
"valueC": "polywrap c",
"valueD": "polywrap d",
"valueE": "polywrap e",
"valueF": "polywrap f",
"valueG": "polywrap g",
"valueH": "polywrap h",
"valueI": "polywrap i",
"valueJ": "polywrap j",
"valueK": "polywrap k",
"valueL": "polywrap l",
}
set_data_with_many_args_options: InvokerOptions[UriPackageOrWrapper] = InvokerOptions(
uri=uri, method="setDataWithManyArgs", args=set_data_with_many_args_args
)

set_data_with_many_args_result = await client.invoke(set_data_with_many_args_options)

set_data_with_many_args_expected = "polywrap apolywrap bpolywrap cpolywrap dpolywrap epolywrap fpolywrap gpolywrap hpolywrap ipolywrap jpolywrap kpolywrap l"
assert set_data_with_many_args_result == set_data_with_many_args_expected

def create_obj(i: int):
return {
"propA": f"a-{i}",
"propB": f"b-{i}",
"propC": f"c-{i}",
"propD": f"d-{i}",
"propE": f"e-{i}",
"propF": f"f-{i}",
"propG": f"g-{i}",
"propH": f"h-{i}",
"propI": f"i-{i}",
"propJ": f"j-{i}",
"propK": f"k-{i}",
"propL": f"l-{i}"
}

set_data_with_many_structure_args_args = {
"valueA": create_obj(1),
"valueB": create_obj(2),
"valueC": create_obj(3),
"valueD": create_obj(4),
"valueE": create_obj(5),
"valueF": create_obj(6),
"valueG": create_obj(7),
"valueH": create_obj(8),
"valueI": create_obj(9),
"valueJ": create_obj(10),
"valueK": create_obj(11),
"valueL": create_obj(12),
}
set_data_with_many_structured_args_options: InvokerOptions[UriPackageOrWrapper] = InvokerOptions(
uri=uri, method="setDataWithManyStructuredArgs", args=set_data_with_many_structure_args_args
)
set_data_with_many_structured_args_result = await client.invoke(set_data_with_many_structured_args_options)
assert set_data_with_many_structured_args_result == True
53 changes: 51 additions & 2 deletions packages/polywrap-client/tests/test_client.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
from pathlib import Path

from polywrap_client_config_builder import PolywrapClientConfigBuilder
from polywrap_plugin import PluginPackage
from polywrap_client import PolywrapClient
from polywrap_manifest import deserialize_wrap_manifest
from polywrap_core import Uri, InvokerOptions, FileReader, UriPackageOrWrapper, ClientConfig
from polywrap_uri_resolvers import BaseUriResolver, SimpleFileReader, StaticResolver
from polywrap_core import (
Uri,
InvokerOptions,
FileReader,
UriPackageOrWrapper,
ClientConfig,
)
from polywrap_uri_resolvers import (
BaseUriResolver,
FsUriResolver,
SimpleFileReader,
StaticResolver,
)
from polywrap_wasm import WasmWrapper


Expand Down Expand Up @@ -144,3 +158,38 @@ async def test_env():
result = await client.invoke(options)

assert result == env


async def test_complex_subinvocation(adder_plugin: PluginPackage[None]):
config = (
PolywrapClientConfigBuilder()
.add_resolver(FsUriResolver(SimpleFileReader()))
.set_redirect(
Uri.from_str("ens/imported-subinvoke.eth"),
Uri.from_str(
f'fs/{Path(__file__).parent.joinpath("cases", "subinvoke", "00-subinvoke").absolute()}'
),
)
.set_redirect(
Uri.from_str("ens/imported-invoke.eth"),
Uri.from_str(
f'fs/{Path(__file__).parent.joinpath("cases", "subinvoke", "01-invoke").absolute()}'
),
)
.set_package(
Uri.from_str("plugin/adder"),
adder_plugin,
)
).build()

client = PolywrapClient(config)
uri = Uri.from_str(
f'fs/{Path(__file__).parent.joinpath("cases", "subinvoke", "02-consumer").absolute()}'
)
args = {"a": 1, "b": 1}
options: InvokerOptions[UriPackageOrWrapper] = InvokerOptions(
uri=uri, method="addFromPluginAndIncrement", args=args
)
result = await client.invoke(options)

assert result == 4
Loading