-
Notifications
You must be signed in to change notification settings - Fork 6
/
rtest.py
327 lines (198 loc) · 6.82 KB
/
rtest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
import os
from matchbox import models
from matchbox.database import db_initialization
db_initialization(os.environ['FIRESTORE'])
print("Test Model.")
class Class(models.Model):
name = models.TextField()
active = models.BooleanField(column_name='is_active')
list_f = models.ListField(blank=True)
map_f = models.MapField(blank=True)
Class.objects.delete()
c1 = Class.objects.create(active=True, name='DD21')
c2 = Class.objects.create(active=True, name='DD22')
c3 = Class.objects.create(active=False, name='CC22')
c4 = Class.objects.create(active=False, name='CC11')
assert len(list(Class.objects.all())) == 4
assert len(list(Class.objects.filter(active=True))) == 2
assert len(list(Class.objects.filter(active=False))) == 2
assert len(list(Class.objects.filter(active=True).filter(name='DD21'))) == 1
assert len(list(Class.objects.filter(active=True).filter(name='DD22'))) == 1
assert len(list(Class.objects.filter(active=False).filter(name='CC22'))) == 1
assert len(list(Class.objects.filter(active=False).filter(name='CC11'))) == 1
Class.objects.delete()
class BaseClass(models.Model):
name = models.TextField()
active = models.BooleanField(column_name='is_active')
list_f = models.ListField(blank=True, column_name='lista')
class Meta:
abstract = True
collection_name = 'collection_name'
class Class2(BaseClass):
map_f = models.MapField(blank=True, column_name='mapa')
Class2.objects.delete()
assert BaseClass._meta.abstract, True
assert BaseClass.collection_name(), 'collection_name'
assert Class2.collection_name(), 'class'
assert not Class2._meta.abstract
Class2.objects.create(
name='DDDD',
active=False,
map_f={'key': 'val'}
)
assert len(list(Class2.objects.filter(map_f__key='val'))) == 1
assert not len(list(Class2.objects.filter(map_f__key='val1')))
c1 = Class2.objects.filter(map_f__key='val').get()
c1.list_f = [1, 2, 3]
c1.save()
c1 = Class2.objects.get(id=c1.id)
assert c1.list_f, [1, 2, 3]
Class2.objects.delete()
class NewBaseClass(models.Model):
name = models.TextField(default='aaaaa')
active = models.BooleanField(column_name='is_active', default=False)
list_f = models.ListField(default=[], column_name='lista')
map_f = models.MapField(default={}, column_name='mapa')
class Meta:
abstract = True
collection_name = 'collection_name'
class NewClass(NewBaseClass):
age = models.IntegerField(default=0)
NewClass.objects.delete()
NewClass.objects.create()
assert len(list(NewClass.objects.all())) == 1
last_nc = list(NewClass.objects.all())[0]
assert isinstance(last_nc.list_f, list) is True
assert isinstance(last_nc.name, str) is True
assert isinstance(last_nc.active, bool) is True
assert isinstance(last_nc.age, int) is True
assert isinstance(last_nc.map_f, dict) is True
assert last_nc.list_f == []
assert last_nc.name == 'aaaaa'
assert last_nc.active is False
assert last_nc.map_f == {}
assert last_nc.age == 0
NewClass.objects.delete()
print("Test ref model")
class A(models.Model):
name = models.TextField()
class B(models.Model):
name = models.TextField()
class NewBaseClass(models.Model):
name = models.TextField(default='aaaaa')
active = models.BooleanField(column_name='is_active', default=False)
list_f = models.ListField(default=[], column_name='lista')
map_f = models.MapField(default={}, column_name='mapa')
ref_a = models.ReferenceField(A)
class Meta:
abstract = True
collection_name = 'collection_name'
class NewClass(NewBaseClass):
ref_b = models.ReferenceField(B)
A.objects.delete()
B.objects.delete()
NewClass.objects.delete()
a1 = A.objects.create(name='a1')
a2 = A(name='a2')
a2.save()
b1 = B.objects.create(name='b1')
b2 = B(name='b2')
b2.save()
nc1 = NewClass(ref_a=a1, name='test', active=False, list_f=[1, 2, 3])
nc1.save()
nc2 = NewClass.objects.create(
ref_a=a2, name='test2', active=True, ref_b=b1
)
assert isinstance(nc1.ref_a, A)
assert nc1.ref_a.id == a1.id
assert nc1.ref_a.name == a1.name
assert nc1.ref_b is None
assert isinstance(nc2.ref_a, A)
assert isinstance(nc2.ref_b, B)
assert nc2.ref_a.id == a2.id
assert nc2.ref_b.name == b1.name
assert nc2.ref_b.id == b1.id
all_ncs = list(NewClass.objects.all())
for anc in all_ncs:
assert isinstance(anc.ref_a, A) or isinstance(anc.ref_a, type(None))
assert isinstance(anc.ref_b, B) or isinstance(anc.ref_b, type(None))
A.objects.delete()
B.objects.delete()
NewClass.objects.delete()
print("Test Sub models")
import os
from matchbox import models
from matchbox.database import db_initialization
db_initialization(os.environ['FIRESTORE'])
class A(models.Model):
name = models.TextField()
active = models.BooleanField()
class B(models.Model):
name = models.TextField()
active = models.BooleanField()
class C(models.Model):
name = models.TextField()
active = models.BooleanField()
a1 = A.objects.create(name='Test', active=True)
a2 = A.objects.create(name='Test2', active=True)
B.set_base_path(a1)
b1 = B.objects.create(name='B Test', active=False)
b2 = B.objects.create(name='B Test 2', active=True)
assert b1.path == ('a', a1.id, 'b')
assert b2.path == ('a', a1.id, 'b')
assert len(list(B.objects.all())) == 2
C.set_base_path(b1)
assert C.path == ('a', a1.id, 'b', b1.id, 'c')
c1 = C.objects.create(name='C Test', active=True)
C.reset_base_path()
assert C.path == ('c', )
C.reset_base_path()
C.set_base_path(b2)
assert C.path == ('a', a1.id, 'b', b2.id, 'c')
c2 = C.objects.create(name='C Test', active=False)
C.reset_base_path()
C.set_base_path(b1)
assert len(list(C.objects.all())) == 1
assert C.objects.get().id == c1.id
C.reset_base_path()
C.set_base_path(b2)
assert len(list(C.objects.all())) == 1
assert C.objects.get().id == c2.id
for a in A.objects.all():
B.set_base_path(a)
for b in B.objects.all():
C.set_base_path(b)
C.objects.delete()
B.objects.delete()
A.objects.delete()
from matchbox import models, database
import os
database.db_initialization(os.environ['FIRESTORE'])
class User(models.Model):
name = models.TextField()
class Message(models.Model):
msg = models.TextField()
user = models.ReferenceField(User)
class Meta:
collection_name = 'messages'
class Room(models.Model):
name = models.TextField()
class Meta:
collection_name = 'rooms'
u_neo = User.objects.create(name='Neo')
u_matrix = User.objects.create(name='Matrix')
r = Room.objects.create(name='roomA')
Message.set_base_path(r)
m1 = Message.objects.create(user=u_neo, msg='Matrix ?')
m2 = Message.objects.create(user=u_matrix, msg='Follow the white rabbit')
assert m1.user.id == u_neo.id
assert m2.user.id == u_matrix.id
m1 = Message.objects.get(id=m1.id)
m2 = Message.objects.get(id=m2.id)
assert m1.user.id == u_neo.id
assert m2.user.id == u_matrix.id
m1.delete()
m2.delete()
r.delete()
u_neo.delete()
u_matrix.delete()