anki/pylib/tests/test_exporting.py

167 lines
4.4 KiB
Python
Raw Normal View History

2021-04-13 10:45:05 +02:00
# Copyright: Ankitects Pty Ltd and contributors
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
# coding: utf-8
import os
import tempfile
from anki import Collection as aopen
from anki.exporting import *
from anki.importing import Anki2Importer
2020-03-22 05:41:01 +01:00
from tests.shared import errorsAfterMidnight
from tests.shared import getEmptyCol as getEmptyColOrig
def getEmptyCol():
col = getEmptyColOrig()
col.upgrade_to_v2_scheduler()
return col
col = None
ds = None
testDir = os.path.dirname(__file__)
2019-12-25 05:18:34 +01:00
def setup1():
global col
col = getEmptyCol()
note = col.newNote()
note["Front"] = "foo"
note["Back"] = "bar<br>"
note.tags = ["tag", "tag2"]
col.addNote(note)
# with a different col
note = col.newNote()
note["Front"] = "baz"
note["Back"] = "qux"
2021-06-27 04:12:23 +02:00
note.note_type()["did"] = col.decks.id("new col")
col.addNote(note)
2019-12-25 05:18:34 +01:00
##########################################################################
2019-12-25 05:18:34 +01:00
def test_export_anki():
setup1()
# create a new col with its own conf to test conf copying
did = col.decks.id("test")
dobj = col.decks.get(did)
confId = col.decks.add_config_returning_id("newconf")
conf = col.decks.get_config(confId)
2019-12-25 05:18:34 +01:00
conf["new"]["perDay"] = 5
col.decks.save(conf)
2021-06-27 06:02:48 +02:00
col.decks.set_config_id_for_deck_dict(dobj, confId)
# export
e = AnkiExporter(col)
fd, newname = tempfile.mkstemp(prefix="ankitest", suffix=".anki2")
newname = str(newname)
os.close(fd)
os.unlink(newname)
e.exportInto(newname)
# exporting should not have changed conf for original deck
2021-06-27 06:02:48 +02:00
conf = col.decks.config_dict_for_deck_id(did)
2019-12-25 05:18:34 +01:00
assert conf["id"] != 1
# connect to new deck
2020-07-17 17:55:57 +02:00
col2 = aopen(newname)
2021-06-27 07:12:22 +02:00
assert col2.card_count() == 2
# as scheduling was reset, should also revert decks to default conf
2020-07-17 17:55:57 +02:00
did = col2.decks.id("test", create=False)
assert did
2021-06-27 06:02:48 +02:00
conf2 = col2.decks.config_dict_for_deck_id(did)
2019-12-25 05:18:34 +01:00
assert conf2["new"]["perDay"] == 20
2020-07-17 17:55:57 +02:00
dobj = col2.decks.get(did)
# conf should be 1
2019-12-25 05:18:34 +01:00
assert dobj["conf"] == 1
# try again, limited to a deck
fd, newname = tempfile.mkstemp(prefix="ankitest", suffix=".anki2")
newname = str(newname)
os.close(fd)
os.unlink(newname)
e.did = 1
e.exportInto(newname)
2020-07-17 17:55:57 +02:00
col2 = aopen(newname)
2021-06-27 07:12:22 +02:00
assert col2.card_count() == 1
2019-12-25 05:18:34 +01:00
def test_export_ankipkg():
setup1()
# add a test file to the media folder
with open(os.path.join(col.media.dir(), "今日.mp3"), "w") as note:
note.write("test")
n = col.newNote()
2019-12-25 05:18:34 +01:00
n["Front"] = "[sound:今日.mp3]"
col.addNote(n)
e = AnkiPackageExporter(col)
fd, newname = tempfile.mkstemp(prefix="ankitest", suffix=".apkg")
newname = str(newname)
os.close(fd)
os.unlink(newname)
e.exportInto(newname)
2019-12-25 05:18:34 +01:00
2020-03-22 05:41:01 +01:00
@errorsAfterMidnight
def test_export_anki_due():
setup1()
col = getEmptyCol()
note = col.newNote()
note["Front"] = "foo"
col.addNote(note)
col.crt -= 86400 * 10
col.sched.reset()
c = col.sched.getCard()
col.sched.answerCard(c, 3)
col.sched.answerCard(c, 3)
# should have ivl of 1, due on day 11
assert c.ivl == 1
assert c.due == 11
assert col.sched.today == 10
assert c.due - col.sched.today == 1
# export
e = AnkiExporter(col)
e.includeSched = True
fd, newname = tempfile.mkstemp(prefix="ankitest", suffix=".anki2")
newname = str(newname)
os.close(fd)
os.unlink(newname)
e.exportInto(newname)
# importing into a new deck, the due date should be equivalent
2020-07-17 17:27:40 +02:00
col2 = getEmptyCol()
imp = Anki2Importer(col2, newname)
imp.run()
2020-07-17 17:27:40 +02:00
c = col2.getCard(c.id)
col2.sched.reset()
assert c.due - col2.sched.today == 1
2019-12-25 05:18:34 +01:00
# def test_export_textcard():
# setup1()
# e = TextCardExporter(col)
# note = unicode(tempfile.mkstemp(prefix="ankitest")[1])
# os.unlink(note)
# e.exportInto(note)
# e.includeTags = True
# e.exportInto(note)
2019-12-25 05:18:34 +01:00
def test_export_textnote():
setup1()
e = TextNoteExporter(col)
fd, note = tempfile.mkstemp(prefix="ankitest")
note = str(note)
os.close(fd)
os.unlink(note)
e.exportInto(note)
with open(note) as file:
assert file.readline() == "foo\tbar<br>\ttag tag2\n"
2019-03-04 23:57:53 +01:00
e.includeTags = False
e.includeHTML = False
e.exportInto(note)
with open(note) as file:
assert file.readline() == "foo\tbar\n"
2019-12-25 05:18:34 +01:00
def test_exporters():
assert "*.apkg" in str(exporters(getEmptyCol()))