def test_json_all_kinds_of():
    """
    Serialize a JSON payload of all kinds of fields.
    """
    lookup = MultiPackageLookup(CachedDarFile(dars.AllKindsOf).archives())
    tt = con(lookup.data_type_name("AllKindsOf:OneOfEverything"))
    ctx = Context(JsonEncoder(), lookup)

    expected = {
        "operator": "Operator",
        "someBoolean": True,
        "someInteger": 10,
        "someDecimal": "10",
        "someMaybe": 10,
        "someMaybeNot": None,
        "someText": "text",
        "someDate": "2000-01-01",
        "someDatetime": "2000-01-01T00:00:00Z",
        "someSimpleList": [1],
        "someSimplePair": {"left": 4, "right": 5},
        "someNestedPair": {"left": {"left": 4, "right": 5}, "right": {"left": 10, "right": 4}},
        "someUglyNesting": {
            "tag": "Both",
            "value": {
                "tag": "Right",
                "value": {"left": {"left": 10, "right": 20}, "right": {"left": 30, "right": 40}},
            },
        },
        "someMeasurement": "5",
        "someEnum": "Green",
        "theUnit": {},
    }

    actual = ctx.convert(
        tt,
        {
            "operator": "Operator",
            "someBoolean": True,
            "someInteger": 10,
            "someDecimal": 10,
            "someMaybe": 10,
            "someMaybeNot": None,
            "someText": "text",
            "someDate": date(2000, 1, 1),
            "someDatetime": datetime(2000, 1, 1),
            "someSimpleList": [1],
            "someSimplePair": {"left": 4, "right": 5},
            "someNestedPair": {"left": {"left": 4, "right": 5}, "right": {"left": 10, "right": 4}},
            "someUglyNesting": {
                "Both": {
                    "Right": {"left": {"left": 10, "right": 20}, "right": {"left": 30, "right": 40}}
                }
            },
            "someMeasurement": 5,
            "someEnum": "Green",
            "theUnit": {},
        },
    )

    assert expected == actual
Exemplo n.º 2
0
    def __init__(self, template_name):
        self.lookup = MultiPackageLookup()
        self.loader = PackageLoader(self.lookup, DarFile(AllKindsOf))
        self.call_order = []
        self.template_name = template_name

        # the lookup should always start out empty
        assert len(self.lookup.archives()) == 0
Exemplo n.º 3
0
def test_wildcard_template_names():
    with DarFile(AllKindsOf) as dar:
        lookup = MultiPackageLookup(dar.archives())

    # make sure that looking for templates, wildcarded by package ref, actually work and return
    # things
    names = [
        name for ref in lookup.package_ids()
        for name in lookup.template_names(f"{ref}:*")
    ]
    assert len(names) == 2
Exemplo n.º 4
0
async def test_pkg_loader_consolidates_concurrent_fetch(executor):
    loop = get_event_loop()
    pkg_ref, contents = load_some_bytes()

    evt1 = Event()
    evt2 = Event()

    class MockPackageService:
        def __init__(self):
            self.call_count = 0

        async def get_package(self, package_id: "PackageRef") -> bytes:
            self.call_count += 1
            if package_id != pkg_ref:
                raise Exception
            evt1.set()
            await evt2.wait()
            return contents

        async def list_package_ids(self) -> "AbstractSet[PackageRef]":
            # we don't expect this method to be called in the test
            raise Exception

    conn = MockPackageService()
    lookup = MultiPackageLookup()
    loader = PackageLoader(lookup, conn)

    # first, call the PackageLoader.load coroutine
    fut1 = ensure_future(loader.load(pkg_ref))

    # wait until we are definitely in the MockPackageService.package_bytes call in one of
    # PackageLoader's background threads
    await loop.run_in_executor(executor, lambda: evt1.wait())

    # now schedule a _second_ PackageLoader.load coroutine; because the first one is still in
    # progress, this should NOT result in a second call to
    # MockPackageService.package_bytes
    fut2 = ensure_future(loader.load(pkg_ref))

    # allow coroutines some time to screw things up
    await sleep(0.1)

    # make sure that neither call to PackageLoader.load has actually come back yet
    assert not fut1.done()
    assert not fut2.done()

    # now unblock MockPackageService.package_bytes, which will return our bytes
    evt2.set()

    # grab the results from both async PackageLoader.load coroutine calls
    pkg1 = await fut1
    pkg2 = await fut2

    # we should have only called package_bytes once
    assert conn.call_count == 1

    # the two Package objects that come back should be identical; creating Package objects are
    # expensive but they are also immutable, so the two calls should return the same instance
    # as an optimization
    assert pkg1 is pkg2
