~kevang/mnemosyne-proj/grade-shortcuts-improvements

« back to all changes in this revision

Viewing changes to mnemosyne/tests/test_cloze.py

  • Committer: pbienst
  • Date: 2006-02-09 16:13:13 UTC
  • Revision ID: svn-v3-trunk0:e5e6b78b-db40-0410-9517-b98c64f8d2c1:trunk:2
Initial revision

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#
2
 
# test_cloze.py <Peter.Bienstman@UGent.be>
3
 
#
4
 
 
5
 
import os
6
 
import shutil
7
 
 
8
 
from mnemosyne_test import MnemosyneTest
9
 
from mnemosyne.libmnemosyne import Mnemosyne
10
 
from mnemosyne.libmnemosyne.ui_components.main_widget import MainWidget
11
 
 
12
 
answer = None
13
 
 
14
 
class Widget(MainWidget):
15
 
 
16
 
    def show_question(self, question, option0, option1, option2):
17
 
        return answer
18
 
 
19
 
 
20
 
class TestCloze(MnemosyneTest):
21
 
 
22
 
    def setup(self):
23
 
        self.initialise_data_dir()
24
 
 
25
 
        self.mnemosyne = Mnemosyne(upload_science_logs=False, interested_in_old_reps=True,
26
 
                    asynchronous_database=True)
27
 
        self.mnemosyne.components.insert(0,
28
 
           ("mnemosyne.libmnemosyne.translators.gettext_translator", "GetTextTranslator"))
29
 
        self.mnemosyne.components.append(\
30
 
            ("test_cloze", "Widget"))
31
 
        self.mnemosyne.components.append(\
32
 
            ("mnemosyne_test", "TestReviewWidget"))
33
 
        self.mnemosyne.initialise(os.path.abspath("dot_test"),  automatic_upgrades=False)
34
 
        self.review_controller().reset()
35
 
 
36
 
        from mnemosyne.libmnemosyne.card_types.cloze import ClozePlugin
37
 
        for plugin in self.plugins():
38
 
            if isinstance(plugin, ClozePlugin):
39
 
                plugin.activate()
40
 
                break
41
 
 
42
 
    def test_validate(self):
43
 
        card_type = self.card_type_with_id("5")
44
 
 
45
 
        fact_data = {"text": "incomplete"}
46
 
        assert card_type.is_fact_data_valid(fact_data) == False
47
 
 
48
 
        fact_data = {"text": "[incomplete"}
49
 
        assert card_type.is_fact_data_valid(fact_data) == False
50
 
 
51
 
        fact_data = {"text": "incomplete]"}
52
 
        assert card_type.is_fact_data_valid(fact_data) == False
53
 
 
54
 
        fact_data = {"text": "[]"}
55
 
        assert card_type.is_fact_data_valid(fact_data) == False
56
 
 
57
 
        fact_data = {"text": "[complete]"}
58
 
        assert card_type.is_fact_data_valid(fact_data) == True
59
 
 
60
 
    def test_add(self):
61
 
        card_type = self.card_type_with_id("5")
62
 
 
63
 
        fact_data = {"text": "a [b] c"}
64
 
 
65
 
        card = self.controller().create_new_cards(fact_data, card_type,
66
 
                                          grade=-1, tag_names=["default"])[0]
67
 
 
68
 
        fact = card.fact
69
 
        card = self.database().cards_from_fact(fact)[0]
70
 
 
71
 
        assert self.database().fact_count() == 1
72
 
        assert self.database().card_count() == 1
73
 
 
74
 
        assert "div.b { text-align: center; }" in card.question()
75
 
        assert "div.b { text-align: center; }" in card.answer()
76
 
 
77
 
    def test_edit(self):
78
 
        card_type = self.card_type_with_id("5")
79
 
 
80
 
        fact_data = {"text": "a [b] [c]"}
81
 
 
82
 
        card = self.controller().create_new_cards(fact_data, card_type,
83
 
                                          grade=-1, tag_names=["default"])[0]
