def test_register_conflicts(self): cs = ConstraintSet("provider") cs.register("foo") cs.register("bar") cs.register("baz") cs.register("qux") cs.parse(["foo=1", "bar=2", "baz=3", "qux=4"]) def assert_ambiguous(strs): e = self.assertRaises(ConstraintError, cs.parse, strs) self.assertTrue(str(e).startswith("Ambiguous constraints")) cs.register_conflicts(["foo"], ["bar", "baz", "qux"]) assert_ambiguous(["foo=1", "bar=2"]) assert_ambiguous(["foo=1", "baz=3"]) assert_ambiguous(["foo=1", "qux=4"]) cs.parse(["foo=1"]) cs.parse(["bar=2", "baz=3", "qux=4"]) cs.register_conflicts(["bar", "baz"], ["qux"]) assert_ambiguous(["bar=2", "qux=4"]) assert_ambiguous(["baz=3", "qux=4"]) cs.parse(["foo=1"]) cs.parse(["bar=2", "baz=3"]) cs.parse(["qux=4"])
def test_register_default_and_converter(self): cs = ConstraintSet("provider") cs.register("foo", default="star", converter=lambda s: "death-" + s) c1 = cs.parse([]) self.assertEquals(c1["foo"], "death-star") c1 = cs.parse(["foo=clock"]) self.assertEquals(c1["foo"], "death-clock")
def test_register_comparer(self): cs = ConstraintSet("provider") cs.register("foo", comparer=operator.ne) c1 = cs.parse(["foo=bar"]).with_series("series") c2 = cs.parse(["foo=bar"]).with_series("series") self.assertFalse(c1.can_satisfy(c2)) self.assertFalse(c2.can_satisfy(c1)) c3 = cs.parse(["foo=baz"]).with_series("series") self.assertTrue(c1.can_satisfy(c3)) self.assertTrue(c3.can_satisfy(c1))
def test_load_validates_known(self): cs = ConstraintSet("provider") cs.register("foo", converter=_raiser(ValueError)) e = self.assertRaises(ConstraintError, cs.load, {"foo": "bar"}) self.assertEquals(str(e), "Bad 'foo' constraint 'bar': bar")
def test_convert_wraps_ValueError(self): cs = ConstraintSet("provider") cs.register("foo", converter=_raiser(ValueError)) cs.register("bar", converter=_raiser(KeyError)) self.assertRaises(ConstraintError, cs.parse, ["foo=1"]) self.assertRaises(KeyError, cs.parse, ["bar=1"])
def test_register_invisible(self): cs = ConstraintSet("provider") cs.register("foo", visible=False) e = self.assertRaises(ConstraintError, cs.parse, ["foo=bar"]) self.assertEquals(str(e), "Cannot set computed constraint: 'foo'")
def test_unregistered_name(self): cs = ConstraintSet("provider") cs.register("bar") e = self.assertRaises(ConstraintError, cs.parse, ["bar=2", "baz=3"]) self.assertEquals(str(e), "Unknown constraint: 'baz'")