Source

graphalchemy / graphalchemy / tests / test_models.py

Full commit
  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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
# -*- coding: utf-8 -*-
'''graphalchemy model test'''

from markupsafe import escape

from graphalchemy.graphs import Graph
from graphalchemy.models.elements import Node
from graphalchemy.models.collectors import In, Out
from graphalchemy.properties import IntegerField, TextField


class Tag(Node):

    text = TextField(indexed=True, slug_from=True, escaped=True)
    user = IntegerField(indexed=True)

    class Meta:
        index = 'tags'
        reference_link = 'TAG_NODE_OF'
        slug_field = 'slug'


class Con(Node):

    down = IntegerField()
    text = TextField(
        indexed=True, full_text=True, slug_from=True, escaped=True
    )
    up = IntegerField()
    user = IntegerField(indexed=True)

    class Meta:
        fork_link = 'CON_FORK_OF'
        fts_index = 'con_text'
        index = 'cons'
        reference_link = 'CON_NODE_OF'
        slug_field = 'slug'
        version_link = 'CON_VERSION_OF'
        versioned = True


class Pro(Node):

    down = IntegerField()
    text = TextField(
        indexed=True, full_text=True, slug_from=True, escaped=True
    )
    up = IntegerField()
    user = IntegerField(indexed=True)

    class Meta:
        fork_link = 'PRO_FORK_OF'
        fts_index = 'pro_text'
        index = 'pros'
        reference_link = 'PRO_NODE_OF'
        slug_field = 'slug'
        version_link = 'PRO_VERSION_OF'
        versioned = True


class Idea(Node):

    down = IntegerField()
    text = TextField(
        indexed=True, full_text=True, slug_from=True, escaped=True
    )
    up = IntegerField()
    user = IntegerField(indexed=True)

    cons = Out(Con, 'CON_AGAINST')
    pros = Out(Pro, 'PRO_FOR')
    questions = Out('Question', 'QUESTION_ABOUT')

    class Meta:
        fork_link = 'IDEA_FORK_OF'
        fts_index = 'idea_text'
        index = 'ideas'
        reference_link = 'IDEA_NODE_OF'
        slug_field = 'slug'
        version_link = 'IDEA_VERSION_OF'
        versioned = True


class Question(Node):

    down = IntegerField()
    text = TextField(
        indexed=True, full_text=True, slug_from=True, escaped=True
    )
    up = IntegerField()
    user = IntegerField(indexed=True)

    ideas = Out('Idea', 'IDEA_ABOUT')
    questions = Out('Question', 'QUESTION_ABOUT')
    tags = In(Tag, 'TAGGED_BY')

    class Meta:
        anchor_link = 'QUESTION_ROOTED_IN'
        fork_link = 'QUESTION_FORK_OF'
        fts_index = 'question_text'
        index = 'questions'
        reference_link = 'QUESTION_NODE_OF'
        slug_field = 'slug'
        version_link = 'QUESTION_VERSION_OF'
        versioned = True