84
 
 
85
 
        fact = card.fact
86
 
        card = self.database().cards_from_fact(fact)[0]
87
 
 
88
 
        assert self.database().fact_count() == 1
89
 
        assert self.database().card_count() == 2
90
 
 
91
 
        fact_data = {"text": "a_ [b_] [c_]"}
92
 
        self.controller().edit_card_and_sisters(card, fact_data,
93
 
               card_type, new_tag_names=["default2"], correspondence=[])
94
 
 
95
 
        for c in self.database().cards_from_fact(fact):
96
 
            assert c.extra_data["cloze"] in c.fact["text"]
97
 
 
98
 
        assert self.database().fact_count() == 1
99
 
        assert self.database().card_count() == 2
100
 
 
101
 
        fact_data = {"text": "a_ [b_]"}
102
 
        self.controller().edit_card_and_sisters(card, fact_data,
103
 
               card_type, new_tag_names=["default2"], correspondence=[])
104
 
 
105
 
        for c in self.database().cards_from_fact(fact):
106
 
            assert c.extra_data["cloze"] in c.fact["text"]
107
 
 
108
 
        assert self.database().fact_count() == 1
109
 
        assert self.database().card_count() == 1
110
 
 
111
 
        fact_data = {"text": "a_ [b_] [d] [e]"}
112
 
        self.controller().edit_card_and_sisters(card, fact_data,
113
 
               card_type, new_tag_names=["default2"], correspondence=[])
114
 
 
115
 
        for c in self.database().cards_from_fact(fact):
116
 
            assert c.extra_data["cloze"] in c.fact["text"]
117
 
 
118
 
        assert self.database().fact_count() == 1
119
 
        assert self.database().card_count() == 3
120
 
 
121
 
    def test_convert(self):
122
 
        card_type = self.card_type_with_id("5")
123
 
 
124
 
        fact_data = {"text": "a [b] [c]"}
125
 
 
126
 
        card = self.controller().create_new_cards(fact_data, card_type,
127
 
                                          grade=-1, tag_names=["default"])[0]
128
 
 
129
 
        fact = card.fact
130
 
        card = self.database().cards_from_fact(fact)[0]
131
 
 
132
 
        new_card_type = self.card_type_with_id("1")
133
 
 
134
 
        global answer
135
 
        answer = 0 # OK.
136
 
        self.controller().change_card_type([fact], card_type, new_card_type,
137
 
                         {'text': 'f'})
138
 
 
139
 
        assert self.database().fact_count() == 1
140
 
        assert self.database().card_count() == 1
141
 
 
142
 
    def test_convert_2(self):
143
 
        card_type = self.card_type_with_id("1")
144
 
 
145
 
        fact_data = {"f": "a [b] [c]"}
146
 
 
147
 
        card = self.controller().create_new_cards(fact_data, card_type,
148
 
                                          grade=-1, tag_names=["default"])[0]
149
 
        fact = card.fact
150
 
        new_fact_data = {"text": "a [b] [c]"}
151
 
 
152
 
        new_card_type = self.card_type_with_id("5")
153
 
 
154
 
        global answer
155
 
        answer = 0 # OK.
156
 
 
157
 
        self.controller().edit_card_and_sisters(card, new_fact_data,
158
 
            new_card_type, ["default"], {"f": "text"})
159
 
 
160
 
    def test_convert_3(self):
161
 
        card_type = self.card_type_with_id("2")
162
 
 
163
 
        fact_data = {"f": "a b c", "b": "back"}
164
 
 
165
 
        card = self.controller().create_new_cards(fact_data, card_type,
166
 
                                          grade=-1, tag_names=["default"])[0]
167
 
 
168
 
        # Make sure the card is not the last one in the database, such that
169
 
        # sqlite does not recycle its id.
170
 
 
171
 
        fact_data = {"f": "__a b c", "b": "__back"}
