@@ -67,7 +67,6 @@ def definition_schema():
6767 [
6868 cs .typed_dict_schema (
6969 {
70- 'name' : cs .typed_dict_field (cs .str_schema ()),
7170 'sub_branch' : cs .typed_dict_field (
7271 cs .with_default_schema (
7372 cs .nullable_schema (cs .definition_reference_schema ('Branch' )), default = None
@@ -82,11 +81,10 @@ def definition_schema():
8281
8382
8483def test_definition_simple (definition_schema ):
85- assert definition_schema .validate_python ({'name' : 'root' }) == {'name' : 'root' , 'sub_branch' : None }
84+ assert definition_schema .validate_python ({}) == {'sub_branch' : None }
8685
8786
8887class BranchModel (TypedDict ):
89- name : str
9088 sub_branch : Optional ['BranchModel' ]
9189
9290
@@ -97,34 +95,30 @@ def test_recursive(definition_schema, data):
9795 assert definition_schema .validate_python (data ) == data
9896
9997
100- @strategies .composite
101- def branch_models_with_cycles (draw , existing = None ):
102- if existing is None :
103- existing = []
104- model = BranchModel (name = draw (strategies .text ()), sub_branch = None )
105- existing .append (model )
106- model ['sub_branch' ] = draw (
107- strategies .none ()
108- | strategies .builds (BranchModel , name = strategies .text (), sub_branch = branch_models_with_cycles (existing ))
109- | strategies .sampled_from (existing )
110- )
111- return model
98+ @given (strategies .integers (min_value = 0 , max_value = 10 ))
99+ @pytest .mark .thread_unsafe # https://github.com/Quansight-Labs/pytest-run-parallel/issues/20
100+ def test_definition_cycles (definition_schema , depth ):
101+ data = BranchModel (sub_branch = None )
102+ model = data
112103
104+ for _ in range (depth ):
105+ next_model = BranchModel (sub_branch = None )
106+ model ['sub_branch' ] = next_model
107+ model = next_model
113108
114- @given (branch_models_with_cycles ())
115- @pytest .mark .thread_unsafe # https://github.com/Quansight-Labs/pytest-run-parallel/issues/20
116- def test_definition_cycles (definition_schema , data ):
117- try :
118- assert definition_schema .validate_python (data ) == data
119- except ValidationError as exc :
120- assert exc .errors (include_url = False ) == [
121- {
122- 'type' : 'recursion_loop' ,
123- 'loc' : IsTuple (length = (1 , None )),
124- 'msg' : 'Recursion error - cyclic reference detected' ,
125- 'input' : AnyThing (),
126- }
127- ]
109+ model ['sub_branch' ] = data
110+
111+ with pytest .raises (ValidationError ) as exc_info :
112+ definition_schema .validate_python (data )
113+
114+ assert exc_info .value .errors (include_url = False ) == [
115+ {
116+ 'type' : 'recursion_loop' ,
117+ 'loc' : IsTuple (length = (1 , None )),
118+ 'msg' : 'Recursion error - cyclic reference detected' ,
119+ 'input' : AnyThing (),
120+ }
121+ ]
128122
129123
130124def test_definition_broken (definition_schema ):
0 commit comments