def test_anki1_diffmodels(): # create a new empty deck dst = getEmptyDeck() # import the 1 card version of the model tmp = getUpgradeDeckPath("diffmodels1.anki") imp = Anki1Importer(dst, tmp) imp.run() before = dst.noteCount() # repeating the process should do nothing imp = Anki1Importer(dst, tmp) imp.run() assert before == dst.noteCount() # then the 2 card version tmp = getUpgradeDeckPath("diffmodels2.anki") imp = Anki1Importer(dst, tmp) imp.run() after = dst.noteCount() # as the model schemas differ, should have been imported as new model assert after == before + 1 # repeating the process should do nothing beforeModels = len(dst.models.all()) imp = Anki1Importer(dst, tmp) imp.run() after = dst.noteCount() assert after == before + 1 assert beforeModels == len(dst.models.all())
def test_anki2_diffmodels(): # create a new empty deck dst = getEmptyDeck() # import the 1 card version of the model tmp = getUpgradeDeckPath("diffmodels2-1.apkg") imp = AnkiPackageImporter(dst, tmp) imp.run() before = dst.noteCount() # repeating the process should do nothing imp = AnkiPackageImporter(dst, tmp) imp.run() assert before == dst.noteCount() # then the 2 card version tmp = getUpgradeDeckPath("diffmodels2-2.apkg") imp = AnkiPackageImporter(dst, tmp) imp.run() after = dst.noteCount() # as the model schemas differ, should have been imported as new model assert after == before + 1 # and the new model should have both cards assert dst.cardCount() == 3 # repeating the process should do nothing imp = AnkiPackageImporter(dst, tmp) imp.run() after = dst.noteCount() assert after == before + 1 assert dst.cardCount() == 3
def test_anki2_updates(): # create a new empty deck dst = getEmptyCol() tmp = getUpgradeDeckPath("update1.apkg") imp = AnkiPackageImporter(dst, tmp) imp.run() assert imp.dupes == 0 assert imp.added == 1 assert imp.updated == 0 # importing again should be idempotent imp = AnkiPackageImporter(dst, tmp) imp.run() assert imp.dupes == 1 assert imp.added == 0 assert imp.updated == 0 # importing a newer note should update assert dst.noteCount() == 1 assert dst.db.scalar("select flds from notes").startswith("hello") tmp = getUpgradeDeckPath("update2.apkg") imp = AnkiPackageImporter(dst, tmp) imp.run() assert imp.dupes == 0 assert imp.added == 0 assert imp.updated == 1 assert dst.noteCount() == 1 assert dst.db.scalar("select flds from notes").startswith("goodbye")
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
def test_suspended(): # create a new empty deck dst = getEmptyDeck() # import the 1 card version of the model tmp = getUpgradeDeckPath("suspended12.anki") imp = Anki1Importer(dst, tmp) imp.run() assert dst.db.scalar("select due from cards") < 0
def test_anki2_diffmodel_templates(): # different from the above as this one tests only the template text being # changed, not the number of cards/fields dst = getEmptyCol() # import the first version of the model tmp = getUpgradeDeckPath("diffmodeltemplates-1.apkg") imp = AnkiPackageImporter(dst, tmp) imp.dupeOnSchemaChange = True imp.run() # then the version with updated template tmp = getUpgradeDeckPath("diffmodeltemplates-2.apkg") imp = AnkiPackageImporter(dst, tmp) imp.dupeOnSchemaChange = True imp.run() # collection should contain the note we imported assert dst.noteCount() == 1 # the front template should contain the text added in the 2nd package tcid = dst.findCards("")[0] # only 1 note in collection tnote = dst.getCard(tcid).note() assert "Changed Front Template" in tnote.cards()[0].template()["qfmt"]
def test_anki1(): # get the deck path to import tmp = getUpgradeDeckPath() # make sure media is imported properly through the upgrade mdir = tmp.replace(".anki2", ".media") if not os.path.exists(mdir): os.mkdir(mdir) open(os.path.join(mdir, "_foo.jpg"), "w").write("foo") # create a new empty deck dst = getEmptyDeck() # import src into dst imp = Anki1Importer(dst, tmp) imp.run() def check(): assert dst.noteCount() == srcNotes assert dst.cardCount() == srcCards assert len(os.listdir(dst.media.dir())) == 1 check() # importing should be idempotent imp = Anki1Importer(dst, tmp) imp.run() check()