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():
d = getEmptyCol()
note = d.newNote()
col = getEmptyCol()
note = col.newNote()
note["Front"] = "1"
note["Back"] = "2"
d.addNote(note)
col.addNote(note)
c = note.cards()[0]
id = d.models.current()["id"]
id = col.models.current()["id"]
assert c.template()["ord"] == 0
def test_genrem():
d = getEmptyCol()
note = d.newNote()
col = getEmptyCol()
note = col.newNote()
note["Front"] = "1"
note["Back"] = ""
d.addNote(note)
col.addNote(note)
assert len(note.cards()) == 1
m = d.models.current()
mm = d.models
m = col.models.current()
mm = col.models
# adding a new template should automatically create cards
t = mm.newTemplate("rev")
t["qfmt"] = "{{Front}}"
@ -51,9 +51,10 @@ def test_genrem():
t = m["tmpls"][1]
t["qfmt"] = "{{Back}}"
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:
d.remove_cards_and_orphaned_notes(n.card_ids)
col.remove_cards_and_orphaned_notes(n.card_ids)
assert len(note.cards()) == 1
# if we add to the note, a card should be automatically generated
note.load()
@ -63,18 +64,18 @@ def test_genrem():
def test_gendeck():
d = getEmptyCol()
cloze = d.models.byName("Cloze")
d.models.setCurrent(cloze)
note = d.newNote()
col = getEmptyCol()
cloze = col.models.byName("Cloze")
col.models.setCurrent(cloze)
note = col.newNote()
note["Text"] = "{{c1::one}}"
d.addNote(note)
assert d.cardCount() == 1
col.addNote(note)
assert col.cardCount() == 1
assert note.cards()[0].did == 1
# set the model to a new default col
newId = d.decks.id("new")
newId = col.decks.id("new")
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
note["Text"] += "{{c2::two}}"
note.flush()

View File

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

View File

@ -61,85 +61,85 @@ def test_remove():
def test_rename():
d = getEmptyCol()
id = d.decks.id("hello::world")
col = getEmptyCol()
id = col.decks.id("hello::world")
# should be able to rename into a completely different branch, creating
# parents as necessary
d.decks.rename(d.decks.get(id), "foo::bar")
names = [n.name for n in d.decks.all_names_and_ids()]
col.decks.rename(col.decks.get(id), "foo::bar")
names = [n.name for n in col.decks.all_names_and_ids()]
assert "foo" in names
assert "foo::bar" in names
assert "hello::world" not in names
# create another col
id = d.decks.id("tmp")
id = col.decks.id("tmp")
# automatically adjusted if a duplicate name
d.decks.rename(d.decks.get(id), "FOO")
names = [n.name for n in d.decks.all_names_and_ids()]
col.decks.rename(col.decks.get(id), "FOO")
names = [n.name for n in col.decks.all_names_and_ids()]
assert "FOO+" in names
# when renaming, the children should be renamed too
d.decks.id("one::two::three")
id = d.decks.id("one")
d.decks.rename(d.decks.get(id), "yo")
names = [n.name for n in d.decks.all_names_and_ids()]
col.decks.id("one::two::three")
id = col.decks.id("one")
col.decks.rename(col.decks.get(id), "yo")
names = [n.name for n in col.decks.all_names_and_ids()]
for n in "yo", "yo::two", "yo::two::three":
assert n in names
# over filtered
filteredId = d.decks.newDyn("filtered")
filtered = d.decks.get(filteredId)
childId = d.decks.id("child")
child = d.decks.get(childId)
assertException(DeckRenameError, lambda: d.decks.rename(child, "filtered::child"))
assertException(DeckRenameError, lambda: d.decks.rename(child, "FILTERED::child"))
filteredId = col.decks.newDyn("filtered")
filtered = col.decks.get(filteredId)
childId = col.decks.id("child")
child = col.decks.get(childId)
assertException(DeckRenameError, lambda: col.decks.rename(child, "filtered::child"))
assertException(DeckRenameError, lambda: col.decks.rename(child, "FILTERED::child"))
def test_renameForDragAndDrop():
d = getEmptyCol()
col = getEmptyCol()
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")
chinese_did = d.decks.id("Chinese")
hsk_did = d.decks.id("Chinese::HSK")
languages_did = col.decks.id("Languages")
chinese_did = col.decks.id("Chinese")
hsk_did = col.decks.id("Chinese::HSK")
# 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"]
# 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"]
# 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"]
# 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"]
# 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"]
# 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"]
# 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"]
# 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"]
# decks are renamed if necessary
new_hsk_did = d.decks.id("hsk")
d.decks.renameForDragAndDrop(new_hsk_did, chinese_did)
new_hsk_did = col.decks.id("hsk")
col.decks.renameForDragAndDrop(new_hsk_did, chinese_did)
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
d.decks.renameForDragAndDrop(hsk_did, "")
col.decks.renameForDragAndDrop(hsk_did, "")
assert deckNames() == ["Chinese", "HSK", "Languages"]

