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_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_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_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_return_specified_error_on_predicate_failure(self): cn = sut.Generic("code", lambda x: x != 1) actual = cn.check(2) assert Error("code") == actual
def test_for_missing_keys(self): cn = sut.Dict(key=[]) actual = cn.check({}) assert Error({"key": Error("missing")}) == actual
def test_return_wrong_type_on_non_dict(self): cn = sut.Dict(key=[sut.InstanceOf(bool)]) actual = cn.check("test") assert Error("wrong-type") == actual
def test_non_trivial_result_when_input_of_wrong_type(self, inp, ty): actual = sut.InstanceOf(ty).check(inp) assert Error('wrong-type') == actual
def test_run_all_constraints_for_a_single_field(self): cn = sut.Dict(key=[sut.InstanceOf(bool), sut.InstanceOf(str)]) actual = cn.check({"key": 1}) assert Error({"key": Error("wrong-type", "wrong-type")}) == actual
def test_convert_list_of_atomic_errors_to_unwrap(self): errors = Error("1", "2", "3") assert errors.unwrap() == ["1", "2", "3"]
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")})
def test_merge_mixed_errors(self): errors = Error("1", {"u": Error("2")}) errors.merge(Error("3", {"v": Error("4")})) assert errors == Error("1", "3", {"u": Error("2"), "v": Error("4")})
def test_return_empty_list_on_predicate_success(self): cn = sut.Generic("code", lambda x: x != 1) actual = cn.check(1) assert Error("code") == actual
def test_convert_nested_errors_to_unwrap(self): errors = Error({"u": Error("1"), "v": Error("2")}) assert errors.unwrap() == [{"u": ["1"], "v": ["2"]}]
def test_convert_mixed_errors_to_unwrap(self): errors = Error("1", {"u": Error("2")}) assert errors.unwrap() == ["1", {"u": ["2"]}]
def test_merge_list_of_atomic_errors(self): errors = Error("1", "2", "3") errors.merge(Error("4", "5", "6")) assert errors == Error("1", "2", "3", "4", "5", "6")
def test_non_trivial_result_on_input_too_large(self): actual = sut.MaxSize(1).check("test") assert Error('max-size') == actual
def test_merge_nested_errors(self): errors = Error({"u": Error("1"), "v": Error("3")}) errors.merge(Error({"u": Error("2"), "w": Error("4")})) assert errors == Error({ "u": Error("1", "2"), "v": Error("3"), "w": Error("4") })