def test_rewritable(self): s = Schema.make(v=String().rewritable())(v='test') eq_(s['v'], 'test') s['v'] = 'new value' eq_(s['v'], 'new value') s.update(v='new value2') eq_(s['v'], 'new value2') s.update({'v' : 'new value3'}) eq_(s['v'], 'new value3')
def test_make_flags(self): S = Schema.make(allow_optional=False, merge_optional=True, immutable=False, v=String()) eq_(S.allow_optional, False) eq_(S.merge_optional, True) eq_(S.immutable, False)
def test_not_rewritable_direct(self): s = Schema.make(v=String())(v='test') s['v'] = 'new value'
#!/usr/bin/env python # -*- encoding:utf-8 -*- from benchmarker import Benchmarker from song2 import Schema from song2.types import * Comment = Schema.make(name=String(), message=String()) Address = Schema.make(country=String(), city=String()) class Person(Schema): name = String() age = Int() hobbies = ArrayOf(str) comments = ArrayOf(Comment) address = Nested(Address) class Person2(Schema): merge_optional = True name = String() age = Int() hobbies = ArrayOf(str) comments = ArrayOf(Comment) address = Nested(Address) loop = 50000 with Benchmarker(loop, width=35) as bench:
#!/usr/bin/env python # -*- encoding:utf-8 -*- from song2 import Schema from song2.types import * Address = Schema.make(addr=String(), country=String()) Hobby = Schema.make(name=String(), years=Int()) class Prop(Schema): text = StringValue() class Person(Schema): name = String() age = Int() comments = StringArray() hobbies = ArrayOf(Hobby) address = Nested(Address) try_default = StringValue(default='this is default') floatproperty = Float() longproperty = Long() dynamic_dict = StringDict(Prop) class Rewritable(Schema): rewritable_field = String().rewritable() class DefaultValue(Schema): message = String(default='please enter a message')
def test_valid(self): s = Schema.make(v=Int())(v=123) eq_(s['v'], 123)
def test_invalid(self): Schema.make(v=Int())(v='test')
def test_valid_dict(self): s = Schema.make(v=StringDict(int))(v={'f1': 1, 'f2' : 2}) eq_(s['v'], {'f1': 1, 'f2' : 2})
def test_invalid_key_type(self): Schema.make(v=StringDict(int))(v={0: 1})
def test_invalid_value_type(self): Schema.make(v=DictOf(str, int))(v={'f1': 'INVALID'})
def test_valie_is_not_nullable(self): Schema.make(v=DictOf(str, int, value_nullable=False))(v={'f1': None})
def test_invalid_key_type(self): Schema.make(v=DictOf(int, int))(v={'INVALID': 1})
def test_valid_dict(self): s = Schema.make(v=DictOf(str, int))(v={'f1': 1, 'f2' : 2}) eq_(s['v'], {'f1': 1, 'f2' : 2})
def test_invalid_element(self): Schema.make(v=IntArray())(v=['test', 1])
def test_valid_tuple(self): s = Schema.make(v=IntArray())(v=(1, 2)) eq_(s['v'], (1, 2))
def test_invalid_value_type(self): Schema.make(v=StringDict(int))(v={'f1': 'INVALID'})
def test_invalid(self): Schema.make(v=StringValue())(v=1234)
def test_valie_is_not_nullable(self): Schema.make(v=StringDict(int, value_nullable=False))(v={'f1': None})
def test_default(self): s = Schema.make(v=Int())() eq_(s['v'], 0)
def test_valie_is_nullable_by_default(self): s = Schema.make(v=StringDict(int))(v={'f1': None}) eq_(s['v'], {'f1': None})
def test_not_nullable(self): Schema.make(v=Int())(v=None)
def test_default(self): s = Schema.make(v=String())() eq_(s['v'], None)
#!/usr/bin/env python # -*- encoding:utf-8 -*- from benchmarker import Benchmarker from song2 import Schema from song2.types import * Comment = Schema.make(name=String(), message=String()) Address = Schema.make(country=String(), city=String()) class Person(Schema): name = String() age = Int() hobbies = ArrayOf(str) comments = ArrayOf(Comment) address = Nested(Address) class Person2(Schema): merge_optional = True name = String() age = Int() hobbies = ArrayOf(str) comments = ArrayOf(Comment) address = Nested(Address) loop = 50000
def test_valid(self): s = Schema.make(v=StringValue())(v='test') eq_(s['v'], 'test')
def test_make(self): S = Schema.make(v1=String(), v2=Int()) ok_(isinstance(S.v1, String)) ok_(isinstance(S.v2, Int)) ok_(isinstance(S(), Schema))
def test_not_nullable_by_default(self): Schema.make(v=StringValue())()
def test_make_with_invalid_property(self): Schema.make(v1=String(), v2='INVALID')
def test_not_empty_by_default(self): Schema.make(v=StringValue())(v='')
def test_not_rewritable_by_merge(self): s = Schema.make(v=String())(v='test') s.update({'v' : 'new value'})
def test_default(self): s = Schema.make(v=StringValue(default='ok'))() eq_(s['v'], 'ok')
def test_rewritable_but_invalid_type(self): s = Schema.make(v=String().rewritable())(v='test') s['v'] = 1
def test_valid_list(self): s = Schema.make(v=IntArray())(v=[1, 2]) eq_(s['v'], [1, 2])