def test_required_inside_optional_dict_in_dict(self): spec = translate({"foo": optional({"a": 1, "b": optional(2)})}) data = {} expected = {"foo": None} assert merge_defaults(spec, data) == expected data = {"foo": None} expected = {"foo": None} assert merge_defaults(spec, data) == expected data = {"foo": {}} # XXX CHANGED: # expected = {'foo': {'a': 1, 'b': 2}} expected = {"foo": {"a": 1, "b": None}} assert merge_defaults(spec, data) == expected data = {"foo": {"a": 3}} # XXX CHANGED: # expected = {'foo': {'a': 3, 'b': 2}} expected = {"foo": {"a": 3, "b": None}} assert merge_defaults(spec, data) == expected data = {"foo": {"b": 3}} expected = {"foo": {"a": 1, "b": 3}} assert merge_defaults(spec, data) == expected
def test_empty(self): # (pre-v0.13 "missing value", now None != MISSING) validate({'a': optional(text_type)}, {'a': text_type('')}) with raises(ValidationError): validate({'a': text_type}, {'a': None}) validate({'a': optional(dict)}, {'a': {}}) with raises(ValidationError): validate({'a': dict}, {'a': None}) validate({'a': optional(list)}, {'a': []}) with raises(ValidationError): validate({'a': list}, {'a': None}) validate({'a': bool}, {'a': True}) validate({'a': bool}, {'a': False}) with raises(ValidationError): validate({'a': optional(bool)}, {'a': None}) with raises(ValidationError): validate({'a': bool}, {'a': None}) # (pre-v0.13 TypeError, now everything has ValidationError as base) with raises(ValidationError): validate({'a': text_type}, {'a': False}) with raises(ValidationError): validate({'a': text_type}, {'a': 0}) with raises(ValidationError): validate({'a': bool}, {'a': ''})
def test_required_inside_optional_dict_in_dict(self): spec = translate({ 'foo': optional({ 'a': 1, 'b': optional(2), }), }) data = {} expected = {'foo': None} assert merge_defaults(spec, data) == expected data = {'foo': None} expected = {'foo': None} assert merge_defaults(spec, data) == expected data = {'foo': {}} # XXX CHANGED: #expected = {'foo': {'a': 1, 'b': 2}} expected = {'foo': {'a': 1, 'b': None}} assert merge_defaults(spec, data) == expected data = {'foo': {'a': 3}} # XXX CHANGED: #expected = {'foo': {'a': 3, 'b': 2}} expected = {'foo': {'a': 3, 'b': None}} assert merge_defaults(spec, data) == expected data = {'foo': {'b': 3}} expected = {'foo': {'a': 1, 'b': 3}} assert merge_defaults(spec, data) == expected
def test_list_with_opt_elem(self): v = translate([optional(str)]) with raises_regexp(ValidationError, 'must be list'): v(None) v([]) with raises_regexp(ValidationError, 'must be str or must not exist'): v([None]) v(['hi']) with raises_regexp(ValidationError, 'must be str'): v([1234])
def test_validate_optional_one_of_in_a_list(self): # unset "optional" should work exactly as in a Rule schema = translate([IsA(str) | IsA(int)]) with raises(ValidationError): schema([]) schema = translate([optional(IsA(str) | IsA(int))]) schema([]) schema([123]) schema([123, 'sss']) with raises(ValidationError): schema([123, 'sss', 999.999])
def get_app_conf(): if __CACHE.get('app_conf'): return __CACHE['app_conf'] path = get_conf_path() if not os.path.exists(path): raise ConfigurationError('File {0} not found'.format(path)) defaults = { 'index': unicode, # e.g. ~/pim #'configs': {}, 'x_ignore': list, # TODO: remove this as soon as all is YAML? # from older cli.py 'flask': dict, 'x_flow': optional({ 'SOURCE_HTML_ROOT': unicode, 'SOURCE_RST_ROOT': unicode, 'SOURCE_TTLBOOKS_ROOT': unicode, 'SOURCE_YAML_ROOT': unicode, }), } # with open(path) as f: # conf = yaml.load(f) conf = caching.get_cached_yaml_file(path, defaults) # conf = manipulation.merged(defaults, conf) # try: # validate_structure(defaults, conf) # except ValidationError as e: # raise ConfigurationError('Configuration: {0}'.format(e)) expandable = ('index',) for k in expandable: conf[k] = os.path.expanduser(conf[k]) result = DotExpandedDict(conf) __CACHE['app_conf'] = result return result
def test_optional(self): assert optional(str) == IsA(str) | ~Exists() assert optional(IsA(str)) == IsA(str) | ~Exists() assert optional('foo') == IsA(str, default='foo') | ~Exists()
opt_key('domain_name'): { 'registrar': unicode, opt_key('username'): unicode, }, # Счета opt_key('account'): dict, # Мебель opt_key('furniture'): dict, # Одежда, обувь opt_key('clothes'): dict, # Музыкальные инструменты opt_key('music'): dict, # Услуги opt_key('service'): dict, opt_key('concerns'): [ optional(concern_schema) ], } project_schema = { 'name': unicode, 'state': unicode, opt_key('urls'): [unicode], opt_key('note'): unicode, opt_key('mail_label'): [unicode], 'concerns': [ optional(concern_schema) ], opt_key('stakeholders'): list, # hashtags opt_key('categories'): [unicode],