|
| 1 | +"""Basic HTTP consumer feature tests.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import json |
| 6 | +import logging |
| 7 | +from typing import Any, Generator |
| 8 | + |
| 9 | +from pytest_bdd import given, parsers, scenario, then |
| 10 | + |
| 11 | +from pact.v3.pact import HttpInteraction, Pact |
| 12 | +from tests.v3.compatiblity_suite.util import string_to_int |
| 13 | +from tests.v3.compatiblity_suite.util.consumer import ( |
| 14 | + the_pact_file_for_the_test_is_generated, |
| 15 | +) |
| 16 | + |
| 17 | +logger = logging.getLogger(__name__) |
| 18 | + |
| 19 | +################################################################################ |
| 20 | +## Scenario |
| 21 | +################################################################################ |
| 22 | + |
| 23 | + |
| 24 | +@scenario( |
| 25 | + "definition/features/V4/http_consumer.feature", |
| 26 | + "Sets the type for the interaction", |
| 27 | +) |
| 28 | +def test_sets_the_type_for_the_interaction() -> None: |
| 29 | + """Sets the type for the interaction.""" |
| 30 | + |
| 31 | + |
| 32 | +@scenario( |
| 33 | + "definition/features/V4/http_consumer.feature", |
| 34 | + "Supports specifying a key for the interaction", |
| 35 | +) |
| 36 | +def test_supports_specifying_a_key_for_the_interaction() -> None: |
| 37 | + """Supports specifying a key for the interaction.""" |
| 38 | + |
| 39 | + |
| 40 | +@scenario( |
| 41 | + "definition/features/V4/http_consumer.feature", |
| 42 | + "Supports specifying the interaction is pending", |
| 43 | +) |
| 44 | +def test_supports_specifying_the_interaction_is_pending() -> None: |
| 45 | + """Supports specifying the interaction is pending.""" |
| 46 | + |
| 47 | + |
| 48 | +@scenario( |
| 49 | + "definition/features/V4/http_consumer.feature", |
| 50 | + "Supports adding comments", |
| 51 | +) |
| 52 | +def test_supports_adding_comments() -> None: |
| 53 | + """Supports adding comments.""" |
| 54 | + |
| 55 | + |
| 56 | +################################################################################ |
| 57 | +## Given |
| 58 | +################################################################################ |
| 59 | + |
| 60 | + |
| 61 | +@given( |
| 62 | + "an HTTP interaction is being defined for a consumer test", |
| 63 | + target_fixture="pact_interaction", |
| 64 | +) |
| 65 | +def an_http_interaction_is_being_defined_for_a_consumer_test() -> ( |
| 66 | + Generator[tuple[Pact, HttpInteraction], Any, None] |
| 67 | +): |
| 68 | + """An HTTP interaction is being defined for a consumer test.""" |
| 69 | + pact = Pact("consumer", "provider") |
| 70 | + pact.with_specification("V4") |
| 71 | + yield (pact, pact.upon_receiving("a request")) |
| 72 | + |
| 73 | + |
| 74 | +@given(parsers.re(r'a key of "(?P<key>[^"]+)" is specified for the HTTP interaction')) |
| 75 | +def a_key_is_specified_for_the_http_interaction( |
| 76 | + pact_interaction: tuple[Pact, HttpInteraction], |
| 77 | + key: str, |
| 78 | +) -> None: |
| 79 | + """A key is specified for the HTTP interaction.""" |
| 80 | + _, interaction = pact_interaction |
| 81 | + interaction.set_key(key) |
| 82 | + |
| 83 | + |
| 84 | +@given("the HTTP interaction is marked as pending") |
| 85 | +def the_http_interaction_is_marked_as_pending( |
| 86 | + pact_interaction: tuple[Pact, HttpInteraction], |
| 87 | +) -> None: |
| 88 | + """The HTTP interaction is marked as pending.""" |
| 89 | + _, interaction = pact_interaction |
| 90 | + interaction.set_pending(pending=True) |
| 91 | + |
| 92 | + |
| 93 | +@given(parsers.re(r'a comment "(?P<comment>[^"]+)" is added to the HTTP interaction')) |
| 94 | +def a_comment_is_added_to_the_http_interaction( |
| 95 | + pact_interaction: tuple[Pact, HttpInteraction], |
| 96 | + comment: str, |
| 97 | +) -> None: |
| 98 | + """A comment of "<comment>" is added to the HTTP interaction.""" |
| 99 | + _, interaction = pact_interaction |
| 100 | + interaction.set_comment("text", [comment]) |
| 101 | + |
| 102 | + |
| 103 | +################################################################################ |
| 104 | +## When |
| 105 | +################################################################################ |
| 106 | + |
| 107 | + |
| 108 | +the_pact_file_for_the_test_is_generated() |
| 109 | + |
| 110 | + |
| 111 | +################################################################################ |
| 112 | +## Then |
| 113 | +################################################################################ |
| 114 | + |
| 115 | + |
| 116 | +@then( |
| 117 | + parsers.re( |
| 118 | + r"the (?P<num>[^ ]+) interaction in the Pact file" |
| 119 | + r' will have a type of "(?P<interaction_type>[^"]+)"' |
| 120 | + ), |
| 121 | + converters={"num": string_to_int}, |
| 122 | +) |
| 123 | +def the_interaction_in_the_pact_file_will_container_provider_states( |
| 124 | + pact_data: dict[str, Any], |
| 125 | + num: int, |
| 126 | + interaction_type: str, |
| 127 | +) -> None: |
| 128 | + """The interaction in the Pact file will container provider states.""" |
| 129 | + assert "interactions" in pact_data |
| 130 | + assert len(pact_data["interactions"]) >= num |
| 131 | + interaction = pact_data["interactions"][num - 1] |
| 132 | + assert interaction["type"] == interaction_type |
| 133 | + |
| 134 | + |
| 135 | +@then( |
| 136 | + parsers.re( |
| 137 | + r"the (?P<num>[^ ]+) interaction in the Pact file" |
| 138 | + r" will have \"(?P<key>[^\"]+)\" = '(?P<value>[^']+)'" |
| 139 | + ), |
| 140 | + converters={"num": string_to_int}, |
| 141 | +) |
| 142 | +def the_interaction_in_the_pact_file_will_have_a_key_of( |
| 143 | + pact_data: dict[str, Any], |
| 144 | + num: int, |
| 145 | + key: str, |
| 146 | + value: str, |
| 147 | +) -> None: |
| 148 | + """The interaction in the Pact file will have a key of value.""" |
| 149 | + assert "interactions" in pact_data |
| 150 | + assert len(pact_data["interactions"]) >= num |
| 151 | + interaction = pact_data["interactions"][num - 1] |
| 152 | + assert key in interaction |
| 153 | + value = json.loads(value) |
| 154 | + if isinstance(value, list): |
| 155 | + assert interaction[key] in value |
| 156 | + else: |
| 157 | + assert interaction[key] == value |
0 commit comments