def test_best_error(self): """ OneOf attempts to select a relevant error to report to user when no branch of its schema can be satisitifed. """ schema = OneOf(Unicode(), KeyDict({"flavor": String()}) ) # an error related to flavor is more specific and useful # than one related to the 1st branch error = self.assertRaises(SchemaExpectationError, schema.coerce, {"flavor": None}, PATH) # the correct error is returned self.assertEquals(str(error), "<path>.flavor: expected string, got None") # here the error related to Unicode is better error = self.assertRaises(SchemaExpectationError, schema.coerce, "a string", PATH) self.assertEquals(str(error), "<path>: expected unicode, got 'a string'") # and the success case still functions self.assertEqual(schema.coerce(u"some unicode", PATH), u"some unicode")
def test_best_error(self): """ OneOf attempts to select a relevant error to report to user when no branch of its schema can be satisitifed. """ schema = OneOf(Unicode(), KeyDict({"flavor": String()})) # an error related to flavor is more specific and useful # than one related to the 1st branch error = self.assertRaises(SchemaExpectationError, schema.coerce, {"flavor": None}, PATH) # the correct error is returned self.assertEquals(str(error), "<path>.flavor: expected string, got None") # here the error related to Unicode is better error = self.assertRaises(SchemaExpectationError, schema.coerce, "a string", PATH) self.assertEquals(str(error), "<path>: expected unicode, got 'a string'") # and the success case still functions self.assertEqual(schema.coerce(u"some unicode", PATH), u"some unicode")
def test_one_of(self): schema = OneOf(Constant(None), Unicode()) self.assertEquals(schema.coerce(None, PATH), None) self.assertEquals(schema.coerce(u"foo", PATH), u"foo")