d->col in tests

obtained by
```
sed -i "s/\bd\b/col/g" pylib/tests/*py qt/tests/*py
```
This commit is contained in:
Arthur Milchior 2020-07-17 05:21:12 +02:00
parent c376714a9b
commit 2c73dcb2e5
12 changed files with 1212 additions and 1207 deletions

View File

@ -21,25 +21,25 @@ def test_delete():
def test_misc(): def test_misc():
d = getEmptyCol() col = getEmptyCol()
note = d.newNote() note = col.newNote()
note["Front"] = "1" note["Front"] = "1"
note["Back"] = "2" note["Back"] = "2"
d.addNote(note) col.addNote(note)
c = note.cards()[0] c = note.cards()[0]
id = d.models.current()["id"] id = col.models.current()["id"]
assert c.template()["ord"] == 0 assert c.template()["ord"] == 0
def test_genrem(): def test_genrem():
d = getEmptyCol() col = getEmptyCol()
note = d.newNote() note = col.newNote()
note["Front"] = "1" note["Front"] = "1"
note["Back"] = "" note["Back"] = ""
d.addNote(note) col.addNote(note)
assert len(note.cards()) == 1 assert len(note.cards()) == 1
m = d.models.current() m = col.models.current()
mm = d.models mm = col.models
# adding a new template should automatically create cards # adding a new template should automatically create cards
t = mm.newTemplate("rev") t = mm.newTemplate("rev")
t["qfmt"] = "{{Front}}" t["qfmt"] = "{{Front}}"
@ -51,9 +51,10 @@ def test_genrem():
t = m["tmpls"][1] t = m["tmpls"][1]
t["qfmt"] = "{{Back}}" t["qfmt"] = "{{Back}}"
mm.save(m, templates=True) mm.save(m, templates=True)
rep = d.backend.get_empty_cards() rep = col.backend.get_empty_cards()
rep = col.backend.get_empty_cards()
for n in rep.notes: for n in rep.notes:
d.remove_cards_and_orphaned_notes(n.card_ids) col.remove_cards_and_orphaned_notes(n.card_ids)
assert len(note.cards()) == 1 assert len(note.cards()) == 1
# if we add to the note, a card should be automatically generated # if we add to the note, a card should be automatically generated
note.load() note.load()
@ -63,18 +64,18 @@ def test_genrem():
def test_gendeck(): def test_gendeck():
d = getEmptyCol() col = getEmptyCol()
cloze = d.models.byName("Cloze") cloze = col.models.byName("Cloze")
d.models.setCurrent(cloze) col.models.setCurrent(cloze)
note = d.newNote() note = col.newNote()
note["Text"] = "{{c1::one}}" note["Text"] = "{{c1::one}}"
d.addNote(note) col.addNote(note)
assert d.cardCount() == 1 assert col.cardCount() == 1
assert note.cards()[0].did == 1 assert note.cards()[0].did == 1
# set the model to a new default col # set the model to a new default col
newId = d.decks.id("new") newId = col.decks.id("new")
cloze["did"] = newId cloze["did"] = newId
d.models.save(cloze, updateReqs=False) col.models.save(cloze, updateReqs=False)
# a newly generated card should share the first card's col # a newly generated card should share the first card's col
note["Text"] += "{{c2::two}}" note["Text"] += "{{c2::two}}"
note.flush() note.flush()

View File

@ -148,15 +148,15 @@ def test_furigana():
def test_translate(): def test_translate():
d = getEmptyCol() col = getEmptyCol()
no_uni = without_unicode_isolation no_uni = without_unicode_isolation
assert ( assert (
d.tr(TR.CARD_TEMPLATE_RENDERING_FRONT_SIDE_PROBLEM) col.tr(TR.CARD_TEMPLATE_RENDERING_FRONT_SIDE_PROBLEM)
== "Front template has a problem:" == "Front template has a problem:"
) )
assert no_uni(d.tr(TR.STATISTICS_REVIEWS, reviews=1)) == "1 review" assert no_uni(col.tr(TR.STATISTICS_REVIEWS, reviews=1)) == "1 review"
assert no_uni(d.tr(TR.STATISTICS_REVIEWS, reviews=2)) == "2 reviews" assert no_uni(col.tr(TR.STATISTICS_REVIEWS, reviews=2)) == "2 reviews"
def test_db_named_args(capsys): def test_db_named_args(capsys):

View File

