tmp->col in tests
This commit is contained in:
parent
e7378734d2
commit
e7d75c2c8d
@ -30,24 +30,24 @@ def clear_tempfile(tf):
|
|||||||
|
|
||||||
|
|
||||||
def test_anki2_mediadupes():
|
def test_anki2_mediadupes():
|
||||||
tmp = getEmptyCol()
|
col = getEmptyCol()
|
||||||
# add a note that references a sound
|
# add a note that references a sound
|
||||||
n = tmp.newNote()
|
n = col.newNote()
|
||||||
n["Front"] = "[sound:foo.mp3]"
|
n["Front"] = "[sound:foo.mp3]"
|
||||||
mid = n.model()["id"]
|
mid = n.model()["id"]
|
||||||
tmp.addNote(n)
|
col.addNote(n)
|
||||||
# add that sound to media folder
|
# add that sound to media folder
|
||||||
with open(os.path.join(tmp.media.dir(), "foo.mp3"), "w") as note:
|
with open(os.path.join(col.media.dir(), "foo.mp3"), "w") as note:
|
||||||
note.write("foo")
|
note.write("foo")
|
||||||
tmp.close()
|
col.close()
|
||||||
# it should be imported correctly into an empty deck
|
# it should be imported correctly into an empty deck
|
||||||
empty = getEmptyCol()
|
empty = getEmptyCol()
|
||||||
imp = Anki2Importer(empty, tmp.path)
|
imp = Anki2Importer(empty, col.path)
|
||||||
imp.run()
|
imp.run()
|
||||||
assert os.listdir(empty.media.dir()) == ["foo.mp3"]
|
assert os.listdir(empty.media.dir()) == ["foo.mp3"]
|
||||||
# and importing again will not duplicate, as the file content matches
|
# and importing again will not duplicate, as the file content matches
|
||||||
empty.remove_cards_and_orphaned_notes(empty.db.list("select id from cards"))
|
empty.remove_cards_and_orphaned_notes(empty.db.list("select id from cards"))
|
||||||
imp = Anki2Importer(empty, tmp.path)
|
imp = Anki2Importer(empty, col.path)
|
||||||
imp.run()
|
imp.run()
|
||||||
assert os.listdir(empty.media.dir()) == ["foo.mp3"]
|
assert os.listdir(empty.media.dir()) == ["foo.mp3"]
|
||||||
n = empty.getNote(empty.db.scalar("select id from notes"))
|
n = empty.getNote(empty.db.scalar("select id from notes"))
|
||||||
@ -57,7 +57,7 @@ def test_anki2_mediadupes():
|
|||||||
empty.remove_cards_and_orphaned_notes(empty.db.list("select id from cards"))
|
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 note:
|
with open(os.path.join(empty.media.dir(), "foo.mp3"), "w") as note:
|
||||||
note.write("bar")
|
note.write("bar")
|
||||||
imp = Anki2Importer(empty, tmp.path)
|
imp = Anki2Importer(empty, col.path)
|
||||||
imp.run()
|
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"))
|
n = empty.getNote(empty.db.scalar("select id from notes"))
|
||||||
@ -67,7 +67,7 @@ def test_anki2_mediadupes():
|
|||||||
empty.remove_cards_and_orphaned_notes(empty.db.list("select id from cards"))
|
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 note:
|
with open(os.path.join(empty.media.dir(), "foo.mp3"), "w") as note:
|
||||||
note.write("bar")
|
note.write("bar")
|
||||||
imp = Anki2Importer(empty, tmp.path)
|
imp = Anki2Importer(empty, col.path)
|
||||||
imp.run()
|
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]
|
||||||
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]
|
||||||
@ -76,24 +76,24 @@ def test_anki2_mediadupes():
|
|||||||
|
|
||||||
|
|
||||||
def test_apkg():
|
def test_apkg():
|
||||||
tmp = getEmptyCol()
|
col = getEmptyCol()
|
||||||
apkg = str(os.path.join(testDir, "support/media.apkg"))
|
apkg = str(os.path.join(testDir, "support/media.apkg"))
|
||||||
imp = AnkiPackageImporter(tmp, apkg)
|
imp = AnkiPackageImporter(col, apkg)
|
||||||
assert os.listdir(tmp.media.dir()) == []
|
assert os.listdir(col.media.dir()) == []
|
||||||
imp.run()
|
imp.run()
|
||||||
assert os.listdir(tmp.media.dir()) == ["foo.wav"]
|
assert os.listdir(col.media.dir()) == ["foo.wav"]
|
||||||
# importing again should be idempotent in terms of media
|
# importing again should be idempotent in terms of media
|
||||||
tmp.remove_cards_and_orphaned_notes(tmp.db.list("select id from cards"))
|
col.remove_cards_and_orphaned_notes(col.db.list("select id from cards"))
|
||||||
imp = AnkiPackageImporter(tmp, apkg)
|
imp = AnkiPackageImporter(col, apkg)
|
||||||
imp.run()
|
imp.run()
|
||||||
assert os.listdir(tmp.media.dir()) == ["foo.wav"]
|
assert os.listdir(col.media.dir()) == ["foo.wav"]
|
||||||
# but if the local file has different data, it will rename
|
# but if the local file has different data, it will rename
|
||||||
tmp.remove_cards_and_orphaned_notes(tmp.db.list("select id from cards"))
|
col.remove_cards_and_orphaned_notes(col.db.list("select id from cards"))
|
||||||
with open(os.path.join(tmp.media.dir(), "foo.wav"), "w") as note:
|
with open(os.path.join(col.media.dir(), "foo.wav"), "w") as note:
|
||||||
note.write("xyz")
|
note.write("xyz")
|
||||||
imp = AnkiPackageImporter(tmp, apkg)
|
imp = AnkiPackageImporter(col, apkg)
|
||||||
imp.run()
|
imp.run()
|
||||||
assert len(os.listdir(tmp.media.dir())) == 2
|
assert len(os.listdir(col.media.dir())) == 2
|
||||||
|
|
||||||
|
|
||||||
def test_anki2_diffmodel_templates():
|
def test_anki2_diffmodel_templates():
|
||||||
@ -101,13 +101,13 @@ def test_anki2_diffmodel_templates():
|
|||||||
# changed, not the number of cards/fields
|
# changed, not the number of cards/fields
|
||||||
dst = getEmptyCol()
|
dst = getEmptyCol()
|
||||||
# import the first version of the model
|
# import the first version of the model
|
||||||
tmp = getUpgradeDeckPath("diffmodeltemplates-1.apkg")
|
col = getUpgradeDeckPath("diffmodeltemplates-1.apkg")
|
||||||
imp = AnkiPackageImporter(dst, tmp)
|
imp = AnkiPackageImporter(dst, col)
|
||||||
imp.dupeOnSchemaChange = True
|
imp.dupeOnSchemaChange = True
|
||||||
imp.run()
|
imp.run()
|
||||||
# then the version with updated template
|
# then the version with updated template
|
||||||
tmp = getUpgradeDeckPath("diffmodeltemplates-2.apkg")
|
col = getUpgradeDeckPath("diffmodeltemplates-2.apkg")
|
||||||
imp = AnkiPackageImporter(dst, tmp)
|
imp = AnkiPackageImporter(dst, col)
|
||||||
imp.dupeOnSchemaChange = True
|
imp.dupeOnSchemaChange = True
|
||||||
imp.run()
|
imp.run()
|
||||||
# collection should contain the note we imported
|
# collection should contain the note we imported
|
||||||
@ -121,14 +121,14 @@ def test_anki2_diffmodel_templates():
|
|||||||
def test_anki2_updates():
|
def test_anki2_updates():
|
||||||
# create a new empty deck
|
# create a new empty deck
|
||||||
dst = getEmptyCol()
|
dst = getEmptyCol()
|
||||||
tmp = getUpgradeDeckPath("update1.apkg")
|
col = getUpgradeDeckPath("update1.apkg")
|
||||||
imp = AnkiPackageImporter(dst, tmp)
|
imp = AnkiPackageImporter(dst, col)
|
||||||
imp.run()
|
imp.run()
|
||||||
assert imp.dupes == 0
|
assert imp.dupes == 0
|
||||||
assert imp.added == 1
|
assert imp.added == 1
|
||||||
assert imp.updated == 0
|
assert imp.updated == 0
|
||||||
# importing again should be idempotent
|
# importing again should be idempotent
|
||||||
imp = AnkiPackageImporter(dst, tmp)
|
imp = AnkiPackageImporter(dst, col)
|
||||||
imp.run()
|
imp.run()
|
||||||
assert imp.dupes == 1
|
assert imp.dupes == 1
|
||||||
assert imp.added == 0
|
assert imp.added == 0
|
||||||
@ -136,8 +136,8 @@ def test_anki2_updates():
|
|||||||
# importing a newer note should update
|
# importing a newer note should update
|
||||||
assert dst.noteCount() == 1
|
assert dst.noteCount() == 1
|
||||||
assert dst.db.scalar("select flds from notes").startswith("hello")
|
assert dst.db.scalar("select flds from notes").startswith("hello")
|
||||||
tmp = getUpgradeDeckPath("update2.apkg")
|
col = getUpgradeDeckPath("update2.apkg")
|
||||||
imp = AnkiPackageImporter(dst, tmp)
|
imp = AnkiPackageImporter(dst, col)
|
||||||
imp.run()
|
imp.run()
|
||||||
assert imp.dupes == 0
|
assert imp.dupes == 0
|
||||||
assert imp.added == 0
|
assert imp.added == 0
|
||||||
|
Loading…
Reference in New Issue
Block a user