| OLD | NEW |
| (Empty) |
| 1 | |
| 2 # Copyright (c) 2001-2004 Twisted Matrix Laboratories. | |
| 3 # See LICENSE for details. | |
| 4 | |
| 5 | |
| 6 # System Imports | |
| 7 from twisted.trial import unittest | |
| 8 from twisted.persisted import sob | |
| 9 from twisted.python import components | |
| 10 import sys, os | |
| 11 | |
| 12 try: | |
| 13 from twisted.web import microdom | |
| 14 gotMicrodom = True | |
| 15 except ImportError: | |
| 16 import warnings | |
| 17 warnings.warn("Not testing xml persistence as twisted.web.microdom " | |
| 18 "not available") | |
| 19 gotMicrodom = False | |
| 20 | |
| 21 class Dummy(components.Componentized): | |
| 22 pass | |
| 23 | |
| 24 objects = [ | |
| 25 1, | |
| 26 "hello", | |
| 27 (1, "hello"), | |
| 28 [1, "hello"], | |
| 29 {1:"hello"}, | |
| 30 ] | |
| 31 | |
| 32 class FakeModule(object): | |
| 33 pass | |
| 34 | |
| 35 class PersistTestCase(unittest.TestCase): | |
| 36 def testStyles(self): | |
| 37 for o in objects: | |
| 38 p = sob.Persistent(o, '') | |
| 39 for style in 'xml source pickle'.split(): | |
| 40 if style == 'xml' and not gotMicrodom: | |
| 41 continue | |
| 42 p.setStyle(style) | |
| 43 p.save(filename='persisttest.'+style) | |
| 44 o1 = sob.load('persisttest.'+style, style) | |
| 45 self.failUnlessEqual(o, o1) | |
| 46 | |
| 47 def testStylesBeingSet(self): | |
| 48 o = Dummy() | |
| 49 o.foo = 5 | |
| 50 o.setComponent(sob.IPersistable, sob.Persistent(o, 'lala')) | |
| 51 for style in 'xml source pickle'.split(): | |
| 52 if style == 'xml' and not gotMicrodom: | |
| 53 continue | |
| 54 sob.IPersistable(o).setStyle(style) | |
| 55 sob.IPersistable(o).save(filename='lala.'+style) | |
| 56 o1 = sob.load('lala.'+style, style) | |
| 57 self.failUnlessEqual(o.foo, o1.foo) | |
| 58 self.failUnlessEqual(sob.IPersistable(o1).style, style) | |
| 59 | |
| 60 | |
| 61 def testNames(self): | |
| 62 o = [1,2,3] | |
| 63 p = sob.Persistent(o, 'object') | |
| 64 for style in 'xml source pickle'.split(): | |
| 65 if style == 'xml' and not gotMicrodom: | |
| 66 continue | |
| 67 p.setStyle(style) | |
| 68 p.save() | |
| 69 o1 = sob.load('object.ta'+style[0], style) | |
| 70 self.failUnlessEqual(o, o1) | |
| 71 for tag in 'lala lolo'.split(): | |
| 72 p.save(tag) | |
| 73 o1 = sob.load('object-'+tag+'.ta'+style[0], style) | |
| 74 self.failUnlessEqual(o, o1) | |
| 75 | |
| 76 def testEncryptedStyles(self): | |
| 77 try: | |
| 78 import Crypto | |
| 79 except ImportError: | |
| 80 raise unittest.SkipTest() | |
| 81 for o in objects: | |
| 82 phrase='once I was the king of spain' | |
| 83 p = sob.Persistent(o, '') | |
| 84 for style in 'xml source pickle'.split(): | |
| 85 if style == 'xml' and not gotMicrodom: | |
| 86 continue | |
| 87 p.setStyle(style) | |
| 88 p.save(filename='epersisttest.'+style, passphrase=phrase) | |
| 89 o1 = sob.load('epersisttest.'+style, style, phrase) | |
| 90 self.failUnlessEqual(o, o1) | |
| 91 | |
| 92 def testPython(self): | |
| 93 open("persisttest.python", 'w').write('foo=[1,2,3] ') | |
| 94 o = sob.loadValueFromFile('persisttest.python', 'foo') | |
| 95 self.failUnlessEqual(o, [1,2,3]) | |
| 96 | |
| 97 def testEncryptedPython(self): | |
| 98 try: | |
| 99 import Crypto | |
| 100 except ImportError: | |
| 101 raise unittest.SkipTest() | |
| 102 phrase='once I was the king of spain' | |
| 103 open("epersisttest.python", 'w').write( | |
| 104 sob._encrypt(phrase, 'foo=[1,2,3]')) | |
| 105 o = sob.loadValueFromFile('epersisttest.python', 'foo', phrase) | |
| 106 self.failUnlessEqual(o, [1,2,3]) | |
| 107 | |
| 108 def testTypeGuesser(self): | |
| 109 self.assertRaises(KeyError, sob.guessType, "file.blah") | |
| 110 self.assertEqual('python', sob.guessType("file.py")) | |
| 111 self.assertEqual('python', sob.guessType("file.tac")) | |
| 112 self.assertEqual('python', sob.guessType("file.etac")) | |
| 113 self.assertEqual('pickle', sob.guessType("file.tap")) | |
| 114 self.assertEqual('pickle', sob.guessType("file.etap")) | |
| 115 self.assertEqual('source', sob.guessType("file.tas")) | |
| 116 self.assertEqual('source', sob.guessType("file.etas")) | |
| 117 if gotMicrodom: | |
| 118 self.assertEqual('xml', sob.guessType("file.tax")) | |
| 119 self.assertEqual('xml', sob.guessType("file.etax")) | |
| 120 | |
| 121 def testEverythingEphemeralGetattr(self): | |
| 122 """ | |
| 123 Verify that _EverythingEphermal.__getattr__ works. | |
| 124 """ | |
| 125 self.fakeMain.testMainModGetattr = 1 | |
| 126 | |
| 127 dirname = self.mktemp() | |
| 128 os.mkdir(dirname) | |
| 129 | |
| 130 filename = os.path.join(dirname, 'persisttest.ee_getattr') | |
| 131 | |
| 132 f = file(filename, 'w') | |
| 133 f.write('import __main__\n') | |
| 134 f.write('if __main__.testMainModGetattr != 1: raise AssertionError\n') | |
| 135 f.write('app = None\n') | |
| 136 f.close() | |
| 137 | |
| 138 sob.load(filename, 'source') | |
| 139 | |
| 140 def testEverythingEphemeralSetattr(self): | |
| 141 """ | |
| 142 Verify that _EverythingEphemeral.__setattr__ won't affect __main__. | |
| 143 """ | |
| 144 self.fakeMain.testMainModSetattr = 1 | |
| 145 | |
| 146 dirname = self.mktemp() | |
| 147 os.mkdir(dirname) | |
| 148 | |
| 149 filename = os.path.join(dirname, 'persisttest.ee_setattr') | |
| 150 f = file(filename, 'w') | |
| 151 f.write('import __main__\n') | |
| 152 f.write('__main__.testMainModSetattr = 2\n') | |
| 153 f.write('app = None\n') | |
| 154 f.close() | |
| 155 | |
| 156 sob.load(filename, 'source') | |
| 157 | |
| 158 self.assertEqual(self.fakeMain.testMainModSetattr, 1) | |
| 159 | |
| 160 def testEverythingEphemeralException(self): | |
| 161 """ | |
| 162 Test that an exception during load() won't cause _EE to mask __main__ | |
| 163 """ | |
| 164 dirname = self.mktemp() | |
| 165 os.mkdir(dirname) | |
| 166 filename = os.path.join(dirname, 'persisttest.ee_exception') | |
| 167 | |
| 168 f = file(filename, 'w') | |
| 169 f.write('raise ValueError\n') | |
| 170 f.close() | |
| 171 | |
| 172 self.assertRaises(ValueError, sob.load, filename, 'source') | |
| 173 self.assertEqual(type(sys.modules['__main__']), FakeModule) | |
| 174 | |
| 175 def setUp(self): | |
| 176 """ | |
| 177 Replace the __main__ module with a fake one, so that it can be mutated | |
| 178 in tests | |
| 179 """ | |
| 180 self.realMain = sys.modules['__main__'] | |
| 181 self.fakeMain = sys.modules['__main__'] = FakeModule() | |
| 182 | |
| 183 def tearDown(self): | |
| 184 """ | |
| 185 Restore __main__ to its original value | |
| 186 """ | |
| 187 sys.modules['__main__'] = self.realMain | |
| 188 | |
| OLD | NEW |