From 21a6c909a1da6f7fd33955c92f9406c0e1211830 Mon Sep 17 00:00:00 2001 From: Kostiantyn Goloveshko Date: Sat, 21 Sep 2024 20:54:52 +0300 Subject: [PATCH] Rework of test for minimal Feature --- .../message_samples/minimal/minimal.feature | 10 - .../minimal/minimal.feature.ts | 6 - python/tests/test_model_load.py | 216 ++++++++++++++++-- 3 files changed, 198 insertions(+), 34 deletions(-) delete mode 100644 python/tests/data/message_samples/minimal/minimal.feature delete mode 100644 python/tests/data/message_samples/minimal/minimal.feature.ts diff --git a/python/tests/data/message_samples/minimal/minimal.feature b/python/tests/data/message_samples/minimal/minimal.feature deleted file mode 100644 index 9bc37141..00000000 --- a/python/tests/data/message_samples/minimal/minimal.feature +++ /dev/null @@ -1,10 +0,0 @@ -Feature: minimal - - Cucumber doesn't execute this markdown, but @cucumber/react renders it - - * This is - * a bullet - * list - - Scenario: cukes - Given I have 42 cukes in my belly diff --git a/python/tests/data/message_samples/minimal/minimal.feature.ts b/python/tests/data/message_samples/minimal/minimal.feature.ts deleted file mode 100644 index 941b5ebf..00000000 --- a/python/tests/data/message_samples/minimal/minimal.feature.ts +++ /dev/null @@ -1,6 +0,0 @@ -import assert from 'assert' -import { Given } from '@cucumber/fake-cucumber' - -Given('I have {int} cukes in my belly', function (cukeCount: number) { - assert(cukeCount) -}) diff --git a/python/tests/test_model_load.py b/python/tests/test_model_load.py index 1c4fe31c..e1ded6e5 100644 --- a/python/tests/test_model_load.py +++ b/python/tests/test_model_load.py @@ -1,28 +1,208 @@ import json from pathlib import Path -from pytest import mark, param +from pytest import mark -from cucumber_messages import Envelope - -resource_path = Path(__file__).parent.absolute() +from cucumber_messages import ( + Ci, + Duration, + Envelope, + ExpressionType, + Feature, + FeatureChild, + GherkinDocument, + Git, + Group, + KeywordType, + Location, + MediaType, + Meta, + Pickle, + PickleStep, + Product, + Scenario, + Source, + SourceReference, + Status, + Step, + StepDefinition, + StepDefinitionPattern, + StepMatchArgument, + StepMatchArgumentsList, + TestCase, + TestCaseFinished, + TestCaseStarted, + TestRunFinished, + TestRunStarted, + TestStep, + TestStepFinished, + TestStepResult, + TestStepStarted, + Timestamp, + Type, +) +resource_path = Path(__file__).parent.absolute() / "data" -@mark.parametrize( - "ast_path", - map( - lambda file: param(file, id=file.name), # type: ignore[no-any-return] - resource_path.rglob("*.ndjson"), +with (resource_path / "message_samples/minimal/minimal.feature.ndjson").open(mode="r") as ast_file: + model_data = [*map(json.loads, ast_file)] +oracle_models = [ + Envelope( + meta=Meta( + protocol_version="22.0.0", + implementation=Product(name="fake-cucumber", version="16.3.0"), + runtime=Product(name="node.js", version="19.7.0"), + os=Product(name="darwin", version="22.4.0"), + cpu=Product(name="x64", version=None), + ci=Ci( + name="GitHub Actions", + url="https://github.com/cucumber-ltd/shouty.rb/actions/runs/154666429", + build_number="154666429", + git=Git( + remote="https://github.com/cucumber-ltd/shouty.rb.git", + revision="99684bcacf01d95875834d87903dcb072306c9ad", + tag=None, + ), + ), + ), + undefined_parameter_type=None, ), -) -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] + Envelope( + source=Source( + uri="samples/minimal/minimal.feature", + data="Feature: minimal\n \n Cucumber doesn't execute this markdown, but @cucumber/react renders it\n \n * This is\n * a bullet\n * list\n \n Scenario: cukes\n Given I have 42 cukes in my belly\n", + media_type=MediaType.text_x_cucumber_gherkin_plain, + ), + undefined_parameter_type=None, + ), + Envelope( + gherkin_document=GherkinDocument( + uri="samples/minimal/minimal.feature", + feature=Feature( + location=Location(line=1, column=1), + tags=[], + language="en", + keyword="Feature", + name="minimal", + description=" Cucumber doesn't execute this markdown, but @cucumber/react renders it\n \n * This is\n * a bullet\n * list", + children=[ + FeatureChild( + scenario=Scenario( + location=Location(line=9, column=3), + tags=[], + keyword="Scenario", + name="cukes", + description="", + steps=[ + Step( + location=Location(line=10, column=5), + keyword="Given ", + keyword_type=KeywordType.context, + text="I have 42 cukes in my belly", + id="1", + ) + ], + examples=[], + id="2", + ) + ) + ], + ), + comments=[], + ), + undefined_parameter_type=None, + ), + Envelope( + pickle=Pickle( + id="4", + uri="samples/minimal/minimal.feature", + name="cukes", + language="en", + steps=[PickleStep(ast_node_ids=["1"], id="3", type=Type.context, text="I have 42 cukes in my belly")], + tags=[], + ast_node_ids=["2"], + ), + undefined_parameter_type=None, + ), + Envelope( + step_definition=StepDefinition( + id="0", + pattern=StepDefinitionPattern( + source="I have {int} cukes in my belly", type=ExpressionType.cucumber_expression + ), + source_reference=SourceReference( + uri="samples/minimal/minimal.feature.ts", location=Location(line=4, column=None) + ), + ), + undefined_parameter_type=None, + ), + Envelope(test_run_started=TestRunStarted(timestamp=Timestamp(seconds=0, nanos=0)), undefined_parameter_type=None), + Envelope( + test_case=TestCase( + id="6", + pickle_id="4", + test_steps=[ + TestStep( + id="5", + pickle_step_id="3", + step_definition_ids=["0"], + step_match_arguments_lists=[ + StepMatchArgumentsList( + step_match_arguments=[ + StepMatchArgument( + group=Group(children=[], start=7, value="42"), parameter_type_name="int" + ) + ] + ) + ], + ) + ], + ), + undefined_parameter_type=None, + ), + Envelope( + test_case_started=TestCaseStarted( + attempt=0, id="7", test_case_id="6", timestamp=Timestamp(seconds=0, nanos=1000000) + ), + undefined_parameter_type=None, + ), + Envelope( + test_step_started=TestStepStarted( + test_case_started_id="7", test_step_id="5", timestamp=Timestamp(seconds=0, nanos=2000000) + ), + undefined_parameter_type=None, + ), + Envelope( + test_step_finished=TestStepFinished( + test_case_started_id="7", + test_step_id="5", + test_step_result=TestStepResult( + duration=Duration(seconds=0, nanos=1000000), status=Status.passed, exception=None + ), + timestamp=Timestamp(seconds=0, nanos=3000000), + ), + undefined_parameter_type=None, + ), + Envelope( + test_case_finished=TestCaseFinished( + test_case_started_id="7", timestamp=Timestamp(seconds=0, nanos=4000000), will_be_retried=False + ), + undefined_parameter_type=None, + ), + Envelope( + test_run_finished=TestRunFinished(success=True, timestamp=Timestamp(seconds=0, nanos=5000000), exception=None), + undefined_parameter_type=None, + ), +] + + +@mark.parametrize("model_datum, oracle_model", zip(model_data, oracle_models)) +def test_simple_load_model(model_datum, oracle_model): + model = Envelope.model_validate(model_datum) # type: ignore[attr-defined] - assert isinstance(model, Envelope) + assert isinstance(model, Envelope) + assert oracle_model == model - dumped_ast_datum = json.loads(model.model_dump_json(by_alias=True, exclude_none=True)) + dumped_ast_datum = json.loads(oracle_model.model_dump_json(by_alias=True, exclude_none=True)) - assert model_datum == dumped_ast_datum + assert model_datum == dumped_ast_datum