def test_anki2_mediadupes(): tmp = getEmptyCol() # add a note that references a sound n = tmp.newNote() n["Front"] = "[sound:foo.mp3]" mid = n.model()["id"] tmp.addNote(n) # add that sound to media folder with open(os.path.join(tmp.media.dir(), "foo.mp3"), "w") as f: f.write("foo") tmp.close() # it should be imported correctly into an empty deck empty = getEmptyCol() imp = Anki2Importer(empty, tmp.path) imp.run() assert os.listdir(empty.media.dir()) == ["foo.mp3"] # and importing again will not duplicate, as the file content matches empty.remove_cards_and_orphaned_notes( empty.db.list("select id from cards")) imp = Anki2Importer(empty, tmp.path) imp.run() assert os.listdir(empty.media.dir()) == ["foo.mp3"] n = empty.getNote(empty.db.scalar("select id from notes")) assert "foo.mp3" in n.fields[0] # if the local file content is different, and import should trigger a # rename empty.remove_cards_and_orphaned_notes( empty.db.list("select id from cards")) with open(os.path.join(empty.media.dir(), "foo.mp3"), "w") as f: f.write("bar") imp = Anki2Importer(empty, tmp.path) imp.run() assert sorted(os.listdir( empty.media.dir())) == ["foo.mp3", "foo_%s.mp3" % mid] n = empty.getNote(empty.db.scalar("select id from notes")) assert "_" in n.fields[0] # if the localized media file already exists, we rewrite the note and # media empty.remove_cards_and_orphaned_notes( empty.db.list("select id from cards")) with open(os.path.join(empty.media.dir(), "foo.mp3"), "w") as f: f.write("bar") imp = Anki2Importer(empty, tmp.path) imp.run() assert sorted(os.listdir( empty.media.dir())) == ["foo.mp3", "foo_%s.mp3" % mid] assert sorted(os.listdir( empty.media.dir())) == ["foo.mp3", "foo_%s.mp3" % mid] n = empty.getNote(empty.db.scalar("select id from notes")) assert "_" in n.fields[0]
def test_export_anki_due(): setup1() deck = getEmptyCol() f = deck.newNote() f["Front"] = "foo" deck.addNote(f) deck.crt -= 86400 * 10 deck.sched.reset() c = deck.sched.getCard() deck.sched.answerCard(c, 3) deck.sched.answerCard(c, 3) # should have ivl of 1, due on day 11 assert c.ivl == 1 assert c.due == 11 assert deck.sched.today == 10 assert c.due - deck.sched.today == 1 # export e = AnkiExporter(deck) 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 deck2 = getEmptyCol() imp = Anki2Importer(deck2, newname) imp.run() c = deck2.getCard(c.id) deck2.sched.reset() assert c.due - deck2.sched.today == 1
def test_export_anki_due(getFuzz): getFuzz.return_value = False deck = getEmptyCol(scheduler='anki.schedv3.Scheduler') f = deck.newNote() f['Front'] = "foo" deck.addNote(f) deck.crt -= 86400 * 10 deck.sched.reset() c = deck.sched.getCard() deck.sched.answerCard(c, 3) deck.sched.answerCard(c, 3) # should have ivl of 1, due on day 12 assert pytest.approx(1.48, c.ivl, 0.01) assert c.due == 12 assert deck.sched.today == 10 assert c.due - deck.sched.today == 2 # export e = AnkiExporter(deck) 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 deck2 = getEmptyCol(scheduler='anki.sched.Scheduler') imp = Anki2Importer(deck2, newname) imp.run() c = deck2.getCard(c.id) deck2.sched.reset() assert c.due - deck2.sched.today == 2
def test_anki2(): global srcNotes, srcCards # get the deck to import tmp = getUpgradeDeckPath() u = Upgrader() src = u.upgrade(tmp) srcpath = src.path srcNotes = src.noteCount() srcCards = src.cardCount() srcRev = src.db.scalar("select count() from revlog") # add a media file for testing open(os.path.join(src.media.dir(), "foo.jpg"), "w").write("foo") src.close() # create a new empty deck dst = getEmptyDeck() # import src into dst imp = Anki2Importer(dst, srcpath) imp.run() def check(): assert dst.noteCount() == srcNotes assert dst.cardCount() == srcCards assert srcRev == dst.db.scalar("select count() from revlog") mids = [int(x) for x in dst.models.models.keys()] assert not dst.db.scalar( "select count() from notes where mid not in "+ids2str(mids)) assert not dst.db.scalar( "select count() from cards where nid not in (select id from notes)") assert not dst.db.scalar( "select count() from revlog where cid not in (select id from cards)") assert dst.fixIntegrity()[0].startswith("Database rebuilt") check() # importing should be idempotent imp.run() check() assert len(os.listdir(dst.media.dir())) == 1