def grapher():
    graph = Graph('foo2')
    def cleanup(): #@IgnorePep8
        graph._db.shutdown()
        import shutil
        shutil.rmtree('foo2')
    graph.register(Tag)
    graph.register(Question)
    graph.register(Pro)
    graph.register(Con)
    graph.register(Idea)
    graph.pipe('anyway', cleanup)
    with graph.session as Session:
        Session.join(Question, Idea, Pro, Con, Tag)
        Session.nodes.index('roots')
        Session.nodes.refer(Question, user=1)
        Session.nodes.refer(Idea, user=1)
        Session.nodes.refer(Pro, user=1)
        Session.nodes.refer(Con, user=1)
        Session.nodes.refer(Tag, user=1)
        tag_1 = Tag(_status=1, text='wicked problem', user=1)
        Session.nodes.add(tag_1)
        tag_1.index_under('tags', 'name', 'wicked problem')
        tag_2 = Tag(text='avoid', user=1, _status=1)
        Session.nodes.add(tag_2)
        tag_2.index_under('tags', 'name', 'avoid')
        root = Session.nodes.root()
        root.data.public.text = 'This is the root'
        question_0 = Session.nodes.anchor(Question, user=1)
        question_1 = Question(
            _status=1,
            down=0,
            text='How do you avoid the wicked problem?',
            up=0,
            user=1,
        )
        Session.nodes.add(question_1)
        question_1.anchor(question_0, user=1)
        question_1.index_under('questions', 'tag', 'wicked problem')
        tag_1.link('TAGGED', question_1)
        question_1.index_under('questions', 'tag', 'avoid')
        tag_2.link('TAGGED', question_1)
        question_1.index_under('questions', 'prime', True)
        question_2 = Question(
            _status=1,
            down=0,
            text="How do you even know it's a wicked problem?",
            up=0,
            user=1,
        )
        Session.nodes.add(question_2)
        question_1.questions.link(question_2, user=1)
        idea_2 = Question(
            _status=1,
            down=0,
            text="You are confused",
            up=0,
            user=1,
            link_props=dict(user=1),
        )
        Session.nodes.add(idea_2)
        question_2.ideas.link(idea_2)
        pro_1 = Pro(
            _status=1,
            down=0,
            text="I'd certainly feel confused",
            up=0,
            user=1,
            link_props=dict(user=1),
        )
        Session.nodes.add(pro_1)
        idea_2.pros.link(pro_1)
        pro_1.up = 6
        pro_1.text = "I'd certainly really feel confused"
        pro_1.user = 2
        con_1 = Idea(
            _status=1,
            down=0,
            text="I don't feel confused",
            up=0,
            user=1,
            link_props=dict(user=1),
        )
        Session.nodes.add(con_1)
        idea_2.cons.link(con_1)
        idea_3 = Idea(
            _status=1,
            down=0,
            text="You are flummoxed",
            up=0,
            user=1,
            link_props=dict(user=1),
        )
        Session.nodes.add(idea_3)
        question_2.ideas.link(idea_3)
        question_3 = Question(
            _status=1,
            down=0,
            text="Isn't that the same as confusion?",
            up=0,
            user=1,
            link_props=dict(user=1),
        )
        Session.nodes.add(question_3)
        idea_3.questions.link(question_3)
        idea_4 = Question(
            _status=1,
            down=0,
            text="No",
            up=0,
            user=1,
            link_props=dict(user=1),
        )
        Session.nodes.add(idea_4)
        question_3.ideas.link(idea_4)
        idea_4.text = 'No, Really'
        idea_5 = Question(
            _status=1,
            down=0,
            text='Yes, but faster!',
            up=0,
            user=1,
            link_props=dict(user=1),
        )
        Session.nodes.add(idea_5)
        question_3.ideas.link(idea_5)
        con_3 = Idea(
            _status=1,
            down=0,
            text="Spans both positions",
            up=0,
            user=1,
            link_props=dict(user=1),
        )
        Session.nodes.add(con_3)
        idea_5.cons.link(con_3)
        con_3.user = 2
        idea_6 = Question(
            _status=1,
            down=0,
            text="You are surrounded by hellfire",
            up=0,
            user=1,
            link_props=dict(user=1),
        )
        Session.nodes.add(idea_6)
        question_2.ideas.link(idea_6)
        question_4 = Idea(
            _status=1,
            down=0,
            text="Is there no appeal from hellfire?",
            up=0,
            user=1,
            link_props=dict(user=1),
        )
        Session.nodes.add(question_4)
        idea_6.questions.link(question_4)
        idea_7 = Question(
            _status=1,
            down=0,
            text="You better hope there's an appeal",
            up=0,
            user=1,
            link_props=dict(user=1),
        )
        Session.nodes.add(idea_7)
        question_4.ideas.link(idea_7)
        idea_1 = Idea(
            _status=1,
            down=0,
            text='DUCK!',
            up=0,
            user=1,
            link_props=dict(user=1),
        )
        Session.nodes.add(idea_1)
        question_1.ideas.link(idea_1)
        question_5 = Question(
            _status=1,
            down=0,
            text="Isn't that a simple problem?",
            up=0,
            user=1,
            link_props=dict(user=1),
        )
        Session.nodes.add(question_5)
        idea_7.questions.link(question_5)
        question_5.up = 15
        pro_2 = Pro(
            _status=1,
            down=0,
            text="It's simple",
            up=0,
            user=1,
            link_props=dict(user=1),
        )
        Session.nodes.add(pro_2)
        idea_1.pros.link(pro_2)
        pro_4 = Session.using(Pro).fork(pro_2)
        pro_2.user = pro_4.user
        pro_3 = Pro(
            _status=1,
            down=0,
            text='It avoids wicked problems shaped like projectiles',
            up=0,
            user=1,
            link_props=dict(user=1),
        )
        Session.nodes.add(pro_3)
        idea_1.pros.link(pro_3)
        pro_3.down = 1
        con_4 = Con(
            _status=1,
            down=0,
            text="It may not actually avoid the problem",
            up=0,
            user=1,
            link_props=dict(user=1),
        )
        Session.nodes.add(con_4)
        idea_1.cons.link(con_4)
        con_4.up = 1
        con_5 = Con(
            _status=1,
            _versions=0,
            down=0,
            text='Harder to do while asleep',
            up=0,
            user=1,
            link_props=dict(user=1),
        )
        Session.nodes.add(con_5)
        idea_1.cons.link(con_5)
        con_5.text = 'Harder to do while asleep with your eyes closed'
        con_5.text = 'Much harder to do while asleep with your eyes closed'
        pro_9 = Pro(
            _status=1,
            down=0,
            text='It avoids wicked problems shaped like projectiles',
            up=0,
            user=1,
            link_props=dict(user=1),
        )
        Session.nodes.add(pro_9)
        idea_1.pros.link(pro_9)
        Session.nodes.remove(pro_9)

    try:
        # questions
        ins = question_1.nodes.ins
        assert ins[3] == idea_1
        assert ins[2] == question_2
        assert ins[1] == question_0.anchor
        assert ins[0] == question_0.reference
