Skip to content

Commit 36c7147

Browse files
alexchen-syslafrech
authored andcommitted
Factorize multiple OneOf/Equal intersection tests into parametrized tests
1 parent e916ed4 commit 36c7147

1 file changed

Lines changed: 66 additions & 57 deletions

File tree

‎tests/test_ext_marshmallow_field.py‎

Lines changed: 66 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -190,70 +190,79 @@ def test_only_allows_valid_properties_in_metadata(spec_fixture):
190190
assert "not_valid" not in res
191191

192192

193-
def test_field_with_choices_multiple(spec_fixture):
194-
field = fields.Str(
195-
validate=[
196-
validate.OneOf(["freddie", "brian", "john"]),
197-
validate.OneOf(["brian", "john", "roger"]),
198-
]
199-
)
200-
res = spec_fixture.openapi.field2property(field)
201-
assert res["enum"] == ["brian", "john"]
202-
203-
204-
def test_field_with_choices_multiple_non_intersecting(spec_fixture):
205-
field = fields.Str(
206-
validate=[
207-
validate.OneOf(["freddie", "brian"]),
208-
validate.OneOf(["john", "roger"]),
209-
]
210-
)
211-
res = spec_fixture.openapi.field2property(field)
212-
assert res["enum"] == []
213-
214-
215-
def test_field_with_choices_and_equal(spec_fixture):
216-
field = fields.Str(
217-
validate=[
218-
validate.OneOf(["freddie", "brian", "john"]),
219-
validate.Equal("brian"),
220-
]
221-
)
222-
res = spec_fixture.openapi.field2property(field)
223-
assert res["enum"] == ["brian"]
224-
225-
226-
def test_field_with_choices_and_equal_non_intersecting(spec_fixture):
227-
field = fields.Str(
228-
validate=[
229-
validate.OneOf(["freddie", "brian"]),
230-
validate.Equal("john"),
231-
]
232-
)
193+
@pytest.mark.parametrize(
194+
("validators", "expected_enum"),
195+
[
196+
(
197+
[
198+
validate.OneOf(["freddie", "brian", "john"]),
199+
validate.OneOf(["brian", "john", "roger"]),
200+
],
201+
{"brian", "john"},
202+
),
203+
(
204+
[
205+
validate.OneOf(["freddie", "brian"]),
206+
validate.OneOf(["john", "roger"]),
207+
],
208+
set(),
209+
),
210+
],
211+
)
212+
def test_field_with_multiple_oneof(spec_fixture, validators, expected_enum):
213+
field = fields.Str(validate=validators)
233214
res = spec_fixture.openapi.field2property(field)
234-
assert res["enum"] == []
215+
assert set(res["enum"]) == expected_enum
235216

236217

237-
def test_field_with_multiple_equal_matching(spec_fixture):
238-
field = fields.Str(
239-
validate=[
240-
validate.Equal("brian"),
241-
validate.Equal("brian"),
242-
]
243-
)
218+
@pytest.mark.parametrize(
219+
("validators", "expected_enum"),
220+
[
221+
(
222+
[
223+
validate.Equal("brian"),
224+
validate.Equal("brian"),
225+
],
226+
{"brian"},
227+
),
228+
(
229+
[
230+
validate.Equal("freddie"),
231+
validate.Equal("brian"),
232+
],
233+
set(),
234+
),
235+
],
236+
)
237+
def test_field_with_multiple_equal(spec_fixture, validators, expected_enum):
238+
field = fields.Str(validate=validators)
244239
res = spec_fixture.openapi.field2property(field)
245-
assert res["enum"] == ["brian"]
240+
assert set(res["enum"]) == expected_enum
246241

247242

248-
def test_field_with_multiple_equal_conflicting(spec_fixture):
249-
field = fields.Str(
250-
validate=[
251-
validate.Equal("freddie"),
252-
validate.Equal("brian"),
253-
]
254-
)
243+
@pytest.mark.parametrize(
244+
("validators", "expected_enum"),
245+
[
246+
(
247+
[
248+
validate.OneOf(["freddie", "brian", "john"]),
249+
validate.Equal("brian"),
250+
],
251+
{"brian"},
252+
),
253+
(
254+
[
255+
validate.OneOf(["freddie", "brian"]),
256+
validate.Equal("john"),
257+
],
258+
set(),
259+
),
260+
],
261+
)
262+
def test_field_with_oneof_and_equal(spec_fixture, validators, expected_enum):
263+
field = fields.Str(validate=validators)
255264
res = spec_fixture.openapi.field2property(field)
256-
assert res["enum"] == []
265+
assert set(res["enum"]) == expected_enum
257266

258267

259268
def test_field_with_additional_metadata(spec_fixture):

0 commit comments

Comments
 (0)