172
 
        self.controller().create_new_cards(fact_data, card_type,
173
 
                                          grade=-1, tag_names=["default"])[0]
174
 
 
175
 
        fact = card.fact
176
 
        new_fact_data = {"text": "a [b] [c]"}
177
 
 
178
 
        new_card_type = self.card_type_with_id("5")
179
 
 
180
 
        global answer
181
 
        answer = 0 # OK.
182
 
 
183
 
        self.controller().edit_card_and_sisters(card, new_fact_data,
184
 
            new_card_type, ["default"], {"f": "text"})
185
 
 
186
 
 
187
 
    def test_convert_abort(self):
188
 
        card_type = self.card_type_with_id("5")
189
 
 
190
 
        fact_data = {"text": "a [b] [c]"}
191
 
 
192
 
        card = self.controller().create_new_cards(fact_data, card_type,
193
 
                                          grade=5, tag_names=["default"])[0]
194
 
 
195
 
        fact = card.fact
196
 
        card = self.database().cards_from_fact(fact)[0]
197
 
 
198
 
        new_card_type = self.card_type_with_id("1")
199
 
 
200
 
        global answer
201
 
        answer = 1 # Cancel.
202
 
        self.controller().change_card_type([fact], card_type, new_card_type,
203
 
                         {'text': 'f'})
204
 
        assert self.database().fact_count() == 1
205
 
        assert self.database().card_count() == 2
206
 
 
207
 
    def test_overlap(self):
208
 
        card_type = self.card_type_with_id("5")
209
 
        fact_data = {"text": "[as] [a]"}
210
 
 
211
 
        cards = self.controller().create_new_cards(fact_data, card_type,
212
 
                                          grade=-1, tag_names=["default"])
213
 
        fact = cards[0].fact
214
 
        assert "[...] a" in cards[0].question()
215
 
        assert "as [...]" in cards[1].question()
216
 
 
217
 
        fact_data = {"text": "[buds] [bud]"}
218
 
        self.controller().edit_card_and_sisters(cards[0], fact_data,
219
 
            card_type, new_tag_names=["default"], correspondence={})
220
 
        cards = self.database().cards_from_fact(fact)
221
 
 
222
 
        assert "[...] bud" in cards[0].question()
223
 
        assert "buds [...]" in cards[1].question()
224
 
 
225
 
    def test_overlap_2(self):
226
 
        card_type = self.card_type_with_id("5")
227
 
        fact_data = {"text": "[as] [a]"}
228
 
 
229
 
        cards = self.controller().create_new_cards(fact_data, card_type,
230
 
                                          grade=-1, tag_names=["default"])
231
 
        fact = cards[0].fact
232
 
        assert "[...] a" in cards[0].question()
233
 
        assert "as [...]" in cards[1].question()
234
 
 
235
 
        fact_data = {"text": "[as] [bud]"}
236
 
        self.controller().edit_card_and_sisters(cards[0], fact_data,
237
 
            card_type, new_tag_names=["default"], correspondence={})
238
 
        cards = self.database().cards_from_fact(fact)
239
 
 
240
 
        assert "[...] bud" in cards[0].question()
241
 
        assert "as [...]" in cards[1].question()
242
 
 
243
 
    def test_overlap_3(self):
244
 
        card_type = self.card_type_with_id("5")
245
 
        fact_data = {"text": "[as] [a]"}
246
 
 
247
 
        cards = self.controller().create_new_cards(fact_data, card_type,
248
 
                                          grade=-1, tag_names=["default"])
249
 
        fact = cards[0].fact
250
 
        assert "[...] a" in cards[0].question()
251
 
        assert "as [...]" in cards[1].question()
252
 
 
253
 
        fact_data = {"text": "[buds] [a]"}
254
 
        self.controller().edit_card_and_sisters(cards[0], fact_data,
255
 
            card_type, new_tag_names=["default"], correspondence={})
256
 
        cards = self.database().cards_from_fact(fact)
