File: test_activate_cards.py

package info (click to toggle)
mnemosyne 2.7.3%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 9,804 kB
  • sloc: python: 34,077; ansic: 837; xml: 625; makefile: 249; sh: 63
file content (453 lines) | stat: -rwxr-xr-x 20,863 bytes parent folder | download | duplicates (2)
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
#
# test_activate_cards.py <Peter.Bienstman@UGent.be>
#

from nose.tools import raises
from mnemosyne_test import MnemosyneTest
from mnemosyne.libmnemosyne import Mnemosyne
from mnemosyne.libmnemosyne.criterion import Criterion
from mnemosyne.libmnemosyne.criteria.default_criterion import DefaultCriterion

class TestActivateCards(MnemosyneTest):

    def test_compare_criteria(self):
        c1 = DefaultCriterion(self.mnemosyne.component_manager)
        c1.deactivated_card_type_fact_view_ids = set()
        c1._tag_ids_active = set([self.database().get_or_create_tag_with_name("default")._id])
        c1._tag_ids_forbidden = set()

        c2 = DefaultCriterion(self.mnemosyne.component_manager)
        c2.deactivated_card_type_fact_view_ids = set()
        c2._tag_ids_active = set([self.database().get_or_create_tag_with_name("default")._id])
        c2._tag_ids_forbidden = set()

        c3 = DefaultCriterion(self.mnemosyne.component_manager)
        c3.deactivated_card_type_fact_view_ids = set()
        c3._tag_ids_active = set([self.database().get_or_create_tag_with_name("default1")._id])
        c3._tag_ids_forbidden = set()

        assert c1 == c2
        assert c1 != c3
        assert c1 != 1

    def test_activate_cards_1(self):
        fact_data = {"f": "question",
                     "b": "answer"}
        card_type_1 = self.card_type_with_id("1")
        self.controller().create_new_cards(fact_data, card_type_1,
                                              grade=-1, tag_names=["default"])
        assert self.database().active_count() == 1

        fact_data = {"f": "question",
                     "b": "answer"}
        card_type_2 = self.card_type_with_id("2")
        self.controller().create_new_cards(fact_data, card_type_2,
                                              grade=-1, tag_names=["default"])
        assert self.database().active_count() == 3

        c = DefaultCriterion(self.mnemosyne.component_manager)
        c.deactivated_card_type_fact_view_ids = set()
        c._tag_ids_active = set([self.database().get_or_create_tag_with_name("default")._id])
        c._tag_ids_forbidden = set()
        self.database().set_current_criterion(c)
        assert self.database().active_count() == 3

        c.deactivated_card_type_fact_view_ids = \
            set([(card_type_2.id, card_type_2.fact_views[0].id)])
        c._tag_ids_active = set([self.database().get_or_create_tag_with_name("default")._id])
        c._tag_ids_forbidden = set()
        self.database().set_current_criterion(c)
        assert self.database().active_count() == 2

        c = DefaultCriterion(self.mnemosyne.component_manager)
        c.deactivated_card_type_fact_view_ids = set()
        c._tag_ids_active = set([self.database().get_or_create_tag_with_name("default2")._id])
        c._tag_ids_forbidden = set()
        self.database().set_current_criterion(c)
        assert self.database().active_count() == 0

        fact_data = {"f": "question2",
                     "b": "answer2"}
        self.controller().create_new_cards(fact_data, card_type_2,
                                              grade=-1, tag_names=["default2"])
        c.deactivated_card_type_fact_view_ids = set()
        c._tag_ids_active = set([self.database().get_or_create_tag_with_name("default2")._id])
        c._tag_ids_forbidden = set()
        self.database().set_current_criterion(c)
        assert self.database().active_count() == 2

        fact_data = {"f": "question3",
                     "b": "answer3"}
        self.controller().create_new_cards(fact_data, card_type_2,
                                              grade=-1, tag_names=["default3",
                                                                  "default4"])
        c.deactivated_card_type_fact_view_ids = set()
        c._tag_ids_active = set([self.database().get_or_create_tag_with_name("default3")._id])
        c._tag_ids_forbidden = set()
        self.database().set_current_criterion(c)
        assert self.database().active_count() == 2

        c.deactivated_card_type_fact_view_ids = \
            set([(card_type_2.id, card_type_2.fact_views[0].id)])
        c._tag_ids_active = set([self.database().get_or_create_tag_with_name("default3")._id])
        c._tag_ids_forbidden = set()
        self.database().set_current_criterion(c)
        assert self.database().active_count() == 1

        c.deactivated_card_type_fact_view_ids = \
            set([(card_type_2.id, card_type_2.fact_views[0].id),
                 (card_type_2.id, card_type_2.fact_views[1].id)])
        c._tag_ids_active = set([self.database().get_or_create_tag_with_name("default3")._id])
        c._tag_ids_forbidden = set()
        self.database().set_current_criterion(c)
        assert self.database().active_count() == 0

    def test_activate_cards_2(self):
        fact_data = {"f": "question",
                     "b": "answer"}
        card_type_1 = self.card_type_with_id("1")
        self.controller().create_new_cards(fact_data, card_type_1,
           grade=-1, tag_names=["active", "forbidden"])
        assert self.database().active_count() == 1

        c = DefaultCriterion(self.mnemosyne.component_manager)
        c.deactivated_card_type_fact_view_ids = set()
        c._tag_ids_active = set([self.database().get_or_create_tag_with_name("active")._id])
        c._tag_ids_forbidden = set()
        self.database().set_current_criterion(c)
        assert self.database().active_count() == 1

        c = DefaultCriterion(self.mnemosyne.component_manager)
        c.deactivated_card_type_fact_view_ids = set()
        c._tag_ids_active = set([self.database().get_or_create_tag_with_name("active")._id])
        c._tag_ids_forbidden = set([self.database().get_or_create_tag_with_name("forbidden")._id])
        self.database().set_current_criterion(c)
        assert self.database().active_count() == 0

    def test_activate_cards_3(self):
        fact_data = {"f": "question",
                     "b": "answer"}
        card_type_1 = self.card_type_with_id("1")
        card = self.controller().create_new_cards(fact_data, card_type_1,
           grade=-1, tag_names=["tag1"])[0]
        self.review_controller().show_new_question()

        assert self.review_controller().card == card
        assert self.review_controller().counters() == (0, 1, 1)

        fact_data = {"f": "question2",
                     "b": "answer2"}
        card_type_2 = self.card_type_with_id("2")
        cards = self.controller().create_new_cards(fact_data, card_type_2,
           grade=-1, tag_names=["tag2"])

        c = DefaultCriterion(self.mnemosyne.component_manager)
        c.deactivated_card_type_fact_view_ids = set()
        c._tag_ids_active = set([self.database().get_or_create_tag_with_name("tag2")._id])
        c._tag_ids_forbidden = set()
        self.database().set_current_criterion(c)
        self.review_controller().reset_but_try_to_keep_current_card()
        self.review_controller().reload_counters()

        assert self.review_controller().card != card
        assert self.review_controller().counters() == (0, 2, 2)

    def test_activate_cards_4(self):
        fact_data = {"f": "question",
                     "b": "answer"}
        card_type_1 = self.card_type_with_id("1")
        card = self.controller().create_new_cards(fact_data, card_type_1,
           grade=-1, tag_names=["tag"])[0]
        self.review_controller().show_new_question()

        assert self.review_controller().card == card
        assert self.review_controller().counters() == (0, 1, 1)

        c = DefaultCriterion(self.mnemosyne.component_manager)
        c.deactivated_card_type_fact_view_ids = set()
        c._tag_ids_active = set([self.database().get_or_create_tag_with_name("tag2")._id])
        c._tag_ids_forbidden = set()
        self.database().set_current_criterion(c)
        self.review_controller().reset_but_try_to_keep_current_card()
        self.review_controller().reload_counters()

        assert self.review_controller().card is None
        assert self.review_controller().counters() == (0, 0, 0)

        c = DefaultCriterion(self.mnemosyne.component_manager)
        c.deactivated_card_type_fact_view_ids = set()
        c._tag_ids_active = set([self.database().get_or_create_tag_with_name("tag")._id])
        c._tag_ids_forbidden = set()
        self.database().set_current_criterion(c)
        self.review_controller().reset_but_try_to_keep_current_card()
        self.review_controller().reload_counters()

        assert self.review_controller().card == card
        assert self.review_controller().counters() == (0, 1, 1)

    def test_activate_cards_new(self):
        fact_data = {"f": "question",
                     "b": "answer"}
        card_type_1 = self.card_type_with_id("1")
        self.controller().create_new_cards(fact_data, card_type_1,
           grade=-1, tag_names=["forbidden"])
        assert self.database().active_count() == 1

        c = DefaultCriterion(self.mnemosyne.component_manager)
        c.deactivated_card_type_fact_view_ids = set()
        c._tag_ids_active = set([self.database().get_or_create_tag_with_name("forbidden")._id])
        c._tag_ids_forbidden = set([self.database().get_or_create_tag_with_name("forbidden")._id])
        self.database().set_current_criterion(c)
        assert self.database().active_count() == 0

        fact_data = {"f": "question2",
                     "b": "answer2"}
        card_type_1 = self.card_type_with_id("1")
        self.controller().create_new_cards(fact_data, card_type_1,
           grade=-1, tag_names=["forbidden"])
        assert self.database().active_count() == 0

    def test_activate_cards_new_2(self):
        fact_data = {"f": "question",
                     "b": "answer"}
        card_type_1 = self.card_type_with_id("1")
        self.controller().create_new_cards(fact_data, card_type_1,
           grade=-1, tag_names=["default"])
        assert self.database().active_count() == 1

        c = DefaultCriterion(self.mnemosyne.component_manager)
        c.deactivated_card_type_fact_view_ids = \
            set([(card_type_1.id, card_type_1.fact_views[0].id)])
        c._tag_ids_active = set()
        c._tag_ids_forbidden = set()
        self.database().set_current_criterion(c)
        assert self.database().active_count() == 0

        fact_data = {"f": "question2",
                     "b": "answer2"}
        card_type_1 = self.card_type_with_id("1")
        self.controller().create_new_cards(fact_data, card_type_1,
           grade=-1, tag_names=["default"])
        assert self.database().active_count() == 0

    def test_activate_cards_edit(self):
        fact_data = {"f": "question3",
                     "b": "answer3"}
        card_type_1 = self.card_type_with_id("1")
        card = self.controller().create_new_cards(fact_data, card_type_1,
           grade=-1, tag_names=["forbidden"])[0]
        assert self.database().active_count() == 1

        c = DefaultCriterion(self.mnemosyne.component_manager)
        c.deactivated_card_type_fact_view_ids = set()
        c._tag_ids_active = set([self.database().get_or_create_tag_with_name("forbidden")._id])
        c._tag_ids_forbidden = set([self.database().get_or_create_tag_with_name("forbidden")._id])
        self.database().set_current_criterion(c)
        assert self.database().active_count() == 0

        card_type_2 = self.card_type_with_id("2")
        self.controller().edit_card_and_sisters(card, card.fact.data,
               card_type_2, new_tag_names=["allowed"], correspondence=[])
        assert self.database().active_count() == 2

        c = list(self.database().criteria())[0]
        assert len(c._tag_ids_forbidden) == 0
        assert len(c._tag_ids_active) == 1

    def test_activate_cards_bulk_edit(self):
        fact_data = {"f": "question3",
                     "b": "answer3"}
        card_type_1 = self.card_type_with_id("1")
        card = self.controller().create_new_cards(fact_data, card_type_1,
           grade=-1, tag_names=["allowed"])[0]
        assert self.database().active_count() == 1

        c = DefaultCriterion(self.mnemosyne.component_manager)
        c.deactivated_card_type_fact_view_ids = set()
        c._tag_ids_active = set([self.database().get_or_create_tag_with_name("allowed")._id])
        c._tag_ids_forbidden = set([self.database().get_or_create_tag_with_name("forbidden")._id])
        self.database().set_current_criterion(c)
        assert self.database().active_count() == 1

        self.database().add_tag_to_cards_with_internal_ids(\
            self.database().get_or_create_tag_with_name("forbidden"), [card._id])
        
        assert self.database().active_count() == 0

    def test_activate_cards_bulk_edit_2(self):
        fact_data = {"f": "question",
                     "b": "answer"}
        card_type_1 = self.card_type_with_id("1")
        card = self.controller().create_new_cards(fact_data, card_type_1,
           grade=-1, tag_names=["forbidden"])[0]

        fact_data = {"f": "question2",
                     "b": "answer2"}
        card = self.controller().create_new_cards(fact_data, card_type_1,
           grade=-1, tag_names=["forbidden"])[0]
        assert self.database().active_count() == 2

        c = DefaultCriterion(self.mnemosyne.component_manager)
        c.deactivated_card_type_fact_view_ids = set()
        c._tag_ids_active = set([self.database().get_or_create_tag_with_name("__UNTAGGED__")._id])
        c._tag_ids_forbidden = set([self.database().get_or_create_tag_with_name("forbidden")._id])
        self.database().set_current_criterion(c)
        assert self.database().active_count() == 0

        self.database().remove_tag_from_cards_with_internal_ids(\
            self.database().get_or_create_tag_with_name("forbidden"), [card._id])

        assert self.database().active_count() == 1
        
    def test_card_type(self):
        from mnemosyne.libmnemosyne.card_types.cloze import ClozePlugin
        for plugin in self.plugins():
            if isinstance(plugin, ClozePlugin):
                cloze_plugin = plugin
                plugin.activate()
                break

        fact_data = {"text": "[foo]"}
        card_type_1 = self.card_type_with_id("5")
        card = self.controller().create_new_cards(fact_data, card_type_1,
           grade=-1, tag_names=["default"])[0]
        assert self.database().active_count() == 1

        c = DefaultCriterion(self.mnemosyne.component_manager)
        c.deactivated_card_type_fact_view_ids = \
            set([(card_type_1.id, card_type_1.fact_views[0].id)])
        c._tag_ids_active = set()
        c._tag_ids_forbidden = set()
        self.database().set_current_criterion(c)
        assert self.database().active_count() == 0

        self.controller().delete_facts_and_their_cards([card.fact])
        plugin.deactivate()
        c = self.database().current_criterion()
        assert len(c.deactivated_card_type_fact_view_ids) == 0

    def test_cached_scheduler_count(self):
        fact_data = {"f": "question",
                     "b": "answer"}
        card_type_1 = self.card_type_with_id("1")
        self.controller().create_new_cards(fact_data, card_type_1,
           grade=-1, tag_names=["tag"])
        assert self.database().active_count() == 1
        self.review_controller().show_new_question()
        assert self.review_controller().active_count == 1

        c = DefaultCriterion(self.mnemosyne.component_manager)
        c.deactivated_card_type_fact_view_ids = set()
        c._tag_ids_active = set()
        c._tag_ids_forbidden = set()
        self.database().set_current_criterion(c)
        self.review_controller().reset_but_try_to_keep_current_card()
        assert self.database().active_count() == 0
        assert self.review_controller().active_count == 0

    def test_activate_cards_5(self):
        fact_data = {"f": "question",
                     "b": "answer"}
        card_type_1 = self.card_type_with_id("1")
        self.controller().create_new_cards(fact_data, card_type_1,
           grade=-1, tag_names=["b"])

        fact_data = {"f": "question2",
                     "b": "answer2"}
        self.controller().create_new_cards(fact_data, card_type_1,
            grade=-1, tag_names=["a"])
        assert self.database().active_count() == 2

        c = DefaultCriterion(self.mnemosyne.component_manager)
        c.deactivated_card_type_fact_view_ids = set()
        c._tag_ids_active = set([self.database().get_or_create_tag_with_name("a")._id,
                                 self.database().get_or_create_tag_with_name("b")._id])
        c._tag_ids_forbidden = set([self.database().get_or_create_tag_with_name("b")._id])
        self.database().set_current_criterion(c)
        assert self.database().active_count() == 1

    @raises(AssertionError)
    def test_activate_cards_6(self):
        fact_data = {"f": "question",
                     "b": "answer"}
        card_type_1 = self.card_type_with_id("1")
        self.controller().create_new_cards(fact_data, card_type_1,
           grade=-1, tag_names=["a"])

        fact_data = {"f": "question2",
                     "b": "answer2"}
        self.controller().create_new_cards(fact_data, card_type_1,
            grade=-1, tag_names=["b"])
        assert self.database().active_count() == 2

        c = DefaultCriterion(self.mnemosyne.component_manager)
        c.deactivated_card_type_fact_view_ids = set()
        c._tag_ids_active = set([])
        c._tag_ids_forbidden = set([self.database().get_or_create_tag_with_name("b")._id])
        self.database().set_current_criterion(c)
        assert self.database().active_count() == 1

    def test_empty_criterion(self):
        c = Criterion(self.mnemosyne.component_manager)
        assert c.is_empty() == False

        tag_id = self.database().get_or_create_tag_with_name("a")._id

        c = DefaultCriterion(self.mnemosyne.component_manager)
        for card_type in self.card_types():
            for fact_view in card_type.fact_views:
                c.deactivated_card_type_fact_view_ids.add((card_type.id, fact_view.id))
        c._tag_ids_active = set([])
        c._tag_ids_forbidden = set()
        assert c.is_empty() == True

        c = DefaultCriterion(self.mnemosyne.component_manager)
        c.deactivated_card_type_fact_view_ids = set()
        c._tag_ids_active = set([])
        c._tag_ids_forbidden = set()
        assert c.is_empty() == True

        c = DefaultCriterion(self.mnemosyne.component_manager)
        c.deactivated_card_type_fact_view_ids = set()
        c._tag_ids_active = set(["__UNTAGGED__"])
        c._tag_ids_forbidden = set([tag_id])
        assert c.is_empty() == False

        c = DefaultCriterion(self.mnemosyne.component_manager)
        c.deactivated_card_type_fact_view_ids = set()
        c._tag_ids_active = set(["__UNTAGGED__"])
        c._tag_ids_forbidden = set([tag_id, "__UNTAGGED__"])
        assert c.is_empty() == True

    def test_inactive_parent(self):

        fact_data = {"f": "question",
                     "b": "answer"}
        card_type_1 = self.card_type_with_id("1")
        self.controller().create_new_cards(fact_data, card_type_1,
           grade=-1, tag_names=["dummy"])

        fact_data = {"f": "question2",
                     "b": "answer2"}
        card_type_1 = self.card_type_with_id("1")
        self.controller().create_new_cards(fact_data, card_type_1,
           grade=-1, tag_names=["a"])

        c = DefaultCriterion(self.mnemosyne.component_manager)
        c.deactivated_card_type_fact_view_ids = set()
        c._tag_ids_active = set([self.database().get_or_create_tag_with_name("dummy")._id])
        c._tag_ids_forbidden = set([self.database().get_or_create_tag_with_name("a")._id])
        self.database().set_current_criterion(c)
        assert self.database().active_count() == 1

        fact_data = {"f": "question3",
                     "b": "answer3"}
        self.controller().create_new_cards(fact_data, card_type_1,
            grade=-1, tag_names=["a::b"])

        assert self.database().active_count() == 1
        fact_data = {"f": "question4",
                     "b": "answer4"}
        self.controller().create_new_cards(fact_data, card_type_1,
            grade=-1, tag_names=["dummy::b"])
        assert self.database().active_count() == 2