Exemple #1
0
  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')
Exemple #2
0
 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)
Exemple #3
0
 def test_not_rewritable_direct(self):
   s = Schema.make(v=String())(v='test')
   s['v'] = 'new value'
Exemple #4
0
#!/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:
Exemple #5
0
#!/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')
Exemple #6
0
 def test_valid(self):
   s = Schema.make(v=Int())(v=123)
   eq_(s['v'], 123)
Exemple #7
0
 def test_invalid(self):
   Schema.make(v=Int())(v='test')
Exemple #8
0
 def test_valid_dict(self):
   s = Schema.make(v=StringDict(int))(v={'f1': 1, 'f2' : 2})
   eq_(s['v'], {'f1': 1, 'f2' : 2})
Exemple #9
0
 def test_invalid_key_type(self):
   Schema.make(v=StringDict(int))(v={0: 1})
Exemple #10
0
 def test_invalid_value_type(self):
   Schema.make(v=DictOf(str, int))(v={'f1': 'INVALID'})
Exemple #11
0
 def test_valie_is_not_nullable(self):
   Schema.make(v=DictOf(str, int, value_nullable=False))(v={'f1': None})
Exemple #12
0
 def test_invalid_key_type(self):
   Schema.make(v=DictOf(int, int))(v={'INVALID': 1})
Exemple #13
0
 def test_valid_dict(self):
   s = Schema.make(v=DictOf(str, int))(v={'f1': 1, 'f2' : 2})
   eq_(s['v'], {'f1': 1, 'f2' : 2})
Exemple #14
0
 def test_invalid_element(self):
   Schema.make(v=IntArray())(v=['test', 1])
Exemple #15
0
 def test_valid_tuple(self):
   s = Schema.make(v=IntArray())(v=(1, 2))
   eq_(s['v'], (1, 2))
Exemple #16
0
 def test_invalid_value_type(self):
   Schema.make(v=StringDict(int))(v={'f1': 'INVALID'})
Exemple #17
0
 def test_invalid(self):
   Schema.make(v=StringValue())(v=1234)
Exemple #18
0
 def test_valie_is_not_nullable(self):
   Schema.make(v=StringDict(int, value_nullable=False))(v={'f1': None})
Exemple #19
0
 def test_default(self):
   s = Schema.make(v=Int())()
   eq_(s['v'], 0)
Exemple #20
0
 def test_valie_is_nullable_by_default(self):
   s = Schema.make(v=StringDict(int))(v={'f1': None})
   eq_(s['v'], {'f1': None})
Exemple #21
0
 def test_not_nullable(self):
   Schema.make(v=Int())(v=None)
Exemple #22
0
 def test_default(self):
   s = Schema.make(v=String())()
   eq_(s['v'], None)
Exemple #23
0
#!/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
Exemple #24
0
 def test_valid(self):
   s = Schema.make(v=StringValue())(v='test')
   eq_(s['v'], 'test')
Exemple #25
0
 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))
Exemple #26
0
 def test_not_nullable_by_default(self):
   Schema.make(v=StringValue())()
Exemple #27
0
 def test_make_with_invalid_property(self):
   Schema.make(v1=String(), v2='INVALID')
Exemple #28
0
 def test_not_empty_by_default(self):
   Schema.make(v=StringValue())(v='')
Exemple #29
0
 def test_not_rewritable_by_merge(self):
   s = Schema.make(v=String())(v='test')
   s.update({'v' : 'new value'})
Exemple #30
0
 def test_default(self):
   s = Schema.make(v=StringValue(default='ok'))()
   eq_(s['v'], 'ok')
Exemple #31
0
 def test_rewritable_but_invalid_type(self):
   s = Schema.make(v=String().rewritable())(v='test')
   s['v'] = 1
Exemple #32
0
 def test_valid_list(self):
   s = Schema.make(v=IntArray())(v=[1, 2])
   eq_(s['v'], [1, 2])