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")
import copy import os import sys import yaml from juju.lib.schema import (SchemaError, KeyDict, Dict, String, Constant, OneOf, Int, Float) from juju.charm.errors import (ServiceConfigError, ServiceConfigValueError) OPTION_SCHEMA = KeyDict( { "type": OneOf( Constant("string"), Constant("str"), # Obsolete Constant("int"), Constant("float")), "default": OneOf(String(), Int(), Float()), "description": String(), }, optional=["default", "description"], ) # Schema used to validate ConfigOptions specifications CONFIG_SCHEMA = KeyDict({ "options": Dict(String(), OPTION_SCHEMA), })
def test_one_of_bad(self): schema = OneOf(Constant(None), Unicode()) error = self.assertRaises(SchemaError, schema.coerce, "<obj>", PATH) # When no values are supported, raise the first error. self.assertEquals(str(error), "<path>: expected None, got '<obj>'")
import os import yaml from juju.charm.errors import MetaDataError from juju.errors import FileNotFound from juju.lib.schema import (SchemaError, Bool, Constant, Dict, Int, KeyDict, OneOf, UnicodeOrString) log = logging.getLogger("juju.charm") UTF8_SCHEMA = UnicodeOrString("utf-8") INTERFACE_SCHEMA = KeyDict({ "interface": UTF8_SCHEMA, "limit": OneOf(Constant(None), Int()), "optional": Bool() }) class InterfaceExpander(object): """Schema coercer that expands the interface shorthand notation. We need this class because our charm shorthand is difficult to work with (unfortunately). So we coerce shorthand and then store the desired format in ZK. Supports the following variants:: provides: server: riak
admin-secret: %(admin-secret)s default-series: oneiric """ SCHEMA = KeyDict({ "default": String(), "environments": Dict(String(), SelectDict("type", { "ec2": KeyDict({"control-bucket": String(), "admin-secret": String(), "access-key": String(), "secret-key": String(), "region": OneOf( Constant("us-east-1"), Constant("us-west-1"), Constant("us-west-2"), Constant("eu-west-1"), Constant("sa-east-1"), Constant("ap-northeast-1"), Constant("ap-southeast-1")), "default-instance-type": String(), "default-ami": String(), "ec2-uri": String(), "s3-uri": String(), "placement": OneOf( Constant("unassigned"), Constant("local")), "default-series": String()}, optional=["access-key", "secret-key", "default-instance-type", "default-ami", "region", "ec2-uri", "s3-uri", "placement"]), "orchestra": KeyDict({"orchestra-server": String(),