Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_sob.py 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. # Copyright (c) Twisted Matrix Laboratories.
  2. # See LICENSE for details.
  3. import os
  4. import sys
  5. from textwrap import dedent
  6. from twisted.persisted import sob
  7. from twisted.persisted.styles import Ephemeral
  8. from twisted.python import components
  9. from twisted.trial import unittest
  10. class Dummy(components.Componentized):
  11. pass
  12. objects = [
  13. 1,
  14. "hello",
  15. (1, "hello"),
  16. [1, "hello"],
  17. {1: "hello"},
  18. ]
  19. class FakeModule:
  20. pass
  21. class PersistTests(unittest.TestCase):
  22. def testStyles(self):
  23. for o in objects:
  24. p = sob.Persistent(o, "")
  25. for style in "source pickle".split():
  26. p.setStyle(style)
  27. p.save(filename="persisttest." + style)
  28. o1 = sob.load("persisttest." + style, style)
  29. self.assertEqual(o, o1)
  30. def testStylesBeingSet(self):
  31. o = Dummy()
  32. o.foo = 5
  33. o.setComponent(sob.IPersistable, sob.Persistent(o, "lala"))
  34. for style in "source pickle".split():
  35. sob.IPersistable(o).setStyle(style)
  36. sob.IPersistable(o).save(filename="lala." + style)
  37. o1 = sob.load("lala." + style, style)
  38. self.assertEqual(o.foo, o1.foo)
  39. self.assertEqual(sob.IPersistable(o1).style, style)
  40. def testPassphraseError(self):
  41. """
  42. Calling save() with a passphrase is an error.
  43. """
  44. p = sob.Persistant(None, "object")
  45. self.assertRaises(TypeError, p.save, "filename.pickle", passphrase="abc")
  46. def testNames(self):
  47. o = [1, 2, 3]
  48. p = sob.Persistent(o, "object")
  49. for style in "source pickle".split():
  50. p.setStyle(style)
  51. p.save()
  52. o1 = sob.load("object.ta" + style[0], style)
  53. self.assertEqual(o, o1)
  54. for tag in "lala lolo".split():
  55. p.save(tag)
  56. o1 = sob.load("object-" + tag + ".ta" + style[0], style)
  57. self.assertEqual(o, o1)
  58. def testPython(self):
  59. with open("persisttest.python", "w") as f:
  60. f.write("foo=[1,2,3] ")
  61. o = sob.loadValueFromFile("persisttest.python", "foo")
  62. self.assertEqual(o, [1, 2, 3])
  63. def testTypeGuesser(self):
  64. self.assertRaises(KeyError, sob.guessType, "file.blah")
  65. self.assertEqual("python", sob.guessType("file.py"))
  66. self.assertEqual("python", sob.guessType("file.tac"))
  67. self.assertEqual("python", sob.guessType("file.etac"))
  68. self.assertEqual("pickle", sob.guessType("file.tap"))
  69. self.assertEqual("pickle", sob.guessType("file.etap"))
  70. self.assertEqual("source", sob.guessType("file.tas"))
  71. self.assertEqual("source", sob.guessType("file.etas"))
  72. def testEverythingEphemeralGetattr(self):
  73. """
  74. L{_EverythingEphermal.__getattr__} will proxy the __main__ module as an
  75. L{Ephemeral} object, and during load will be transparent, but after
  76. load will return L{Ephemeral} objects from any accessed attributes.
  77. """
  78. self.fakeMain.testMainModGetattr = 1
  79. dirname = self.mktemp()
  80. os.mkdir(dirname)
  81. filename = os.path.join(dirname, "persisttest.ee_getattr")
  82. global mainWhileLoading
  83. mainWhileLoading = None
  84. with open(filename, "w") as f:
  85. f.write(
  86. dedent(
  87. """
  88. app = []
  89. import __main__
  90. app.append(__main__.testMainModGetattr == 1)
  91. try:
  92. __main__.somethingElse
  93. except AttributeError:
  94. app.append(True)
  95. else:
  96. app.append(False)
  97. from twisted.test import test_sob
  98. test_sob.mainWhileLoading = __main__
  99. """
  100. )
  101. )
  102. loaded = sob.load(filename, "source")
  103. self.assertIsInstance(loaded, list)
  104. self.assertTrue(loaded[0], "Expected attribute not set.")
  105. self.assertTrue(loaded[1], "Unexpected attribute set.")
  106. self.assertIsInstance(mainWhileLoading, Ephemeral)
  107. self.assertIsInstance(mainWhileLoading.somethingElse, Ephemeral)
  108. del mainWhileLoading
  109. def testEverythingEphemeralSetattr(self):
  110. """
  111. Verify that _EverythingEphemeral.__setattr__ won't affect __main__.
  112. """
  113. self.fakeMain.testMainModSetattr = 1
  114. dirname = self.mktemp()
  115. os.mkdir(dirname)
  116. filename = os.path.join(dirname, "persisttest.ee_setattr")
  117. with open(filename, "w") as f:
  118. f.write("import __main__\n")
  119. f.write("__main__.testMainModSetattr = 2\n")
  120. f.write("app = None\n")
  121. sob.load(filename, "source")
  122. self.assertEqual(self.fakeMain.testMainModSetattr, 1)
  123. def testEverythingEphemeralException(self):
  124. """
  125. Test that an exception during load() won't cause _EE to mask __main__
  126. """
  127. dirname = self.mktemp()
  128. os.mkdir(dirname)
  129. filename = os.path.join(dirname, "persisttest.ee_exception")
  130. with open(filename, "w") as f:
  131. f.write("raise ValueError\n")
  132. self.assertRaises(ValueError, sob.load, filename, "source")
  133. self.assertEqual(type(sys.modules["__main__"]), FakeModule)
  134. def setUp(self):
  135. """
  136. Replace the __main__ module with a fake one, so that it can be mutated
  137. in tests
  138. """
  139. self.realMain = sys.modules["__main__"]
  140. self.fakeMain = sys.modules["__main__"] = FakeModule()
  141. def tearDown(self):
  142. """
  143. Restore __main__ to its original value
  144. """
  145. sys.modules["__main__"] = self.realMain