@ -61,85 +61,85 @@ def test_remove():
def test_rename(): def test_rename():
d = getEmptyCol() col = getEmptyCol()
id = d.decks.id("hello::world") id = col.decks.id("hello::world")
# should be able to rename into a completely different branch, creating # should be able to rename into a completely different branch, creating
# parents as necessary # parents as necessary
d.decks.rename(d.decks.get(id), "foo::bar") col.decks.rename(col.decks.get(id), "foo::bar")
names = [n.name for n in d.decks.all_names_and_ids()] names = [n.name for n in col.decks.all_names_and_ids()]
assert "foo" in names assert "foo" in names
assert "foo::bar" in names assert "foo::bar" in names
assert "hello::world" not in names assert "hello::world" not in names
# create another col # create another col
id = d.decks.id("tmp") id = col.decks.id("tmp")
# automatically adjusted if a duplicate name # automatically adjusted if a duplicate name
d.decks.rename(d.decks.get(id), "FOO") col.decks.rename(col.decks.get(id), "FOO")
names = [n.name for n in d.decks.all_names_and_ids()] names = [n.name for n in col.decks.all_names_and_ids()]
assert "FOO+" in names assert "FOO+" in names
# when renaming, the children should be renamed too # when renaming, the children should be renamed too
d.decks.id("one::two::three") col.decks.id("one::two::three")
id = d.decks.id("one") id = col.decks.id("one")
d.decks.rename(d.decks.get(id), "yo") col.decks.rename(col.decks.get(id), "yo")
names = [n.name for n in d.decks.all_names_and_ids()] names = [n.name for n in col.decks.all_names_and_ids()]
for n in "yo", "yo::two", "yo::two::three": for n in "yo", "yo::two", "yo::two::three":
assert n in names assert n in names
# over filtered # over filtered
filteredId = d.decks.newDyn("filtered") filteredId = col.decks.newDyn("filtered")
filtered = d.decks.get(filteredId) filtered = col.decks.get(filteredId)
childId = d.decks.id("child") childId = col.decks.id("child")
child = d.decks.get(childId) child = col.decks.get(childId)
assertException(DeckRenameError, lambda: d.decks.rename(child, "filtered::child")) assertException(DeckRenameError, lambda: col.decks.rename(child, "filtered::child"))
assertException(DeckRenameError, lambda: d.decks.rename(child, "FILTERED::child")) assertException(DeckRenameError, lambda: col.decks.rename(child, "FILTERED::child"))
def test_renameForDragAndDrop(): def test_renameForDragAndDrop():
d = getEmptyCol() col = getEmptyCol()
def deckNames(): def deckNames():
return [n.name for n in d.decks.all_names_and_ids(skip_empty_default=True)] return [n.name for n in col.decks.all_names_and_ids(skip_empty_default=True)]
languages_did = d.decks.id("Languages") languages_did = col.decks.id("Languages")
chinese_did = d.decks.id("Chinese") chinese_did = col.decks.id("Chinese")
hsk_did = d.decks.id("Chinese::HSK") hsk_did = col.decks.id("Chinese::HSK")
# Renaming also renames children # Renaming also renames children
d.decks.renameForDragAndDrop(chinese_did, languages_did) col.decks.renameForDragAndDrop(chinese_did, languages_did)
assert deckNames() == ["Languages", "Languages::Chinese", "Languages::Chinese::HSK"] assert deckNames() == ["Languages", "Languages::Chinese", "Languages::Chinese::HSK"]
# Dragging a col onto itself is a no-op # Dragging a col onto itself is a no-op
d.decks.renameForDragAndDrop(languages_did, languages_did) col.decks.renameForDragAndDrop(languages_did, languages_did)
assert deckNames() == ["Languages", "Languages::Chinese", "Languages::Chinese::HSK"] assert deckNames() == ["Languages", "Languages::Chinese", "Languages::Chinese::HSK"]
# Dragging a col onto its parent is a no-op # Dragging a col onto its parent is a no-op
d.decks.renameForDragAndDrop(hsk_did, chinese_did) col.decks.renameForDragAndDrop(hsk_did, chinese_did)
assert deckNames() == ["Languages", "Languages::Chinese", "Languages::Chinese::HSK"] assert deckNames() == ["Languages", "Languages::Chinese", "Languages::Chinese::HSK"]
# Dragging a col onto a descendant is a no-op # Dragging a col onto a descendant is a no-op
d.decks.renameForDragAndDrop(languages_did, hsk_did) col.decks.renameForDragAndDrop(languages_did, hsk_did)
assert deckNames() == ["Languages", "Languages::Chinese", "Languages::Chinese::HSK"] assert deckNames() == ["Languages", "Languages::Chinese", "Languages::Chinese::HSK"]
# Can drag a grandchild onto its grandparent. It becomes a child # Can drag a grandchild onto its grandparent. It becomes a child
d.decks.renameForDragAndDrop(hsk_did, languages_did) col.decks.renameForDragAndDrop(hsk_did, languages_did)
assert deckNames() == ["Languages", "Languages::Chinese", "Languages::HSK"] assert deckNames() == ["Languages", "Languages::Chinese", "Languages::HSK"]
# Can drag a col onto its sibling # Can drag a col onto its sibling
d.decks.renameForDragAndDrop(hsk_did, chinese_did) col.decks.renameForDragAndDrop(hsk_did, chinese_did)
assert deckNames() == ["Languages", "Languages::Chinese", "Languages::Chinese::HSK"] assert deckNames() == ["Languages", "Languages::Chinese", "Languages::Chinese::HSK"]
# Can drag a col back to the top level # Can drag a col back to the top level
d.decks.renameForDragAndDrop(chinese_did, None) col.decks.renameForDragAndDrop(chinese_did, None)
assert deckNames() == ["Chinese", "Chinese::HSK", "Languages"] assert deckNames() == ["Chinese", "Chinese::HSK", "Languages"]
# Dragging a top level col to the top level is a no-op # Dragging a top level col to the top level is a no-op
d.decks.renameForDragAndDrop(chinese_did, None) col.decks.renameForDragAndDrop(chinese_did, None)
assert deckNames() == ["Chinese", "Chinese::HSK", "Languages"] assert deckNames() == ["Chinese", "Chinese::HSK", "Languages"]
# decks are renamed if necessary # decks are renamed if necessary
new_hsk_did = d.decks.id("hsk") new_hsk_did = col.decks.id("hsk")
d.decks.renameForDragAndDrop(new_hsk_did, chinese_did) col.decks.renameForDragAndDrop(new_hsk_did, chinese_did)
assert deckNames() == ["Chinese", "Chinese::HSK", "Chinese::hsk+", "Languages"] assert deckNames() == ["Chinese", "Chinese::HSK", "Chinese::hsk+", "Languages"]
d.decks.rem(new_hsk_did) col.decks.rem(new_hsk_did)
# '' is a convenient alias for the top level DID # '' is a convenient alias for the top level DID
d.decks.renameForDragAndDrop(hsk_did, "") col.decks.renameForDragAndDrop(hsk_did, "")
assert deckNames() == ["Chinese", "HSK", "Languages"] assert deckNames() == ["Chinese", "HSK", "Languages"]

