Beispiel #1
0
 def test_valid_document(self):
     "a complex document"
     spec = {
         'text': text_type,
         'tags': [text_type],
         'views': int,
         'comments': [
             {
                 'time': datetime.datetime,
                 'text': text_type,
             },
         ]
     }
     data = {
         'text': text_type('Hello world!'),
         'tags': [text_type('hello'), text_type('world')],
         'views': 2,
         'comments': [
             {
                 'time': datetime.datetime(2000,1,1),
                 'text': text_type('Is there anybody out there?')
             },
             {
                 'time': datetime.datetime.utcnow(),
                 'text': text_type('Yes, I am, why?')
             },
         ],
     }
     validate(spec, data)
Beispiel #2
0
 def test_valid_document(self):
     "a complex document"
     spec = {
         'text': text_type,
         'tags': [text_type],
         'views': int,
         'comments': [
             {
                 'time': datetime.datetime,
                 'text': text_type,
             },
         ]
     }
     data = {
         'text':
         text_type('Hello world!'),
         'tags': [text_type('hello'),
                  text_type('world')],
         'views':
         2,
         'comments': [
             {
                 'time': datetime.datetime(2000, 1, 1),
                 'text': text_type('Is there anybody out there?')
             },
             {
                 'time': datetime.datetime.utcnow(),
                 'text': text_type('Yes, I am, why?')
             },
         ],
     }
     validate(spec, data)
Beispiel #3
0
def test_dot_expanded_dict():
    obj = modeling.DotExpandedDict(foo=dict(bar=123))
    assert obj.foo.bar == 123

    obj.foo.bar = text_type('Whoa')
    assert obj.foo.bar == text_type('Whoa')
    assert obj.foo.bar == obj['foo']['bar']

    obj = modeling.DotExpandedDict(comments=[{'text': 'hi'}])
    assert obj.comments[0].text == obj['comments'][0]['text']
Beispiel #4
0
def test_dot_expanded_dict():
    obj = modeling.DotExpandedDict(foo=dict(bar=123))
    assert obj.foo.bar == 123

    obj.foo.bar = text_type("Whoa")
    assert obj.foo.bar == text_type("Whoa")
    assert obj.foo.bar == obj["foo"]["bar"]

    obj = modeling.DotExpandedDict(comments=[{"text": "hi"}])
    assert obj.comments[0].text == obj["comments"][0]["text"]
Beispiel #5
0
    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': ''})
Beispiel #6
0
    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': ''})
Beispiel #7
0
def test_dot_expanded_dict_mixin():
    class Entry(modeling.DotExpandedDictMixin, dict):
        pass

    entry = Entry(foo=123)

    # getattr -> getitem
    assert entry['foo'] == 123
    assert entry['foo'] == entry.foo
    with pytest.raises(AttributeError):
        entry.nonexistent_key

    # setattr -> setitem
    entry.foo = 'bar'
    assert entry.foo == 'bar'
    assert entry.foo == entry['foo']

    # setattr -> setitem  won't work if key did not exist
    #   (reason: ambiguity of intent)
    entry.title = text_type('zzz')
    assert 'title' not in entry
    assert hasattr(entry, 'title')
Beispiel #8
0
def test_dot_expanded_dict_mixin():
    class Entry(modeling.DotExpandedDictMixin, dict):
        pass

    entry = Entry(foo=123)

    # getattr -> getitem
    assert entry["foo"] == 123
    assert entry["foo"] == entry.foo
    with pytest.raises(AttributeError):
        entry.nonexistent_key

    # setattr -> setitem
    entry.foo = "bar"
    assert entry.foo == "bar"
    assert entry.foo == entry["foo"]

    # setattr -> setitem  won't work if key did not exist
    #   (reason: ambiguity of intent)
    entry.title = text_type("zzz")
    assert "title" not in entry
    assert hasattr(entry, "title")
Beispiel #9
0
 def test_unicode_instance(self):
     validate({'a': text_type('foo')}, {'a': text_type('hello')})
     with raises(ValidationError):
         validate({'a': text_type('foo')}, {'a': 123})
Beispiel #10
0
 def test_unicode_instance(self):
     validate({'a': text_type('foo')}, {'a': text_type('hello')})
     with raises(ValidationError):
         validate({'a': text_type('foo')}, {'a': 123})