test_share.py 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. # coding: utf-8
  2. import pickle
  3. import pytest
  4. from synergine2.base import IdentifiedObject
  5. from synergine2.exceptions import UnknownSharedData
  6. from synergine2 import share
  7. from tests import BaseTest
  8. class TestShare(BaseTest):
  9. def test_simple_share_with_class(self):
  10. shared = share.SharedDataManager()
  11. class Foo(object):
  12. counter = shared.create('counter', 0)
  13. foo = Foo()
  14. foo.counter = 42
  15. assert shared.get('counter') == 42
  16. foo.counter = 48
  17. assert shared.get('counter') == 48
  18. def test_refresh_without_commit(self):
  19. shared = share.SharedDataManager()
  20. class Foo(object):
  21. counter = shared.create('counter', 0)
  22. foo = Foo()
  23. foo.counter = 42
  24. assert shared.get('counter') == 42
  25. shared.refresh()
  26. with pytest.raises(UnknownSharedData):
  27. shared.get('counter')
  28. def test_commit(self):
  29. shared = share.SharedDataManager()
  30. class Foo(object):
  31. counter = shared.create('counter', 0)
  32. foo = Foo()
  33. foo.counter = 42
  34. assert shared.get('counter') == 42
  35. shared.commit()
  36. assert shared.get('counter') == 42
  37. def test_commit_then_refresh(self):
  38. shared = share.SharedDataManager()
  39. class Foo(object):
  40. counter = shared.create('counter', 0)
  41. foo = Foo()
  42. foo.counter = 42
  43. assert shared.get('counter') == 42
  44. shared.commit()
  45. shared.refresh()
  46. assert shared.get('counter') == 42
  47. def test_position_index(self):
  48. class ListIndex(share.SharedDataIndex):
  49. def add(self, subject, value):
  50. try:
  51. values = self.shared_data_manager.get(self.key)
  52. except UnknownSharedData:
  53. values = []
  54. values.append(value)
  55. self.shared_data_manager.set(self.key, values)
  56. def remove(self, subject, value):
  57. values = self.shared_data_manager.get(self.key)
  58. values.remove(value)
  59. self.shared_data_manager.set(self.key, values)
  60. shared = share.SharedDataManager()
  61. class Foo(IdentifiedObject):
  62. position = shared.create_self(
  63. 'position',
  64. (0, 0, 0),
  65. indexes=[shared.make_index(ListIndex, 'positions')],
  66. )
  67. with pytest.raises(UnknownSharedData):
  68. shared.get('positions')
  69. foo = Foo()
  70. foo.position = (0, 1, 2)
  71. assert shared.get('{}_position'.format(foo.id)) == (0, 1, 2)
  72. assert shared.get('positions') == [(0, 1, 2)]
  73. foo2 = Foo()
  74. foo2.position = (3, 4, 5)
  75. assert shared.get('{}_position'.format(foo.id)) == (0, 1, 2)
  76. assert shared.get('{}_position'.format(foo2.id)) == (3, 4, 5)
  77. assert shared.get('positions') == [(0, 1, 2), (3, 4, 5)]
  78. foo2.position = (6, 7, 8)
  79. assert shared.get('{}_position'.format(foo2.id)) == (6, 7, 8)
  80. assert shared.get('positions') == [(0, 1, 2), (6, 7, 8)]
  81. def test_auto_self_shared_data(self):
  82. shared = share.SharedDataManager()
  83. class MySubject(IdentifiedObject):
  84. age = shared.create_self('age', 0)
  85. @property
  86. def id(self):
  87. return id(self)
  88. subject = MySubject()
  89. assert subject.age == 0
  90. def test_auto_self_shared_data_with_callable(self):
  91. shared = share.SharedDataManager()
  92. class MySubject(IdentifiedObject):
  93. colors = shared.create_self('colors', lambda: [])
  94. @property
  95. def id(self):
  96. return id(self)
  97. subjecta = MySubject()
  98. subjectb = MySubject()
  99. assert subjecta.colors == []
  100. assert subjectb.colors == []
  101. subjecta.colors = ['toto']
  102. subjectb.colors = ['tata']
  103. assert subjecta.colors == ['toto']
  104. assert subjectb.colors == ['tata']
  105. def test_update_self_dict_with_pointer(self):
  106. shared = share.SharedDataManager()
  107. class Foo(IdentifiedObject):
  108. data = shared.create_self('data', lambda: {})
  109. foo = Foo()
  110. foo.data = {'foo': 'bar'}
  111. assert foo.data == {'foo': 'bar'}
  112. foo.data['foo'] = 'buz'
  113. # In this case local data is used
  114. assert foo.data == {'foo': 'buz'}
  115. shared.commit()
  116. shared.purge_data()
  117. # In this case, "data" key was pending and has been commit
  118. assert foo.data == {'foo': 'buz'}
  119. # In this case "data" key was NOT pending and has not been commit
  120. foo.data['foo'] = 'bAz'
  121. shared.commit()
  122. shared.purge_data()
  123. assert foo.data == {'foo': 'bAz'}
  124. def test_update_self_list_with_pointer(self):
  125. shared = share.SharedDataManager()
  126. class Foo(IdentifiedObject):
  127. data = shared.create_self('data', [])
  128. foo = Foo()
  129. foo.data = ['foo']
  130. assert foo.data == ['foo']
  131. foo.data.append('bar')
  132. assert foo.data == ['foo', 'bar']
  133. shared.commit()
  134. assert foo.data == ['foo', 'bar']
  135. foo.data.append('bAr')
  136. shared.commit()
  137. assert foo.data == ['foo', 'bar', 'bAr']
  138. def test_update_list_with_pointer(self):
  139. shared = share.SharedDataManager()
  140. class Foo(IdentifiedObject):
  141. data = shared.create('data', [])
  142. foo = Foo()
  143. foo.data = ['foo']
  144. assert foo.data == ['foo']
  145. assert shared.get('data') == ['foo']
  146. foo.data.append('bar')
  147. assert shared.get('data') == ['foo', 'bar']
  148. def test_update_dict_with_pointer(self):
  149. shared = share.SharedDataManager()
  150. class Foo(IdentifiedObject):
  151. data = shared.create('data', lambda: {})
  152. foo = Foo()
  153. foo.data = {'foo': 'bar'}
  154. assert shared.get('data') == {'foo': 'bar'}
  155. foo.data['foo'] = 'buz'
  156. # In this case local data is used
  157. assert shared.get('data') == {'foo': 'buz'}
  158. def test_update_dict_in_dict_with_pointer(self):
  159. shared = share.SharedDataManager()
  160. class Foo(IdentifiedObject):
  161. data = shared.create_self('data', lambda: {})
  162. foo = Foo()
  163. foo.data = {'foo': {'bar': 'baz'}}
  164. assert foo.data == {'foo': {'bar': 'baz'}}
  165. foo.data['foo'] = {'bar': 'baz'}
  166. # In this case local data is used
  167. assert foo.data == {'foo': {'bar': 'baz'}}
  168. shared.commit()
  169. shared.purge_data()
  170. # In this case, "data" key was pending and has been commit
  171. assert foo.data == {'foo': {'bar': 'baz'}}
  172. # In this case "data" key was NOT pending and has not been commit
  173. foo.data['foo']['bar'] = 'biz'
  174. foo.data = dict(foo.data)
  175. shared.commit()
  176. shared.purge_data()
  177. assert foo.data == {'foo': {'bar': 'biz'}}
  178. class TestIndexes(BaseTest):
  179. def test_list_index(self):
  180. shared = share.SharedDataManager()
  181. class Foo(IdentifiedObject):
  182. position = shared.create_self(
  183. 'position',
  184. (0, 0, 0),
  185. indexes=[shared.make_index(share.ListIndex, 'positions')],
  186. )
  187. with pytest.raises(UnknownSharedData):
  188. shared.get('positions')
  189. foo = Foo()
  190. foo.position = (0, 1, 2)
  191. assert shared.get('{}_position'.format(foo.id)) == (0, 1, 2)
  192. assert shared.get('positions') == [(0, 1, 2)]
  193. foo2 = Foo()
  194. foo2.position = (3, 4, 5)
  195. assert shared.get('{}_position'.format(foo.id)) == (0, 1, 2)
  196. assert shared.get('{}_position'.format(foo2.id)) == (3, 4, 5)
  197. assert shared.get('positions') == [(0, 1, 2), (3, 4, 5)]
  198. foo2.position = (6, 7, 8)
  199. assert shared.get('{}_position'.format(foo2.id)) == (6, 7, 8)
  200. assert shared.get('positions') == [(0, 1, 2), (6, 7, 8)]