test_share.py 4.8KB

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