<s> from unittest import TestCase <EOL> from pyschema . types import * <EOL> import pyschema <EOL> class TypeTests ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . loader = pyschema . loads <EOL> self . dumper = pyschema . dumps <EOL> def assertValue ( self , record_cls , input_value , output_value ) : <EOL> rec = record_cls ( field = input_value ) <EOL> self . assertEquals ( rec . field , input_value ) <EOL> serialized = self . dumper ( rec ) <EOL> deserialized = self . loader ( serialized , schema = record_cls ) <EOL> self . assertEquals ( deserialized . field , output_value ) <EOL> def assertCompliant ( self , record_cls , allowed_values , forbidden_values ) : <EOL> for value in allowed_values : <EOL> self . assertValue ( record_cls , value , value ) <EOL> for value in forbidden_values : <EOL> rec = record_cls ( field = value ) <EOL> self . assertEquals ( rec . field , value ) <EOL> def dump_fail ( ) : <EOL> str ( self . dumper ( rec ) ) <EOL> self . assertRaises ( <EOL> ValueError , <EOL> dump_fail <EOL> ) <EOL> def test_text ( self ) : <EOL> @ pyschema . no_auto_store ( ) <EOL> class TextRecord ( pyschema . Record ) : <EOL> field = Text ( ) <EOL> allowed = [ u"<STR_LIT:foo>" , "<STR_LIT:bar>" ] <EOL> forbidden = [ <NUM_LIT:100> , False ] <EOL> self . assertCompliant ( TextRecord , allowed , forbidden ) <EOL> def test_integer ( self ) : <EOL> @ pyschema . no_auto_store ( ) <EOL> class IntegerRecord ( pyschema . Record ) : <EOL> field = Integer ( ) <EOL> allowed = [ <NUM_LIT:100> , <NUM_LIT:2> ** <NUM_LIT:32> - <NUM_LIT:1> , - ( <NUM_LIT:2> ** <NUM_LIT:32> ) , <NUM_LIT:1> L ] <EOL> forbidden = [ "<STR_LIT:1>" , <NUM_LIT> , False , True ] <EOL> self . assertCompliant ( IntegerRecord , allowed , forbidden ) <EOL> def test_boolean ( self ) : <EOL> @ pyschema . no_auto_store ( ) <EOL> class BooleanRecord ( pyschema . Record ) : <EOL> field = Boolean ( ) <EOL> allowed = [ True , False , <NUM_LIT:0> , <NUM_LIT:1> ] <EOL> forbidden = [ "<STR_LIT:True>" , "<STR_LIT:t>" , "<STR_LIT:f>" , "<STR_LIT:False>" , <NUM_LIT:2> , - <NUM_LIT:1> ] <EOL> self . assertCompliant ( BooleanRecord , allowed , forbidden ) <EOL> def test_float ( self ) : <EOL> @ pyschema . no_auto_store ( ) <EOL> class FloatRecord ( pyschema . Record ) : <EOL> field = Float ( ) <EOL> allowed = [ <NUM_LIT:0.1> , - <NUM_LIT:0.5> , <NUM_LIT> , <NUM_LIT> ] <EOL> forbidden = [ "<STR_LIT>" , "<STR_LIT>" , True , False ] <EOL> self . assertCompliant ( FloatRecord , allowed , forbidden ) <EOL> def test_date ( self ) : <EOL> @ pyschema . no_auto_store ( ) <EOL> class DateRecord ( pyschema . Record ) : <EOL> field = Date ( ) <EOL> allowed = [ datetime . date ( <NUM_LIT> , <NUM_LIT:4> , <NUM_LIT:20> ) ] <EOL> forbidden = [ '<STR_LIT>' , <NUM_LIT> ] <EOL> self . assertCompliant ( DateRecord , allowed , forbidden ) <EOL> def test_datetime ( self ) : <EOL> @ pyschema . no_auto_store ( ) <EOL> class DateTimeRecord ( pyschema . Record ) : <EOL> field = DateTime ( ) <EOL> allowed = [ datetime . datetime ( <NUM_LIT> , <NUM_LIT:4> , <NUM_LIT:20> , <NUM_LIT:12> , <NUM_LIT:0> , <NUM_LIT:0> ) , datetime . datetime ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT> , <NUM_LIT:3> , <NUM_LIT:3> , <NUM_LIT> ) ] <EOL> forbidden = [ '<STR_LIT>' , <NUM_LIT> ] <EOL> self . assertCompliant ( DateTimeRecord , allowed , forbidden ) <EOL> def test_bytes ( self ) : <EOL> @ pyschema . no_auto_store ( ) <EOL> class BytesRecord ( pyschema . Record ) : <EOL> field = Bytes ( ) <EOL> allowed = [ "<STR_LIT>" ] <EOL> forbidden = [ u"<STR_LIT>" ] <EOL> self . assertCompliant ( BytesRecord , allowed , forbidden ) <EOL> def test_enum ( self ) : <EOL> @ pyschema . no_auto_store ( ) <EOL> class EnumRecord ( pyschema . Record ) : <EOL> field = Enum ( [ "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> allowed = [ "<STR_LIT>" , "<STR_LIT>" , u"<STR_LIT>" ] <EOL> forbidden = [ "<STR_LIT:foo>" , "<STR_LIT:bar>" , "<STR_LIT>" , True ] <EOL> self . assertCompliant ( EnumRecord , allowed , forbidden ) <EOL> def test_list ( self ) : <EOL> @ pyschema . no_auto_store ( ) <EOL> class ListRecord ( pyschema . Record ) : <EOL> field = List ( Text ( ) ) <EOL> allowed = [ [ u"<STR_LIT:foo>" , u"<STR_LIT:bar>" ] , [ ] ] <EOL> forbidden = [ [ <NUM_LIT:1> ] , { } , set ( ) ] <EOL> self . assertCompliant ( ListRecord , allowed , forbidden ) <EOL> self . assertValue ( ListRecord , ( ) , [ ] ) <EOL> self . assertValue ( ListRecord , ( u"<STR_LIT>" , ) , [ u"<STR_LIT>" ] ) <EOL> def test_subrecord ( self ) : <EOL> @ pyschema . no_auto_store ( ) <EOL> class SubRecordRecord ( pyschema . Record ) : <EOL> @ pyschema . no_auto_store ( ) <EOL> class Inner ( pyschema . Record ) : <EOL> field = Text ( ) <EOL> field = SubRecord ( Inner ) <EOL> allowed = [ SubRecordRecord . Inner ( field = u"<STR_LIT:foo>" ) ] <EOL> forbidden = [ u"<STR_LIT:foo>" , { "<STR_LIT>" : u"<STR_LIT:foo>" } ] <EOL> self . assertCompliant ( SubRecordRecord , allowed , forbidden ) <EOL> def test_subrecord_self ( self ) : <EOL> @ pyschema . no_auto_store ( ) <EOL> class SelfReference ( pyschema . Record ) : <EOL> field = SubRecord ( SELF ) <EOL> allowed = [ SelfReference ( <EOL> field = SelfReference ( <EOL> field = SelfReference ( ) <EOL> ) <EOL> ) ] <EOL> self . assertCompliant ( SelfReference , allowed , [ ] ) <EOL> def test_map ( self ) : <EOL> @ pyschema . no_auto_store ( ) <EOL> class MapRecord ( pyschema . Record ) : <EOL> field = Map ( Integer ( ) ) <EOL> allowed = [ { "<STR_LIT:foo>" : <NUM_LIT> } , { u"<STR_LIT:foo>" : <NUM_LIT:2> } , { } ] <EOL> forbidden = [ [ ( "<STR_LIT:foo>" , <NUM_LIT> ) ] , { "<STR_LIT:foo>" : "<STR_LIT:2>" } ] <EOL> self . assertCompliant ( MapRecord , allowed , forbidden ) <EOL> def test_nested ( self ) : <EOL> @ pyschema . no_auto_store ( ) <EOL> class NestedRecord ( pyschema . Record ) : <EOL> @ pyschema . no_auto_store ( ) <EOL> class Inner ( pyschema . Record ) : <EOL> field = Text ( ) <EOL> field = Map ( List ( SubRecord ( Inner ) ) ) <EOL> allowed = [ { "<STR_LIT:foo>" : [ NestedRecord . Inner ( field = u"<STR_LIT:bar>" ) ] } ] <EOL> forbidden = [ ] <EOL> self . assertCompliant ( NestedRecord , allowed , forbidden ) <EOL> def test_nested_self ( self ) : <EOL> @ pyschema . no_auto_store ( ) <EOL> class NestedRecord ( pyschema . Record ) : <EOL> field = Map ( List ( SubRecord ( SELF ) ) ) <EOL> allowed = [ { "<STR_LIT:foo>" : [ <EOL> NestedRecord ( field = { <EOL> "<STR_LIT:bar>" : [ NestedRecord ( ) ] <EOL> } <EOL> ) ] } <EOL> ] <EOL> forbidden = [ ] <EOL> self . assertCompliant ( NestedRecord , allowed , forbidden ) </s>
