Skip to content

Commit

Permalink
refactor(tests): minor drying message consumer tests
Browse files Browse the repository at this point in the history
Signed-off-by: JP-Ellis <josh@jpellis.me>
  • Loading branch information
JP-Ellis committed Jun 20, 2024
1 parent aed2e09 commit 31b9ed4
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 64 deletions.
29 changes: 11 additions & 18 deletions tests/v3/compatibility_suite/test_v3_message_consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
import json
import logging
import re
from pathlib import Path
from typing import Any, List, NamedTuple
from typing import TYPE_CHECKING, Any, List, NamedTuple

from pytest_bdd import (
given,
Expand All @@ -17,12 +16,19 @@
when,
)

from pact.v3.pact import AsyncMessageInteraction, InteractionVerificationError, Pact
from tests.v3.compatibility_suite.util import (
FIXTURES_ROOT,
PactInteractionTuple,
parse_markdown_table,
)
from tests.v3.compatibility_suite.util.consumer import (
a_message_integration_is_being_defined_for_a_consumer_test,
)

if TYPE_CHECKING:
from pathlib import Path

from pact.v3.pact import AsyncMessageInteraction, InteractionVerificationError

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -122,20 +128,7 @@ def test_supports_the_use_of_generators_with_message_metadata() -> None:
################################################################################


@given(
"a message integration is being defined for a consumer test",
target_fixture="pact_interaction",
)
def a_message_integration_is_being_defined_for_a_consumer_test() -> (
PactInteractionTuple[AsyncMessageInteraction]
):
"""A message integration is being defined for a consumer test."""
pact = Pact("consumer", "provider")
pact.with_specification("V3")
return PactInteractionTuple(
pact,
pact.upon_receiving("an asynchronous message", "Async"),
)
a_message_integration_is_being_defined_for_a_consumer_test("V3")


