proxy.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. import unittest
  2. import odml.tools.event
  3. from test.samplefile import parse
  4. import odml
  5. from odml.tools import proxy
  6. class TestProxy(unittest.TestCase):
  7. maxDiff = None
  8. def setUp(self):
  9. self.doc = parse("""
  10. s1[t1]
  11. - s2[t2]
  12. - p1
  13. """)
  14. def test_private_events(self):
  15. event_log = []
  16. def record(context):
  17. event_log.append((context.obj, context.action, context.val))
  18. p = odml.Property(name="p", value="1")
  19. p.add_change_handler(record)
  20. p.value = "2"
  21. p.value = "1"
  22. log1 = event_log
  23. event_log = []
  24. pp = proxy.PropertyProxy(p)
  25. pp.value = "2"
  26. pp.value = "1"
  27. self.assertEqual(log1, event_log)
  28. p.remove_change_handler(record)
  29. pp.add_change_handler(record)
  30. event_log = []
  31. pp.value = "2"
  32. pp.value = "1"
  33. self.assertEqual(log1, event_log)
  34. def test_section_events(self):
  35. s = odml.Section("sec1")
  36. p = odml.Property(name="p", value="1")
  37. s.append(p)
  38. event_log = []
  39. def record(context):
  40. event_log.append((context.obj, context.action, context.val))
  41. s.add_change_handler(record)
  42. p.value = "2"
  43. p.value = "1"
  44. log1 = event_log
  45. event_log = []
  46. s.remove_change_handler(record)
  47. ps = proxy.NonexistantSection("psec")
  48. pp = proxy.PropertyProxy(p)
  49. ps.append(pp)
  50. ps.add_change_handler(record)
  51. p.value = "2"
  52. p.value = "1"
  53. self.assertEqual(log1, event_log)
  54. def test_proxy_equality(self):
  55. p = odml.Property(name="p", value="1")
  56. pp = proxy.PropertyProxy(p)
  57. self.assertTrue(p == pp)
  58. s = odml.Section(name="sec1")
  59. ps = proxy.MappedSection(s)
  60. s.append(p)
  61. self.assertEqual(s, ps)
  62. def test_section_proxy(self):
  63. s = odml.Section("sec1")
  64. p = odml.Property(name="p", value="1")
  65. s.append(p)
  66. ps = proxy.MappedSection(s)
  67. # forward attributes
  68. ps.name = "sec2"
  69. self.assertEqual(s.name, "sec2")
  70. p2 = odml.Property(name="p2", value="2")
  71. # append to proxy section, creates a proxy in ps
  72. # and the original object in s
  73. ps.append(p2)
  74. self.assertIn(p2, s)
  75. self.assertIn(p2, ps)
  76. self.assertIs(s.contains(p2), p2)
  77. self.assertIsInstance(ps.contains(p2), proxy.Proxy)
  78. # removing from proxy section, removes both
  79. ps.remove(p2)
  80. self.assertNotIn(p2, s)
  81. self.assertNotIn(p2, ps)
  82. # appending to section, creates a proxy in ps
  83. s.append(p2)
  84. self.assertIn(p2, s)
  85. self.assertIn(p2, ps)
  86. self.assertIs(s.contains(p2), p2)
  87. self.assertIsInstance(ps.contains(p2), proxy.Proxy)
  88. # removing removes from ps too
  89. s.remove(p2)
  90. self.assertNotIn(p2, s)
  91. self.assertNotIn(p2, ps)
  92. # appending creates in both, removing in ps removes in both
  93. s.append(p2)
  94. ps.remove(p2)
  95. self.assertNotIn(p2, s)
  96. self.assertNotIn(p2, ps)
  97. # append a proxy to ps, both original and proxy compare to
  98. # be 'in' ps
  99. pp = proxy.PropertyProxy(p)
  100. # you can use proxy_append to add an explicit proxy obj (without affecting
  101. # the original section) or you can append the object to the original section
  102. # so that the proxy object is created in the proxy section, too
  103. ps.proxy_append(pp) # this one is only in ps
  104. self.assertIn(pp, ps)
  105. self.assertIn(p, ps) # as p == pp, this also holds true
  106. # even if the name is changed
  107. ps.name = "p3"
  108. self.assertEqual(p.name, "p") # does not change the name of p
  109. self.assertEqual(ps.name, "p3") # but the one of ps
  110. self.assertIn(pp, ps) # both are in ps though
  111. self.assertIn(p, ps)
  112. # and we can remove it again
  113. ps.remove(p)
  114. self.assertNotIn(p, ps)
  115. self.assertNotIn(pp, ps)
  116. self.assertNotIn(p, s)
  117. s2 = odml.Section("sec3")
  118. # a mapped section added to another mapped section
  119. # will only appear there
  120. ps2 = proxy.MappedSection(s2)
  121. ps.proxy_append(ps2)
  122. self.assertIn(ps2, ps)
  123. self.assertIn(s2, ps)
  124. self.assertNotIn(s2, s)
  125. # and we can remove it again
  126. ps.remove(s2)
  127. self.assertNotIn(ps2, ps)
  128. if __name__ == '__main__':
  129. unittest.main()