View File

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

View File

@ -7,17 +7,17 @@ from tests.shared import getEmptyCol
def test_latex():
d = getEmptyCol()
col = getEmptyCol()
# change latex cmd to simulate broken build
import anki.latex
anki.latex.pngCommands[0][0] = "nolatex"
# add a note with latex
note = d.newNote()
note = col.newNote()
note["Front"] = "[latex]hello[/latex]"
d.addNote(note)
col.addNote(note)
# 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
msg = note.cards()[0].q()
assert "executing nolatex" in msg
@ -29,29 +29,29 @@ def test_latex():
# fix path
anki.latex.pngCommands[0][0] = "latex"
# check media db should cause latex to be generated
d.media.render_all_latex()
assert len(os.listdir(d.media.dir())) == 1
col.media.render_all_latex()
assert len(os.listdir(col.media.dir())) == 1
assert ".png" in note.cards()[0].q()
# adding new notes should cause generation on question display
note = d.newNote()
note = col.newNote()
note["Front"] = "[latex]world[/latex]"
d.addNote(note)
col.addNote(note)
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
note = d.newNote()
note = col.newNote()
note["Front"] = " [latex]world[/latex]"
d.addNote(note)
assert len(os.listdir(d.media.dir())) == 2
col.addNote(note)
assert len(os.listdir(col.media.dir())) == 2
oldcard = note.cards()[0]
assert ".png" in oldcard.q()
# if we turn off building, then previous cards should work, but cards with
# missing media will show a broken image
anki.latex.build = False
note = d.newNote()
note = col.newNote()
note["Front"] = "[latex]foo[/latex]"
d.addNote(note)
assert len(os.listdir(d.media.dir())) == 2
col.addNote(note)
assert len(os.listdir(col.media.dir())) == 2
assert ".png" in oldcard.q()
# turn it on again so other test don't suffer
anki.latex.build = True
@ -87,9 +87,9 @@ def test_latex():
def _test_includes_bad_command(bad):
d = getEmptyCol()
note = d.newNote()
col = getEmptyCol()
note = col.newNote()
note["Front"] = "[latex]%s[/latex]" % bad
d.addNote(note)
col.addNote(note)
q = note.cards()[0].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
def test_add():
d = getEmptyCol()
col = getEmptyCol()
dir = tempfile.mkdtemp(prefix="anki")
path = os.path.join(dir, "foo.jpg")
with open(path, "w") as note:
note.write("hello")
# 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
assert d.media.addFile(path) == "foo.jpg"
assert col.media.addFile(path) == "foo.jpg"
# but if it has a different sha1, it should
with open(path, "w") as note:
note.write("world")
assert d.media.addFile(path) == "foo-7c211433f02071597741e6ff5a8ea34789abbf43.jpg"
assert col.media.addFile(path) == "foo-7c211433f02071597741e6ff5a8ea34789abbf43.jpg"
def test_strings():
d = getEmptyCol()
mf = d.media.filesInStr
mid = d.models.current()["id"]
col = getEmptyCol()
mf = col.media.filesInStr
mid = col.models.current()["id"]
assert mf(mid, "aoeu") == []
assert mf(mid, "aoeu<img src='foo.jpg'>ao") == ["foo.jpg"]
assert mf(mid, "aoeu<img src='foo.jpg' style='test'>ao") == ["foo.jpg"]
@ -42,37 +42,37 @@ def test_strings():
"fo",
]
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[sound:foo.mp3]aoeu") == "aoeuaoeu"
assert sp("a<img src=yo>oeu") == "aoeu"
es = d.media.escapeImages
es = col.media.escapeImages
assert es("aoeu") == "aoeu"
assert es("<img src='http://foo.com'>") == "<img src='http://foo.com'>"
assert es('<img src="foo bar.jpg">') == '<img src="foo%20bar.jpg">'
def test_deckIntegration():
d = getEmptyCol()
col = getEmptyCol()
# create a media dir
d.media.dir()
col.media.dir()
# put a file into it
file = str(os.path.join(testDir, "support/fake.png"))
d.media.addFile(file)
col.media.addFile(file)
# add a note which references it
note = d.newNote()
note = col.newNote()
note["Front"] = "one"
note["Back"] = "<img src='fake.png'>"
d.addNote(note)
col.addNote(note)
# and one which references a non-existent file
note = d.newNote()
note = col.newNote()
note["Front"] = "one"
note["Back"] = "<img src='fake2.png'>"
d.addNote(note)
col.addNote(note)
# 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")
# check media
ret = d.media.check()
ret = col.media.check()
assert ret.missing == ["fake2.png"]
assert ret.unused == ["foo.jpg"]