@given(
Expand Down Expand Up @@ -321,7 +314,7 @@ def a_pact_file_for_the_message_interaction_will_maybe_have_been_written(
success: bool, # noqa: FBT001
) -> None:
"""A Pact file for the message interaction will maybe have been written."""
assert Path(temp_dir / "pacts" / "consumer-provider.json").exists() == success
assert (temp_dir / "pacts" / "consumer-provider.json").exists() == success


@then(parsers.re(r'the consumer test error will be "(?P<error>[^"]+)"'))
Expand Down
66 changes: 23 additions & 43 deletions tests/v3/compatibility_suite/test_v4_message_consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,23 @@
from __future__ import annotations

import json
from typing import TYPE_CHECKING, Any, Generator, NamedTuple
from typing import TYPE_CHECKING, Any

from pytest_bdd import (
given,
parsers,
scenario,
then,
when,
)

from pact.v3.pact import AsyncMessageInteraction, Pact
from tests.v3.compatibility_suite.util import string_to_int
from tests.v3.compatibility_suite.util import PactInteractionTuple, string_to_int
from tests.v3.compatibility_suite.util.consumer import (
a_message_integration_is_being_defined_for_a_consumer_test,
the_pact_file_for_the_test_is_generated,
)

if TYPE_CHECKING:
from pathlib import Path


class PactInteraction(NamedTuple):
"""Holder class for Pact and Interaction."""

pact: Pact
interaction: AsyncMessageInteraction
from pact.v3.pact import AsyncMessageInteraction


@scenario(
Expand All @@ -35,11 +30,6 @@ def test_sets_the_type_for_the_interaction() -> None:
"""Sets the type for the interaction."""


@scenario("definition/features/V4/message_consumer.feature", "Supports adding comments")
def test_supports_adding_comments() -> None:
"""Supports adding comments."""


@scenario(
"definition/features/V4/message_consumer.feature",
"Supports specifying a key for the interaction",
Expand All @@ -56,16 +46,27 @@ def test_supports_specifying_the_interaction_is_pending() -> None:
"""Supports specifying the interaction is pending."""


@scenario(
"definition/features/V4/message_consumer.feature",
"Supports adding comments",
)
def test_supports_adding_comments() -> None:
"""Supports adding comments."""


################################################################################
## Given
################################################################################

a_message_integration_is_being_defined_for_a_consumer_test("V4")


@given(
parsers.re(r'a comment "(?P<comment>[^"]+)" is added to the message interaction')
)
def a_comment_is_added_to_the_message_interaction(
pact_interaction: PactInteraction, comment: str
pact_interaction: PactInteractionTuple[AsyncMessageInteraction],
comment: str,
) -> None:
"""A comment "{comment}" is added to the message interaction."""
pact_interaction.interaction.add_text_comment(comment)
Expand All @@ -74,30 +75,17 @@ def a_comment_is_added_to_the_message_interaction(
@given(
parsers.re(r'a key of "(?P<key>[^"]+)" is specified for the message interaction')
)
def a_key_is_specified_for_the_http_interaction(
pact_interaction: PactInteraction,
def a_key_is_specified_for_the_message_interaction(
pact_interaction: PactInteractionTuple[AsyncMessageInteraction],
key: str,
) -> None:
"""A key is specified for the HTTP interaction."""
pact_interaction.interaction.set_key(key)


@given(
"a message interaction is being defined for a consumer test",
target_fixture="pact_interaction",
)
def a_message_interaction_is_being_defined_for_a_consumer_test() -> (
Generator[PactInteraction, Any, None]
):
"""A message integration is being defined for a consumer test."""
pact = Pact("consumer", "provider")
pact.with_specification("V4")
yield PactInteraction(pact, pact.upon_receiving("a request", "Async"))


@given("the message interaction is marked as pending")
def the_message_interaction_is_marked_as_pending(
pact_interaction: PactInteraction,
pact_interaction: PactInteractionTuple[AsyncMessageInteraction],
) -> None:
"""The message interaction is marked as pending."""
pact_interaction.interaction.set_pending(pending=True)
Expand All @@ -108,15 +96,7 @@ def the_message_interaction_is_marked_as_pending(
################################################################################


@when("the Pact file for the test is generated", target_fixture="pact_data")
def the_pact_file_for_the_test_is_generated(
pact_interaction: PactInteraction, temp_dir: Path
) -> Generator[Any, Any, None]:
"""The Pact file for the test is generated."""
(temp_dir / "pacts").mkdir(exist_ok=True, parents=True)
pact_interaction.pact.write_file(temp_dir / "pacts")
with (temp_dir / "pacts" / "consumer-provider.json").open() as file:
yield json.load(file)
the_pact_file_for_the_test_is_generated()


################################################################################
Expand Down
36 changes: 33 additions & 3 deletions tests/v3/compatibility_suite/util/consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

import pytest
import requests
from pytest_bdd import parsers, then, when
from pytest_bdd import given, parsers, then, when
from yarl import URL

from pact.v3 import Pact
Expand All @@ -27,11 +27,41 @@
from collections.abc import Generator
from pathlib import Path

from pact.v3.pact import HttpInteraction, PactServer
from pact.v3.interaction._async_message_interaction import AsyncMessageInteraction
from pact.v3.pact import PactServer
from tests.v3.compatibility_suite.util import InteractionDefinition

logger = logging.getLogger(__name__)

################################################################################
## Given
################################################################################


def a_message_integration_is_being_defined_for_a_consumer_test(
version: str,
stacklevel: int = 1,
) -> None:
@given(
parsers.re(
r"a message (integration|interaction) "
r"is being defined for a consumer test"
),
target_fixture="pact_interaction",
stacklevel=stacklevel + 1,
)
def _() -> PactInteractionTuple[AsyncMessageInteraction]:
"""
A message integration is being defined for a consumer test.
"""
pact = Pact("consumer", "provider")
pact.with_specification(version)
return PactInteractionTuple(
pact,
pact.upon_receiving("an asynchronous message", "Async"),
)


################################################################################
## When
################################################################################
Expand Down Expand Up @@ -202,7 +232,7 @@ def the_pact_file_for_the_test_is_generated(stacklevel: int = 1) -> None:
)
def _(
temp_dir: Path,
pact_interaction: PactInteractionTuple[HttpInteraction],
pact_interaction: PactInteractionTuple[Any],
) -> dict[str, Any]:
"""The Pact file for the test is generated."""
pact_interaction.pact.write_file(temp_dir)
Expand Down

0 comments on commit 31b9ed4

Please sign in to comment.