Procházet zdrojové kódy

add ListIndex to shares indexes

Bastien Sevajol před 6 roky
rodič
revize
c7f773491e
2 změnil soubory, kde provedl 62 přidání a 12 odebrání
  1. 15 0
      synergine2/share.py
  2. 47 12
      tests/test_share.py

+ 15 - 0
synergine2/share.py Zobrazit soubor

@@ -122,3 +122,18 @@ class SharedDataManager(object):
122 122
 
123 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 Zobrazit soubor

@@ -2,14 +2,13 @@
2 2
 import pytest
3 3
 
4 4
 from synergine2.exceptions import UnknownSharedData
5
-from synergine2.share import SharedDataManager
6
-from synergine2.share import SharedDataIndex
5
+from synergine2 import share
7 6
 from tests import BaseTest
8 7
 
9 8
 
10 9
 class TestShare(BaseTest):
11 10
     def test_simple_share_with_class(self):
12
-        shared = SharedDataManager()
11
+        shared = share.SharedDataManager()
13 12
 
14 13
         class Foo(object):
15 14
             counter = shared.create('counter', value=0)
@@ -24,7 +23,7 @@ class TestShare(BaseTest):
24 23
         assert shared.get('counter') == 48
25 24
 
26 25
     def test_default_value(self):
27
-        shared = SharedDataManager()
26
+        shared = share.SharedDataManager()
28 27
 
29 28
         class Foo(object):
30 29
             counter = shared.create('counter', 0)
@@ -39,7 +38,7 @@ class TestShare(BaseTest):
39 38
         assert shared.get('counter') == 48
40 39
 
41 40
     def test_dynamic_key(self):
42
-        shared = SharedDataManager()
41
+        shared = share.SharedDataManager()
43 42
 
44 43
         class Foo(object):
45 44
             counter = shared.create(
@@ -62,7 +61,7 @@ class TestShare(BaseTest):
62 61
         assert shared.get(foo.id, 'counter') == 48
63 62
 
64 63
     def test_multiple_uses(self):
65
-        shared = SharedDataManager()
64
+        shared = share.SharedDataManager()
66 65
 
67 66
         class Foo(object):
68 67
             position = shared.create(
@@ -87,7 +86,7 @@ class TestShare(BaseTest):
87 86
         assert shared.get('{}_position'.format(foo2.id)) == (3, 4, 5)
88 87
 
89 88
     def test_update_dict_with_pointer(self):
90
-        shared = SharedDataManager()
89
+        shared = share.SharedDataManager()
91 90
 
92 91
         class Foo(object):
93 92
             data = shared.create('data', {})
@@ -101,7 +100,7 @@ class TestShare(BaseTest):
101 100
         assert shared.get('data') == {'foo': 'buz'}
102 101
 
103 102
     def test_refresh_without_commit(self):
104
-        shared = SharedDataManager()
103
+        shared = share.SharedDataManager()
105 104
 
106 105
         class Foo(object):
107 106
             counter = shared.create('counter', 0)
@@ -116,7 +115,7 @@ class TestShare(BaseTest):
116 115
             shared.get('counter')
117 116
 
118 117
     def test_commit(self):
119
-        shared = SharedDataManager()
118
+        shared = share.SharedDataManager()
120 119
 
121 120
         class Foo(object):
122 121
             counter = shared.create('counter', 0)
@@ -130,7 +129,7 @@ class TestShare(BaseTest):
130 129
         assert shared.get('counter') == 42
131 130
 
132 131
     def test_commit_then_refresh(self):
133
-        shared = SharedDataManager()
132
+        shared = share.SharedDataManager()
134 133
 
135 134
         class Foo(object):
136 135
             counter = shared.create('counter', 0)
@@ -145,7 +144,7 @@ class TestShare(BaseTest):
145 144
         assert shared.get('counter') == 42
146 145
 
147 146
     def test_position_index(self):
148
-        class ListIndex(SharedDataIndex):
147
+        class ListIndex(share.SharedDataIndex):
149 148
             def add(self, value):
150 149
                 try:
151 150
                     values = self.shared_data_manager.get(self.key)
@@ -160,7 +159,7 @@ class TestShare(BaseTest):
160 159
                 values.remove(value)
161 160
                 self.shared_data_manager.set(self.key, values)
162 161
 
163
-        shared = SharedDataManager()
162
+        shared = share.SharedDataManager()
164 163
 
165 164
         class Foo(object):
166 165
             position = shared.create(
@@ -192,3 +191,39 @@ class TestShare(BaseTest):
192 191
         foo2.position = (6, 7, 8)
193 192
         assert shared.get('{}_position'.format(foo2.id)) == (6, 7, 8)
194 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)]