Skip to content

Commit

Permalink
refactor(tests): move InteractionDefinition in own module
Browse files Browse the repository at this point in the history
The `__init__` was getting a bit too cluttered, so I have refactored the
entire `InteractionDefinition` class into its own module. I have also
refacted the nested classes to stand alone now.

A minor refactor was also made to avoid assigning custom attributes to
the `Flask` app, and some changes were made to the provider
initialisation to avoid passing a redundant `self.app` in a method call.

Signed-off-by: JP-Ellis <[email protected]>
  • Loading branch information
JP-Ellis committed Jul 17, 2024
1 parent 5381b6a commit c7f6667
Show file tree
Hide file tree
Showing 11 changed files with 822 additions and 785 deletions.
10 changes: 5 additions & 5 deletions tests/v3/compatibility_suite/test_v1_consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@

from pytest_bdd import given, parsers, scenario

from tests.v3.compatibility_suite.util import (
InteractionDefinition,
parse_markdown_table,
)
from tests.v3.compatibility_suite.util import parse_markdown_table
from tests.v3.compatibility_suite.util.consumer import (
a_response_is_returned,
request_n_is_made_to_the_mock_server,
Expand All @@ -34,6 +31,9 @@
the_pact_test_is_done,
the_payload_will_contain_the_json_document,
)
from tests.v3.compatibility_suite.util.interaction_definition import (
InteractionDefinition,
)

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -320,7 +320,7 @@ def the_following_http_interactions_have_been_defined(
# Parse the table into a more useful format
interactions: dict[int, InteractionDefinition] = {}
for row in content:
interactions[int(row["No"])] = InteractionDefinition(**row)
interactions[int(row["No"])] = InteractionDefinition(**row) # type: ignore[arg-type]
return interactions


Expand Down
6 changes: 3 additions & 3 deletions tests/v3/compatibility_suite/test_v1_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
import pytest
from pytest_bdd import given, parsers, scenario

from tests.v3.compatibility_suite.util import (
from tests.v3.compatibility_suite.util import parse_markdown_table
from tests.v3.compatibility_suite.util.interaction_definition import (
InteractionDefinition,
parse_markdown_table,
)
from tests.v3.compatibility_suite.util.provider import (
a_failed_verification_result_will_be_published_back,
Expand Down Expand Up @@ -407,7 +407,7 @@ def the_following_http_interactions_have_been_defined(
# Parse the table into a more useful format
interactions: dict[int, InteractionDefinition] = {}
for row in content:
interactions[int(row["No"])] = InteractionDefinition(**row)
interactions[int(row["No"])] = InteractionDefinition(**row) # type: ignore[arg-type]
return interactions


Expand Down
10 changes: 5 additions & 5 deletions tests/v3/compatibility_suite/test_v2_consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@

from pytest_bdd import given, parsers, scenario

from tests.v3.compatibility_suite.util import (
InteractionDefinition,
parse_markdown_table,
)
from tests.v3.compatibility_suite.util import parse_markdown_table
from tests.v3.compatibility_suite.util.consumer import (
a_response_is_returned,
request_n_is_made_to_the_mock_server,
Expand All @@ -35,6 +32,9 @@
the_pact_test_is_done,
the_payload_will_contain_the_json_document,
)
from tests.v3.compatibility_suite.util.interaction_definition import (
InteractionDefinition,
)

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -196,7 +196,7 @@ def the_following_http_interactions_have_been_defined(
# Parse the table into a more useful format
interactions: dict[int, InteractionDefinition] = {}
for row in table:
interactions[int(row["No"])] = InteractionDefinition(**row)
interactions[int(row["No"])] = InteractionDefinition(**row) # type: ignore[arg-type]

return interactions

Expand Down
6 changes: 3 additions & 3 deletions tests/v3/compatibility_suite/test_v2_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
import pytest
from pytest_bdd import given, parsers, scenario

from tests.v3.compatibility_suite.util import (
from tests.v3.compatibility_suite.util import parse_markdown_table
from tests.v3.compatibility_suite.util.interaction_definition import (
InteractionDefinition,
parse_markdown_table,
)
from tests.v3.compatibility_suite.util.provider import (
a_pact_file_for_interaction_is_to_be_verified,
Expand Down Expand Up @@ -125,7 +125,7 @@ def the_following_http_interactions_have_been_defined(
# Parse the table into a more useful format
interactions: dict[int, InteractionDefinition] = {}
for row in content:
interactions[int(row["No"])] = InteractionDefinition(**row)
interactions[int(row["No"])] = InteractionDefinition(**row) # type: ignore[arg-type]
return interactions


Expand Down
13 changes: 7 additions & 6 deletions tests/v3/compatibility_suite/test_v3_message_producer.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@

from pact.v3.pact import Pact
from tests.v3.compatibility_suite.util import (
InteractionDefinition,
parse_horizontal_markdown_table,
parse_markdown_table,
)
from tests.v3.compatibility_suite.util.interaction_definition import (
InteractionDefinition,
InteractionState,
)
from tests.v3.compatibility_suite.util.provider import (
a_provider_is_started_that_can_generate_the_message,
a_provider_state_callback_is_configured,
Expand Down Expand Up @@ -229,7 +232,7 @@ def a_pact_file_for_is_to_be_verified_with_the_following(
interaction_definition = InteractionDefinition(
type="Async",
description=name,
**table,
**table, # type: ignore[arg-type]
)
interaction_definition.add_to_pact(pact, name)
(temp_dir / "pacts").mkdir(exist_ok=True, parents=True)
Expand Down Expand Up @@ -284,16 +287,14 @@ def a_pact_file_for_is_to_be_verified_with_provider_state(
description=name,
body=fixture,
)
interaction_definition.states = [InteractionDefinition.State(provider_state)]
interaction_definition.states = [InteractionState(provider_state)]
interaction_definition.add_to_pact(pact, name)
(temp_dir / "pacts").mkdir(exist_ok=True, parents=True)
pact.write_file(temp_dir / "pacts")
verifier.add_source(temp_dir / "pacts")
with (temp_dir / "provider_states").open("w") as f:
logger.debug("Writing provider state to %s", temp_dir / "provider_states")
json.dump(
[s.as_dict() for s in [InteractionDefinition.State(provider_state)]], f
)
json.dump([s.as_dict() for s in [InteractionState(provider_state)]], f)


@given(
Expand Down
6 changes: 3 additions & 3 deletions tests/v3/compatibility_suite/test_v3_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
import pytest
from pytest_bdd import given, parsers, scenario

from tests.v3.compatibility_suite.util import (
from tests.v3.compatibility_suite.util import parse_markdown_table
from tests.v3.compatibility_suite.util.interaction_definition import (
InteractionDefinition,
parse_markdown_table,
)
from tests.v3.compatibility_suite.util.provider import (
a_pact_file_for_interaction_is_to_be_verified_with_a_provider_states_defined,
Expand Down Expand Up @@ -100,7 +100,7 @@ def the_following_http_interactions_have_been_defined(
# Parse the table into a more useful format
interactions: dict[int, InteractionDefinition] = {}
for row in content:
interactions[int(row["No"])] = InteractionDefinition(**row)
interactions[int(row["No"])] = InteractionDefinition(**row) # type: ignore[arg-type]
return interactions


Expand Down
6 changes: 3 additions & 3 deletions tests/v3/compatibility_suite/test_v4_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
import pytest
from pytest_bdd import given, parsers, scenario

from tests.v3.compatibility_suite.util import (
from tests.v3.compatibility_suite.util import parse_markdown_table
from tests.v3.compatibility_suite.util.interaction_definition import (
InteractionDefinition,
parse_markdown_table,
)
from tests.v3.compatibility_suite.util.provider import (
a_pact_file_for_interaction_is_to_be_verified,
Expand Down Expand Up @@ -104,7 +104,7 @@ def the_following_http_interactions_have_been_defined(
# Parse the table into a more useful format
interactions: dict[int, InteractionDefinition] = {}
for row in content:
interactions[int(row["No"])] = InteractionDefinition(**row)
interactions[int(row["No"])] = InteractionDefinition(**row) # type: ignore[arg-type]
return interactions


Expand Down
Loading

0 comments on commit c7f6667

Please sign in to comment.