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