View File

@ -90,8 +90,8 @@ def test_findCards():
assert col.findCards("is:suspended") == [c.id] assert col.findCards("is:suspended") == [c.id]
# nids # nids
assert col.findCards("nid:54321") == [] assert col.findCards("nid:54321") == []
assert len(col.findCards("nid:%d" % note.id)) == 2 assert len(col.findCards(f"nid:{note.id}")) == 2
assert len(col.findCards("nid:%d,%d" % (f1id, f2id))) == 2 assert len(col.findCards(f"nid:{f1id},{f2id}")) == 2
# templates # templates
assert len(col.findCards("card:foo")) == 0 assert len(col.findCards("card:foo")) == 0
assert len(col.findCards('"card:card 1"')) == 4 assert len(col.findCards('"card:card 1"')) == 4

View File

@ -7,17 +7,17 @@ from tests.shared import getEmptyCol
def test_latex(): def test_latex():
d = getEmptyCol() col = getEmptyCol()
# change latex cmd to simulate broken build # change latex cmd to simulate broken build
import anki.latex import anki.latex
anki.latex.pngCommands[0][0] = "nolatex" anki.latex.pngCommands[0][0] = "nolatex"
# add a note with latex # add a note with latex
note = d.newNote() note = col.newNote()
note["Front"] = "[latex]hello[/latex]" note["Front"] = "[latex]hello[/latex]"
d.addNote(note) col.addNote(note)
# but since latex couldn't run, there's nothing there # but since latex couldn't run, there's nothing there
assert len(os.listdir(d.media.dir())) == 0 assert len(os.listdir(col.media.dir())) == 0
# check the error message # check the error message
msg = note.cards()[0].q() msg = note.cards()[0].q()
assert "executing nolatex" in msg assert "executing nolatex" in msg
@ -29,29 +29,29 @@ def test_latex():
# fix path # fix path
anki.latex.pngCommands[0][0] = "latex" anki.latex.pngCommands[0][0] = "latex"
# check media db should cause latex to be generated # check media db should cause latex to be generated
d.media.render_all_latex() col.media.render_all_latex()
assert len(os.listdir(d.media.dir())) == 1 assert len(os.listdir(col.media.dir())) == 1
assert ".png" in note.cards()[0].q() assert ".png" in note.cards()[0].q()
# adding new notes should cause generation on question display # adding new notes should cause generation on question display
note = d.newNote() note = col.newNote()
note["Front"] = "[latex]world[/latex]" note["Front"] = "[latex]world[/latex]"
d.addNote(note) col.addNote(note)
note.cards()[0].q() note.cards()[0].q()
assert len(os.listdir(d.media.dir())) == 2 assert len(os.listdir(col.media.dir())) == 2
# another note with the same media should reuse # another note with the same media should reuse
note = d.newNote() note = col.newNote()
note["Front"] = " [latex]world[/latex]" note["Front"] = " [latex]world[/latex]"
d.addNote(note) col.addNote(note)
assert len(os.listdir(d.media.dir())) == 2 assert len(os.listdir(col.media.dir())) == 2
oldcard = note.cards()[0] oldcard = note.cards()[0]
assert ".png" in oldcard.q() assert ".png" in oldcard.q()
# if we turn off building, then previous cards should work, but cards with # if we turn off building, then previous cards should work, but cards with
# missing media will show a broken image # missing media will show a broken image
anki.latex.build = False anki.latex.build = False
note = d.newNote() note = col.newNote()
note["Front"] = "[latex]foo[/latex]" note["Front"] = "[latex]foo[/latex]"
d.addNote(note) col.addNote(note)
assert len(os.listdir(d.media.dir())) == 2 assert len(os.listdir(col.media.dir())) == 2
assert ".png" in oldcard.q() assert ".png" in oldcard.q()
# turn it on again so other test don't suffer # turn it on again so other test don't suffer
anki.latex.build = True anki.latex.build = True
@ -87,9 +87,9 @@ def test_latex():
def _test_includes_bad_command(bad): def _test_includes_bad_command(bad):
d = getEmptyCol() col = getEmptyCol()
note = d.newNote() note = col.newNote()
note["Front"] = "[latex]%s[/latex]" % bad note["Front"] = "[latex]%s[/latex]" % bad
d.addNote(note) col.addNote(note)
q = note.cards()[0].q() q = note.cards()[0].q()
return ("'%s' is not allowed on cards" % bad in q, "Card content: %s" % q) return ("'%s' is not allowed on cards" % bad in q, "Card content: %s" % q)

View File