257
 
 
258
 
        assert "[...] a" in cards[0].question()
259
 
        assert "buds [...]" in cards[1].question()
260
 
 
261
 
    def test_edit_2(self):
262
 
        card_type = self.card_type_with_id("5")
263
 
        fact_data = {"text": "[consumerist] lifestyles"}
264
 
 
265
 
        cards = self.controller().create_new_cards(fact_data, card_type,
266
 
                                          grade=-1, tag_names=["default"])
267
 
        fact = cards[0].fact
268
 
 
269
 
        fact_data = {"text": "[consumerism]"}
270
 
        self.controller().edit_card_and_sisters(cards[0], fact_data,
271
 
            card_type, new_tag_names=["default"], correspondence={})
272
 
        cards = self.database().cards_from_fact(fact)
273
 
 
274
 
        question = cards[0].question()
275
 
        assert "[...]" in question
276
 
        assert "consumerism" not in question
277
 
        assert "consumerist" not in question
278
 
        assert "lifestyles" not in question
279
 
 
280
 
    def test_duplicate(self):
281
 
        card_type = self.card_type_with_id("5")
282
 
        fact_data = {"text": "[a] [a]"}
283
 
 
284
 
        cards = self.controller().create_new_cards(fact_data, card_type,
285
 
                                          grade=-1, tag_names=["default"])
286
 
 
287
 
        assert "[...] a" in cards[0].question()
288
 
        assert "a [...]" in cards[1].question()
289
 
 
290
 
    def test_henrik_1(self):
291
 
        card_type = self.card_type_with_id("5")
292
 
        fact_data = {"text": """I [1302] ble [Ingebjorg] (datteren til [Eufemia]
293
 
og [Hakon V]) lovet bort til hertug [Erik] av [Sverige]. Hun var da [ett] ar
294
 
gammel...."""}
295
 
 
296
 
        cards = self.controller().create_new_cards(fact_data, card_type,
297
 
                                          grade=3, tag_names=["default"])
298
 
 
299
 
        next_reps = []
300
 
        for card in cards:
301
 
            assert card.question().count("[") == 1
302
 
            next_reps.append(card.next_rep)
303
 
        assert len(next_reps) == len(cards)
304
 
 
305
 
    def test_henrik_2(self):
306
 
        card_type = self.card_type_with_id("5")
307
 
        fact_data = {"text": """Since his death in [1992], [Francis Bacon]'s
308
 
reputation has steadily
309
 
grown. Despite [Margaret Thatcher] having famously described him as
310
 
"that man who paints those [dreadful pictures]", he was the subject of
311
 
two major [Tate retrospectives] during his lifetime and received a
312
 
third in 2008"""}
313
 
 
314
 
        cards = self.controller().create_new_cards(fact_data, card_type,
315
 
                                          grade=-1, tag_names=["default"])
316
 
 
317
 
        for card in cards:
318
 
            assert card.question().count("[") == 1
319
 
 
320
 
    def test_convert_many(self):
321
 
        card_type = self.card_type_with_id("5")
322
 
        fact_data = {"text": "[a] [b]"}
323
 
 
324
 
        cards = self.controller().create_new_cards(fact_data, card_type,
325
 
                                          grade=-1, tag_names=["default"])
326
 
 
327
 
        fact = cards[0].fact
328
 
 
329
 
        global answer
330
 
        answer = 0 # OK
331
 
 
332
 
        self.controller().change_card_type([fact], card_type,
333
 
            self.card_type_with_id("1"), correspondence={"text": "f"})
334
 
 
335
 
        fact = self.database().fact(fact._id, is_id_internal=True)
336
 
        assert "text" not in fact.data
337
 
        assert "f" in fact.data
338
 
 
339
 
    def test_convert_many_cancel(self):
340
 
        card_type = self.card_type_with_id("5")
341
 
        fact_data = {"text": "[a] [b]"}