#        outs = question_1.nodes.outs
#        questions = question_1.questions
        link_outs = question_1.links.outs
        assert link_outs[3].kind == 'IDEA_ABOUT'
        assert link_outs[3] == Idea.links.get(link_outs[3].id)
        assert link_outs[2].kind == 'QUESTION_ABOUT'
        assert link_outs[1].kind == 'QUESTION_ROOTED_IN'
        assert link_outs[0].kind == 'QUESTION_NODE_OF'
#        link_ins = question_1.links.ins
#        ideas = question_1.ideas
#        tags = question_1.tags
        assert question_1.data.private._status == 1
        assert question_1.down == 0
        assert question_1.text == 'How do you avoid the wicked problem?'
        assert question_1.up == 0
        assert question_1.user == 1
        assert question_2.data.private._status == 1
        assert question_2.down == 0
        assert question_2.text == escape(
            "How do you even know it's a wicked problem?"
        )
        assert question_2.up == 0
        assert question_2.user == 1
        assert question_3.data.private._status == 1
        assert question_3.down == 0
        assert question_3.text == escape("Isn't that the same as confusion?")
        assert question_3.up == 0
        assert question_3.user == 1
        assert question_4.data.private._status == 1
        assert question_4.down == 0
        assert question_4.text == escape("Is there no appeal from hellfire?")
        assert question_4.up == 0
        assert question_4.user == 1
        assert question_5.data.private._status == 1
        assert question_5.down == 0
        assert question_5.up == 15
        assert question_5.text == escape("Isn't that a simple problem?")
        assert question_5.user == 1
        # ideas
        assert idea_1.data.private._status == 1
        assert idea_1.down == 0
        assert idea_1.text == 'DUCK!'
        assert idea_1.up == 0
        assert idea_1.user == 1
        assert idea_2.data.private._status == 1
        assert idea_2.down == 0
        assert idea_2.text == "You are confused"
        assert idea_2.up == 0
        assert idea_2.user == 1
        assert idea_3.data.private._status == 1
        assert idea_3.down == 0
        assert idea_3.text == "You are flummoxed"
        assert idea_3.up == 0
        assert idea_3.user == 1
        assert idea_4.data.private._status == 1
        assert idea_4.down == 0
        assert idea_4.text == 'No, Really'
        assert idea_4.up == 0
        assert idea_4.user == 1
        assert idea_5.data.private._status == 1
        assert idea_5.down == 0
        assert idea_5.text == 'Yes, but faster!'
        assert idea_5.up == 0
        assert idea_5.user == 1
        assert idea_6.data.private._status == 1
        assert idea_6.down == 0
        assert idea_6.text == "You are surrounded by hellfire"
        assert idea_6.up == 0
        assert idea_6.user == 1
        # pros
        assert pro_1.data.private._status == 1
        assert pro_1.down == 0
        assert pro_1.text == escape("I'd certainly really feel confused")
        assert pro_1.up == 6
        assert pro_1.user == 2
        assert pro_2.data.private._status == 1
        assert pro_2.down == 0
        assert pro_2.text == escape("It's simple")
        assert pro_2.up == 0
        assert pro_2.user == 1
        assert pro_3.data.private._status == 1
        assert pro_3.down == 1
        assert pro_3.text == escape(
            'It avoids wicked problems shaped like projectiles'
        )
        assert pro_3.up == 0
        assert pro_3.user == 1
        assert pro_4.data.private._status == 1
        assert pro_4.down == 0
        assert pro_4.text == escape("It's simple")
        assert pro_4.up == 0
        assert pro_4.user == 1
        # cons
        assert con_1.data.private._status == 1
        assert con_1.down == 0
        assert con_1.text == escape("I don't feel confused")
        assert con_1.up == 0
        assert con_1.user == 1
        assert con_3.data.private._status == 1
        assert con_3.down == 0
        assert con_3.text == "Spans both positions"
        assert con_3.up == 0
        assert con_3.user == 2
        assert con_4.data.private._status == 1
        assert con_4.down == 0
        assert con_4.text == "It may not actually avoid the problem"
        assert con_4.up == 1
        assert con_4.user == 1
        assert con_5.data.private._status == 1
        assert con_5.down == 0
        assert con_5.text == escape(
            'Much harder to do while asleep with your eyes closed'
        )
        assert con_5.up == 0
        assert con_5.user == 1
        con_6 = Con.n.get(con_5.id)
        assert con_6.user == con_5.user
        assert con_6.down == con_5.down
        assert con_6.text == con_5.text
        assert con_6.data.private._status == con_5.data.private._status
        # tags
        assert tag_1.text == 'wicked problem'
        assert tag_1.user == 1
        assert tag_2.text == 'avoid'
        assert tag_2.user == 1
        assert root.data.public.text == 'This is the root'
    finally:
        cleanup()

if __name__ == '__main__':
    import cProfile
    cProfile.run('grapher()')