Skip to content

Commit bed2cf5

Browse files
committed
add direct draw tests for HypothesisWorks#3819; condense and narrow assertion logic
1 parent dc8b310 commit bed2cf5

File tree

1 file changed

+39
-17
lines changed

1 file changed

+39
-17
lines changed

hypothesis-python/tests/cover/test_sampled_from.py

+39-17
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,35 @@ def test_suggests_elements_instead_of_annotations():
195195
class TestErrorNoteBehavior3819:
196196
elements = (st.booleans(), st.decimals(), st.integers(), st.text())
197197

198+
@staticmethod
199+
@given(st.data())
200+
def direct_without_error(data):
201+
data.draw(st.sampled_from((st.floats(), st.binary())))
202+
203+
@staticmethod
204+
@given(st.data())
205+
def direct_with_non_type_error(data):
206+
data.draw(st.sampled_from(st.characters(), st.floats()))
207+
raise Exception("Contains SearchStrategy, but no note addition!")
208+
209+
@staticmethod
210+
@given(st.data())
211+
def direct_with_type_error_without_substring(data):
212+
data.draw(st.sampled_from(st.booleans(), st.binary()))
213+
raise TypeError("Substring not in message!")
214+
215+
@staticmethod
216+
@given(st.data())
217+
def direct_with_type_error_with_substring_but_not_all_strategies(data):
218+
data.draw(st.sampled_from(st.booleans(), False, True))
219+
raise TypeError("Contains SearchStrategy, but no note addition!")
220+
221+
@staticmethod
222+
@given(st.data())
223+
def direct_all_strategies_with_type_error_with_substring(data):
224+
data.draw(st.sampled_from((st.dates(), st.datetimes())))
225+
raise TypeError("This message contains SearchStrategy as substring!")
226+
198227
@staticmethod
199228
@given(st.lists(st.sampled_from(elements)))
200229
def indirect_without_error(_):
@@ -216,9 +245,9 @@ def indirect_with_type_error_with_substring_but_not_all_strategies(_):
216245
raise TypeError("Contains SearchStrategy, but no note addition!")
217246

218247
@staticmethod
219-
@given(st.lists(st.sampled_from(elements)))
220-
def indirect_all_strategies_with_type_error_with_substring(_):
221-
raise TypeError("Contains SearchStrategy in message")
248+
@given(st.lists(st.sampled_from(elements), min_size=1))
249+
def indirect_all_strategies_with_type_error_with_substring(objs):
250+
raise TypeError("Contains SearchStrategy in message, trigger note!")
222251

223252
@pytest.mark.parametrize(
224253
["func_to_call", "exp_err_cls", "should_exp_msg"],
@@ -227,35 +256,30 @@ def indirect_all_strategies_with_type_error_with_substring(_):
227256
for f, err, msg_exp in [
228257
(f, TypeError, True)
229258
for f in (
230-
args_with_type_error_with_message_substring,
231-
kwargs_with_type_error_with_message_substring,
259+
direct_all_strategies_with_type_error_with_substring,
232260
indirect_all_strategies_with_type_error_with_substring,
233261
)
234262
]
235263
+ [
236264
(f, TypeError, False)
237265
for f in (
238-
args_with_type_error_without_message_substring,
239-
kwargs_with_type_error_without_message_substring,
240-
type_error_but_not_all_strategies_args,
241-
type_error_but_not_all_strategies_kwargs,
266+
direct_with_type_error_without_substring,
267+
direct_with_type_error_with_substring_but_not_all_strategies,
242268
indirect_with_type_error_without_substring,
243269
indirect_with_type_error_with_substring_but_not_all_strategies,
244270
)
245271
]
246272
+ [
247273
(f, Exception, False)
248274
for f in (
249-
non_type_error_args,
250-
non_type_error_kwargs,
275+
direct_with_non_type_error,
251276
indirect_with_non_type_error,
252277
)
253278
]
254279
+ [
255280
(f, None, False)
256281
for f in (
257-
args_without_error,
258-
kwargs_without_error,
282+
direct_without_error,
259283
indirect_without_error,
260284
)
261285
]
@@ -272,11 +296,9 @@ def test_error_appropriate_error_note_3819(
272296
else:
273297
assert True
274298
else:
275-
with pytest.raises(Exception) as err_ctx:
299+
with pytest.raises(exp_err_cls) as err_ctx:
276300
func_to_call()
277-
err = err_ctx.value
278-
assert type(err) is exp_err_cls
279-
notes = getattr(err, "__notes__", [])
301+
notes = getattr(err_ctx.value, "__notes__", [])
280302
msg_in_notes = (
281303
"sample_from was given a collection of strategies; was one_of intended?"
282304
in notes

0 commit comments

Comments
 (0)