diff --git a/swh/model/hypothesis_strategies.py b/swh/model/hypothesis_strategies.py --- a/swh/model/hypothesis_strategies.py +++ b/swh/model/hypothesis_strategies.py @@ -380,31 +380,45 @@ ).map(Snapshot.from_dict) -def objects(): - return one_of( - origins().map(lambda x: ("origin", x)), - origin_visits().map(lambda x: ("origin_visit", x)), - origin_visit_statuses().map(lambda x: ("origin_visit_status", x)), - snapshots().map(lambda x: ("snapshot", x)), - releases().map(lambda x: ("release", x)), - revisions().map(lambda x: ("revision", x)), - directories().map(lambda x: ("directory", x)), - contents().map(lambda x: ("content", x)), - ) +def objects(blacklist_types=("origin_visit_status",)): + """generates a random couple (type, obj) + which obj is an instance of the Model class corresponding to obj_type. + """ + args = [ + obj_gen().map(lambda x, obj_type=obj_type: (obj_type, x)) + for (obj_type, obj_gen) in ( + ("origin", origins), + ("origin_visit", origin_visits), + ("origin_visit_status", origin_visit_statuses), + ("snapshot", snapshots), + ("release", releases), + ("revision", revisions), + ("directory", directories), + ("content", contents), + ) + if obj_type not in blacklist_types + ] + return one_of(*args) -def object_dicts(): + +def object_dicts(blacklist_types=("origin_visit_status",)): """generates a random couple (type, dict) which dict is suitable for .from_dict() factory methods. """ - return one_of( - origins_d().map(lambda x: ("origin", x)), - origin_visits_d().map(lambda x: ("origin_visit", x)), - origin_visit_statuses_d().map(lambda x: ("origin_visit_status", x)), - snapshots_d().map(lambda x: ("snapshot", x)), - releases_d().map(lambda x: ("release", x)), - revisions_d().map(lambda x: ("revision", x)), - directories_d().map(lambda x: ("directory", x)), - contents_d().map(lambda x: ("content", x)), - ) + args = [ + obj_gen().map(lambda x, obj_type=obj_type: (obj_type, x)) + for (obj_type, obj_gen) in [ + ("origin", origins_d), + ("origin_visit", origin_visits_d), + ("origin_visit_status", origin_visit_statuses_d), + ("snapshot", snapshots_d), + ("release", releases_d), + ("revision", revisions_d), + ("directory", directories_d), + ("content", contents_d), + ] + if obj_type not in blacklist_types + ] + return one_of(*args) diff --git a/swh/model/tests/test_hypothesis_strategies.py b/swh/model/tests/test_hypothesis_strategies.py --- a/swh/model/tests/test_hypothesis_strategies.py +++ b/swh/model/tests/test_hypothesis_strategies.py @@ -16,12 +16,17 @@ target_types = ("content", "directory", "revision", "release", "snapshot", "alias") -@given(objects()) +@given(objects(blacklist_types=())) def test_generation(obj_type_and_obj): (obj_type, object_) = obj_type_and_obj attr.validate(object_) +@given(object_dicts(blacklist_types=("origin_visit", "directory"))) +def test_generation_blacklist(obj_type_and_obj): + assert obj_type_and_obj[0] not in ("origin_visit", "directory") + + def assert_nested_dict(obj): """Tests the object is a nested dict and contains no more class from swh.model.model.""" @@ -38,7 +43,7 @@ assert False, obj -@given(object_dicts()) +@given(object_dicts(blacklist_types=())) def test_dicts_generation(obj_type_and_obj): (obj_type, object_) = obj_type_and_obj assert_nested_dict(object_) @@ -59,6 +64,11 @@ assert branch is None or branch["target_type"] in target_types +@given(object_dicts(blacklist_types=("release", "content"))) +def test_dicts_generation_blacklist(obj_type_and_obj): + assert obj_type_and_obj[0] not in ("release", "content") + + @given(objects()) def test_model_to_dicts(obj_type_and_obj): (obj_type, object_) = obj_type_and_obj