File: test_review_controller.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 (179 lines) | stat: -rwxr-xr-x 7,920 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
#
# test_review_controller.py <Peter.Bienstman@UGent.be>
#

import os
import sys
import shutil

from mnemosyne_test import MnemosyneTest
from mnemosyne.libmnemosyne import Mnemosyne
from mnemosyne.libmnemosyne.criteria.default_criterion import DefaultCriterion
from mnemosyne.libmnemosyne.ui_components.review_widget import ReviewWidget

expected_scheduled_count = None

class MyReviewWidget(ReviewWidget):

    def update_status_bar_counters(self):
        scheduled_count, non_memorised_count, active_count = \
            self.review_controller().counters()
        if expected_scheduled_count is not None:
            assert scheduled_count == expected_scheduled_count

    def redraw_now(self):
        pass


class TestReviewController(MnemosyneTest):

    def setup(self):
        global expected_scheduled_count
        expected_scheduled_count = None
        self.initialise_data_dir()
        path = os.path.join(os.getcwd(), "..", "mnemosyne", "libmnemosyne",
                            "renderers")
        if path not in sys.path:
            sys.path.append(path)
        self.mnemosyne = Mnemosyne(upload_science_logs=False, interested_in_old_reps=True,
            asynchronous_database=True)
        self.mnemosyne.components.insert(0,
           ("mnemosyne.libmnemosyne.gui_translators.gettext_gui_translator", "GetTextGuiTranslator"))
        self.mnemosyne.components.append(\
            ("mnemosyne.libmnemosyne.ui_components.main_widget", "MainWidget"))
        self.mnemosyne.gui_for_component["ScheduledForgottenNew"] = \
            [("test_review_controller", "MyReviewWidget")]
        self.mnemosyne.components.append(\
            ("mnemosyne.libmnemosyne.ui_components.dialogs", "EditCardDialog"))
        self.mnemosyne.initialise(os.path.abspath("dot_test"), automatic_upgrades=False)
        self.review_controller().reset()

    def test_1(self):
        card_1 = None
        self.review_controller().reset()
        for i in range(10):
            fact_data = {"f": "question" + str(i),
                         "b": "answer" + str(i)}
            if i % 2:
                card_type = self.card_type_with_id("1")
            else:
                card_type = self.card_type_with_id("2")
            card = self.controller().create_new_cards(fact_data, card_type,
                    grade=4, tag_names=["default" + str(i)])[0]
            if i == 0:
                card_1 = card
                card.next_rep -= 1000 * 24 * 60 * 60
                self.database().update_card(card)
        self.review_controller().set_render_chain("default")
        self.review_controller().show_new_question()
        self.review_controller().reset_but_try_to_keep_current_card()
        self.review_controller().show_answer()
        assert self.review_controller().card == card_1
        assert self.review_controller().counters() == (1, 0, 15)
        self.review_controller().grade_answer(0)
        assert self.review_controller().counters() == (0, 1, 15)
        self.review_controller().grade_answer(2)
        assert self.review_controller().counters() == (0, 0, 15)
        self.review_controller().next_rep_string(0)
        self.review_controller().next_rep_string(1)
        self.review_controller().next_rep_string(2)

    def test_2(self):
        card_1 = None
        self.review_controller().reset()
        for i in range(10):
            fact_data = {"f": "question" + str(i),
                         "b": "answer" + str(i)}
            if i % 2:
                card_type = self.card_type_with_id("1")
            else:
                card_type = self.card_type_with_id("2")
            card = self.controller().create_new_cards(fact_data, card_type,
                    grade=4, tag_names=["default" + str(i)])[0]
            if i == 0:
                card_1 = card
                card.next_rep -= 1000 * 24 * 60 * 60
                self.database().update_card(card)
        self.review_controller().show_new_question()
        assert self.review_controller().card == card_1
        self.review_controller().reload_counters()
        assert self.review_controller().counters() == (1, 0, 15)
        self.review_controller().grade_answer(0)
        self.review_controller().reload_counters()
        assert self.review_controller().counters() == (0, 1, 15)
        self.review_controller().grade_answer(2)
        self.review_controller().reload_counters()
        assert self.review_controller().counters() == (0, 0, 15)

        self.mnemosyne.review_widget().set_grade_enabled(1, True)

    def test_reset_but_try_to_keep_current_card_turned_inactive(self):
        card_type = self.card_type_with_id("1")
        fact_data = {"f": "1", "b": "b"}
        card = self.controller().create_new_cards(fact_data, card_type,
                     grade=-1, tag_names=["forbidden"])[0]
        self.review_controller().show_new_question()
        assert self.review_controller().card == card

        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

        assert self.review_controller().card == card
        self.review_controller().reset_but_try_to_keep_current_card()
        assert self.review_controller().card is None

    def test_last_card(self):
        card_type = self.card_type_with_id("1")
        for data in ["1", "2", "3"]:
            fact_data = {"f": data, "b": data}
            self.controller().create_new_cards(fact_data, card_type, grade=-1, tag_names=[])
        self.review_controller().show_new_question()
        self.review_controller().show_answer()
        for i in range(5):
            self.review_controller().grade_answer(0)
            self.review_controller().show_answer()
        for i in range(2):
            self.review_controller().grade_answer(2)
            self.review_controller().show_answer()
        for i in range(6):
            self.review_controller().grade_answer(0)
            self.review_controller().show_answer()

    def test_counters(self):
        global expected_scheduled_count
        card_type = self.card_type_with_id("1")
        fact_data = {"f": '1', "b": '1'}
        card = self.controller().create_new_cards(fact_data, card_type, grade=5, tag_names=[])[0]
        card.next_rep = 0
        self.database().update_card(card)
        expected_scheduled_count = 1
        self.review_controller().show_new_question()
        assert self.review_controller().scheduled_count == 1
        assert self.review_controller().counters()[0] == 1
        self.review_controller().show_answer()
        expected_scheduled_count = 0
        self.review_controller().grade_answer(0)
        assert self.review_controller().scheduled_count == 0
        assert self.review_controller().counters()[0] == 0

    def test_counters_prefetch(self):
        global expected_scheduled_count
        card_type = self.card_type_with_id("1")
        for data in ['1', '2', '3', '4']:
            fact_data = {"f": data, "b": data}
            card = self.controller().create_new_cards(fact_data, card_type, grade=5, tag_names=[])[0]
            card.next_rep = 0
            self.database().update_card(card)
        expected_scheduled_count = 4
        self.review_controller().show_new_question()
        assert self.review_controller().scheduled_count == 4
        assert self.review_controller().counters()[0] == 4
        self.review_controller().show_answer()
        expected_scheduled_count = 3
        self.review_controller().grade_answer(3)
        assert self.review_controller().scheduled_count == 3
        assert self.review_controller().counters()[0] == 3