From 52b617cf38f87d1f483063f858f215a4394b1eb8 Mon Sep 17 00:00:00 2001
From: Valentin Lorentz <vlorentz@softwareheritage.org>
Date: Wed, 21 Aug 2019 17:40:01 +0200
Subject: [PATCH] Add support for dangling snapshot branches.

---
 swh/model/hypothesis_strategies.py            | 7 +++++--
 swh/model/model.py                            | 4 ++--
 swh/model/tests/test_hypothesis_strategies.py | 2 +-
 3 files changed, 8 insertions(+), 5 deletions(-)

diff --git a/swh/model/hypothesis_strategies.py b/swh/model/hypothesis_strategies.py
index 4393e9b2..3a205a3c 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 26a534d5..ee994cdf 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 0df1fee4..2182064c 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
-- 
GitLab