<s> from decimal import Decimal <EOL> import datetime <EOL> from jsonobject . exceptions import BadValueError <EOL> from jsonobject import JsonObject , ObjectProperty , DateTimeProperty <EOL> import unittest2 <EOL> from jsonobject . base import get_settings <EOL> class StringConversionsTest ( unittest2 . TestCase ) : <EOL> EXAMPLES = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:date>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } , <EOL> '<STR_LIT:list>' : [ '<STR_LIT:1.0>' , '<STR_LIT>' ] , <EOL> } <EOL> EXAMPLES_CONVERTED = { <EOL> '<STR_LIT>' : Decimal ( '<STR_LIT>' ) , <EOL> '<STR_LIT:date>' : datetime . date ( <NUM_LIT> , <NUM_LIT:2> , <NUM_LIT:4> ) , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : Decimal ( '<STR_LIT>' ) , <EOL> } , <EOL> '<STR_LIT:list>' : [ Decimal ( '<STR_LIT:1.0>' ) , datetime . date ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:1> ) ] , <EOL> '<STR_LIT>' : datetime . datetime ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:3> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ) <EOL> } <EOL> def test_default_conversions ( self ) : <EOL> class Foo ( JsonObject ) : <EOL> pass <EOL> foo = Foo . wrap ( self . EXAMPLES ) <EOL> for key , value in self . EXAMPLES_CONVERTED . items ( ) : <EOL> self . assertEqual ( getattr ( foo , key ) , value ) <EOL> def test_no_conversions ( self ) : <EOL> class Foo ( JsonObject ) : <EOL> class Meta ( object ) : <EOL> string_conversions = ( ) <EOL> foo = Foo . wrap ( self . EXAMPLES ) <EOL> for key , value in self . EXAMPLES . items ( ) : <EOL> self . assertEqual ( getattr ( foo , key ) , value ) <EOL> def test_nested_1 ( self ) : <EOL> class Bar ( JsonObject ) : <EOL> pass <EOL> class Foo ( JsonObject ) : <EOL> bar = ObjectProperty ( Bar ) <EOL> class Meta ( object ) : <EOL> string_conversions = ( ) <EOL> foo = Foo . wrap ( { <EOL> '<STR_LIT>' : '<STR_LIT:1.0>' , <EOL> '<STR_LIT:bar>' : { '<STR_LIT>' : '<STR_LIT>' } <EOL> } ) <EOL> self . assertEqual ( foo . decimal , '<STR_LIT:1.0>' ) <EOL> self . assertNotEqual ( foo . decimal , Decimal ( '<STR_LIT:1.0>' ) ) <EOL> self . assertEqual ( foo . bar . decimal , Decimal ( '<STR_LIT>' ) ) <EOL> def test_nested_2 ( self ) : <EOL> class Bar ( JsonObject ) : <EOL> class Meta ( object ) : <EOL> string_conversions = ( ) <EOL> class Foo ( JsonObject ) : <EOL> bar = ObjectProperty ( Bar ) <EOL> foo = Foo . wrap ( { <EOL> '<STR_LIT>' : '<STR_LIT:1.0>' , <EOL> '<STR_LIT:bar>' : { '<STR_LIT>' : '<STR_LIT>' } <EOL> } ) <EOL> self . assertNotEqual ( foo . decimal , '<STR_LIT:1.0>' ) <EOL> self . assertEqual ( foo . decimal , Decimal ( '<STR_LIT:1.0>' ) ) <EOL> self . assertEqual ( foo . bar . decimal , '<STR_LIT>' ) <EOL> def test_update_properties ( self ) : <EOL> class Foo ( JsonObject ) : <EOL> class Meta ( object ) : <EOL> update_properties = { datetime . datetime : ExactDateTimeProperty } <EOL> self . assertEqual ( <EOL> get_settings ( Foo ) . type_config . properties [ datetime . datetime ] , <EOL> ExactDateTimeProperty <EOL> ) <EOL> with self . assertRaisesRegexp ( BadValueError , <EOL> '<STR_LIT>' ) : <EOL> Foo . wrap ( self . EXAMPLES ) <EOL> examples = self . EXAMPLES . copy ( ) <EOL> examples [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> examples_converted = self . EXAMPLES_CONVERTED . copy ( ) <EOL> examples_converted [ '<STR_LIT>' ] = datetime . datetime ( <EOL> <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:3> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT> ) <EOL> foo = Foo . wrap ( examples ) <EOL> for key , value in examples_converted . items ( ) : <EOL> self . assertEqual ( getattr ( foo , key ) , value ) <EOL> class ExactDateTimeProperty ( DateTimeProperty ) : <EOL> def __init__ ( self , ** kwargs ) : <EOL> if '<STR_LIT>' in kwargs : <EOL> assert kwargs [ '<STR_LIT>' ] is True <EOL> kwargs [ '<STR_LIT>' ] = True <EOL> super ( ExactDateTimeProperty , self ) . __init__ ( ** kwargs ) </s>
