Browse Source

add ListIndex to shares indexes

Bastien Sevajol 7 years ago
parent
commit
c7f773491e
2 changed files with 62 additions and 12 deletions
  1. 15 0
      synergine2/share.py
  2. 47 12
      tests/test_share.py

+ 15 - 0
synergine2/share.py View File

122
 
122
 
123
         return shared_property
123
         return shared_property
124
 
124
 
125
+
126
+class ListIndex(SharedDataIndex):
127
+    def add(self, value):
128
+        try:
129
+            values = self.shared_data_manager.get(self.key)
130
+        except UnknownSharedData:
131
+            values = []
132
+
133
+        values.append(value)
134
+        self.shared_data_manager.set(self.key, values)
135
+
136
+    def remove(self, value):
137
+        values = self.shared_data_manager.get(self.key)
138
+        values.remove(value)
139
+        self.shared_data_manager.set(self.key, values)

+ 47 - 12
tests/test_share.py View File

2
 import pytest
2
 import pytest
3
 
3
 
4
 from synergine2.exceptions import UnknownSharedData
4
 from synergine2.exceptions import UnknownSharedData
5
-from synergine2.share import SharedDataManager
6
-from synergine2.share import SharedDataIndex
5
+from synergine2 import share
7
 from tests import BaseTest
6
 from tests import BaseTest
8
 
7
 
9
 
8
 
10
 class TestShare(BaseTest):
9
 class TestShare(BaseTest):
11
     def test_simple_share_with_class(self):
10
     def test_simple_share_with_class(self):
12
-        shared = SharedDataManager()
11
+        shared = share.SharedDataManager()
13
 
12
 
14
         class Foo(object):
13
         class Foo(object):
15
             counter = shared.create('counter', value=0)
14
             counter = shared.create('counter', value=0)
24
         assert shared.get('counter') == 48
23
         assert shared.get('counter') == 48
25
 
24
 
26
     def test_default_value(self):
25
     def test_default_value(self):
27
-        shared = SharedDataManager()
26
+        shared = share.SharedDataManager()
28
 
27
 
29
         class Foo(object):
28
         class Foo(object):
30
             counter = shared.create('counter', 0)
29
             counter = shared.create('counter', 0)
39
         assert shared.get('counter') == 48
38
         assert shared.get('counter') == 48
40
 
39
 
41
     def test_dynamic_key(self):
40
     def test_dynamic_key(self):
42
-        shared = SharedDataManager()
41
+        shared = share.SharedDataManager()
43
 
42
 
44
         class Foo(object):
43
         class Foo(object):
45
             counter = shared.create(
44
             counter = shared.create(
62
         assert shared.get(foo.id, 'counter') == 48
61
         assert shared.get(foo.id, 'counter') == 48
63
 
62
 
64
     def test_multiple_uses(self):
63
     def test_multiple_uses(self):
65
-        shared = SharedDataManager()
64
+        shared = share.SharedDataManager()
66
 
65
 
67
         class Foo(object):
66
         class Foo(object):
68
             position = shared.create(
67
             position = shared.create(
87
         assert shared.get('{}_position'.format(foo2.id)) == (3, 4, 5)
86
         assert shared.get('{}_position'.format(foo2.id)) == (3, 4, 5)
88
 
87
 
89
     def test_update_dict_with_pointer(self):
88
     def test_update_dict_with_pointer(self):
90
-        shared = SharedDataManager()
89
+        shared = share.SharedDataManager()
91
 
90
 
92
         class Foo(object):
91
         class Foo(object):
93
             data = shared.create('data', {})
92
             data = shared.create('data', {})
101
         assert shared.get('data') == {'foo': 'buz'}
100
         assert shared.get('data') == {'foo': 'buz'}
102
 
101
 
103
     def test_refresh_without_commit(self):
102
     def test_refresh_without_commit(self):
104
-        shared = SharedDataManager()
103
+        shared = share.SharedDataManager()
105
 
104
 
106
         class Foo(object):
105
         class Foo(object):
107
             counter = shared.create('counter', 0)
106
             counter = shared.create('counter', 0)
116
             shared.get('counter')
115
             shared.get('counter')
117
 
116
 
118
     def test_commit(self):
117
     def test_commit(self):
119
-        shared = SharedDataManager()
118
+        shared = share.SharedDataManager()
120
 
119
 
121
         class Foo(object):
120
         class Foo(object):
122
             counter = shared.create('counter', 0)
121
             counter = shared.create('counter', 0)
130
         assert shared.get('counter') == 42
129
         assert shared.get('counter') == 42
131
 
130
 
132
     def test_commit_then_refresh(self):
131
     def test_commit_then_refresh(self):
133
-        shared = SharedDataManager()
132
+        shared = share.SharedDataManager()
134
 
133
 
135
         class Foo(object):
134
         class Foo(object):
136
             counter = shared.create('counter', 0)
135
             counter = shared.create('counter', 0)
145
         assert shared.get('counter') == 42
144
         assert shared.get('counter') == 42
146
 
145
 
147
     def test_position_index(self):
146
     def test_position_index(self):
148
-        class ListIndex(SharedDataIndex):
147
+        class ListIndex(share.SharedDataIndex):
149
             def add(self, value):
148
             def add(self, value):
150
                 try:
149
                 try:
151
                     values = self.shared_data_manager.get(self.key)
150
                     values = self.shared_data_manager.get(self.key)
160
                 values.remove(value)
159
                 values.remove(value)
161
                 self.shared_data_manager.set(self.key, values)
160
                 self.shared_data_manager.set(self.key, values)
162
 
161
 
163
-        shared = SharedDataManager()
162
+        shared = share.SharedDataManager()
164
 
163
 
165
         class Foo(object):
164
         class Foo(object):
166
             position = shared.create(
165
             position = shared.create(
192
         foo2.position = (6, 7, 8)
191
         foo2.position = (6, 7, 8)
193
         assert shared.get('{}_position'.format(foo2.id)) == (6, 7, 8)
192
         assert shared.get('{}_position'.format(foo2.id)) == (6, 7, 8)
194
         assert shared.get('positions') == [(0, 1, 2), (6, 7, 8)]
193
         assert shared.get('positions') == [(0, 1, 2), (6, 7, 8)]
194
+
195
+
196
+class TestIndexes(BaseTest):
197
+    def test_list_index(self):
198
+        shared = share.SharedDataManager()
199
+
200
+        class Foo(object):
201
+            position = shared.create(
202
+                '{id}_position',
203
+                (0, 0, 0),
204
+                indexes=[shared.make_index(share.ListIndex, 'positions')],
205
+            )
206
+
207
+            @property
208
+            def id(self):
209
+                return id(self)
210
+
211
+        with pytest.raises(UnknownSharedData):
212
+            shared.get('positions')
213
+
214
+        foo = Foo()
215
+        foo.position = (0, 1, 2)
216
+
217
+        assert shared.get('{}_position'.format(foo.id)) == (0, 1, 2)
218
+        assert shared.get('positions') == [(0, 1, 2)]
219
+
220
+        foo2 = Foo()
221
+        foo2.position = (3, 4, 5)
222
+
223
+        assert shared.get('{}_position'.format(foo.id)) == (0, 1, 2)
224
+        assert shared.get('{}_position'.format(foo2.id)) == (3, 4, 5)
225
+        assert shared.get('positions') == [(0, 1, 2), (3, 4, 5)]
226
+
227
+        foo2.position = (6, 7, 8)
228
+        assert shared.get('{}_position'.format(foo2.id)) == (6, 7, 8)
229
+        assert shared.get('positions') == [(0, 1, 2), (6, 7, 8)]