diff --git a/swh/model/hypothesis_strategies.py b/swh/model/hypothesis_strategies.py index 4393e9b2b40b017104a0b5fad79295e6825b545d..3a205a3ca779def557ab2eb11b8d615b8e9d2c8e 100644 --- a/swh/model/hypothesis_strategies.py +++ b/swh/model/hypothesis_strategies.py @@ -180,7 +180,10 @@ def branch_targets(*, only_objects=False): def snapshots(draw, *, min_size=0, max_size=100, only_objects=False): branches = draw(dictionaries( keys=branch_names(), - values=branch_targets(only_objects=only_objects), + values=one_of( + none(), + branch_targets(only_objects=only_objects) + ), min_size=min_size, max_size=max_size, )) @@ -202,7 +205,7 @@ def snapshots(draw, *, min_size=0, max_size=100, only_objects=False): try: id_ = snapshot_identifier({ 'branches': { - name: branch.to_dict() + name: branch.to_dict() if branch else None for (name, branch) in branches.items()}}) except ValueError as e: for (source, target) in e.args[1]: diff --git a/swh/model/model.py b/swh/model/model.py index 26a534d53f7d21fd13f08404d8717f6f1c09a85e..ee994cdf3eb7b78ad3235541201b73f8cf43acf3 100644 --- a/swh/model/model.py +++ b/swh/model/model.py @@ -194,7 +194,7 @@ class Snapshot(BaseModel): return { 'id': self.id, 'branches': { - name: branch.to_dict() + name: branch.to_dict() if branch else None for (name, branch) in self.branches.items() } } @@ -204,7 +204,7 @@ class Snapshot(BaseModel): return cls( id=d['id'], branches={ - name: SnapshotBranch.from_dict(branch) + name: SnapshotBranch.from_dict(branch) if branch else None for (name, branch) in d['branches'].items() }) diff --git a/swh/model/tests/test_hypothesis_strategies.py b/swh/model/tests/test_hypothesis_strategies.py index 0df1fee479413ca453fb40092dd64a78d4b39407..2182064c3dbbf1745258b867762758c89ea20a65 100644 --- a/swh/model/tests/test_hypothesis_strategies.py +++ b/swh/model/tests/test_hypothesis_strategies.py @@ -59,4 +59,4 @@ def test_dicts_generation(obj_type_and_obj): assert object_['target_type'] in target_types elif obj_type == 'snapshot': for branch in object_['branches'].values(): - assert branch['target_type'] in target_types + assert branch is None or branch['target_type'] in target_types