Skip to content
Snippets Groups Projects
Commit ebd38077 authored by David Douard's avatar David Douard
Browse files

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.
parent bfba3bdd
No related branches found
No related tags found
No related merge requests found
......@@ -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)
......@@ -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
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment