Skip to content
Open
Show file tree
Hide file tree
Changes from 4 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
18 changes: 12 additions & 6 deletions hugr-py/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from typing_extensions import Self

from hugr import ext, tys
from hugr.envelope import EnvelopeConfig
from hugr.envelope import EnvelopeConfig, EnvelopeFormat
from hugr.hugr import Hugr
from hugr.ops import AsExtOp, Command, Const, Custom, DataflowOp, ExtOp, RegisteredOp
from hugr.package import Package
Expand Down Expand Up @@ -185,16 +185,22 @@ def validate(
# `hugr convert --format`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like the compressed ones you added are not included in hugr convert --format? Could you update this comment accordingly?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Modified the comments to add some more clarity.

FORMATS = {
"json": EnvelopeConfig.TEXT,
"json-compressed": EnvelopeConfig(format=EnvelopeFormat.JSON, zstd=0),
"model-exts": EnvelopeConfig.BINARY,
"model-exts-no-compression": EnvelopeConfig(
format=EnvelopeFormat.MODEL_WITH_EXTS, zstd=None
),
}
# Envelope formats used when exporting test hugrs.
WRITE_FORMATS = ["json", "model-exts"]
WRITE_FORMATS = [
"json",
"json-compressed",
"model-exts",
"model-exts-no-compression",
]
# Envelope formats used as target for `hugr convert` before loading back the
# test hugrs.
#
# Model envelopes cannot currently be loaded from python.
# TODO: Add model envelope loading to python, and add it to the list.
LOAD_FORMATS = ["json"]
LOAD_FORMATS = ["json", "model-exts"]

cmd = [*_base_command(), "validate", "-"]

Expand Down
25 changes: 17 additions & 8 deletions hugr-py/tests/test_envelope.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,27 @@ def package() -> Package:
return Package([mod.hugr, mod2.hugr])


def test_envelope(package: Package):
# Binary compression roundtrip
for format in [
@pytest.mark.parametrize(
"compression", [None, 0], ids=["compression:None", "compression:0"]
)
@pytest.mark.parametrize(
"format",
[
EnvelopeFormat.JSON,
EnvelopeFormat.MODEL,
EnvelopeFormat.MODEL_WITH_EXTS,
]:
for compression in [None, 0]:
encoded = package.to_bytes(EnvelopeConfig(format=format, zstd=compression))
decoded = Package.from_bytes(encoded)
assert decoded == package
],
)
def test_envelope_binary(
package: Package, compression: int | None, format: EnvelopeFormat
):
# Binary compression roundtrip
encoded = package.to_bytes(EnvelopeConfig(format=format, zstd=compression))
decoded = Package.from_bytes(encoded)
assert decoded == package


def test_envelope_text(package: Package):
# String roundtrip
encoded_str = package.to_str(EnvelopeConfig.TEXT)
decoded = Package.from_str(encoded_str)
Expand Down
Loading