342
 
 
343
 
        cards = self.controller().create_new_cards(fact_data, card_type,
344
 
                                          grade=5, tag_names=["default"])
345
 
 
346
 
        fact = cards[0].fact
347
 
 
348
 
        global answer
349
 
        answer = 1 # cancel
350
 
 
351
 
        self.controller().change_card_type([fact], card_type,
352
 
            self.card_type_with_id("1"), correspondence={"text": "f"})
353
 
 
354
 
        fact = self.database().fact(fact._id, is_id_internal=True)
355
 
        assert "text" in fact.data
356
 
        assert "f" not in fact.data
357
 
 
358
 
    def test_hint(self):
359
 
        card_type = self.card_type_with_id("5")
360
 
        fact_data = {"text": """bla [cloze:hint]"""}
361
 
 
362
 
        card = self.controller().create_new_cards(fact_data, card_type,
363
 
                                          grade=-1, tag_names=["default"])[0]
364
 
 
365
 
        assert "cloze" not in card.question()
366
 
        assert "hint" in card.question()
367
 
        assert "cloze" in card.answer()
368
 
        assert "hint" not in card.answer()
369
 
 
370
 
    def test_hint_2(self):
371
 
        card_type = self.card_type_with_id("5")
372
 
        fact_data = {"text": """bla [cloze:hint] [other cloze:other_hint]"""}
373
 
 
374
 
        card = self.controller().create_new_cards(fact_data, card_type,
375
 
                                          grade=-1, tag_names=["default"])[0]
376
 
 
377
 
        assert "bla [hint] other cloze" in card.question()
378
 
        assert "other hint" not in card.question()
379
 
 
380
 
    def test_clone(self):
381
 
        card_type = self.card_type_with_id("5")
382
 
        card_type = self.controller().clone_card_type(\
383
 
            card_type, ("5 clone"))
384
 
        fact_data = {"text": """bla [cloze:hint] [other cloze:other_hint]"""}
385
 
        card = self.controller().create_new_cards(fact_data, card_type,
386
 
                                          grade=-1, tag_names=["default"])[0]
387
 
 
388
 
        assert "bla [hint] other cloze" in card.question()
389
 
        assert "other hint" not in card.question()
390
 
 
391
 
    def test_latex(self):
392
 
        card_type = self.card_type_with_id("5")
393
 
 
394
 
        fact_data = {"text": """<$>[\mathbf{F}]=[q]([\mathbf{E}]+[\mathbf{v}\times\mathbf{B}]</$>"""}
395
 
        card = self.controller().create_new_cards(fact_data, card_type,
396
 
                                          grade=-1, tag_names=["default"])[0]
397
 
        assert "<img src" in card.answer()
398
 
 
399
 
        fact_data = {"text": """<$$>[\mathbf{F}]=[q]([\mathbf{E}]+[\mathbf{v}\times\mathbf{B}]</$$>"""}
400
 
        card = self.controller().create_new_cards(fact_data, card_type,
401
 
                                          grade=-1, tag_names=["default"])[0]
402
 
        assert "<img src" in card.answer()
403
 
 
404
 
        fact_data = {"text": """<latex>[\mathbf{F}]=[q]([\mathbf{E}]+[\mathbf{v}\times\mathbf{B}]</latex>"""}
405
 
        card = self.controller().create_new_cards(fact_data, card_type,
406
 
                                          grade=-1, tag_names=["default"])[0]
407
 
        assert "<img src" in card.answer()
408
 
 
409
 
    def test_latex_2(self):
410
 
        card_type = self.card_type_with_id("5")
411
 
 
412
 
        fact_data = {"text": """<$>[a]\\left[b\\right]</$>"""}
413
 
        cards = self.controller().create_new_cards(fact_data, card_type,
414
 
                                          grade=-1, tag_names=["default"])
415
 
        assert len(cards) == 1
416
 
        card = cards[0]
417
 
        assert "<img src" in card.question()
418
 
        assert "<img src" in card.answer()