View File

@ -33,79 +33,79 @@ def test_modelCopy():
def test_fields():
d = getEmptyCol()
note = d.newNote()
col = getEmptyCol()
note = col.newNote()
note["Front"] = "1"
note["Back"] = "2"
d.addNote(note)
m = d.models.current()
col.addNote(note)
m = col.models.current()
# 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"]
h = d.models.scmhash(m)
h = col.models.scmhash(m)
# add a field
note = d.models.newField("foo")
d.models.addField(m, note)
assert d.getNote(d.models.nids(m)[0]).fields == ["1", "2", ""]
assert d.models.scmhash(m) != h
note = col.models.newField("foo")
col.models.addField(m, note)
assert col.getNote(col.models.nids(m)[0]).fields == ["1", "2", ""]
assert col.models.scmhash(m) != h
# rename it
note = m["flds"][2]
d.models.renameField(m, note, "bar")
assert d.getNote(d.models.nids(m)[0])["bar"] == ""
col.models.renameField(m, note, "bar")
assert col.getNote(col.models.nids(m)[0])["bar"] == ""
# delete back
d.models.remField(m, m["flds"][1])
assert d.getNote(d.models.nids(m)[0]).fields == ["1", ""]
col.models.remField(m, m["flds"][1])
assert col.getNote(col.models.nids(m)[0]).fields == ["1", ""]
# move 0 -> 1
d.models.moveField(m, m["flds"][0], 1)
assert d.getNote(d.models.nids(m)[0]).fields == ["", "1"]
col.models.moveField(m, m["flds"][0], 1)
assert col.getNote(col.models.nids(m)[0]).fields == ["", "1"]
# move 1 -> 0
d.models.moveField(m, m["flds"][1], 0)
assert d.getNote(d.models.nids(m)[0]).fields == ["1", ""]
col.models.moveField(m, m["flds"][1], 0)
assert col.getNote(col.models.nids(m)[0]).fields == ["1", ""]
# add another and put in middle
note = d.models.newField("baz")
d.models.addField(m, note)
note = d.getNote(d.models.nids(m)[0])
note = col.models.newField("baz")
col.models.addField(m, note)
note = col.getNote(col.models.nids(m)[0])
note["baz"] = "2"
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
d.models.moveField(m, m["flds"][2], 1)
assert d.getNote(d.models.nids(m)[0]).fields == ["1", "2", ""]
col.models.moveField(m, m["flds"][2], 1)
assert col.getNote(col.models.nids(m)[0]).fields == ["1", "2", ""]
# move 0 -> 2
d.models.moveField(m, m["flds"][0], 2)
assert d.getNote(d.models.nids(m)[0]).fields == ["2", "", "1"]
col.models.moveField(m, m["flds"][0], 2)
assert col.getNote(col.models.nids(m)[0]).fields == ["2", "", "1"]
# move 0 -> 1
d.models.moveField(m, m["flds"][0], 1)
assert d.getNote(d.models.nids(m)[0]).fields == ["", "2", "1"]
col.models.moveField(m, m["flds"][0], 1)
assert col.getNote(col.models.nids(m)[0]).fields == ["", "2", "1"]
def test_templates():
d = getEmptyCol()
m = d.models.current()
mm = d.models
col = getEmptyCol()
m = col.models.current()
mm = col.models
t = mm.newTemplate("Reverse")
t["qfmt"] = "{{Back}}"
t["afmt"] = "{{Front}}"
mm.addTemplate(m, t)
mm.save(m)
note = d.newNote()
note = col.newNote()
note["Front"] = "1"
note["Back"] = "2"
d.addNote(note)
assert d.cardCount() == 2
col.addNote(note)
assert col.cardCount() == 2
(c, c2) = note.cards()
# first card should have first ord
assert c.ord == 0
assert c2.ord == 1
# switch templates
d.models.moveTemplate(m, c.template(), 1)
col.models.moveTemplate(m, c.template(), 1)
c.load()
c2.load()
assert c.ord == 1
assert c2.ord == 0
# removing a template should delete its cards
d.models.remTemplate(m, m["tmpls"][0])
assert d.cardCount() == 1
col.models.remTemplate(m, m["tmpls"][0])
assert col.cardCount() == 1
# and should have updated the other cards' ordinals
c = note.cards()[0]
assert c.ord == 0
@ -113,18 +113,20 @@ def test_templates():
# it shouldn't be possible to orphan notes by removing templates
t = mm.newTemplate("template name")
mm.addTemplate(m, t)
d.models.remTemplate(m, m["tmpls"][0])
col.models.remTemplate(m, m["tmpls"][0])
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
)
def test_cloze_ordinals():
d = getEmptyCol()
d.models.setCurrent(d.models.byName("Cloze"))
m = d.models.current()
mm = d.models
col = getEmptyCol()
col.models.setCurrent(col.models.byName("Cloze"))
m = col.models.current()
mm = col.models
# We replace the default Cloze template
t = mm.newTemplate("ChainedCloze")
@ -132,12 +134,12 @@ def test_cloze_ordinals():
t["afmt"] = "{{text:cloze:Text}}"
mm.addTemplate(m, t)
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}}"
d.addNote(note)
assert d.cardCount() == 2
col.addNote(note)
assert col.cardCount() == 2
(c, c2) = note.cards()
# first card should have first ord
assert c.ord == 0
@ -145,40 +147,40 @@ def test_cloze_ordinals():
def test_text():
d = getEmptyCol()
m = d.models.current()
col = getEmptyCol()
m = col.models.current()
m["tmpls"][0]["qfmt"] = "{{text:Front}}"
d.models.save(m)
note = d.newNote()
col.models.save(m)
note = col.newNote()
note["Front"] = "hello<b>world"
d.addNote(note)
col.addNote(note)
assert "helloworld" in note.cards()[0].q()
def test_cloze():
d = getEmptyCol()
d.models.setCurrent(d.models.byName("Cloze"))
note = d.newNote()
col = getEmptyCol()
col.models.setCurrent(col.models.byName("Cloze"))
note = col.newNote()
assert note.model()["name"] == "Cloze"
# a cloze model with no clozes is not empty
note["Text"] = "nothing"
assert d.addNote(note)
assert col.addNote(note)
# try with one cloze
note = d.newNote()
note = col.newNote()
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>world</span>" in note.cards()[0].a()
# and with a comment
note = d.newNote()
note = col.newNote()
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>world</span>" in note.cards()[0].a()
# and with 2 clozes
note = d.newNote()
note = col.newNote()
note["Text"] = "hello {{c1::world}} {{c2::bar}}"
assert d.addNote(note) == 2
assert col.addNote(note) == 2
(c1, c2) = note.cards()
assert "<span class=cloze>[...]</span> bar" in c1.q()
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()
# if there are multiple answers for a single cloze, they are given in a
# list
note = d.newNote()
note = col.newNote()
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 (
note.cards()[0].a()
)
# if we add another cloze, a card should be generated
cnt = d.cardCount()
cnt = col.cardCount()
note["Text"] = "{{c2::hello}} {{c1::foo}}"
note.flush()
assert d.cardCount() == cnt + 1
assert col.cardCount() == cnt + 1
# 0 or negative indices are not supported
note["Text"] += "{{c0::zero}} {{c-1:foo}}"
note.flush()
@ -204,13 +206,13 @@ def test_cloze():
def test_cloze_mathjax():
d = getEmptyCol()
d.models.setCurrent(d.models.byName("Cloze"))
note = d.newNote()
col = getEmptyCol()
col.models.setCurrent(col.models.byName("Cloze"))
note = col.newNote()
note[
"Text"
] = 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 "class=cloze" in note.cards()[0].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()[4].q()
note = d.newNote()
note = col.newNote()
note["Text"] = r"\(a\) {{c1::b}} \[ {{c1::c}} \]"
assert d.addNote(note)
assert col.addNote(note)
assert len(note.cards()) == 1
assert (
note.cards()[0]
@ -230,22 +232,22 @@ def test_cloze_mathjax():
def test_typecloze():
d = getEmptyCol()
m = d.models.byName("Cloze")
d.models.setCurrent(m)
col = getEmptyCol()
m = col.models.byName("Cloze")
col.models.setCurrent(m)
m["tmpls"][0]["qfmt"] = "{{cloze:Text}}{{type:cloze:Text}}"
d.models.save(m)
note = d.newNote()
col.models.save(m)
note = col.newNote()
note["Text"] = "hello {{c1::world}}"
d.addNote(note)
col.addNote(note)
assert "[[type:cloze:Text]]" in note.cards()[0].q()
def test_chained_mods():
d = getEmptyCol()
d.models.setCurrent(d.models.byName("Cloze"))
m = d.models.current()
mm = d.models
col = getEmptyCol()
col.models.setCurrent(col.models.byName("Cloze"))
m = col.models.current()
mm = col.models
# We replace the default Cloze template
t = mm.newTemplate("ChainedCloze")
@ -253,9 +255,9 @@ def test_chained_mods():
t["afmt"] = "{{cloze:text:Text}}"
mm.addTemplate(m, t)
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>'
a1 = "<b>sentence</b>"
q2 = '<span style="color:red">en chaine</span>'
@ -266,7 +268,7 @@ def test_chained_mods():
q2,
a2,
)
assert d.addNote(note) == 1
assert col.addNote(note) == 1
assert (
"This <span class=cloze>[sentence]</span> demonstrates <span class=cloze>[chained]</span> clozes."
in note.cards()[0].q()
@ -366,8 +368,8 @@ def test_req():
return
assert len(model["tmpls"]) == len(model["req"])
d = getEmptyCol()
mm = d.models
col = getEmptyCol()
mm = col.models
basic = mm.byName("Basic")
assert "req" in 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():
d = getEmptyCol()
note = d.newNote()
col = getEmptyCol()
note = col.newNote()
note["Front"] = "foo"
d.addNote(note)
col.addNote(note)
c = note.cards()[0]
# card stats
assert d.cardStats(c)
d.reset()
c = d.sched.getCard()
d.sched.answerCard(c, 3)
d.sched.answerCard(c, 2)
assert d.cardStats(c)
assert col.cardStats(c)
col.reset()
c = col.sched.getCard()
col.sched.answerCard(c, 3)
col.sched.answerCard(c, 2)
assert col.cardStats(c)
def test_graphs_empty():
d = getEmptyCol()
assert d.stats().report()
col = getEmptyCol()
assert col.stats().report()
def test_graphs():
dir = tempfile.gettempdir()
d = getEmptyCol()
g = d.stats()
col = getEmptyCol()
g = col.stats()
rep = g.report()
with open(os.path.join(dir, "test.html"), "w", encoding="UTF-8") as note:
note.write(rep)

View File

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

View File

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