def test_dangling_foreign_key_returns_error(self, sess): cn = sut.FromModel(ChildA) actual = cn.check({"child_id": 1, "parent_id": 1, "name": "x"}, session=sess) assert actual == Error({"parent_id": Error("does-not-exist")})
def test_existing_foreign_key_passes(self, sess): cn = sut.FromModel(ChildA) sess.add(Parent(parent_id=1)) actual = cn.check({"child_id": 1, "parent_id": 1, "name": "x"}, session=sess) assert not actual
def test_duplicate_field_merges_with_other_errors(self, sess): cn = sut.FromModel(ChildA) sess.add(Parent(parent_id=1)) sess.add(ChildA(child_id=1, name=11 * "x", parent_id=1)) actual = cn.check({"child_id": 2, "parent_id": 1, "name": 11 * "x"}, session=sess) assert actual == Error({"name": Error("max-size", "duplicate")})
def test_duplicate_field_in_context_returns_error(self, sess): cn = sut.FromModel(ChildA) sess.add(Parent(parent_id=1)) sess.add(ChildA(name="x", parent_id=1)) actual = cn.check({"child_id": 314, "parent_id": 1, "name": "x"}, session=sess) assert actual == Error({"name": Error("duplicate")})
def test_ignore_correctly_applied_to_multi_path_constraints(self, sess): sess.add(Parent(parent_id=1)) sess.add(Parent(parent_id=2)) sess.add(ChildA(child_id=1, parent_id=1, name='A')) sess.add(ChildB(child_id=1, parent_id=2, name='B')) sess.commit() cn = sut.FromModel(GrandChild, ignore=set([Parent])) actual = cn.check({"parent_a_id": 1, "parent_b_id": 1}, session=sess) assert actual == Error({"name": Error("missing")})
def test_consider_foreign_key_dangling_when_out_of_context(self, sess): # TODO: refactor this test cn = sut.FromModel(ChildA) owner = Owner(owner_id=1) sess.add(owner) sess.add(Owner(owner_id=2)) sess.add(Parent(parent_id=1, owner_id=2)) actual = cn.check({"parent_id": 1, "name": "x"}, session=sess, within=owner) assert actual == Error({"parent_id": Error("does-not-exist")})
def test_multi_path_constraints_trigger(self, sess): # TODO: refactor this test sess.add(Parent(parent_id=1)) sess.add(Parent(parent_id=2)) sess.add(ChildA(child_id=1, parent_id=1, name='A')) sess.add(ChildB(child_id=1, parent_id=2, name='B')) sess.commit() cn = sut.FromModel(GrandChild) actual = cn.check({"parent_a_id": 1, "parent_b_id": 1}, session=sess) unwrapped = actual.unwrap() assert len(unwrapped) == 1 assert set(unwrapped[0].keys()[0]) == {"parent_a_id", "parent_b_id"} assert unwrapped[0].keys()[1] == "name" assert unwrapped[0].values() == [["mismatch (parent)"], ["missing"]]
def test_missing_non_nullable_returns_error(self, sess): cn = sut.FromModel(ChildA) actual = cn.check({"child_id": 1}, session=sess) assert actual == Error({"name": Error("missing")})