From ebd38077962687e0f962d1edab275f6d33b72312 Mon Sep 17 00:00:00 2001 From: David Douard <david.douard@sdfa3.org> Date: Tue, 21 Apr 2020 11:33:32 +0200 Subject: [PATCH] Add a blacklist_types argument to object_dicts() and objects() hypothesis strategies so one can choose not to generate some of the object types. Blacklist "origin_visit_status" by default to prevent breaking dependent packages' tests. --- swh/model/hypothesis_strategies.py | 58 ++++++++++++------- swh/model/tests/test_hypothesis_strategies.py | 14 ++++- 2 files changed, 48 insertions(+), 24 deletions(-) diff --git a/swh/model/hypothesis_strategies.py b/swh/model/hypothesis_strategies.py index 585d4233..fbc7f27f 100644 --- a/swh/model/hypothesis_strategies.py +++ b/swh/model/hypothesis_strategies.py @@ -380,31 +380,45 @@ def snapshots(*, min_size=0, max_size=100, only_objects=False): ).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 <ModelForType>.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 index b790f9a3..e5465ec3 100644 --- a/swh/model/tests/test_hypothesis_strategies.py +++ b/swh/model/tests/test_hypothesis_strategies.py @@ -16,12 +16,17 @@ from swh.model.model import TargetType 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(objects(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 @@ def assert_nested_dict(obj): 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 @@ def test_dicts_generation(obj_type_and_obj): 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 -- GitLab