test_share.py 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. # coding: utf-8
  2. import pytest
  3. from synergine2.exceptions import UnknownSharedData
  4. from synergine2.share import SharedDataManager
  5. from tests import BaseTest
  6. class TestShare(BaseTest):
  7. def test_simple_share_with_class(self):
  8. shared = SharedDataManager()
  9. class Foo(object):
  10. counter = shared.create('counter', 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_dynamic_key(self):
  17. shared = SharedDataManager()
  18. class Foo(object):
  19. counter = shared.create(
  20. '{id}_counter',
  21. (0, 0, 0),
  22. indexes=[],
  23. )
  24. @property
  25. def id(self):
  26. return id(self)
  27. foo = Foo()
  28. foo.counter = 42
  29. assert shared.get('{}_counter'.format(foo.id)) == 42
  30. foo.counter = 48
  31. assert shared.get('{}_counter'.format(foo.id)) == 48
  32. def test_update_dict_with_pointer(self):
  33. shared = SharedDataManager()
  34. class Foo(object):
  35. data = shared.create('data', {})
  36. foo = Foo()
  37. foo.data = {'foo': 'bar'}
  38. assert shared.get('data') == {'foo': 'bar'}
  39. foo.data['foo'] = 'buz'
  40. assert shared.get('data') == {'foo': 'buz'}
  41. def test_refresh_without_commit(self):
  42. shared = SharedDataManager()
  43. class Foo(object):
  44. counter = shared.create('counter', 0)
  45. foo = Foo()
  46. foo.counter = 42
  47. assert shared.get('counter') == 42
  48. shared.refresh()
  49. with pytest.raises(UnknownSharedData):
  50. shared.get('counter')
  51. def test_commit(self):
  52. shared = SharedDataManager()
  53. class Foo(object):
  54. counter = shared.create('counter', 0)
  55. foo = Foo()
  56. foo.counter = 42
  57. assert shared.get('counter') == 42
  58. shared.commit()
  59. assert shared.get('counter') == 42
  60. def test_commit_then_refresh(self):
  61. shared = SharedDataManager()
  62. class Foo(object):
  63. counter = shared.create('counter', 0)
  64. foo = Foo()
  65. foo.counter = 42
  66. assert shared.get('counter') == 42
  67. shared.commit()
  68. shared.refresh()
  69. assert shared.get('counter') == 42
  70. def test_indexes(self):
  71. pass