2
# test_cloze.py <Peter.Bienstman@UGent.be>
8
from mnemosyne_test import MnemosyneTest
9
from mnemosyne.libmnemosyne import Mnemosyne
10
from mnemosyne.libmnemosyne.ui_components.main_widget import MainWidget
14
class Widget(MainWidget):
16
def show_question(self, question, option0, option1, option2):
20
class TestCloze(MnemosyneTest):
23
self.initialise_data_dir()
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()
36
from mnemosyne.libmnemosyne.card_types.cloze import ClozePlugin
37
for plugin in self.plugins():
38
if isinstance(plugin, ClozePlugin):
42
def test_validate(self):
43
card_type = self.card_type_with_id("5")
45
fact_data = {"text": "incomplete"}
46
assert card_type.is_fact_data_valid(fact_data) == False
48
fact_data = {"text": "[incomplete"}
49
assert card_type.is_fact_data_valid(fact_data) == False
51
fact_data = {"text": "incomplete]"}
52
assert card_type.is_fact_data_valid(fact_data) == False
54
fact_data = {"text": "[]"}
55
assert card_type.is_fact_data_valid(fact_data) == False
57
fact_data = {"text": "[complete]"}
58
assert card_type.is_fact_data_valid(fact_data) == True
61
card_type = self.card_type_with_id("5")
63
fact_data = {"text": "a [b] c"}
65
card = self.controller().create_new_cards(fact_data, card_type,
66
grade=-1, tag_names=["default"])[0]
69
card = self.database().cards_from_fact(fact)[0]
71
assert self.database().fact_count() == 1
72
assert self.database().card_count() == 1
74
assert "div.b { text-align: center; }" in card.question()
75
assert "div.b { text-align: center; }" in card.answer()
78
card_type = self.card_type_with_id("5")
80
fact_data = {"text": "a [b] [c]"}
82
card = self.controller().create_new_cards(fact_data, card_type,
83
grade=-1, tag_names=["default"])[0]
86
card = self.database().cards_from_fact(fact)[0]
88
assert self.database().fact_count() == 1
89
assert self.database().card_count() == 2
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=[])
95
for c in self.database().cards_from_fact(fact):
96
assert c.extra_data["cloze"] in c.fact["text"]
98
assert self.database().fact_count() == 1
99
assert self.database().card_count() == 2
101
fact_data = {"text": "a_ [b_]"}
102
self.controller().edit_card_and_sisters(card, fact_data,
103
card_type, new_tag_names=["default2"], correspondence=[])
105
for c in self.database().cards_from_fact(fact):
106
assert c.extra_data["cloze"] in c.fact["text"]
108
assert self.database().fact_count() == 1
109
assert self.database().card_count() == 1
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=[])
115
for c in self.database().cards_from_fact(fact):
116
assert c.extra_data["cloze"] in c.fact["text"]
118
assert self.database().fact_count() == 1
119
assert self.database().card_count() == 3
121
def test_convert(self):
122
card_type = self.card_type_with_id("5")
124
fact_data = {"text": "a [b] [c]"}
126
card = self.controller().create_new_cards(fact_data, card_type,
127
grade=-1, tag_names=["default"])[0]
130
card = self.database().cards_from_fact(fact)[0]
132
new_card_type = self.card_type_with_id("1")
136
self.controller().change_card_type([fact], card_type, new_card_type,
139
assert self.database().fact_count() == 1
140
assert self.database().card_count() == 1
142
def test_convert_2(self):
143
card_type = self.card_type_with_id("1")
145
fact_data = {"f": "a [b] [c]"}
147
card = self.controller().create_new_cards(fact_data, card_type,
148
grade=-1, tag_names=["default"])[0]
150
new_fact_data = {"text": "a [b] [c]"}
152
new_card_type = self.card_type_with_id("5")
157
self.controller().edit_card_and_sisters(card, new_fact_data,
158
new_card_type, ["default"], {"f": "text"})
160
def test_convert_3(self):
161
card_type = self.card_type_with_id("2")
163
fact_data = {"f": "a b c", "b": "back"}
165
card = self.controller().create_new_cards(fact_data, card_type,
166
grade=-1, tag_names=["default"])[0]
168
# Make sure the card is not the last one in the database, such that
169
# sqlite does not recycle its id.
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]
176
new_fact_data = {"text": "a [b] [c]"}
178
new_card_type = self.card_type_with_id("5")
183
self.controller().edit_card_and_sisters(card, new_fact_data,
184
new_card_type, ["default"], {"f": "text"})
187
def test_convert_abort(self):
188
card_type = self.card_type_with_id("5")
190
fact_data = {"text": "a [b] [c]"}
192
card = self.controller().create_new_cards(fact_data, card_type,
193
grade=5, tag_names=["default"])[0]
196
card = self.database().cards_from_fact(fact)[0]
198
new_card_type = self.card_type_with_id("1")
202
self.controller().change_card_type([fact], card_type, new_card_type,
204
assert self.database().fact_count() == 1
205
assert self.database().card_count() == 2
207
def test_overlap(self):
208
card_type = self.card_type_with_id("5")
209
fact_data = {"text": "[as] [a]"}
211
cards = self.controller().create_new_cards(fact_data, card_type,
212
grade=-1, tag_names=["default"])
214
assert "[...] a" in cards[0].question()
215
assert "as [...]" in cards[1].question()
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)
222
assert "[...] bud" in cards[0].question()
223
assert "buds [...]" in cards[1].question()
225
def test_overlap_2(self):
226
card_type = self.card_type_with_id("5")
227
fact_data = {"text": "[as] [a]"}
229
cards = self.controller().create_new_cards(fact_data, card_type,
230
grade=-1, tag_names=["default"])
232
assert "[...] a" in cards[0].question()
233
assert "as [...]" in cards[1].question()
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)
240
assert "[...] bud" in cards[0].question()
241
assert "as [...]" in cards[1].question()
243
def test_overlap_3(self):
244
card_type = self.card_type_with_id("5")
245
fact_data = {"text": "[as] [a]"}
247
cards = self.controller().create_new_cards(fact_data, card_type,
248
grade=-1, tag_names=["default"])
250
assert "[...] a" in cards[0].question()
251
assert "as [...]" in cards[1].question()
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)
258
assert "[...] a" in cards[0].question()
259
assert "buds [...]" in cards[1].question()
261
def test_edit_2(self):
262
card_type = self.card_type_with_id("5")
263
fact_data = {"text": "[consumerist] lifestyles"}
265
cards = self.controller().create_new_cards(fact_data, card_type,
266
grade=-1, tag_names=["default"])
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)
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
280
def test_duplicate(self):
281
card_type = self.card_type_with_id("5")
282
fact_data = {"text": "[a] [a]"}
284
cards = self.controller().create_new_cards(fact_data, card_type,
285
grade=-1, tag_names=["default"])
287
assert "[...] a" in cards[0].question()
288
assert "a [...]" in cards[1].question()
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
296
cards = self.controller().create_new_cards(fact_data, card_type,
297
grade=3, tag_names=["default"])
301
assert card.question().count("[") == 1
302
next_reps.append(card.next_rep)
303
assert len(next_reps) == len(cards)
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
314
cards = self.controller().create_new_cards(fact_data, card_type,
315
grade=-1, tag_names=["default"])
318
assert card.question().count("[") == 1
320
def test_convert_many(self):
321
card_type = self.card_type_with_id("5")
322
fact_data = {"text": "[a] [b]"}
324
cards = self.controller().create_new_cards(fact_data, card_type,
325
grade=-1, tag_names=["default"])
332
self.controller().change_card_type([fact], card_type,
333
self.card_type_with_id("1"), correspondence={"text": "f"})
335
fact = self.database().fact(fact._id, is_id_internal=True)
336
assert "text" not in fact.data
337
assert "f" in fact.data
339
def test_convert_many_cancel(self):
340
card_type = self.card_type_with_id("5")
341
fact_data = {"text": "[a] [b]"}
343
cards = self.controller().create_new_cards(fact_data, card_type,
344
grade=5, tag_names=["default"])
351
self.controller().change_card_type([fact], card_type,
352
self.card_type_with_id("1"), correspondence={"text": "f"})
354
fact = self.database().fact(fact._id, is_id_internal=True)
355
assert "text" in fact.data
356
assert "f" not in fact.data
359
card_type = self.card_type_with_id("5")
360
fact_data = {"text": """bla [cloze:hint]"""}
362
card = self.controller().create_new_cards(fact_data, card_type,
363
grade=-1, tag_names=["default"])[0]
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()
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]"""}
374
card = self.controller().create_new_cards(fact_data, card_type,
375
grade=-1, tag_names=["default"])[0]
377
assert "bla [hint] other cloze" in card.question()
378
assert "other hint" not in card.question()
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]
388
assert "bla [hint] other cloze" in card.question()
389
assert "other hint" not in card.question()
391
def test_latex(self):
392
card_type = self.card_type_with_id("5")
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()
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()
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()
409
def test_latex_2(self):
410
card_type = self.card_type_with_id("5")
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
417
assert "<img src" in card.question()
418
assert "<img src" in card.answer()