<s> import sys , os <EOL> try : <EOL> import Crypto . Cipher . AES <EOL> except ImportError : <EOL> Crypto = None <EOL> from twisted . trial import unittest <EOL> from twisted . persisted import sob <EOL> from twisted . python import components <EOL> class Dummy ( components . Componentized ) : <EOL> pass <EOL> objects = [ <EOL> <NUM_LIT:1> , <EOL> "<STR_LIT:hello>" , <EOL> ( <NUM_LIT:1> , "<STR_LIT:hello>" ) , <EOL> [ <NUM_LIT:1> , "<STR_LIT:hello>" ] , <EOL> { <NUM_LIT:1> : "<STR_LIT:hello>" } , <EOL> ] <EOL> class FakeModule ( object ) : <EOL> pass <EOL> class PersistTestCase ( unittest . TestCase ) : <EOL> def testStyles ( self ) : <EOL> for o in objects : <EOL> p = sob . Persistent ( o , '<STR_LIT>' ) <EOL> for style in '<STR_LIT>' . split ( ) : <EOL> p . setStyle ( style ) <EOL> p . save ( filename = '<STR_LIT>' + style ) <EOL> o1 = sob . load ( '<STR_LIT>' + style , style ) <EOL> self . failUnlessEqual ( o , o1 ) <EOL> def testStylesBeingSet ( self ) : <EOL> o = Dummy ( ) <EOL> o . foo = <NUM_LIT:5> <EOL> o . setComponent ( sob . IPersistable , sob . Persistent ( o , '<STR_LIT>' ) ) <EOL> for style in '<STR_LIT>' . split ( ) : <EOL> sob . IPersistable ( o ) . setStyle ( style ) <EOL> sob . IPersistable ( o ) . save ( filename = '<STR_LIT>' + style ) <EOL> o1 = sob . load ( '<STR_LIT>' + style , style ) <EOL> self . failUnlessEqual ( o . foo , o1 . foo ) <EOL> self . failUnlessEqual ( sob . IPersistable ( o1 ) . style , style ) <EOL> def testNames ( self ) : <EOL> o = [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] <EOL> p = sob . Persistent ( o , '<STR_LIT:object>' ) <EOL> for style in '<STR_LIT>' . split ( ) : <EOL> p . setStyle ( style ) <EOL> p . save ( ) <EOL> o1 = sob . load ( '<STR_LIT>' + style [ <NUM_LIT:0> ] , style ) <EOL> self . failUnlessEqual ( o , o1 ) <EOL> for tag in '<STR_LIT>' . split ( ) : <EOL> p . save ( tag ) <EOL> o1 = sob . load ( '<STR_LIT>' + tag + '<STR_LIT>' + style [ <NUM_LIT:0> ] , style ) <EOL> self . failUnlessEqual ( o , o1 ) <EOL> def testEncryptedStyles ( self ) : <EOL> for o in objects : <EOL> phrase = '<STR_LIT>' <EOL> p = sob . Persistent ( o , '<STR_LIT>' ) <EOL> for style in '<STR_LIT>' . split ( ) : <EOL> p . setStyle ( style ) <EOL> p . save ( filename = '<STR_LIT>' + style , passphrase = phrase ) <EOL> o1 = sob . load ( '<STR_LIT>' + style , style , phrase ) <EOL> self . failUnlessEqual ( o , o1 ) <EOL> if Crypto is None : <EOL> testEncryptedStyles . skip = "<STR_LIT>" <EOL> def testPython ( self ) : <EOL> f = open ( "<STR_LIT>" , '<STR_LIT:w>' ) <EOL> f . write ( '<STR_LIT>' ) <EOL> f . close ( ) <EOL> o = sob . loadValueFromFile ( '<STR_LIT>' , '<STR_LIT:foo>' ) <EOL> self . failUnlessEqual ( o , [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> def testEncryptedPython ( self ) : <EOL> phrase = '<STR_LIT>' <EOL> f = open ( "<STR_LIT>" , '<STR_LIT:w>' ) <EOL> f . write ( <EOL> sob . _encrypt ( phrase , '<STR_LIT>' ) ) <EOL> f . close ( ) <EOL> o = sob . loadValueFromFile ( '<STR_LIT>' , '<STR_LIT:foo>' , phrase ) <EOL> self . failUnlessEqual ( o , [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> if Crypto is None : <EOL> testEncryptedPython . skip = "<STR_LIT>" <EOL> def testTypeGuesser ( self ) : <EOL> self . assertRaises ( KeyError , sob . guessType , "<STR_LIT>" ) <EOL> self . assertEqual ( '<STR_LIT>' , sob . guessType ( "<STR_LIT>" ) ) <EOL> self . assertEqual ( '<STR_LIT>' , sob . guessType ( "<STR_LIT>" ) ) <EOL> self . assertEqual ( '<STR_LIT>' , sob . guessType ( "<STR_LIT>" ) ) <EOL> self . assertEqual ( '<STR_LIT>' , sob . guessType ( "<STR_LIT>" ) ) <EOL> self . assertEqual ( '<STR_LIT>' , sob . guessType ( "<STR_LIT>" ) ) <EOL> self . assertEqual ( '<STR_LIT:source>' , sob . guessType ( "<STR_LIT>" ) ) <EOL> self . assertEqual ( '<STR_LIT:source>' , sob . guessType ( "<STR_LIT>" ) ) <EOL> def testEverythingEphemeralGetattr ( self ) : <EOL> """<STR_LIT>""" <EOL> self . fakeMain . testMainModGetattr = <NUM_LIT:1> <EOL> dirname = self . mktemp ( ) <EOL> os . mkdir ( dirname ) <EOL> filename = os . path . join ( dirname , '<STR_LIT>' ) <EOL> f = file ( filename , '<STR_LIT:w>' ) <EOL> f . write ( '<STR_LIT>' ) <EOL> f . write ( '<STR_LIT>' ) <EOL> f . write ( '<STR_LIT>' ) <EOL> f . close ( ) <EOL> sob . load ( filename , '<STR_LIT:source>' ) <EOL> def testEverythingEphemeralSetattr ( self ) : <EOL> """<STR_LIT>""" <EOL> self . fakeMain . testMainModSetattr = <NUM_LIT:1> <EOL> dirname = self . mktemp ( ) <EOL> os . mkdir ( dirname ) <EOL> filename = os . path . join ( dirname , '<STR_LIT>' ) <EOL> f = file ( filename , '<STR_LIT:w>' ) <EOL> f . write ( '<STR_LIT>' ) <EOL> f . write ( '<STR_LIT>' ) <EOL> f . write ( '<STR_LIT>' ) <EOL> f . close ( ) <EOL> sob . load ( filename , '<STR_LIT:source>' ) <EOL> self . assertEqual ( self . fakeMain . testMainModSetattr , <NUM_LIT:1> ) <EOL> def testEverythingEphemeralException ( self ) : <EOL> """<STR_LIT>""" <EOL> dirname = self . mktemp ( ) <EOL> os . mkdir ( dirname ) <EOL> filename = os . path . join ( dirname , '<STR_LIT>' ) <EOL> f = file ( filename , '<STR_LIT:w>' ) <EOL> f . write ( '<STR_LIT>' ) <EOL> f . close ( ) <EOL> self . assertRaises ( ValueError , sob . load , filename , '<STR_LIT:source>' ) <EOL> self . assertEqual ( type ( sys . modules [ '<STR_LIT:__main__>' ] ) , FakeModule ) <EOL> def setUp ( self ) : <EOL> """<STR_LIT>""" <EOL> self . realMain = sys . modules [ '<STR_LIT:__main__>' ] <EOL> self . fakeMain = sys . modules [ '<STR_LIT:__main__>' ] = FakeModule ( ) <EOL> def tearDown ( self ) : <EOL> """<STR_LIT>""" <EOL> sys . modules [ '<STR_LIT:__main__>' ] = self . realMain </s>
