Skip to content

Commit

Permalink
Disable creating contracts with invalid spans
Browse files Browse the repository at this point in the history
  • Loading branch information
realsuayip committed Jan 19, 2024
1 parent 6bbf077 commit fced377
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pcontract/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ def init(
meta: dict[str, Any] | None = None,
) -> Contract:
initial_branch = Branch(start_at=start_at, end_at=end_at, data=data)
if (not initial_branch.span) or (zero > initial_branch.span):
raise ValueError("%s spans nothing." % initial_branch)
return cls(items=[initial_branch], meta=meta)

def branch(
Expand Down
15 changes: 15 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,3 +226,18 @@ def test_dataref(self):
self.assertEqual({"_ref": m1.uuid}, r2.data)

self.assertEqual({"key": "mars"}, m3.data)

def test_contract_init_spans_nothing(self):
with self.assertRaisesRegex(ValueError, r".spans nothing"):
Contract.init(
start_at=self.start,
end_at=self.start,
data={"key": "venus"},
)

with self.assertRaisesRegex(ValueError, r".spans nothing"):
Contract.init(
start_at=self.start,
end_at=self.start - datetime.timedelta(days=1),
data={"key": "venus"},
)

0 comments on commit fced377

Please sign in to comment.