Skip to content

Commit 9c6f95f

Browse files
authored
chore: add model-exts conversion check (#2613)
Now that we are able to import models in python (#2581) we can enable the roundtrip checks in the `validate` fixture of the `hugr-py` tests. I have also added a couple more models to check when writing, mainly JSON with compression and BINARY with no compression. This adds around 5 seconds of running time to the `hugr-py` tests. And I've separated the `test_envelope.py::test_envelope` test case into `test_envelope_binary` (parametrized for each `EnvelopeFormat` and `compression`) and `test_envelope_text`.
1 parent f3cfff4 commit 9c6f95f

File tree

2 files changed

+32
-14
lines changed

2 files changed

+32
-14
lines changed

hugr-py/tests/conftest.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from typing_extensions import Self
99

1010
from hugr import cli, ext, tys
11-
from hugr.envelope import EnvelopeConfig
11+
from hugr.envelope import EnvelopeConfig, EnvelopeFormat
1212
from hugr.hugr import Hugr
1313
from hugr.ops import AsExtOp, Command, Const, Custom, DataflowOp, ExtOp, RegisteredOp
1414
from hugr.package import Package
@@ -166,16 +166,25 @@ def validate(
166166
if os.environ.get("HUGR_RENDER_DOT"):
167167
dot.pipe("svg")
168168

169-
# Encoding formats to test, indexed by the format name as used by
170-
# `hugr convert --format`.
169+
# Encoding formats to test. Note that these include other formats than
170+
# those supported by `hugr convert`.
171171
FORMATS = {
172172
"json": EnvelopeConfig.TEXT,
173+
"json-compressed": EnvelopeConfig(format=EnvelopeFormat.JSON, zstd=0),
173174
"model-exts": EnvelopeConfig.BINARY,
175+
"model-exts-no-compression": EnvelopeConfig(
176+
format=EnvelopeFormat.MODEL_WITH_EXTS, zstd=None
177+
),
174178
}
175179
# Envelope formats used when exporting test hugrs.
176-
WRITE_FORMATS = ["json", "model-exts"]
177-
# Envelope formats used as target for `hugr convert` before loading back the
178-
# test hugrs.
180+
WRITE_FORMATS = [
181+
"json",
182+
"json-compressed",
183+
"model-exts",
184+
"model-exts-no-compression",
185+
]
186+
# Envelope formats used as target before loading back the test hugrs.
187+
# These should correspond to the formats supported by `hugr convert`.
179188
LOAD_FORMATS = ["json", "model-exts"]
180189

181190
# validate text and binary formats

hugr-py/tests/test_envelope.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,27 @@ def package() -> Package:
3030
return Package([mod.hugr, mod2.hugr])
3131

3232

33-
def test_envelope(package: Package):
34-
# Binary compression roundtrip
35-
for format in [
33+
@pytest.mark.parametrize(
34+
"compression", [None, 0], ids=["compression:None", "compression:0"]
35+
)
36+
@pytest.mark.parametrize(
37+
"format",
38+
[
3639
EnvelopeFormat.JSON,
3740
EnvelopeFormat.MODEL,
3841
EnvelopeFormat.MODEL_WITH_EXTS,
39-
]:
40-
for compression in [None, 0]:
41-
encoded = package.to_bytes(EnvelopeConfig(format=format, zstd=compression))
42-
decoded = Package.from_bytes(encoded)
43-
assert decoded == package
42+
],
43+
)
44+
def test_envelope_binary(
45+
package: Package, compression: int | None, format: EnvelopeFormat
46+
):
47+
# Binary compression roundtrip
48+
encoded = package.to_bytes(EnvelopeConfig(format=format, zstd=compression))
49+
decoded = Package.from_bytes(encoded)
50+
assert decoded == package
51+
4452

53+
def test_envelope_text(package: Package):
4554
# String roundtrip
4655
encoded_str = package.to_str(EnvelopeConfig.TEXT)
4756
decoded = Package.from_str(encoded_str)

0 commit comments

Comments
 (0)