def test_select_dict(self): schema = SelectDict("name", { "foo": KeyDict({"value": Int()}), "bar": KeyDict({"value": String()}) }) self.assertEquals(schema.coerce({ "name": "foo", "value": 1 }, PATH), { "name": "foo", "value": 1 }) self.assertEquals(schema.coerce({ "name": "bar", "value": "one" }, PATH), { "name": "bar", "value": "one" })
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")
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), }) WARNED_STR_IS_OBSOLETE = False class ConfigOptions(object):
def test_dict(self): self.assertEquals( Dict(Int(), String()).coerce({32: "hello."}, PATH), {32: "hello."})
def test_string_bad_int(self): error = self.assertRaises(SchemaError, String().coerce, 1, PATH) self.assertEquals(str(error), "<path>: expected string, got 1")
def test_string_bad_unicode(self): error = self.assertRaises(SchemaError, String().coerce, u"foo", PATH) self.assertEquals(str(error), "<path>: expected string, got u'foo'")
def test_string(self): self.assertEquals(String().coerce("foo", PATH), "foo")
DEFAULT_CONFIG_PATH = "~/.juju/environments.yaml" SAMPLE_CONFIG = """\ environments: sample: type: ec2 control-bucket: %(control-bucket)s 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(),