test_share.py 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. # coding: utf-8
  2. import pytest
  3. from synergine2.exceptions import UnknownSharedData
  4. from synergine2 import share
  5. from tests import BaseTest
  6. class TestShare(BaseTest):
  7. def test_simple_share_with_class(self):
  8. shared = share.SharedDataManager()
  9. class Foo(object):
  10. counter = shared.create('counter', value=0)
  11. foo = Foo()
  12. foo.counter = 42
  13. assert shared.get('counter') == 42
  14. foo.counter = 48
  15. assert shared.get('counter') == 48
  16. def test_default_value(self):
  17. shared = share.SharedDataManager()
  18. class Foo(object):
  19. counter = shared.create('counter', 0)
  20. foo = Foo()
  21. foo.counter = 42
  22. assert shared.get('counter') == 42
  23. foo.counter = 48
  24. assert shared.get('counter') == 48
  25. def test_dynamic_key(self):
  26. shared = share.SharedDataManager()
  27. class Foo(object):
  28. counter = shared.create(
  29. ['{id}', 'counter'],
  30. value=0,
  31. indexes=[],
  32. )
  33. @property
  34. def id(self):
  35. return id(self)
  36. foo = Foo()
  37. foo.counter = 42
  38. assert shared.get(foo.id, 'counter') == 42
  39. foo.counter = 48
  40. assert shared.get(foo.id, 'counter') == 48
  41. def test_multiple_uses(self):
  42. shared = share.SharedDataManager()
  43. class Foo(object):
  44. position = shared.create(
  45. '{id}_position',
  46. (0, 0, 0),
  47. indexes=[],
  48. )
  49. @property
  50. def id(self):
  51. return id(self)
  52. foo = Foo()
  53. foo.position = (0, 1, 2)
  54. assert shared.get('{}_position'.format(foo.id)) == (0, 1, 2)
  55. foo2 = Foo()
  56. foo2.position = (3, 4, 5)
  57. assert shared.get('{}_position'.format(foo.id)) == (0, 1, 2)
  58. assert shared.get('{}_position'.format(foo2.id)) == (3, 4, 5)
  59. def test_update_dict_with_pointer(self):
  60. shared = share.SharedDataManager()
  61. class Foo(object):
  62. data = shared.create('data', {})
  63. foo = Foo()
  64. foo.data = {'foo': 'bar'}
  65. assert shared.get('data') == {'foo': 'bar'}
  66. foo.data['foo'] = 'buz'
  67. assert shared.get('data') == {'foo': 'buz'}
  68. def test_refresh_without_commit(self):
  69. shared = share.SharedDataManager()
  70. class Foo(object):
  71. counter = shared.create('counter', 0)
  72. foo = Foo()
  73. foo.counter = 42
  74. assert shared.get('counter') == 42
  75. shared.refresh()
  76. with pytest.raises(UnknownSharedData):
  77. shared.get('counter')
  78. def test_commit(self):
  79. shared = share.SharedDataManager()
  80. class Foo(object):
  81. counter = shared.create('counter', 0)
  82. foo = Foo()
  83. foo.counter = 42
  84. assert shared.get('counter') == 42
  85. shared.commit()
  86. assert shared.get('counter') == 42
  87. def test_commit_then_refresh(self):
  88. shared = share.SharedDataManager()
  89. class Foo(object):
  90. counter = shared.create('counter', 0)
  91. foo = Foo()
  92. foo.counter = 42
  93. assert shared.get('counter') == 42
  94. shared.commit()
  95. shared.refresh()
  96. assert shared.get('counter') == 42
  97. def test_position_index(self):
  98. class ListIndex(share.SharedDataIndex):
  99. def add(self, value):
  100. try:
  101. values = self.shared_data_manager.get(self.key)
  102. except UnknownSharedData:
  103. values = []
  104. values.append(value)
  105. self.shared_data_manager.set(self.key, values)
  106. def remove(self, value):
  107. values = self.shared_data_manager.get(self.key)
  108. values.remove(value)
  109. self.shared_data_manager.set(self.key, values)
  110. shared = share.SharedDataManager()
  111. class Foo(object):
  112. position = shared.create(
  113. '{id}_position',
  114. (0, 0, 0),
  115. indexes=[shared.make_index(ListIndex, 'positions')],
  116. )
  117. @property
  118. def id(self):
  119. return id(self)
  120. with pytest.raises(UnknownSharedData):
  121. shared.get('positions')
  122. foo = Foo()
  123. foo.position = (0, 1, 2)
  124. assert shared.get('{}_position'.format(foo.id)) == (0, 1, 2)
  125. assert shared.get('positions') == [(0, 1, 2)]
  126. foo2 = Foo()
  127. foo2.position = (3, 4, 5)
  128. assert shared.get('{}_position'.format(foo.id)) == (0, 1, 2)
  129. assert shared.get('{}_position'.format(foo2.id)) == (3, 4, 5)
  130. assert shared.get('positions') == [(0, 1, 2), (3, 4, 5)]
  131. foo2.position = (6, 7, 8)
  132. assert shared.get('{}_position'.format(foo2.id)) == (6, 7, 8)
  133. assert shared.get('positions') == [(0, 1, 2), (6, 7, 8)]
  134. class TestIndexes(BaseTest):
  135. def test_list_index(self):
  136. shared = share.SharedDataManager()
  137. class Foo(object):
  138. position = shared.create(
  139. '{id}_position',
  140. (0, 0, 0),
  141. indexes=[shared.make_index(share.ListIndex, 'positions')],
  142. )
  143. @property
  144. def id(self):
  145. return id(self)
  146. with pytest.raises(UnknownSharedData):
  147. shared.get('positions')
  148. foo = Foo()
  149. foo.position = (0, 1, 2)
  150. assert shared.get('{}_position'.format(foo.id)) == (0, 1, 2)
  151. assert shared.get('positions') == [(0, 1, 2)]
  152. foo2 = Foo()
  153. foo2.position = (3, 4, 5)
  154. assert shared.get('{}_position'.format(foo.id)) == (0, 1, 2)
  155. assert shared.get('{}_position'.format(foo2.id)) == (3, 4, 5)
  156. assert shared.get('positions') == [(0, 1, 2), (3, 4, 5)]
  157. foo2.position = (6, 7, 8)
  158. assert shared.get('{}_position'.format(foo2.id)) == (6, 7, 8)
  159. assert shared.get('positions') == [(0, 1, 2), (6, 7, 8)]