<s> from __future__ import print_function , unicode_literals <EOL> import six <EOL> import unittest <EOL> import threading <EOL> import thread <EOL> import pickle <EOL> import datetime <EOL> import xmlrpclib <EOL> import urllib2 <EOL> import xml . parsers . expat as expat_parser <EOL> import voodoo . mapper as mapper <EOL> class MyClass ( object ) : <EOL> def __init__ ( self , first_field , second_field , third_field ) : <EOL> object . __init__ ( self ) <EOL> self . _first_field = first_field <EOL> self . _second_field = second_field <EOL> self . _third_field = third_field <EOL> self . sum_of_fields = first_field + second_field <EOL> self . another = threading . Lock ( ) <EOL> self . yet_another = <NUM_LIT> <EOL> def my_method ( self ) : <EOL> self . another . acquire ( ) <EOL> self . another . release ( ) <EOL> class MyOtherClass : <EOL> def __init__ ( self , param ) : <EOL> self . param = param <EOL> self . param2 = open ( __file__ ) <EOL> class JetAnotherClass : <EOL> def __init__ ( self , param ) : <EOL> self . otherReference = param <EOL> class MyClass2 ( object ) : <EOL> pass <EOL> class MyError ( Exception ) : pass <EOL> class SimpleClass : pass <EOL> class MapperTestCase ( unittest . TestCase ) : <EOL> def test_dto_generator ( self ) : <EOL> mc = MyClass ( <NUM_LIT:1> , <NUM_LIT:2> , ( <NUM_LIT:1> , <NUM_LIT:2> ) ) <EOL> mc . and_another = MyClass ( <NUM_LIT:1> , <NUM_LIT:3> , ( <NUM_LIT:1> , <NUM_LIT:3> ) ) <EOL> moc = MyOtherClass ( <NUM_LIT:4> + <NUM_LIT> ) <EOL> jac1 = JetAnotherClass ( None ) <EOL> jac2 = JetAnotherClass ( jac1 ) <EOL> jac1 . otherReference = jac2 <EOL> try : <EOL> self . assertRaises ( <EOL> pickle . PicklingError , <EOL> pickle . dumps , <EOL> mc <EOL> ) <EOL> except : <EOL> first_assertion_failed = True <EOL> else : <EOL> first_assertion_failed = False <EOL> try : <EOL> self . assertRaises ( <EOL> TypeError , <EOL> pickle . dumps , <EOL> mc <EOL> ) <EOL> except : <EOL> if first_assertion_failed : <EOL> raise <EOL> self . assertRaises ( <EOL> TypeError , <EOL> pickle . dumps , <EOL> moc <EOL> ) <EOL> my_dto = mapper . dto_generator ( mc ) <EOL> self . assertEquals ( <EOL> my_dto . _first_field , <EOL> mc . _first_field <EOL> ) <EOL> self . assertEquals ( <EOL> my_dto . _second_field , <EOL> mc . _second_field <EOL> ) <EOL> self . assertEquals ( <EOL> my_dto . _third_field , <EOL> mc . _third_field <EOL> ) <EOL> self . assertEquals ( <EOL> my_dto . sum_of_fields , <EOL> mc . sum_of_fields <EOL> ) <EOL> self . assertNotEquals ( <EOL> type ( my_dto . another ) , <EOL> thread . LockType <EOL> ) <EOL> self . assertEquals ( <EOL> my_dto . yet_another , <EOL> mc . yet_another <EOL> ) <EOL> self . assertEquals ( <EOL> my_dto . and_another . _first_field , <EOL> mc . and_another . _first_field <EOL> ) <EOL> my_other_dto = mapper . dto_generator ( moc ) <EOL> self . assertEquals ( <EOL> my_other_dto . param , <EOL> moc . param <EOL> ) <EOL> self . assertEquals ( <EOL> hasattr ( my_other_dto , '<STR_LIT>' ) , <EOL> False <EOL> ) <EOL> my_other_jac1 = mapper . dto_generator ( jac1 ) <EOL> my_other_jac1 . otherReference <EOL> pickled = pickle . dumps ( my_dto ) <EOL> my_dto2 = pickle . loads ( pickled ) <EOL> pickled = pickle . dumps ( my_other_dto ) <EOL> my_other_dto2 = pickle . loads ( pickled ) <EOL> my_dto2 = mapper . load_from_dto ( my_dto2 ) <EOL> my_other_dto2 = mapper . load_from_dto ( my_other_dto2 ) <EOL> my_dto2 . my_method ( ) <EOL> def test_mapping_ignorable ( self ) : <EOL> native_obj = expat_parser . ParserCreate ( ) <EOL> self . assertRaises ( <EOL> pickle . PicklingError , <EOL> pickle . dumps , <EOL> native_obj <EOL> ) <EOL> class MyClass ( object ) : <EOL> pass <EOL> my_class = MyClass ( ) <EOL> my_class . native_obj = native_obj <EOL> dto = mapper . dto_generator ( my_class ) <EOL> my_class2 = mapper . load_from_dto ( dto ) <EOL> self . assertTrue ( isinstance ( my_class2 . native_obj , six . string_types ) ) <EOL> def test_dto_not_comparable_instances ( self ) : <EOL> dt = xmlrpclib . DateTime ( ) <EOL> dto = mapper . dto_generator ( { "<STR_LIT:foo>" : dt } ) <EOL> dt2 = mapper . load_from_dto ( dto ) <EOL> self . assertEquals ( [ "<STR_LIT:foo>" ] , dt2 . keys ( ) ) <EOL> self . assertEquals ( dt . value , dt2 [ "<STR_LIT:foo>" ] . value ) <EOL> def test_dto_exception ( self ) : <EOL> exception = MyError ( "<STR_LIT:foo>" ) <EOL> dto = mapper . dto_generator ( exception ) <EOL> generated = mapper . load_from_dto ( dto ) <EOL> self . assertTrue ( isinstance ( generated , MyError ) ) <EOL> self . assertEquals ( "<STR_LIT:foo>" , generated . args [ <NUM_LIT:0> ] ) <EOL> def test_dto_builtin ( self ) : <EOL> exception = AttributeError ( "<STR_LIT:foo>" ) <EOL> dto = mapper . dto_generator ( exception ) <EOL> generated = mapper . load_from_dto ( dto ) <EOL> self . assertTrue ( isinstance ( generated , AttributeError ) ) <EOL> self . assertEquals ( "<STR_LIT:foo>" , generated . args [ <NUM_LIT:0> ] ) <EOL> def test_dto_datetime ( self ) : <EOL> sc = SimpleClass ( ) <EOL> sc . time = datetime . datetime ( <NUM_LIT> , <NUM_LIT:12> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) <EOL> dto = mapper . dto_generator ( sc ) <EOL> sc2 = mapper . load_from_dto ( dto ) <EOL> self . assertEquals ( <NUM_LIT> , sc2 . time . year ) <EOL> self . assertEquals ( <NUM_LIT:12> , sc2 . time . month ) <EOL> self . assertEquals ( <NUM_LIT> , sc2 . time . day ) <EOL> self . assertEquals ( <NUM_LIT> , sc2 . time . hour ) <EOL> self . assertEquals ( <NUM_LIT> , sc2 . time . minute ) <EOL> def test_skip_recoverables ( self ) : <EOL> a = SimpleClass ( ) <EOL> a . l = threading . Lock ( ) <EOL> dto = mapper . dto_generator ( a ) <EOL> a2 = mapper . load_from_dto ( dto , skip_recoverables = True ) <EOL> self . assertEquals ( None , a2 . l ) <EOL> a3 = mapper . load_from_dto ( dto , skip_recoverables = False ) <EOL> self . assertTrue ( isinstance ( a3 . l , thread . LockType ) ) <EOL> def test_remove_unpickables_lock ( self ) : <EOL> a = SimpleClass ( ) <EOL> a . l = threading . Lock ( ) <EOL> mapper . remove_unpickables ( a ) <EOL> self . assertEquals ( None , a . l ) <EOL> def test_remove_unpickables_not_comparable_instances ( self ) : <EOL> dt = xmlrpclib . DateTime ( ) <EOL> mapper . remove_unpickables ( { "<STR_LIT:foo>" : dt } ) <EOL> def test_remove_unpickables_datetime ( self ) : <EOL> sc = SimpleClass ( ) <EOL> sc . time = datetime . datetime ( <NUM_LIT> , <NUM_LIT:12> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) <EOL> mapper . remove_unpickables ( sc ) <EOL> self . assertEquals ( <NUM_LIT> , sc . time . year ) <EOL> self . assertEquals ( <NUM_LIT:12> , sc . time . month ) <EOL> self . assertEquals ( <NUM_LIT> , sc . time . day ) <EOL> self . assertEquals ( <NUM_LIT> , sc . time . hour ) <EOL> self . assertEquals ( <NUM_LIT> , sc . time . minute ) <EOL> def test_remove_unpickables_ignorable ( self ) : <EOL> parser = expat_parser . ParserCreate ( ) <EOL> self . assertRaises ( <EOL> pickle . PicklingError , <EOL> pickle . dumps , <EOL> parser <EOL> ) <EOL> my_class = MyClass2 ( ) <EOL> my_class . parser = parser <EOL> mapper . remove_unpickables ( my_class ) <EOL> pickle . dumps ( my_class ) <EOL> self . assertEqual ( None , my_class . parser ) <EOL> def test_remove_unpickables_general ( self ) : <EOL> mc = MyClass ( <NUM_LIT:1> , <NUM_LIT:2> , ( <NUM_LIT:1> , <NUM_LIT:2> ) ) <EOL> mc . and_another = MyClass ( <NUM_LIT:1> , <NUM_LIT:3> , ( <NUM_LIT:1> , <NUM_LIT:3> ) ) <EOL> moc = MyOtherClass ( <NUM_LIT:4> + <NUM_LIT> ) <EOL> jac1 = JetAnotherClass ( None ) <EOL> jac2 = JetAnotherClass ( jac1 ) <EOL> jac1 . otherReference = jac2 <EOL> try : <EOL> self . assertRaises ( <EOL> pickle . PicklingError , <EOL> pickle . dumps , <EOL> mc <EOL> ) <EOL> except : <EOL> failed_first_time = True <EOL> else : <EOL> failed_first_time = False <EOL> try : <EOL> self . assertRaises ( <EOL> TypeError , <EOL> pickle . dumps , <EOL> mc <EOL> ) <EOL> except : <EOL> if failed_first_time : <EOL> raise <EOL> self . assertRaises ( <EOL> TypeError , <EOL> pickle . dumps , <EOL> moc <EOL> ) <EOL> mapper . remove_unpickables ( mc ) <EOL> self . assertEquals ( <EOL> <NUM_LIT:1> , <EOL> mc . _first_field <EOL> ) <EOL> self . assertEquals ( <EOL> <NUM_LIT:2> , <EOL> mc . _second_field <EOL> ) <EOL> self . assertEquals ( <EOL> ( <NUM_LIT:1> , <NUM_LIT:2> ) , <EOL> mc . _third_field <EOL> ) <EOL> self . assertEquals ( <EOL> <NUM_LIT:1> + <NUM_LIT:2> , <EOL> mc . sum_of_fields <EOL> ) <EOL> self . assertEquals ( <EOL> None , <EOL> mc . another <EOL> ) <EOL> self . assertEquals ( <EOL> <NUM_LIT> , <EOL> mc . yet_another <EOL> ) <EOL> self . assertEquals ( <EOL> <NUM_LIT:1> , <EOL> mc . and_another . _first_field <EOL> ) <EOL> mapper . remove_unpickables ( moc ) <EOL> self . assertEquals ( <EOL> <NUM_LIT:4> + <NUM_LIT> , <EOL> moc . param <EOL> ) <EOL> self . assertEquals ( <EOL> None , <EOL> moc . param2 <EOL> ) <EOL> mapper . remove_unpickables ( jac1 ) <EOL> pickled = pickle . dumps ( mc ) <EOL> pickle . loads ( pickled ) <EOL> pickled = pickle . dumps ( moc ) <EOL> pickle . loads ( pickled ) <EOL> def test_remove_unpickables_http_exception ( self ) : <EOL> try : <EOL> urllib2 . urlopen ( "<STR_LIT>" ) <EOL> self . fail ( "<STR_LIT>" ) <EOL> except urllib2 . URLError as e : <EOL> pass <EOL> except urllib2 . HTTPError as e : <EOL> pass <EOL> removed = mapper . remove_unpickables ( e ) <EOL> pickled = pickle . dumps ( removed ) <EOL> pickle . loads ( pickled ) <EOL> def test_condition ( self ) : <EOL> c1 = threading . Condition ( ) <EOL> dto = mapper . dto_generator ( c1 ) <EOL> c2 = mapper . load_from_dto ( dto ) <EOL> self . assertTrue ( hasattr ( c2 , '<STR_LIT>' ) ) <EOL> self . assertTrue ( hasattr ( c2 , '<STR_LIT>' ) ) <EOL> def suite ( ) : <EOL> return unittest . makeSuite ( MapperTestCase ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