Exemplo n.º 5
0
async def test_pkg_loader_only_fetches_once(executor):
    pkg_ref, contents = load_some_bytes()

    class MockPackageService:
        def __init__(self):
            self.call_count = 0

        async def get_package(self, package_id: "PackageRef") -> bytes:
            self.call_count += 1
            if package_id != pkg_ref:
                raise Exception
            return contents

        async def list_package_ids(self) -> "AbstractSet[PackageRef]":
            return frozenset([pkg_ref])

    conn = MockPackageService()
    lookup = MultiPackageLookup()
    loader = PackageLoader(lookup, conn)

    # first, call the PackageLoader.load coroutine
    pkg1 = await loader.load(pkg_ref)

    # now call it again
    pkg2 = await loader.load(pkg_ref)

    # we should have only called package_bytes once
    assert conn.call_count == 1

    # the two Package objects that come back should be identical; creating Package objects are
    # expensive but they are also immutable, so the two calls should return the same instance
    # as an optimization
    assert pkg1 is pkg2
Exemplo n.º 6
0
class PackageLoaderTest:
    def __init__(self, template_name):
        self.lookup = MultiPackageLookup()
        self.loader = PackageLoader(self.lookup, DarFile(AllKindsOf))
        self.call_order = []
        self.template_name = template_name

        # the lookup should always start out empty
        assert len(self.lookup.archives()) == 0

    def some_fn(self) -> "TypeConName":
        try:
            name = self.lookup.template_name(self.template_name)
            self.call_order.append("good call")
            return name
        except:
            self.call_order.append("exception")
            raise
async def test_autoload_explicit_packages(sandbox):
    with DarFile(Simple) as dar_file:
        lookup = MultiPackageLookup()
        lookup.add_archive(*dar_file.archives())
        fqtn = lookup.template_name("Simple:OperatorRole")

    # Start the sandbox and make sure our package is loaded.
    logging.info("Preloading the DAR...")
    network = Network()
    network.set_config(url=sandbox)
    await network.aio_global().ensure_dar(Simple)

    # Now start the sandbox again, but without any preloading.
    logging.info("Now running the real test...")
    network = Network()
    network.set_config(url=sandbox, eager_package_fetch=False)
    client = network.aio_new_party()
    fut = ensure_future(network.aio_run())
    await client.ready()

    with pytest.raises(PackageNotFoundError):
        # this call SHOULD fail because the package has not yet locally been loaded
        _ = network.lookup.template(fqtn)

    # this call should NOT fail because we tolerate searches on templates when
    # fully specified but the package has not yet been fetched
    contracts = client.find_active(fqtn)
    assert len(contracts) == 0

    # this call should nonetheless succeed because dazl fetches packages that it sees
    # it's missed
    logging.info("Submitting the create...")
    await client.create(fqtn, {"operator": client.party})

    network.shutdown()
    await fut
Exemplo n.º 8
0
def dar_fixture() -> "DarFixture":
    with DarFile(Pending) as dar:
        lookup = MultiPackageLookup(dar.archives())

        yield DarFixture(dar, lookup)
Exemplo n.º 9
0
def lookup():
    return MultiPackageLookup(CachedDarFile(dars.AllKindsOf).archives())
Exemplo n.º 10
0
def test_render_metadata():
    with DarFile(Pending) as dar:
        pp = DamlPrettyPrinter(lookup=MultiPackageLookup(dar.archives()),
                               context=PrettyOptions(show_hidden_types=True))
        pp.render_store()