@ -8,25 +8,25 @@ from .shared import getEmptyCol, testDir
# copying files to media folder # copying files to media folder
def test_add(): def test_add():
d = getEmptyCol() col = getEmptyCol()
dir = tempfile.mkdtemp(prefix="anki") dir = tempfile.mkdtemp(prefix="anki")
path = os.path.join(dir, "foo.jpg") path = os.path.join(dir, "foo.jpg")
with open(path, "w") as note: with open(path, "w") as note:
note.write("hello") note.write("hello")
# new file, should preserve name # new file, should preserve name
assert d.media.addFile(path) == "foo.jpg" assert col.media.addFile(path) == "foo.jpg"
# adding the same file again should not create a duplicate # adding the same file again should not create a duplicate
assert d.media.addFile(path) == "foo.jpg" assert col.media.addFile(path) == "foo.jpg"
# but if it has a different sha1, it should # but if it has a different sha1, it should
with open(path, "w") as note: with open(path, "w") as note:
note.write("world") note.write("world")
assert d.media.addFile(path) == "foo-7c211433f02071597741e6ff5a8ea34789abbf43.jpg" assert col.media.addFile(path) == "foo-7c211433f02071597741e6ff5a8ea34789abbf43.jpg"
def test_strings(): def test_strings():
d = getEmptyCol() col = getEmptyCol()
mf = d.media.filesInStr mf = col.media.filesInStr
mid = d.models.current()["id"] mid = col.models.current()["id"]
assert mf(mid, "aoeu") == [] assert mf(mid, "aoeu") == []
assert mf(mid, "aoeu<img src='foo.jpg'>ao") == ["foo.jpg"] assert mf(mid, "aoeu<img src='foo.jpg'>ao") == ["foo.jpg"]
assert mf(mid, "aoeu<img src='foo.jpg' style='test'>ao") == ["foo.jpg"] assert mf(mid, "aoeu<img src='foo.jpg' style='test'>ao") == ["foo.jpg"]
@ -42,37 +42,37 @@ def test_strings():
"fo", "fo",
] ]
assert mf(mid, "aou[sound:foo.mp3]aou") == ["foo.mp3"] assert mf(mid, "aou[sound:foo.mp3]aou") == ["foo.mp3"]
sp = d.media.strip sp = col.media.strip
assert sp("aoeu") == "aoeu" assert sp("aoeu") == "aoeu"
assert sp("aoeu[sound:foo.mp3]aoeu") == "aoeuaoeu" assert sp("aoeu[sound:foo.mp3]aoeu") == "aoeuaoeu"
assert sp("a<img src=yo>oeu") == "aoeu" assert sp("a<img src=yo>oeu") == "aoeu"
es = d.media.escapeImages es = col.media.escapeImages
assert es("aoeu") == "aoeu" assert es("aoeu") == "aoeu"
assert es("<img src='http://foo.com'>") == "<img src='http://foo.com'>" assert es("<img src='http://foo.com'>") == "<img src='http://foo.com'>"
assert es('<img src="foo bar.jpg">') == '<img src="foo%20bar.jpg">' assert es('<img src="foo bar.jpg">') == '<img src="foo%20bar.jpg">'
def test_deckIntegration(): def test_deckIntegration():
d = getEmptyCol() col = getEmptyCol()
# create a media dir # create a media dir
d.media.dir() col.media.dir()
# put a file into it # put a file into it
file = str(os.path.join(testDir, "support/fake.png")) file = str(os.path.join(testDir, "support/fake.png"))
d.media.addFile(file) col.media.addFile(file)
# add a note which references it # add a note which references it
note = d.newNote() note = col.newNote()
note["Front"] = "one" note["Front"] = "one"
note["Back"] = "<img src='fake.png'>" note["Back"] = "<img src='fake.png'>"
d.addNote(note) col.addNote(note)
# and one which references a non-existent file # and one which references a non-existent file
note = d.newNote() note = col.newNote()
note["Front"] = "one" note["Front"] = "one"
note["Back"] = "<img src='fake2.png'>" note["Back"] = "<img src='fake2.png'>"
d.addNote(note) col.addNote(note)
# and add another file which isn't used # and add another file which isn't used
with open(os.path.join(d.media.dir(), "foo.jpg"), "w") as note: with open(os.path.join(col.media.dir(), "foo.jpg"), "w") as note:
note.write("test") note.write("test")
# check media # check media
ret = d.media.check() ret = col.media.check()
assert ret.missing == ["fake2.png"] assert ret.missing == ["fake2.png"]
assert ret.unused == ["foo.jpg"] assert ret.unused == ["foo.jpg"]

View File

