File: test_media.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 (424 lines) | stat: -rwxr-xr-x 20,238 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
#
# test_media.py <Peter.Bienstman@UGent.be>
#

import os
import sys
import shutil

from mnemosyne_test import MnemosyneTest
from mnemosyne.libmnemosyne import Mnemosyne
from openSM2sync.log_entry import EventTypes
from mnemosyne.libmnemosyne.ui_components.main_widget import MainWidget

filename = ""
answer = 0

class Widget(MainWidget):

    def get_filename_to_open(self, a, b, c):
        return filename

    def show_question(self, question, option0, option1, option2):
        if question.startswith("Found unused"):
            return answer
        else:
            raise NotImplementedError

class TestMedia(MnemosyneTest):

    def restart(self):
        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(\
            ("test_media", "Widget"))
        self.mnemosyne.gui_for_component["ScheduledForgottenNew"] = \
            [("mnemosyne_test", "TestReviewWidget")]
        self.mnemosyne.initialise(os.path.abspath("dot_test"), automatic_upgrades=False)
        self.review_controller().reset()

    def test_sound_1(self):
        global filename

        filename = ""
        self.controller().show_insert_sound_dialog("")

    def test_sound_2(self):
        global filename

        open("a.ogg", "w")
        filename = os.path.abspath("a.ogg")
        self.controller().show_insert_sound_dialog("")
        assert os.path.exists(os.path.join(self.database().media_dir(), "a.ogg"))

        filename = os.path.join(self.database().media_dir(), "a.ogg")
        self.controller().show_insert_sound_dialog("")
        assert os.path.exists(os.path.join(self.database().media_dir(), "a.ogg"))

    def test_sound_2_unicode(self):
        global filename

        open(chr(40960) + "a.ogg", "w")
        filename = os.path.abspath(chr(40960) + "a.ogg")
        self.controller().show_insert_sound_dialog("")
        assert os.path.exists(os.path.join(self.database().media_dir(), chr(40960) + "a.ogg"))

        filename = os.path.join(self.database().media_dir(), chr(40960) + "a.ogg")
        self.controller().show_insert_sound_dialog("")
        assert os.path.exists(os.path.join(self.database().media_dir(), chr(40960) + "a.ogg"))

    def test_sound_3(self):
        global filename

        open("a.ogg", "w")

        filename = os.path.abspath("a.ogg")
        self.controller().show_insert_sound_dialog("")
        assert os.path.exists(os.path.join(self.database().media_dir(), "a.ogg"))

        self.controller().show_insert_sound_dialog("")
        assert os.path.exists(os.path.join(self.database().media_dir(), "a_1.ogg"))

        self.controller().show_insert_sound_dialog("")
        assert os.path.exists(os.path.join(self.database().media_dir(), "a_2.ogg"))

    def test_img_1(self):
        global filename

        filename = ""
        self.controller().show_insert_img_dialog("")

    def test_img_2(self):
        global filename

        open("a.ogg", "w")
        filename = os.path.abspath("a.ogg")
        self.controller().show_insert_img_dialog("")
        assert os.path.exists(os.path.join(self.database().media_dir(), "a.ogg"))

        filename = os.path.join(self.database().media_dir(), "a.ogg")
        self.controller().show_insert_img_dialog("")
        assert os.path.exists(os.path.join(self.database().media_dir(), "a.ogg"))

    def test_media_subdir(self):
        global filename

        subdir = os.path.join(self.database().media_dir(), "subdir")
        os.mkdir(subdir)
        filename = os.path.join(subdir, "b.ogg")
        open(filename, "w")
        self.controller().show_insert_img_dialog("")
        assert os.path.exists(os.path.join(self.database().media_dir(),
                                           "subdir", "b.ogg"))

    def test_card(self):
        open("a.ogg", "w")
        full_path = os.path.abspath("a.ogg").replace("\\", "/")
        fact_data = {"f": "<img src=\"%s\">" % full_path,
                     "b": "answer"}
        card_type = self.card_type_with_id("1")
        card = self.controller().create_new_cards(fact_data, card_type,
                                              grade=-1, tag_names=["default"])[0]
        full_path_in_media_dir = \
            os.path.join(self.database().media_dir(), "a.ogg").replace("\\", "/")
        # Make sure we don't reuse existing objects.
        card = self.database().card(card._id, is_id_internal=True)
        assert os.path.exists(full_path_in_media_dir)
        assert full_path not in card.fact.data["f"]
        assert full_path_in_media_dir not in card.fact.data["f"]
        assert full_path not in card.question()
        assert full_path_in_media_dir in card.question()
        assert self.database().con.execute(\
            "select count() from log where event_type=?",
            (EventTypes.ADDED_MEDIA_FILE, )).fetchone()[0] == 1

    def test_audio_start_stop(self):
        open("a.ogg", "w")
        full_path = os.path.abspath("a.ogg").replace("\\", "/")
        fact_data = {"f": "<audio src=\"%s\" start=\"1\" stop=\"3\">" % full_path,
                     "b": "answer"}
        card_type = self.card_type_with_id("1")
        card = self.controller().create_new_cards(fact_data, card_type,
                                              grade=-1, tag_names=["default"])[0]
        full_path_in_media_dir = \
            os.path.join(self.database().media_dir(), "a.ogg").replace("\\", "/")
        # Make sure we don't reuse existing objects.
        card = self.database().card(card._id, is_id_internal=True)
        assert os.path.exists(full_path_in_media_dir)
        assert full_path not in card.fact.data["f"]
        assert full_path_in_media_dir not in card.fact.data["f"]
        assert full_path.replace(" ", "%20") not in card.question()
        assert full_path_in_media_dir.replace(" ", "%20") in card.question()
        assert self.database().con.execute(\
            "select count() from log where event_type=?",
            (EventTypes.ADDED_MEDIA_FILE, )).fetchone()[0] == 1

    def test_external_media(self):
        fact_data = {"f": "<img src=\"http://www.chine-nouvelle.com/jdd/public/ct/pinyinaudio/shu4.mp3\"",
                     "b": "answer"}
        card_type = self.card_type_with_id("1")
        card = self.controller().create_new_cards(fact_data, card_type,
                                              grade=-1, tag_names=["default"])[0]
        assert "http" in card.question()
        assert "file" not in card.question()
        assert self.database().con.execute(\
            "select count() from log where event_type=?",
            (EventTypes.ADDED_MEDIA_FILE, )).fetchone()[0] == 0

    def test_card_2(self):
        fact_data = {"f": "<img src=\"a.ogg>", # Missing closing "
                     "b": "answer"}
        card_type = self.card_type_with_id("1")
        card = self.controller().create_new_cards(fact_data, card_type,
                                              grade=-1, tag_names=["default"])[0]
        # Make sure we don't reuse existing objects.
        card = self.database().card(card._id, is_id_internal=True)
        assert os.path.join(self.database().media_dir(), "a.ogg") \
               not in card.question()

    def test_missing_media(self):
        fact_data = {"f": "<img src=\"missing.ogg\">",
                     "b": "answer"}
        card_type = self.card_type_with_id("1")
        card = self.controller().create_new_cards(fact_data, card_type,
                                              grade=-1, tag_names=["default"])[0]
        assert "src_missing" in card.question()

    def test_long_media(self):
        filename = 220*"a"
        fact_data = {"f": "<img src=\"%s\">" % (filename, ),
                     "b": "answer"}
        card_type = self.card_type_with_id("1")
        card = self.controller().create_new_cards(fact_data, card_type,
                                              grade=-1, tag_names=["default"])[0]

    def test_card_edit_none(self):
        open("a.ogg", "w")
        full_path = os.path.abspath("a.ogg")

        fact_data = {"f": "<img src=\"%s\">" % full_path,
                     "b": "answer"}
        card_type = self.card_type_with_id("1")
        card = self.controller().create_new_cards(fact_data, card_type,
                                              grade=-1, tag_names=["default"])[0]
        full_path_in_media_dir = \
            os.path.join(self.database().media_dir(), "a.ogg").replace("\\", "/")

        fact_data = {"f": "edited <img src=\"%s\">" % "a.ogg",
                     "b": "answer"}
        self.controller().edit_card_and_sisters(card, fact_data,
           card_type, new_tag_names=["bla"], correspondence=None)
        # Make sure we don't reuse existing objects.
        card = self.database().card(card._id, is_id_internal=True)
        assert os.path.exists(full_path_in_media_dir)
        assert full_path not in card.fact.data["f"]
        assert full_path_in_media_dir not in card.fact.data["f"]
        assert full_path not in card.question()
        assert full_path_in_media_dir in card.question()
        assert self.database().con.execute(\
            "select count() from log where event_type=?",
            (EventTypes.ADDED_MEDIA_FILE, )).fetchone()[0] == 1

    def test_card_edit_add(self):
        open("a.ogg", "w")
        full_path = os.path.abspath("a.ogg").replace("\\", "/")

        fact_data = {"f": "<img src=\"%s\">" % full_path,
                     "b": "answer"}
        card_type = self.card_type_with_id("1")
        card = self.controller().create_new_cards(fact_data, card_type,
                                              grade=-1, tag_names=["default"])[0]
        # Make sure we don't reuse existing objects.
        card = self.database().card(card._id, is_id_internal=True)
        open("b.ogg", "w")
        full_path = os.path.abspath("b.ogg").replace("\\", "/")
        fact_data = {"f": "edited <img src=\"%s\"> <img src=\"%s\">" \
                     % ("a.ogg", full_path),
                     "b": "answer"}
        self.controller().edit_card_and_sisters(card, fact_data,
           card_type, new_tag_names=["bla"], correspondence=None)
        card = self.database().card(card._id, is_id_internal=True)
        full_path_in_media_dir = \
            os.path.join(self.database().media_dir(), "b.ogg").replace("\\", "/")
        assert os.path.exists(full_path_in_media_dir)
        assert full_path not in card.fact.data["f"]
        assert full_path_in_media_dir not in card.fact.data["f"]
        assert full_path not in card.question()
        assert full_path_in_media_dir in card.question()
        assert self.database().con.execute(\
            "select count() from log where event_type=?",
            (EventTypes.ADDED_MEDIA_FILE, )).fetchone()[0] == 2

    def test_card_edit_delete(self):
        open("a.ogg", "w")
        full_path = os.path.abspath("a.ogg").replace("\\", "/")

        fact_data = {"f": "<img src=\"%s\">" % full_path,
                     "b": "answer"}
        card_type = self.card_type_with_id("1")
        card = self.controller().create_new_cards(fact_data, card_type,
                                              grade=-1, tag_names=["default"])[0]
        # Make sure we don't reuse existing objects.
        card = self.database().card(card._id, is_id_internal=True)
        fact_data = {"f": "edited ",
                     "b": "answer"}
        self.controller().edit_card_and_sisters(card, fact_data,
           card_type, new_tag_names=["bla"], correspondence=None)
        # Make sure we don't reuse existing objects.
        card = self.database().card(card._id, is_id_internal=True)
        full_path_in_media_dir = \
            os.path.join(self.database().media_dir(), "a.ogg").replace("\\", "/")
        self.database().delete_unused_media_files(self.database().unused_media_files())
        assert not os.path.exists(full_path_in_media_dir)
        assert full_path_in_media_dir not in card.question()
        assert self.database().con.execute(\
            "select count() from log where event_type=?",
            (EventTypes.ADDED_MEDIA_FILE, )).fetchone()[0] == 1
        assert self.database().con.execute(\
            "select count() from log where event_type=?",
            (EventTypes.DELETED_MEDIA_FILE, )).fetchone()[0] == 1

    def test_card_edit_delete_used_by_other(self):
        open("a.ogg", "w")
        full_path = os.path.abspath("a.ogg")

        fact_data = {"f": "<img src=\"%s\">" % full_path,
                     "b": "answer"}
        card_type = self.card_type_with_id("1")
        card = self.controller().create_new_cards(fact_data, card_type,
                                              grade=-1, tag_names=["default"])[0]
        # Make sure we don't reuse existing objects.
        card = self.database().card(card._id, is_id_internal=True)
        fact_data = {"f": "2 <img src=\'%s\'>" % "a.ogg",
                     "b": "answer"}
        self.controller().create_new_cards(fact_data, card_type,
                                           grade=-1, tag_names=["default"])[0]
        fact_data = {"f": "edited",
                     "b": "answer"}
        self.controller().edit_card_and_sisters(card, fact_data,
           card_type, new_tag_names=["bla"], correspondence=None)
        # Make sure we don't reuse existing objects.
        card = self.database().card(card._id, is_id_internal=True)
        full_path_in_media_dir = os.path.join(self.database().media_dir(), "a.ogg")
        assert os.path.exists(full_path_in_media_dir) # Don't delete file.
        assert full_path_in_media_dir not in card.question()
        assert self.database().con.execute(\
            "select count() from log where event_type=?",
            (EventTypes.ADDED_MEDIA_FILE, )).fetchone()[0] == 1

    def test_delete_fact(self):
        open("a.ogg", "w")
        full_path = os.path.abspath("a.ogg")

        fact_data = {"f": "<img src=\"%s\">" % full_path,
                     "b": "answer"}
        card_type = self.card_type_with_id("1")
        card = self.controller().create_new_cards(fact_data, card_type,
                                              grade=-1, tag_names=["default"])[0]
        # Make sure we don't reuse existing objects.
        card = self.database().card(card._id, is_id_internal=True)
        self.controller().delete_facts_and_their_cards([card.fact])
        self.database().delete_unused_media_files(self.database().unused_media_files())
        full_path_in_media_dir = os.path.join(self.database().media_dir(), "a.ogg")
        assert not os.path.exists(full_path_in_media_dir) # Autodelete.
        assert self.database().con.execute(\
            "select count() from log where event_type=?",
            (EventTypes.ADDED_MEDIA_FILE, )).fetchone()[0] == 1

    def test_delete_fact_used_by_other(self):
        open("a.ogg", "w")
        full_path = os.path.abspath("a.ogg")

        fact_data = {"f": "<img src=\"%s\">" % full_path,
                     "b": "answer"}
        card_type = self.card_type_with_id("1")
        card = self.controller().create_new_cards(fact_data, card_type,
                                              grade=-1, tag_names=["default"])[0]
        # Make sure we don't reuse existing objects.
        card = self.database().card(card._id, is_id_internal=True)
        fact_data = {"f": "2 <img src=\"%s\">" % "a.ogg",
                     "b": "answer"}
        self.controller().create_new_cards(fact_data, card_type,
                                              grade=-1, tag_names=["default"])[0]
        self.controller().delete_facts_and_their_cards([card.fact])
        full_path_in_media_dir = os.path.join(self.database().media_dir(), "a.ogg")
        assert os.path.exists(full_path_in_media_dir) # Not deleted.
        assert self.database().con.execute(\
            "select count() from log where event_type=?",
            (EventTypes.ADDED_MEDIA_FILE, )).fetchone()[0] == 1

    def test_unused(self):
        open(os.path.join(self.database().media_dir(), "a.ogg"), "w")
        os.mkdir(os.path.join(self.database().media_dir(), "sub"))
        open(os.path.join(self.database().media_dir(), "sub", "b.ogg"), "w")

        os.mkdir(os.path.join(self.database().media_dir(), "_keep"))
        open(os.path.join(self.database().media_dir(), "_keep", "b.ogg"), "w")

        open("c.ogg", "w")
        full_path = os.path.abspath("c.ogg")

        fact_data = {"f": "<img src=\"%s\">" % full_path,
                     "b": "answer"}
        card_type = self.card_type_with_id("1")
        card = self.controller().create_new_cards(fact_data, card_type,
                                              grade=-1, tag_names=["default"])[0]

        assert os.path.exists(os.path.join(self.database().media_dir(), "a.ogg"))
        assert os.path.exists(os.path.join(self.database().media_dir(), "sub"))
        assert os.path.exists(os.path.join(self.database().media_dir(), "sub", "b.ogg"))
        assert os.path.exists(os.path.join(self.database().media_dir(), "_keep"))
        assert os.path.exists(os.path.join(self.database().media_dir(), "_keep", "b.ogg"))

        self.database().delete_unused_media_files(self.database().unused_media_files())

        assert not os.path.exists(os.path.join(self.database().media_dir(), "a.ogg"))
        assert not os.path.exists(os.path.join(self.database().media_dir(), "sub"))
        assert not os.path.exists(os.path.join(self.database().media_dir(), "sub", "b.ogg"))
        assert os.path.exists(os.path.join(self.database().media_dir(), "c.ogg"))
        assert os.path.exists(os.path.join(self.database().media_dir(), "_keep"))
        assert os.path.exists(os.path.join(self.database().media_dir(), "_keep", "b.ogg"))

    def test_unused_latex(self):
        fact_data = {"f": "<latex>a</latex>",
                     "b": "answer"}
        card_type = self.card_type_with_id("1")
        card = self.controller().create_new_cards(fact_data, card_type,
                                              grade=-1, tag_names=["default"])[0]
        card.question()
        latex_dir = os.path.join(self.database().media_dir(), "_latex")
        assert os.path.exists(latex_dir)
        self.database().delete_unused_media_files(self.database().unused_media_files())
        assert not os.path.exists(latex_dir)

    def test_database_not_in_datadir(self):
        assert "dot_test" in self.database().media_dir()
        self.database().new(os.path.abspath("outside.db"))
        assert self.database().media_dir() == os.path.join(\
            os.path.dirname(os.path.abspath("outside.db")), "outside.db_media").replace("\\", "/")
        assert "dot_test" not in self.database().media_dir()

    def teardown(self):
        MnemosyneTest.teardown(self)
        if os.path.exists("a.ogg"):
            os.remove("a.ogg")
        if os.path.exists("b.ogg"):
            os.remove("b.ogg")
        if os.path.exists("c.ogg"):
            os.remove("c.ogg")
        if os.path.exists(chr(40960) + "a.ogg"):
            os.remove(chr(40960) + "a.ogg")
        if os.path.exists("sub"):
            shutil.rmtree("sub")
        if os.path.exists("_keep"):
            shutil.rmtree("_keep")
        if os.path.exists("outside.db"):
            os.remove("outside.db")
        if os.path.exists("outside.db-journal"):
            os.remove("outside.db-journal")