Skip to content

Commit

Permalink
Use CCK examples load on test
Browse files Browse the repository at this point in the history
  • Loading branch information
elchupanebrej committed Dec 8, 2024
1 parent 3d5ec13 commit 5408f1c
Showing 1 changed file with 42 additions and 18 deletions.
60 changes: 42 additions & 18 deletions python/tests/test_model_load.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,52 @@
import json
from itertools import chain
import re
from pathlib import Path

from importlib_resources import files
from pytest import mark, param
from git import Repo
from packaging import version
from pytest import fixture

from cucumber_messages import Envelope


@mark.parametrize(
"ast_path",
map(
lambda file: param(file, id=file.name), # type: ignore[no-any-return]
chain.from_iterable(map(lambda p: p.glob("*.ndjson"), files("message_samples").iterdir())),
),
)
def test_simple_load_model(ast_path: Path):
with ast_path.open(mode="r") as ast_file:
for ast_line in ast_file:
model_datum = json.loads(ast_line)
model = Envelope.model_validate(model_datum) # type: ignore[attr-defined]
@fixture
def compatibility_kit_repo(tmpdir):
repo_path = Path(tmpdir) / "compatibility-kit"
repo = Repo.clone_from(
"https://github.com/cucumber/compatibility-kit.git",
str(repo_path),
branch="main",
)
repo_tags = list(filter(lambda tag: tag is not None, map(lambda tag: getattr(tag.tag, "tag", None), repo.tags)))

assert isinstance(model, Envelope)
version_pattern = re.compile(r"((.*/)?)v(\d+\.\d+\.\d+)")
last_version = sorted(
map(
version.parse,
map(
lambda match: match.groups()[-1],
filter(lambda match: match is not None, map(lambda tag: re.match(version_pattern, tag), repo_tags)),
),
)
)[-1]

dumped_ast_datum = json.loads(model.model_dump_json(by_alias=True, exclude_none=True)) # type: ignore[attr-defined] # migration to pydantic2
last_version_tag = next(filter(lambda tag: re.search(re.escape(str(last_version)), tag), repo_tags))

assert model_datum == dumped_ast_datum
repo.git.checkout(last_version_tag)

return repo_path


def test_simple_load_model(compatibility_kit_repo):
for ast_path in (compatibility_kit_repo / "devkit" / "samples").rglob("*.ndjson"):
print(f"Checking ${ast_path}")
with ast_path.open(mode="r") as ast_file:
for ast_line in ast_file:
model_datum = json.loads(ast_line)
model = Envelope.model_validate(model_datum) # type: ignore[attr-defined]

assert isinstance(model, Envelope)

dumped_ast_datum = json.loads(model.model_dump_json(by_alias=True, exclude_none=True)) # type: ignore[attr-defined] # migration to pydantic2

assert model_datum == dumped_ast_datum

0 comments on commit 5408f1c

Please sign in to comment.