@ -33,79 +33,79 @@ def test_modelCopy():
def test_fields(): def test_fields():
d = getEmptyCol() col = getEmptyCol()
note = d.newNote() note = col.newNote()
note["Front"] = "1" note["Front"] = "1"
note["Back"] = "2" note["Back"] = "2"
d.addNote(note) col.addNote(note)
m = d.models.current() m = col.models.current()
# make sure renaming a field updates the templates # make sure renaming a field updates the templates
d.models.renameField(m, m["flds"][0], "NewFront") col.models.renameField(m, m["flds"][0], "NewFront")
assert "{{NewFront}}" in m["tmpls"][0]["qfmt"] assert "{{NewFront}}" in m["tmpls"][0]["qfmt"]
h = d.models.scmhash(m) h = col.models.scmhash(m)
# add a field # add a field
note = d.models.newField("foo") note = col.models.newField("foo")
d.models.addField(m, note) col.models.addField(m, note)
assert d.getNote(d.models.nids(m)[0]).fields == ["1", "2", ""] assert col.getNote(col.models.nids(m)[0]).fields == ["1", "2", ""]
assert d.models.scmhash(m) != h assert col.models.scmhash(m) != h
# rename it # rename it
note = m["flds"][2] note = m["flds"][2]
d.models.renameField(m, note, "bar") col.models.renameField(m, note, "bar")
assert d.getNote(d.models.nids(m)[0])["bar"] == "" assert col.getNote(col.models.nids(m)[0])["bar"] == ""
# delete back # delete back
d.models.remField(m, m["flds"][1]) col.models.remField(m, m["flds"][1])
assert d.getNote(d.models.nids(m)[0]).fields == ["1", ""] assert col.getNote(col.models.nids(m)[0]).fields == ["1", ""]
# move 0 -> 1 # move 0 -> 1
d.models.moveField(m, m["flds"][0], 1) col.models.moveField(m, m["flds"][0], 1)
assert d.getNote(d.models.nids(m)[0]).fields == ["", "1"] assert col.getNote(col.models.nids(m)[0]).fields == ["", "1"]
# move 1 -> 0 # move 1 -> 0
d.models.moveField(m, m["flds"][1], 0) col.models.moveField(m, m["flds"][1], 0)
assert d.getNote(d.models.nids(m)[0]).fields == ["1", ""] assert col.getNote(col.models.nids(m)[0]).fields == ["1", ""]
# add another and put in middle # add another and put in middle
note = d.models.newField("baz") note = col.models.newField("baz")
d.models.addField(m, note) col.models.addField(m, note)
note = d.getNote(d.models.nids(m)[0]) note = col.getNote(col.models.nids(m)[0])
note["baz"] = "2" note["baz"] = "2"
note.flush() note.flush()
assert d.getNote(d.models.nids(m)[0]).fields == ["1", "", "2"] assert col.getNote(col.models.nids(m)[0]).fields == ["1", "", "2"]
# move 2 -> 1 # move 2 -> 1
d.models.moveField(m, m["flds"][2], 1) col.models.moveField(m, m["flds"][2], 1)
assert d.getNote(d.models.nids(m)[0]).fields == ["1", "2", ""] assert col.getNote(col.models.nids(m)[0]).fields == ["1", "2", ""]
# move 0 -> 2 # move 0 -> 2
d.models.moveField(m, m["flds"][0], 2) col.models.moveField(m, m["flds"][0], 2)
assert d.getNote(d.models.nids(m)[0]).fields == ["2", "", "1"] assert col.getNote(col.models.nids(m)[0]).fields == ["2", "", "1"]
# move 0 -> 1 # move 0 -> 1
d.models.moveField(m, m["flds"][0], 1) col.models.moveField(m, m["flds"][0], 1)
assert d.getNote(d.models.nids(m)[0]).fields == ["", "2", "1"] assert col.getNote(col.models.nids(m)[0]).fields == ["", "2", "1"]
def test_templates(): def test_templates():
d = getEmptyCol() col = getEmptyCol()
m = d.models.current() m = col.models.current()
mm = d.models mm = col.models
t = mm.newTemplate("Reverse") t = mm.newTemplate("Reverse")
t["qfmt"] = "{{Back}}" t["qfmt"] = "{{Back}}"
t["afmt"] = "{{Front}}" t["afmt"] = "{{Front}}"
mm.addTemplate(m, t) mm.addTemplate(m, t)
mm.save(m) mm.save(m)
note = d.newNote() note = col.newNote()
note["Front"] = "1" note["Front"] = "1"
note["Back"] = "2" note["Back"] = "2"
d.addNote(note) col.addNote(note)
assert d.cardCount() == 2 assert col.cardCount() == 2
(c, c2) = note.cards() (c, c2) = note.cards()
# first card should have first ord # first card should have first ord
assert c.ord == 0 assert c.ord == 0
assert c2.ord == 1 assert c2.ord == 1
# switch templates # switch templates
d.models.moveTemplate(m, c.template(), 1) col.models.moveTemplate(m, c.template(), 1)
c.load() c.load()
c2.load() c2.load()
assert c.ord == 1 assert c.ord == 1
assert c2.ord == 0 assert c2.ord == 0
# removing a template should delete its cards # removing a template should delete its cards
d.models.remTemplate(m, m["tmpls"][0]) col.models.remTemplate(m, m["tmpls"][0])
assert d.cardCount() == 1 assert col.cardCount() == 1
# and should have updated the other cards' ordinals # and should have updated the other cards' ordinals
c = note.cards()[0] c = note.cards()[0]
assert c.ord == 0 assert c.ord == 0
@ -113,18 +113,20 @@ def test_templates():
# it shouldn't be possible to orphan notes by removing templates # it shouldn't be possible to orphan notes by removing templates
t = mm.newTemplate("template name") t = mm.newTemplate("template name")
mm.addTemplate(m, t) mm.addTemplate(m, t)
d.models.remTemplate(m, m["tmpls"][0]) col.models.remTemplate(m, m["tmpls"][0])
assert ( assert (
d.db.scalar("select count() from cards where nid not in (select id from notes)") col.db.scalar(
"select count() from cards where nid not in (select id from notes)"
)
== 0 == 0
) )
def test_cloze_ordinals(): def test_cloze_ordinals():
d = getEmptyCol() col = getEmptyCol()
d.models.setCurrent(d.models.byName("Cloze")) col.models.setCurrent(col.models.byName("Cloze"))
m = d.models.current() m = col.models.current()
mm = d.models mm = col.models
# We replace the default Cloze template # We replace the default Cloze template
t = mm.newTemplate("ChainedCloze") t = mm.newTemplate("ChainedCloze")
@ -132,12 +134,12 @@ def test_cloze_ordinals():
t["afmt"] = "{{text:cloze:Text}}" t["afmt"] = "{{text:cloze:Text}}"
mm.addTemplate(m, t) mm.addTemplate(m, t)
mm.save(m) mm.save(m)
d.models.remTemplate(m, m["tmpls"][0]) col.models.remTemplate(m, m["tmpls"][0])
note = d.newNote() note = col.newNote()
note["Text"] = "{{c1::firstQ::firstA}}{{c2::secondQ::secondA}}" note["Text"] = "{{c1::firstQ::firstA}}{{c2::secondQ::secondA}}"
d.addNote(note) col.addNote(note)
assert d.cardCount() == 2 assert col.cardCount() == 2
(c, c2) = note.cards() (c, c2) = note.cards()
# first card should have first ord # first card should have first ord
assert c.ord == 0 assert c.ord == 0
@ -145,40 +147,40 @@ def test_cloze_ordinals():
def test_text(): def test_text():
d = getEmptyCol() col = getEmptyCol()
m = d.models.current() m = col.models.current()
m["tmpls"][0]["qfmt"] = "{{text:Front}}" m["tmpls"][0]["qfmt"] = "{{text:Front}}"
d.models.save(m) col.models.save(m)
note = d.newNote() note = col.newNote()
note["Front"] = "hello<b>world" note["Front"] = "hello<b>world"
d.addNote(note) col.addNote(note)
assert "helloworld" in note.cards()[0].q() assert "helloworld" in note.cards()[0].q()
def test_cloze(): def test_cloze():
d = getEmptyCol() col = getEmptyCol()
d.models.setCurrent(d.models.byName("Cloze")) col.models.setCurrent(col.models.byName("Cloze"))
note = d.newNote() note = col.newNote()
assert note.model()["name"] == "Cloze" assert note.model()["name"] == "Cloze"
# a cloze model with no clozes is not empty # a cloze model with no clozes is not empty
note["Text"] = "nothing" note["Text"] = "nothing"
assert d.addNote(note) assert col.addNote(note)
# try with one cloze # try with one cloze
note = d.newNote() note = col.newNote()
note["Text"] = "hello {{c1::world}}" note["Text"] = "hello {{c1::world}}"
assert d.addNote(note) == 1 assert col.addNote(note) == 1
assert "hello <span class=cloze>[...]</span>" in note.cards()[0].q() assert "hello <span class=cloze>[...]</span>" in note.cards()[0].q()
assert "hello <span class=cloze>world</span>" in note.cards()[0].a() assert "hello <span class=cloze>world</span>" in note.cards()[0].a()
# and with a comment # and with a comment
note = d.newNote() note = col.newNote()
note["Text"] = "hello {{c1::world::typical}}" note["Text"] = "hello {{c1::world::typical}}"
assert d.addNote(note) == 1 assert col.addNote(note) == 1
assert "<span class=cloze>[typical]</span>" in note.cards()[0].q() assert "<span class=cloze>[typical]</span>" in note.cards()[0].q()
assert "<span class=cloze>world</span>" in note.cards()[0].a() assert "<span class=cloze>world</span>" in note.cards()[0].a()
# and with 2 clozes # and with 2 clozes
note = d.newNote() note = col.newNote()
note["Text"] = "hello {{c1::world}} {{c2::bar}}" note["Text"] = "hello {{c1::world}} {{c2::bar}}"
assert d.addNote(note) == 2 assert col.addNote(note) == 2
(c1, c2) = note.cards() (c1, c2) = note.cards()
assert "<span class=cloze>[...]</span> bar" in c1.q() assert "<span class=cloze>[...]</span> bar" in c1.q()
assert "<span class=cloze>world</span> bar" in c1.a() assert "<span class=cloze>world</span> bar" in c1.a()
@ -186,17 +188,17 @@ def test_cloze():
assert "world <span class=cloze>bar</span>" in c2.a() assert "world <span class=cloze>bar</span>" in c2.a()
# if there are multiple answers for a single cloze, they are given in a # if there are multiple answers for a single cloze, they are given in a
# list # list
note = d.newNote() note = col.newNote()
note["Text"] = "a {{c1::b}} {{c1::c}}" note["Text"] = "a {{c1::b}} {{c1::c}}"
assert d.addNote(note) == 1 assert col.addNote(note) == 1
assert "<span class=cloze>b</span> <span class=cloze>c</span>" in ( assert "<span class=cloze>b</span> <span class=cloze>c</span>" in (
note.cards()[0].a() note.cards()[0].a()
) )
# if we add another cloze, a card should be generated # if we add another cloze, a card should be generated
cnt = d.cardCount() cnt = col.cardCount()
note["Text"] = "{{c2::hello}} {{c1::foo}}" note["Text"] = "{{c2::hello}} {{c1::foo}}"
note.flush() note.flush()
assert d.cardCount() == cnt + 1 assert col.cardCount() == cnt + 1
# 0 or negative indices are not supported # 0 or negative indices are not supported
note["Text"] += "{{c0::zero}} {{c-1:foo}}" note["Text"] += "{{c0::zero}} {{c-1:foo}}"
note.flush() note.flush()
@ -204,13 +206,13 @@ def test_cloze():
def test_cloze_mathjax(): def test_cloze_mathjax():
d = getEmptyCol() col = getEmptyCol()
d.models.setCurrent(d.models.byName("Cloze")) col.models.setCurrent(col.models.byName("Cloze"))
note = d.newNote() note = col.newNote()
note[ note[
"Text" "Text"
] = r"{{c1::ok}} \(2^2\) {{c2::not ok}} \(2^{{c3::2}}\) \(x^3\) {{c4::blah}} {{c5::text with \(x^2\) jax}}" ] = r"{{c1::ok}} \(2^2\) {{c2::not ok}} \(2^{{c3::2}}\) \(x^3\) {{c4::blah}} {{c5::text with \(x^2\) jax}}"
assert d.addNote(note) assert col.addNote(note)
assert len(note.cards()) == 5 assert len(note.cards()) == 5
assert "class=cloze" in note.cards()[0].q() assert "class=cloze" in note.cards()[0].q()
assert "class=cloze" in note.cards()[1].q() assert "class=cloze" in note.cards()[1].q()
@ -218,9 +220,9 @@ def test_cloze_mathjax():
assert "class=cloze" in note.cards()[3].q() assert "class=cloze" in note.cards()[3].q()
assert "class=cloze" in note.cards()[4].q() assert "class=cloze" in note.cards()[4].q()
note = d.newNote() note = col.newNote()
note["Text"] = r"\(a\) {{c1::b}} \[ {{c1::c}} \]" note["Text"] = r"\(a\) {{c1::b}} \[ {{c1::c}} \]"
assert d.addNote(note) assert col.addNote(note)
assert len(note.cards()) == 1 assert len(note.cards()) == 1
assert ( assert (
note.cards()[0] note.cards()[0]
@ -230,22 +232,22 @@ def test_cloze_mathjax():
def test_typecloze(): def test_typecloze():
d = getEmptyCol() col = getEmptyCol()
m = d.models.byName("Cloze") m = col.models.byName("Cloze")
d.models.setCurrent(m) col.models.setCurrent(m)
m["tmpls"][0]["qfmt"] = "{{cloze:Text}}{{type:cloze:Text}}" m["tmpls"][0]["qfmt"] = "{{cloze:Text}}{{type:cloze:Text}}"
d.models.save(m) col.models.save(m)
note = d.newNote() note = col.newNote()
note["Text"] = "hello {{c1::world}}" note["Text"] = "hello {{c1::world}}"
d.addNote(note) col.addNote(note)
assert "[[type:cloze:Text]]" in note.cards()[0].q() assert "[[type:cloze:Text]]" in note.cards()[0].q()
def test_chained_mods(): def test_chained_mods():
d = getEmptyCol() col = getEmptyCol()
d.models.setCurrent(d.models.byName("Cloze")) col.models.setCurrent(col.models.byName("Cloze"))
m = d.models.current() m = col.models.current()
mm = d.models mm = col.models
# We replace the default Cloze template # We replace the default Cloze template
t = mm.newTemplate("ChainedCloze") t = mm.newTemplate("ChainedCloze")
@ -253,9 +255,9 @@ def test_chained_mods():
t["afmt"] = "{{cloze:text:Text}}" t["afmt"] = "{{cloze:text:Text}}"
mm.addTemplate(m, t) mm.addTemplate(m, t)
mm.save(m) mm.save(m)
d.models.remTemplate(m, m["tmpls"][0]) col.models.remTemplate(m, m["tmpls"][0])
note = d.newNote() note = col.newNote()
q1 = '<span style="color:red">phrase</span>' q1 = '<span style="color:red">phrase</span>'
a1 = "<b>sentence</b>" a1 = "<b>sentence</b>"
q2 = '<span style="color:red">en chaine</span>' q2 = '<span style="color:red">en chaine</span>'
@ -266,7 +268,7 @@ def test_chained_mods():
q2, q2,
a2, a2,
) )
assert d.addNote(note) == 1 assert col.addNote(note) == 1
assert ( assert (
"This <span class=cloze>[sentence]</span> demonstrates <span class=cloze>[chained]</span> clozes." "This <span class=cloze>[sentence]</span> demonstrates <span class=cloze>[chained]</span> clozes."
in note.cards()[0].q() in note.cards()[0].q()
@ -366,8 +368,8 @@ def test_req():
return return
assert len(model["tmpls"]) == len(model["req"]) assert len(model["tmpls"]) == len(model["req"])
d = getEmptyCol() col = getEmptyCol()
mm = d.models mm = col.models
basic = mm.byName("Basic") basic = mm.byName("Basic")
assert "req" in basic assert "req" in basic
reqSize(basic) reqSize(basic)

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -8,29 +8,29 @@ from tests.shared import getEmptyCol
def test_stats(): def test_stats():
d = getEmptyCol() col = getEmptyCol()
note = d.newNote() note = col.newNote()
note["Front"] = "foo" note["Front"] = "foo"
d.addNote(note) col.addNote(note)
c = note.cards()[0] c = note.cards()[0]
# card stats # card stats
assert d.cardStats(c) assert col.cardStats(c)
d.reset() col.reset()
c = d.sched.getCard() c = col.sched.getCard()
d.sched.answerCard(c, 3) col.sched.answerCard(c, 3)
d.sched.answerCard(c, 2) col.sched.answerCard(c, 2)
assert d.cardStats(c) assert col.cardStats(c)
def test_graphs_empty(): def test_graphs_empty():
d = getEmptyCol() col = getEmptyCol()
assert d.stats().report() assert col.stats().report()
def test_graphs(): def test_graphs():
dir = tempfile.gettempdir() dir = tempfile.gettempdir()
d = getEmptyCol() col = getEmptyCol()
g = d.stats() g = col.stats()
rep = g.report() rep = g.report()
with open(os.path.join(dir, "test.html"), "w", encoding="UTF-8") as note: with open(os.path.join(dir, "test.html"), "w", encoding="UTF-8") as note:
note.write(rep) note.write(rep)

View File

@ -2,14 +2,14 @@ from tests.shared import getEmptyCol
def test_deferred_frontside(): def test_deferred_frontside():
d = getEmptyCol() col = getEmptyCol()
m = d.models.current() m = col.models.current()
m["tmpls"][0]["qfmt"] = "{{custom:Front}}" m["tmpls"][0]["qfmt"] = "{{custom:Front}}"
d.models.save(m) col.models.save(m)
note = d.newNote() note = col.newNote()
note["Front"] = "xxtest" note["Front"] = "xxtest"
note["Back"] = "" note["Back"] = ""
d.addNote(note) col.addNote(note)
assert "xxtest" in note.cards()[0].a() assert "xxtest" in note.cards()[0].a()

View File

@ -13,84 +13,84 @@ def getEmptyCol():
def test_op(): def test_op():
d = getEmptyCol() col = getEmptyCol()
# should have no undo by default # should have no undo by default
assert not d.undoName() assert not col.undoName()
# let's adjust a study option # let's adjust a study option
d.save("studyopts") col.save("studyopts")
d.conf["abc"] = 5 col.conf["abc"] = 5
# it should be listed as undoable # it should be listed as undoable
assert d.undoName() == "studyopts" assert col.undoName() == "studyopts"
# with about 5 minutes until it's clobbered # with about 5 minutes until it's clobbered
assert time.time() - d._lastSave < 1 assert time.time() - col._lastSave < 1
# undoing should restore the old value # undoing should restore the old value
d.undo() col.undo()
assert not d.undoName() assert not col.undoName()
assert "abc" not in d.conf assert "abc" not in col.conf
# an (auto)save will clear the undo # an (auto)save will clear the undo
d.save("foo") col.save("foo")
assert d.undoName() == "foo" assert col.undoName() == "foo"
d.save() col.save()
assert not d.undoName() assert not col.undoName()
# and a review will, too # and a review will, too
d.save("add") col.save("add")
note = d.newNote() note = col.newNote()
note["Front"] = "one" note["Front"] = "one"
d.addNote(note) col.addNote(note)
d.reset() col.reset()
assert d.undoName() == "add" assert col.undoName() == "add"
c = d.sched.getCard() c = col.sched.getCard()
d.sched.answerCard(c, 2) col.sched.answerCard(c, 2)
assert d.undoName() == "Review" assert col.undoName() == "Review"
def test_review(): def test_review():
d = getEmptyCol() col = getEmptyCol()
d.conf["counts"] = COUNT_REMAINING col.conf["counts"] = COUNT_REMAINING
note = d.newNote() note = col.newNote()
note["Front"] = "one" note["Front"] = "one"
d.addNote(note) col.addNote(note)
d.reset() col.reset()
assert not d.undoName() assert not col.undoName()
# answer # answer
assert d.sched.counts() == (1, 0, 0) assert col.sched.counts() == (1, 0, 0)
c = d.sched.getCard() c = col.sched.getCard()
assert c.queue == QUEUE_TYPE_NEW assert c.queue == QUEUE_TYPE_NEW
d.sched.answerCard(c, 3) col.sched.answerCard(c, 3)
assert c.left == 1001 assert c.left == 1001
assert d.sched.counts() == (0, 1, 0) assert col.sched.counts() == (0, 1, 0)
assert c.queue == QUEUE_TYPE_LRN assert c.queue == QUEUE_TYPE_LRN
# undo # undo
assert d.undoName() assert col.undoName()
d.undo() col.undo()
d.reset() col.reset()
assert d.sched.counts() == (1, 0, 0) assert col.sched.counts() == (1, 0, 0)
c.load() c.load()
assert c.queue == QUEUE_TYPE_NEW assert c.queue == QUEUE_TYPE_NEW
assert c.left != 1001 assert c.left != 1001
assert not d.undoName() assert not col.undoName()
# we should be able to undo multiple answers too # we should be able to undo multiple answers too
note = d.newNote() note = col.newNote()
note["Front"] = "two" note["Front"] = "two"
d.addNote(note) col.addNote(note)
d.reset() col.reset()
assert d.sched.counts() == (2, 0, 0) assert col.sched.counts() == (2, 0, 0)
c = d.sched.getCard() c = col.sched.getCard()
d.sched.answerCard(c, 3) col.sched.answerCard(c, 3)
c = d.sched.getCard() c = col.sched.getCard()
d.sched.answerCard(c, 3) col.sched.answerCard(c, 3)
assert d.sched.counts() == (0, 2, 0) assert col.sched.counts() == (0, 2, 0)
d.undo() col.undo()
d.reset() col.reset()
assert d.sched.counts() == (1, 1, 0) assert col.sched.counts() == (1, 1, 0)
d.undo() col.undo()
d.reset() col.reset()
assert d.sched.counts() == (2, 0, 0) assert col.sched.counts() == (2, 0, 0)
# performing a normal op will clear the review queue # performing a normal op will clear the review queue
c = d.sched.getCard() c = col.sched.getCard()
d.sched.answerCard(c, 3) col.sched.answerCard(c, 3)
assert d.undoName() == "Review" assert col.undoName() == "Review"
d.save("foo") col.save("foo")
assert d.undoName() == "foo" assert col.undoName() == "foo"
d.undo() col.undo()
assert not d.undoName() assert not col.undoName()