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,45 +380,66 @@ ).map(Snapshot.from_dict) -def objects(blacklist_types=("origin_visit_status",)): +def objects(blacklist_types=("origin_visit_status",), split_content=False): """generates a random couple (type, obj) which obj is an instance of the Model class corresponding to obj_type. + + `blacklist_types` is a list of obj_type to exclude from the strategy. + + If `split_content` is True, generates Content and SkippedContent under different + obj_type, resp. "content" and "skipped_content". """ + strategies = [ + ("origin", origins), + ("origin_visit", origin_visits), + ("origin_visit_status", origin_visit_statuses), + ("snapshot", snapshots), + ("release", releases), + ("revision", revisions), + ("directory", directories), + ] + if split_content: + strategies.append(("content", present_contents)) + strategies.append(("skipped_content", skipped_contents)) + else: + strategies.append(("content", contents)) 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), - ) + for (obj_type, obj_gen) in strategies if obj_type not in blacklist_types ] return one_of(*args) -def object_dicts(blacklist_types=("origin_visit_status",)): +def object_dicts(blacklist_types=("origin_visit_status",), split_content=False): """generates a random couple (type, dict) which dict is suitable for .from_dict() factory methods. + + `blacklist_types` is a list of obj_type to exclude from the strategy. + + If `split_content` is True, generates Content and SkippedContent under different + obj_type, resp. "content" and "skipped_content". + """ + strategies = [ + ("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), + ] + if split_content: + strategies.append(("content", present_contents_d)) + strategies.append(("skipped_content", skipped_contents_d)) + else: + strategies.append(("content", contents_d)) 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), - ] + for (obj_type, obj_gen) in strategies 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 @@ -14,6 +14,16 @@ target_types = ("content", "directory", "revision", "release", "snapshot", "alias") +all_but_skipped_content = ( + "origin", + "origin_visit", + "origin_visit_status", + "snapshot", + "release", + "revision", + "directory", + "content", +) @given(objects(blacklist_types=())) @@ -22,6 +32,18 @@ attr.validate(object_) +@given(objects(split_content=False)) +def test_generation_merged_content(obj_type_and_obj): + # we should never generate a "skipped_content" here + assert obj_type_and_obj[0] != "skipped_content" + + +@given(objects(split_content=True, blacklist_types=all_but_skipped_content)) +def test_generation_split_content(obj_type_and_obj): + # we should only generate "skipped_content" + assert obj_type_and_obj[0] == "skipped_content" + + @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") @@ -64,6 +86,18 @@ assert branch is None or branch["target_type"] in target_types +@given(object_dicts(split_content=False)) +def test_dicts_generation_merged_content(obj_type_and_obj): + # we should never generate a "skipped_content" here + assert obj_type_and_obj[0] != "skipped_content" + + +@given(object_dicts(split_content=True, blacklist_types=all_but_skipped_content)) +def test_dicts_generation_split_content(obj_type_and_obj): + # we should only generate "skipped_content" + assert obj_type_and_obj[0] == "skipped_content" + + @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")