From c376714a9bd558474a91f68ce0dc9cfaa9334d71 Mon Sep 17 00:00:00 2001 From: Arthur Milchior Date: Fri, 17 Jul 2020 05:21:01 +0200 Subject: [PATCH] deck->col in tests Obtained by ``` sed -i "s/\bdeck\b/col/g" pylib/tests/*py qt/tests/*py ``` --- pylib/tests/test_cards.py | 28 +-- pylib/tests/test_collection.py | 76 ++++---- pylib/tests/test_decks.py | 88 ++++----- pylib/tests/test_exporting.py | 72 +++---- pylib/tests/test_find.py | 334 ++++++++++++++++----------------- pylib/tests/test_importing.py | 82 ++++---- pylib/tests/test_models.py | 56 +++--- 7 files changed, 368 insertions(+), 368 deletions(-) diff --git a/pylib/tests/test_cards.py b/pylib/tests/test_cards.py index 74123c164..7c94aa0eb 100644 --- a/pylib/tests/test_cards.py +++ b/pylib/tests/test_cards.py @@ -4,20 +4,20 @@ from tests.shared import getEmptyCol def test_delete(): - deck = getEmptyCol() - note = deck.newNote() + col = getEmptyCol() + note = col.newNote() note["Front"] = "1" note["Back"] = "2" - deck.addNote(note) + col.addNote(note) cid = note.cards()[0].id - deck.reset() - deck.sched.answerCard(deck.sched.getCard(), 2) - deck.remove_cards_and_orphaned_notes([cid]) - assert deck.cardCount() == 0 - assert deck.noteCount() == 0 - assert deck.db.scalar("select count() from notes") == 0 - assert deck.db.scalar("select count() from cards") == 0 - assert deck.db.scalar("select count() from graves") == 2 + col.reset() + col.sched.answerCard(col.sched.getCard(), 2) + col.remove_cards_and_orphaned_notes([cid]) + assert col.cardCount() == 0 + assert col.noteCount() == 0 + assert col.db.scalar("select count() from notes") == 0 + assert col.db.scalar("select count() from cards") == 0 + assert col.db.scalar("select count() from graves") == 2 def test_misc(): @@ -71,11 +71,11 @@ def test_gendeck(): d.addNote(note) assert d.cardCount() == 1 assert note.cards()[0].did == 1 - # set the model to a new default deck + # set the model to a new default col newId = d.decks.id("new") cloze["did"] = newId d.models.save(cloze, updateReqs=False) - # a newly generated card should share the first card's deck + # a newly generated card should share the first card's col note["Text"] += "{{c2::two}}" note.flush() assert note.cards()[1].did == 1 @@ -83,7 +83,7 @@ def test_gendeck(): note["Text"] += "{{c3::three}}" note.flush() assert note.cards()[2].did == 1 - # if one of the cards is in a different deck, it should revert to the + # if one of the cards is in a different col, it should revert to the # model default c = note.cards()[1] c.did = newId diff --git a/pylib/tests/test_collection.py b/pylib/tests/test_collection.py index 141bdb5ee..00f199788 100644 --- a/pylib/tests/test_collection.py +++ b/pylib/tests/test_collection.py @@ -19,17 +19,17 @@ def test_create_open(): os.unlink(path) except OSError: pass - deck = aopen(path) + col = aopen(path) # for open() - newPath = deck.path - newMod = deck.mod - deck.close() - del deck + newPath = col.path + newMod = col.mod + col.close() + del col # reopen - deck = aopen(newPath) - assert deck.mod == newMod - deck.close() + col = aopen(newPath) + assert col.mod == newMod + col.close() # non-writeable dir if isWin: @@ -45,36 +45,36 @@ def test_create_open(): def test_noteAddDelete(): - deck = getEmptyCol() + col = getEmptyCol() # add a note - note = deck.newNote() + note = col.newNote() note["Front"] = "one" note["Back"] = "two" - n = deck.addNote(note) + n = col.addNote(note) assert n == 1 # test multiple cards - add another template - m = deck.models.current() - mm = deck.models + m = col.models.current() + mm = col.models t = mm.newTemplate("Reverse") t["qfmt"] = "{{Back}}" t["afmt"] = "{{Front}}" mm.addTemplate(m, t) mm.save(m) - assert deck.cardCount() == 2 + assert col.cardCount() == 2 # creating new notes should use both cards - note = deck.newNote() + note = col.newNote() note["Front"] = "three" note["Back"] = "four" - n = deck.addNote(note) + n = col.addNote(note) assert n == 2 - assert deck.cardCount() == 4 + assert col.cardCount() == 4 # check q/a generation c0 = note.cards()[0] assert "three" in c0.q() # it should not be a duplicate assert not note.dupeOrEmpty() # now let's make a duplicate - note2 = deck.newNote() + note2 = col.newNote() note2["Front"] = "one" note2["Back"] = "" assert note2.dupeOrEmpty() @@ -84,57 +84,57 @@ def test_noteAddDelete(): def test_fieldChecksum(): - deck = getEmptyCol() - note = deck.newNote() + col = getEmptyCol() + note = col.newNote() note["Front"] = "new" note["Back"] = "new2" - deck.addNote(note) - assert deck.db.scalar("select csum from notes") == int("c2a6b03f", 16) + col.addNote(note) + assert col.db.scalar("select csum from notes") == int("c2a6b03f", 16) # changing the val should change the checksum note["Front"] = "newx" note.flush() - assert deck.db.scalar("select csum from notes") == int("302811ae", 16) + assert col.db.scalar("select csum from notes") == int("302811ae", 16) def test_addDelTags(): - deck = getEmptyCol() - note = deck.newNote() + col = getEmptyCol() + note = col.newNote() note["Front"] = "1" - deck.addNote(note) - note2 = deck.newNote() + col.addNote(note) + note2 = col.newNote() note2["Front"] = "2" - deck.addNote(note2) + col.addNote(note2) # adding for a given id - deck.tags.bulkAdd([note.id], "foo") + col.tags.bulkAdd([note.id], "foo") note.load() note2.load() assert "foo" in note.tags assert "foo" not in note2.tags # should be canonified - deck.tags.bulkAdd([note.id], "foo aaa") + col.tags.bulkAdd([note.id], "foo aaa") note.load() assert note.tags[0] == "aaa" assert len(note.tags) == 2 def test_timestamps(): - deck = getEmptyCol() - assert len(deck.models.all_names_and_ids()) == len(get_stock_notetypes(deck)) + col = getEmptyCol() + assert len(col.models.all_names_and_ids()) == len(get_stock_notetypes(col)) for i in range(100): - addBasicModel(deck) - assert len(deck.models.all_names_and_ids()) == 100 + len(get_stock_notetypes(deck)) + addBasicModel(col) + assert len(col.models.all_names_and_ids()) == 100 + len(get_stock_notetypes(col)) def test_furigana(): - deck = getEmptyCol() - mm = deck.models + col = getEmptyCol() + mm = col.models m = mm.current() # filter should work m["tmpls"][0]["qfmt"] = "{{kana:Front}}" mm.save(m) - n = deck.newNote() + n = col.newNote() n["Front"] = "foo[abc]" - deck.addNote(n) + col.addNote(n) c = n.cards()[0] assert c.q().endswith("abc") # and should avoid sound diff --git a/pylib/tests/test_decks.py b/pylib/tests/test_decks.py index a441b9d57..ab2d0b677 100644 --- a/pylib/tests/test_decks.py +++ b/pylib/tests/test_decks.py @@ -5,59 +5,59 @@ from tests.shared import assertException, getEmptyCol def test_basic(): - deck = getEmptyCol() - # we start with a standard deck - assert len(deck.decks.all_names_and_ids()) == 1 + col = getEmptyCol() + # we start with a standard col + assert len(col.decks.all_names_and_ids()) == 1 # it should have an id of 1 - assert deck.decks.name(1) - # create a new deck - parentId = deck.decks.id("new deck") + assert col.decks.name(1) + # create a new col + parentId = col.decks.id("new deck") assert parentId - assert len(deck.decks.all_names_and_ids()) == 2 + assert len(col.decks.all_names_and_ids()) == 2 # should get the same id - assert deck.decks.id("new deck") == parentId - # we start with the default deck selected - assert deck.decks.selected() == 1 - assert deck.decks.active() == [1] - # we can select a different deck - deck.decks.select(parentId) - assert deck.decks.selected() == parentId - assert deck.decks.active() == [parentId] + assert col.decks.id("new deck") == parentId + # we start with the default col selected + assert col.decks.selected() == 1 + assert col.decks.active() == [1] + # we can select a different col + col.decks.select(parentId) + assert col.decks.selected() == parentId + assert col.decks.active() == [parentId] # let's create a child - childId = deck.decks.id("new deck::child") - deck.sched.reset() + childId = col.decks.id("new deck::child") + col.sched.reset() # it should have been added to the active list - assert deck.decks.selected() == parentId - assert deck.decks.active() == [parentId, childId] + assert col.decks.selected() == parentId + assert col.decks.active() == [parentId, childId] # we can select the child individually too - deck.decks.select(childId) - assert deck.decks.selected() == childId - assert deck.decks.active() == [childId] + col.decks.select(childId) + assert col.decks.selected() == childId + assert col.decks.active() == [childId] # parents with a different case should be handled correctly - deck.decks.id("ONE") - m = deck.models.current() - m["did"] = deck.decks.id("one::two") - deck.models.save(m, updateReqs=False) - n = deck.newNote() + col.decks.id("ONE") + m = col.models.current() + m["did"] = col.decks.id("one::two") + col.models.save(m, updateReqs=False) + n = col.newNote() n["Front"] = "abc" - deck.addNote(n) + col.addNote(n) def test_remove(): - deck = getEmptyCol() - # create a new deck, and add a note/card to it - g1 = deck.decks.id("g1") - note = deck.newNote() + col = getEmptyCol() + # create a new col, and add a note/card to it + g1 = col.decks.id("g1") + note = col.newNote() note["Front"] = "1" note.model()["did"] = g1 - deck.addNote(note) + col.addNote(note) c = note.cards()[0] assert c.did == g1 - assert deck.cardCount() == 1 - deck.decks.rem(g1) - assert deck.cardCount() == 0 + assert col.cardCount() == 1 + col.decks.rem(g1) + assert col.cardCount() == 0 # if we try to get it, we get the default - assert deck.decks.name(c.did) == "[no deck]" + assert col.decks.name(c.did) == "[no deck]" def test_rename(): @@ -70,7 +70,7 @@ def test_rename(): assert "foo" in names assert "foo::bar" in names assert "hello::world" not in names - # create another deck + # create another col id = d.decks.id("tmp") # automatically adjusted if a duplicate name d.decks.rename(d.decks.get(id), "FOO") @@ -106,15 +106,15 @@ def test_renameForDragAndDrop(): d.decks.renameForDragAndDrop(chinese_did, languages_did) assert deckNames() == ["Languages", "Languages::Chinese", "Languages::Chinese::HSK"] - # Dragging a deck onto itself is a no-op + # Dragging a col onto itself is a no-op d.decks.renameForDragAndDrop(languages_did, languages_did) assert deckNames() == ["Languages", "Languages::Chinese", "Languages::Chinese::HSK"] - # Dragging a deck onto its parent is a no-op + # Dragging a col onto its parent is a no-op d.decks.renameForDragAndDrop(hsk_did, chinese_did) assert deckNames() == ["Languages", "Languages::Chinese", "Languages::Chinese::HSK"] - # Dragging a deck onto a descendant is a no-op + # Dragging a col onto a descendant is a no-op d.decks.renameForDragAndDrop(languages_did, hsk_did) assert deckNames() == ["Languages", "Languages::Chinese", "Languages::Chinese::HSK"] @@ -122,15 +122,15 @@ def test_renameForDragAndDrop(): d.decks.renameForDragAndDrop(hsk_did, languages_did) assert deckNames() == ["Languages", "Languages::Chinese", "Languages::HSK"] - # Can drag a deck onto its sibling + # Can drag a col onto its sibling d.decks.renameForDragAndDrop(hsk_did, chinese_did) assert deckNames() == ["Languages", "Languages::Chinese", "Languages::Chinese::HSK"] - # Can drag a deck back to the top level + # Can drag a col back to the top level d.decks.renameForDragAndDrop(chinese_did, None) assert deckNames() == ["Chinese", "Chinese::HSK", "Languages"] - # Dragging a top level deck 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) assert deckNames() == ["Chinese", "Chinese::HSK", "Languages"] diff --git a/pylib/tests/test_exporting.py b/pylib/tests/test_exporting.py index a74d5af74..67e98852e 100644 --- a/pylib/tests/test_exporting.py +++ b/pylib/tests/test_exporting.py @@ -16,25 +16,25 @@ def getEmptyCol(): return col -deck = None +col = None ds = None testDir = os.path.dirname(__file__) def setup1(): - global deck - deck = getEmptyCol() - note = deck.newNote() + global col + col = getEmptyCol() + note = col.newNote() note["Front"] = "foo" note["Back"] = "bar
" note.tags = ["tag", "tag2"] - deck.addNote(note) - # with a different deck - note = deck.newNote() + col.addNote(note) + # with a different col + note = col.newNote() note["Front"] = "baz" note["Back"] = "qux" - note.model()["did"] = deck.decks.id("new deck") - deck.addNote(note) + note.model()["did"] = col.decks.id("new col") + col.addNote(note) ########################################################################## @@ -42,23 +42,23 @@ def setup1(): def test_export_anki(): setup1() - # create a new deck with its own conf to test conf copying - did = deck.decks.id("test") - dobj = deck.decks.get(did) - confId = deck.decks.add_config_returning_id("newconf") - conf = deck.decks.get_config(confId) + # create a new col with its own conf to test conf copying + did = col.decks.id("test") + dobj = col.decks.get(did) + confId = col.decks.add_config_returning_id("newconf") + conf = col.decks.get_config(confId) conf["new"]["perDay"] = 5 - deck.decks.save(conf) - deck.decks.setConf(dobj, confId) + col.decks.save(conf) + col.decks.setConf(dobj, confId) # export - e = AnkiExporter(deck) + e = AnkiExporter(col) fd, newname = tempfile.mkstemp(prefix="ankitest", suffix=".anki2") newname = str(newname) os.close(fd) os.unlink(newname) e.exportInto(newname) # exporting should not have changed conf for original deck - conf = deck.decks.confForDid(did) + conf = col.decks.confForDid(did) assert conf["id"] != 1 # connect to new deck d2 = aopen(newname) @@ -85,12 +85,12 @@ def test_export_anki(): def test_export_ankipkg(): setup1() # add a test file to the media folder - with open(os.path.join(deck.media.dir(), "今日.mp3"), "w") as note: + with open(os.path.join(col.media.dir(), "今日.mp3"), "w") as note: note.write("test") - n = deck.newNote() + n = col.newNote() n["Front"] = "[sound:今日.mp3]" - deck.addNote(n) - e = AnkiPackageExporter(deck) + col.addNote(n) + e = AnkiPackageExporter(col) fd, newname = tempfile.mkstemp(prefix="ankitest", suffix=".apkg") newname = str(newname) os.close(fd) @@ -101,23 +101,23 @@ def test_export_ankipkg(): @errorsAfterMidnight def test_export_anki_due(): setup1() - deck = getEmptyCol() - note = deck.newNote() + col = getEmptyCol() + note = col.newNote() note["Front"] = "foo" - deck.addNote(note) - deck.crt -= 86400 * 10 - deck.flush() - deck.sched.reset() - c = deck.sched.getCard() - deck.sched.answerCard(c, 3) - deck.sched.answerCard(c, 3) + col.addNote(note) + col.crt -= 86400 * 10 + col.flush() + col.sched.reset() + c = col.sched.getCard() + col.sched.answerCard(c, 3) + col.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 + assert col.sched.today == 10 + assert c.due - col.sched.today == 1 # export - e = AnkiExporter(deck) + e = AnkiExporter(col) e.includeSched = True fd, newname = tempfile.mkstemp(prefix="ankitest", suffix=".anki2") newname = str(newname) @@ -135,7 +135,7 @@ def test_export_anki_due(): # def test_export_textcard(): # setup1() -# e = TextCardExporter(deck) +# e = TextCardExporter(col) # note = unicode(tempfile.mkstemp(prefix="ankitest")[1]) # os.unlink(note) # e.exportInto(note) @@ -145,7 +145,7 @@ def test_export_anki_due(): def test_export_textnote(): setup1() - e = TextNoteExporter(deck) + e = TextNoteExporter(col) fd, note = tempfile.mkstemp(prefix="ankitest") note = str(note) os.close(fd) diff --git a/pylib/tests/test_find.py b/pylib/tests/test_find.py index b75ce24cd..573f94726 100644 --- a/pylib/tests/test_find.py +++ b/pylib/tests/test_find.py @@ -12,284 +12,284 @@ class DummyCollection: def test_findCards(): - deck = getEmptyCol() - note = deck.newNote() + col = getEmptyCol() + note = col.newNote() note["Front"] = "dog" note["Back"] = "cat" note.tags.append("monkey animal_1 * %") - deck.addNote(note) + col.addNote(note) f1id = note.id firstCardId = note.cards()[0].id - note = deck.newNote() + note = col.newNote() note["Front"] = "goats are fun" note["Back"] = "sheep" note.tags.append("sheep goat horse animal11") - deck.addNote(note) + col.addNote(note) f2id = note.id - note = deck.newNote() + note = col.newNote() note["Front"] = "cat" note["Back"] = "sheep" - deck.addNote(note) + col.addNote(note) catCard = note.cards()[0] - m = deck.models.current() - m = deck.models.copy(m) - mm = deck.models + m = col.models.current() + m = col.models.copy(m) + mm = col.models t = mm.newTemplate("Reverse") t["qfmt"] = "{{Back}}" t["afmt"] = "{{Front}}" mm.addTemplate(m, t) mm.save(m) - note = deck.newNote() + note = col.newNote() note["Front"] = "test" note["Back"] = "foo bar" - deck.addNote(note) - deck.save() + col.addNote(note) + col.save() latestCardIds = [c.id for c in note.cards()] # tag searches - assert len(deck.findCards("tag:*")) == 5 - assert len(deck.findCards("tag:\\*")) == 1 - assert len(deck.findCards("tag:%")) == 5 - assert len(deck.findCards("tag:\\%")) == 1 - assert len(deck.findCards("tag:animal_1")) == 2 - assert len(deck.findCards("tag:animal\\_1")) == 1 - assert not deck.findCards("tag:donkey") - assert len(deck.findCards("tag:sheep")) == 1 - assert len(deck.findCards("tag:sheep tag:goat")) == 1 - assert len(deck.findCards("tag:sheep tag:monkey")) == 0 - assert len(deck.findCards("tag:monkey")) == 1 - assert len(deck.findCards("tag:sheep -tag:monkey")) == 1 - assert len(deck.findCards("-tag:sheep")) == 4 - deck.tags.bulkAdd(deck.db.list("select id from notes"), "foo bar") - assert len(deck.findCards("tag:foo")) == len(deck.findCards("tag:bar")) == 5 - deck.tags.bulkRem(deck.db.list("select id from notes"), "foo") - assert len(deck.findCards("tag:foo")) == 0 - assert len(deck.findCards("tag:bar")) == 5 + assert len(col.findCards("tag:*")) == 5 + assert len(col.findCards("tag:\\*")) == 1 + assert len(col.findCards("tag:%")) == 5 + assert len(col.findCards("tag:\\%")) == 1 + assert len(col.findCards("tag:animal_1")) == 2 + assert len(col.findCards("tag:animal\\_1")) == 1 + assert not col.findCards("tag:donkey") + assert len(col.findCards("tag:sheep")) == 1 + assert len(col.findCards("tag:sheep tag:goat")) == 1 + assert len(col.findCards("tag:sheep tag:monkey")) == 0 + assert len(col.findCards("tag:monkey")) == 1 + assert len(col.findCards("tag:sheep -tag:monkey")) == 1 + assert len(col.findCards("-tag:sheep")) == 4 + col.tags.bulkAdd(col.db.list("select id from notes"), "foo bar") + assert len(col.findCards("tag:foo")) == len(col.findCards("tag:bar")) == 5 + col.tags.bulkRem(col.db.list("select id from notes"), "foo") + assert len(col.findCards("tag:foo")) == 0 + assert len(col.findCards("tag:bar")) == 5 # text searches - assert len(deck.findCards("cat")) == 2 - assert len(deck.findCards("cat -dog")) == 1 - assert len(deck.findCards("cat -dog")) == 1 - assert len(deck.findCards("are goats")) == 1 - assert len(deck.findCards('"are goats"')) == 0 - assert len(deck.findCards('"goats are"')) == 1 + assert len(col.findCards("cat")) == 2 + assert len(col.findCards("cat -dog")) == 1 + assert len(col.findCards("cat -dog")) == 1 + assert len(col.findCards("are goats")) == 1 + assert len(col.findCards('"are goats"')) == 0 + assert len(col.findCards('"goats are"')) == 1 # card states c = note.cards()[0] c.queue = c.type = CARD_TYPE_REV - assert deck.findCards("is:review") == [] + assert col.findCards("is:review") == [] c.flush() - assert deck.findCards("is:review") == [c.id] - assert deck.findCards("is:due") == [] + assert col.findCards("is:review") == [c.id] + assert col.findCards("is:due") == [] c.due = 0 c.queue = QUEUE_TYPE_REV c.flush() - assert deck.findCards("is:due") == [c.id] - assert len(deck.findCards("-is:due")) == 4 + assert col.findCards("is:due") == [c.id] + assert len(col.findCards("-is:due")) == 4 c.queue = -1 # ensure this card gets a later mod time c.flush() - deck.db.execute("update cards set mod = mod + 1 where id = ?", c.id) - assert deck.findCards("is:suspended") == [c.id] + col.db.execute("update cards set mod = mod + 1 where id = ?", c.id) + assert col.findCards("is:suspended") == [c.id] # nids - assert deck.findCards("nid:54321") == [] - assert len(deck.findCards("nid:%d" % note.id)) == 2 - assert len(deck.findCards("nid:%d,%d" % (f1id, f2id))) == 2 + assert col.findCards("nid:54321") == [] + assert len(col.findCards("nid:%d" % note.id)) == 2 + assert len(col.findCards("nid:%d,%d" % (f1id, f2id))) == 2 # templates - assert len(deck.findCards("card:foo")) == 0 - assert len(deck.findCards('"card:card 1"')) == 4 - assert len(deck.findCards("card:reverse")) == 1 - assert len(deck.findCards("card:1")) == 4 - assert len(deck.findCards("card:2")) == 1 + assert len(col.findCards("card:foo")) == 0 + assert len(col.findCards('"card:card 1"')) == 4 + assert len(col.findCards("card:reverse")) == 1 + assert len(col.findCards("card:1")) == 4 + assert len(col.findCards("card:2")) == 1 # fields - assert len(deck.findCards("front:dog")) == 1 - assert len(deck.findCards("-front:dog")) == 4 - assert len(deck.findCards("front:sheep")) == 0 - assert len(deck.findCards("back:sheep")) == 2 - assert len(deck.findCards("-back:sheep")) == 3 - assert len(deck.findCards("front:do")) == 0 - assert len(deck.findCards("front:*")) == 5 + assert len(col.findCards("front:dog")) == 1 + assert len(col.findCards("-front:dog")) == 4 + assert len(col.findCards("front:sheep")) == 0 + assert len(col.findCards("back:sheep")) == 2 + assert len(col.findCards("-back:sheep")) == 3 + assert len(col.findCards("front:do")) == 0 + assert len(col.findCards("front:*")) == 5 # ordering - deck.conf["sortType"] = "noteCrt" - deck.flush() - assert deck.findCards("front:*", order=True)[-1] in latestCardIds - assert deck.findCards("", order=True)[-1] in latestCardIds - deck.conf["sortType"] = "noteFld" - deck.flush() - assert deck.findCards("", order=True)[0] == catCard.id - assert deck.findCards("", order=True)[-1] in latestCardIds - deck.conf["sortType"] = "cardMod" - deck.flush() - assert deck.findCards("", order=True)[-1] in latestCardIds - assert deck.findCards("", order=True)[0] == firstCardId - deck.conf["sortBackwards"] = True - deck.flush() - assert deck.findCards("", order=True)[0] in latestCardIds + col.conf["sortType"] = "noteCrt" + col.flush() + assert col.findCards("front:*", order=True)[-1] in latestCardIds + assert col.findCards("", order=True)[-1] in latestCardIds + col.conf["sortType"] = "noteFld" + col.flush() + assert col.findCards("", order=True)[0] == catCard.id + assert col.findCards("", order=True)[-1] in latestCardIds + col.conf["sortType"] = "cardMod" + col.flush() + assert col.findCards("", order=True)[-1] in latestCardIds + assert col.findCards("", order=True)[0] == firstCardId + col.conf["sortBackwards"] = True + col.flush() + assert col.findCards("", order=True)[0] in latestCardIds assert ( - deck.find_cards("", order=BuiltinSortKind.CARD_DUE, reverse=False)[0] + col.find_cards("", order=BuiltinSortKind.CARD_DUE, reverse=False)[0] == firstCardId ) assert ( - deck.find_cards("", order=BuiltinSortKind.CARD_DUE, reverse=True)[0] + col.find_cards("", order=BuiltinSortKind.CARD_DUE, reverse=True)[0] != firstCardId ) # model - assert len(deck.findCards("note:basic")) == 3 - assert len(deck.findCards("-note:basic")) == 2 - assert len(deck.findCards("-note:foo")) == 5 - # deck - assert len(deck.findCards("deck:default")) == 5 - assert len(deck.findCards("-deck:default")) == 0 - assert len(deck.findCards("-deck:foo")) == 5 - assert len(deck.findCards("deck:def*")) == 5 - assert len(deck.findCards("deck:*EFAULT")) == 5 - assert len(deck.findCards("deck:*cefault")) == 0 + assert len(col.findCards("note:basic")) == 3 + assert len(col.findCards("-note:basic")) == 2 + assert len(col.findCards("-note:foo")) == 5 + # col + assert len(col.findCards("deck:default")) == 5 + assert len(col.findCards("-deck:default")) == 0 + assert len(col.findCards("-deck:foo")) == 5 + assert len(col.findCards("deck:def*")) == 5 + assert len(col.findCards("deck:*EFAULT")) == 5 + assert len(col.findCards("deck:*cefault")) == 0 # full search - note = deck.newNote() + note = col.newNote() note["Front"] = "helloworld" note["Back"] = "abc" - deck.addNote(note) + col.addNote(note) # as it's the sort field, it matches - assert len(deck.findCards("helloworld")) == 2 - # assert len(deck.findCards("helloworld", full=True)) == 2 + assert len(col.findCards("helloworld")) == 2 + # assert len(col.findCards("helloworld", full=True)) == 2 # if we put it on the back, it won't (note["Front"], note["Back"]) = (note["Back"], note["Front"]) note.flush() - assert len(deck.findCards("helloworld")) == 0 - # assert len(deck.findCards("helloworld", full=True)) == 2 - # assert len(deck.findCards("back:helloworld", full=True)) == 2 + assert len(col.findCards("helloworld")) == 0 + # assert len(col.findCards("helloworld", full=True)) == 2 + # assert len(col.findCards("back:helloworld", full=True)) == 2 # searching for an invalid special tag should not error with pytest.raises(Exception): - len(deck.findCards("is:invalid")) - # should be able to limit to parent deck, no children - id = deck.db.scalar("select id from cards limit 1") - deck.db.execute( - "update cards set did = ? where id = ?", deck.decks.id("Default::Child"), id + len(col.findCards("is:invalid")) + # should be able to limit to parent col, no children + id = col.db.scalar("select id from cards limit 1") + col.db.execute( + "update cards set did = ? where id = ?", col.decks.id("Default::Child"), id ) - deck.save() - assert len(deck.findCards("deck:default")) == 7 - assert len(deck.findCards("deck:default::child")) == 1 - assert len(deck.findCards("deck:default -deck:default::*")) == 6 + col.save() + assert len(col.findCards("deck:default")) == 7 + assert len(col.findCards("deck:default::child")) == 1 + assert len(col.findCards("deck:default -deck:default::*")) == 6 # properties - id = deck.db.scalar("select id from cards limit 1") - deck.db.execute( + id = col.db.scalar("select id from cards limit 1") + col.db.execute( "update cards set queue=2, ivl=10, reps=20, due=30, factor=2200 " "where id = ?", id, ) - assert len(deck.findCards("prop:ivl>5")) == 1 - assert len(deck.findCards("prop:ivl<5")) > 1 - assert len(deck.findCards("prop:ivl>=5")) == 1 - assert len(deck.findCards("prop:ivl=9")) == 0 - assert len(deck.findCards("prop:ivl=10")) == 1 - assert len(deck.findCards("prop:ivl!=10")) > 1 - assert len(deck.findCards("prop:due>0")) == 1 + assert len(col.findCards("prop:ivl>5")) == 1 + assert len(col.findCards("prop:ivl<5")) > 1 + assert len(col.findCards("prop:ivl>=5")) == 1 + assert len(col.findCards("prop:ivl=9")) == 0 + assert len(col.findCards("prop:ivl=10")) == 1 + assert len(col.findCards("prop:ivl!=10")) > 1 + assert len(col.findCards("prop:due>0")) == 1 # due dates should work - assert len(deck.findCards("prop:due=29")) == 0 - assert len(deck.findCards("prop:due=30")) == 1 + assert len(col.findCards("prop:due=29")) == 0 + assert len(col.findCards("prop:due=30")) == 1 # ease factors - assert len(deck.findCards("prop:ease=2.3")) == 0 - assert len(deck.findCards("prop:ease=2.2")) == 1 - assert len(deck.findCards("prop:ease>2")) == 1 - assert len(deck.findCards("-prop:ease>2")) > 1 + assert len(col.findCards("prop:ease=2.3")) == 0 + assert len(col.findCards("prop:ease=2.2")) == 1 + assert len(col.findCards("prop:ease>2")) == 1 + assert len(col.findCards("-prop:ease>2")) > 1 # recently failed if not isNearCutoff(): - assert len(deck.findCards("rated:1:1")) == 0 - assert len(deck.findCards("rated:1:2")) == 0 - c = deck.sched.getCard() - deck.sched.answerCard(c, 2) - assert len(deck.findCards("rated:1:1")) == 0 - assert len(deck.findCards("rated:1:2")) == 1 - c = deck.sched.getCard() - deck.sched.answerCard(c, 1) - assert len(deck.findCards("rated:1:1")) == 1 - assert len(deck.findCards("rated:1:2")) == 1 - assert len(deck.findCards("rated:1")) == 2 - assert len(deck.findCards("rated:0:2")) == 0 - assert len(deck.findCards("rated:2:2")) == 1 + assert len(col.findCards("rated:1:1")) == 0 + assert len(col.findCards("rated:1:2")) == 0 + c = col.sched.getCard() + col.sched.answerCard(c, 2) + assert len(col.findCards("rated:1:1")) == 0 + assert len(col.findCards("rated:1:2")) == 1 + c = col.sched.getCard() + col.sched.answerCard(c, 1) + assert len(col.findCards("rated:1:1")) == 1 + assert len(col.findCards("rated:1:2")) == 1 + assert len(col.findCards("rated:1")) == 2 + assert len(col.findCards("rated:0:2")) == 0 + assert len(col.findCards("rated:2:2")) == 1 # added - assert len(deck.findCards("added:0")) == 0 - deck.db.execute("update cards set id = id - 86400*1000 where id = ?", id) - assert len(deck.findCards("added:1")) == deck.cardCount() - 1 - assert len(deck.findCards("added:2")) == deck.cardCount() + assert len(col.findCards("added:0")) == 0 + col.db.execute("update cards set id = id - 86400*1000 where id = ?", id) + assert len(col.findCards("added:1")) == col.cardCount() - 1 + assert len(col.findCards("added:2")) == col.cardCount() else: print("some find tests disabled near cutoff") # empty field - assert len(deck.findCards("front:")) == 0 - note = deck.newNote() + assert len(col.findCards("front:")) == 0 + note = col.newNote() note["Front"] = "" note["Back"] = "abc2" - assert deck.addNote(note) == 1 - assert len(deck.findCards("front:")) == 1 + assert col.addNote(note) == 1 + assert len(col.findCards("front:")) == 1 # OR searches and nesting - assert len(deck.findCards("tag:monkey or tag:sheep")) == 2 - assert len(deck.findCards("(tag:monkey OR tag:sheep)")) == 2 - assert len(deck.findCards("-(tag:monkey OR tag:sheep)")) == 6 - assert len(deck.findCards("tag:monkey or (tag:sheep sheep)")) == 2 - assert len(deck.findCards("tag:monkey or (tag:sheep octopus)")) == 1 + assert len(col.findCards("tag:monkey or tag:sheep")) == 2 + assert len(col.findCards("(tag:monkey OR tag:sheep)")) == 2 + assert len(col.findCards("-(tag:monkey OR tag:sheep)")) == 6 + assert len(col.findCards("tag:monkey or (tag:sheep sheep)")) == 2 + assert len(col.findCards("tag:monkey or (tag:sheep octopus)")) == 1 # flag with pytest.raises(Exception): - deck.findCards("flag:12") + col.findCards("flag:12") def test_findReplace(): - deck = getEmptyCol() - note = deck.newNote() + col = getEmptyCol() + note = col.newNote() note["Front"] = "foo" note["Back"] = "bar" - deck.addNote(note) - note2 = deck.newNote() + col.addNote(note) + note2 = col.newNote() note2["Front"] = "baz" note2["Back"] = "foo" - deck.addNote(note2) + col.addNote(note2) nids = [note.id, note2.id] # should do nothing - assert deck.findReplace(nids, "abc", "123") == 0 + assert col.findReplace(nids, "abc", "123") == 0 # global replace - assert deck.findReplace(nids, "foo", "qux") == 2 + assert col.findReplace(nids, "foo", "qux") == 2 note.load() assert note["Front"] == "qux" note2.load() assert note2["Back"] == "qux" # single field replace - assert deck.findReplace(nids, "qux", "foo", field="Front") == 1 + assert col.findReplace(nids, "qux", "foo", field="Front") == 1 note.load() assert note["Front"] == "foo" note2.load() assert note2["Back"] == "qux" # regex replace - assert deck.findReplace(nids, "B.r", "reg") == 0 + assert col.findReplace(nids, "B.r", "reg") == 0 note.load() assert note["Back"] != "reg" - assert deck.findReplace(nids, "B.r", "reg", regex=True) == 1 + assert col.findReplace(nids, "B.r", "reg", regex=True) == 1 note.load() assert note["Back"] == "reg" def test_findDupes(): - deck = getEmptyCol() - note = deck.newNote() + col = getEmptyCol() + note = col.newNote() note["Front"] = "foo" note["Back"] = "bar" - deck.addNote(note) - note2 = deck.newNote() + col.addNote(note) + note2 = col.newNote() note2["Front"] = "baz" note2["Back"] = "bar" - deck.addNote(note2) - f3 = deck.newNote() + col.addNote(note2) + f3 = col.newNote() f3["Front"] = "quux" f3["Back"] = "bar" - deck.addNote(f3) - f4 = deck.newNote() + col.addNote(f3) + f4 = col.newNote() f4["Front"] = "quuux" f4["Back"] = "nope" - deck.addNote(f4) - r = deck.findDupes("Back") + col.addNote(f4) + r = col.findDupes("Back") assert r[0][0] == "bar" assert len(r[0][1]) == 3 # valid search - r = deck.findDupes("Back", "bar") + r = col.findDupes("Back", "bar") assert r[0][0] == "bar" assert len(r[0][1]) == 3 # excludes everything - r = deck.findDupes("Back", "invalid") + r = col.findDupes("Back", "invalid") assert not r # front isn't dupe - assert deck.findDupes("Front") == [] + assert col.findDupes("Front") == [] diff --git a/pylib/tests/test_importing.py b/pylib/tests/test_importing.py index 43c9f5a20..6748ac6c7 100644 --- a/pylib/tests/test_importing.py +++ b/pylib/tests/test_importing.py @@ -147,9 +147,9 @@ def test_anki2_updates(): def test_csv(): - deck = getEmptyCol() + col = getEmptyCol() file = str(os.path.join(testDir, "support/text-2fields.txt")) - i = TextImporter(deck, file) + i = TextImporter(col, file) i.initMapping() i.run() # four problems - too many & too few fields, a missing front, and a @@ -161,7 +161,7 @@ def test_csv(): assert len(i.log) == 10 assert i.total == 5 # but importing should not clobber tags if they're unmapped - n = deck.getNote(deck.db.scalar("select id from notes")) + n = col.getNote(col.db.scalar("select id from notes")) n.addTag("test") n.flush() i.run() @@ -172,58 +172,58 @@ def test_csv(): i.run() assert i.total == 0 # and if dupes mode, will reimport everything - assert deck.cardCount() == 5 + assert col.cardCount() == 5 i.importMode = 2 i.run() # includes repeated field assert i.total == 6 - assert deck.cardCount() == 11 - deck.close() + assert col.cardCount() == 11 + col.close() def test_csv2(): - deck = getEmptyCol() - mm = deck.models + col = getEmptyCol() + mm = col.models m = mm.current() note = mm.newField("Three") mm.addField(m, note) mm.save(m) - n = deck.newNote() + n = col.newNote() n["Front"] = "1" n["Back"] = "2" n["Three"] = "3" - deck.addNote(n) + col.addNote(n) # an update with unmapped fields should not clobber those fields file = str(os.path.join(testDir, "support/text-update.txt")) - i = TextImporter(deck, file) + i = TextImporter(col, file) i.initMapping() i.run() n.load() assert n["Front"] == "1" assert n["Back"] == "x" assert n["Three"] == "3" - deck.close() + col.close() def test_tsv_tag_modified(): - deck = getEmptyCol() - mm = deck.models + col = getEmptyCol() + mm = col.models m = mm.current() note = mm.newField("Top") mm.addField(m, note) mm.save(m) - n = deck.newNote() + n = col.newNote() n["Front"] = "1" n["Back"] = "2" n["Top"] = "3" n.addTag("four") - deck.addNote(n) + col.addNote(n) # https://stackoverflow.com/questions/23212435/permission-denied-to-write-to-my-temporary-file with NamedTemporaryFile(mode="w", delete=False) as tf: tf.write("1\tb\tc\n") tf.flush() - i = TextImporter(deck, tf.name) + i = TextImporter(col, tf.name) i.initMapping() i.tagModified = "boom" i.run() @@ -238,29 +238,29 @@ def test_tsv_tag_modified(): assert len(n.tags) == 2 assert i.updateCount == 1 - deck.close() + col.close() def test_tsv_tag_multiple_tags(): - deck = getEmptyCol() - mm = deck.models + col = getEmptyCol() + mm = col.models m = mm.current() note = mm.newField("Top") mm.addField(m, note) mm.save(m) - n = deck.newNote() + n = col.newNote() n["Front"] = "1" n["Back"] = "2" n["Top"] = "3" n.addTag("four") n.addTag("five") - deck.addNote(n) + col.addNote(n) # https://stackoverflow.com/questions/23212435/permission-denied-to-write-to-my-temporary-file with NamedTemporaryFile(mode="w", delete=False) as tf: tf.write("1\tb\tc\n") tf.flush() - i = TextImporter(deck, tf.name) + i = TextImporter(col, tf.name) i.initMapping() i.tagModified = "five six" i.run() @@ -272,27 +272,27 @@ def test_tsv_tag_multiple_tags(): assert n["Top"] == "c" assert list(sorted(n.tags)) == list(sorted(["four", "five", "six"])) - deck.close() + col.close() def test_csv_tag_only_if_modified(): - deck = getEmptyCol() - mm = deck.models + col = getEmptyCol() + mm = col.models m = mm.current() note = mm.newField("Left") mm.addField(m, note) mm.save(m) - n = deck.newNote() + n = col.newNote() n["Front"] = "1" n["Back"] = "2" n["Left"] = "3" - deck.addNote(n) + col.addNote(n) # https://stackoverflow.com/questions/23212435/permission-denied-to-write-to-my-temporary-file with NamedTemporaryFile(mode="w", delete=False) as tf: tf.write("1,2,3\n") tf.flush() - i = TextImporter(deck, tf.name) + i = TextImporter(col, tf.name) i.initMapping() i.tagModified = "right" i.run() @@ -302,31 +302,31 @@ def test_csv_tag_only_if_modified(): assert n.tags == [] assert i.updateCount == 0 - deck.close() + col.close() @pytest.mark.filterwarnings("ignore:Using or importing the ABCs") def test_supermemo_xml_01_unicode(): - deck = getEmptyCol() + col = getEmptyCol() file = str(os.path.join(testDir, "support/supermemo1.xml")) - i = SupermemoXmlImporter(deck, file) + i = SupermemoXmlImporter(col, file) # i.META.logToStdOutput = True i.run() assert i.total == 1 - cid = deck.db.scalar("select id from cards") - c = deck.getCard(cid) + cid = col.db.scalar("select id from cards") + c = col.getCard(cid) # Applies A Factor-to-E Factor conversion assert c.factor == 2879 assert c.reps == 7 - deck.close() + col.close() def test_mnemo(): - deck = getEmptyCol() + col = getEmptyCol() file = str(os.path.join(testDir, "support/mnemo.db")) - i = MnemosyneImporter(deck, file) + i = MnemosyneImporter(col, file) i.run() - assert deck.cardCount() == 7 - assert "a_longer_tag" in deck.tags.all() - assert deck.db.scalar("select count() from cards where type = 0") == 1 - deck.close() + assert col.cardCount() == 7 + assert "a_longer_tag" in col.tags.all() + assert col.db.scalar("select count() from cards where type = 0") == 1 + col.close() diff --git a/pylib/tests/test_models.py b/pylib/tests/test_models.py index 9d14f580c..7afaf4ca9 100644 --- a/pylib/tests/test_models.py +++ b/pylib/tests/test_models.py @@ -8,20 +8,20 @@ from tests.shared import getEmptyCol def test_modelDelete(): - deck = getEmptyCol() - note = deck.newNote() + col = getEmptyCol() + note = col.newNote() note["Front"] = "1" note["Back"] = "2" - deck.addNote(note) - assert deck.cardCount() == 1 - deck.models.rem(deck.models.current()) - assert deck.cardCount() == 0 + col.addNote(note) + assert col.cardCount() == 1 + col.models.rem(col.models.current()) + assert col.cardCount() == 0 def test_modelCopy(): - deck = getEmptyCol() - m = deck.models.current() - m2 = deck.models.copy(m) + col = getEmptyCol() + m = col.models.current() + m2 = col.models.copy(m) assert m2["name"] == "Basic copy" assert m2["id"] != m["id"] assert len(m2["flds"]) == 2 @@ -29,7 +29,7 @@ def test_modelCopy(): assert len(m2["flds"]) == len(m["flds"]) assert len(m["tmpls"]) == 1 assert len(m2["tmpls"]) == 1 - assert deck.models.scmhash(m) == deck.models.scmhash(m2) + assert col.models.scmhash(m) == col.models.scmhash(m2) def test_fields(): @@ -278,24 +278,24 @@ def test_chained_mods(): def test_modelChange(): - deck = getEmptyCol() - cloze = deck.models.byName("Cloze") + col = getEmptyCol() + cloze = col.models.byName("Cloze") # enable second template and add a note - m = deck.models.current() - mm = deck.models + m = col.models.current() + mm = col.models t = mm.newTemplate("Reverse") t["qfmt"] = "{{Back}}" t["afmt"] = "{{Front}}" mm.addTemplate(m, t) mm.save(m) basic = m - note = deck.newNote() + note = col.newNote() note["Front"] = "note" note["Back"] = "b123" - deck.addNote(note) + col.addNote(note) # switch fields map = {0: 1, 1: 0} - deck.models.change(basic, [note.id], basic, map, None) + col.models.change(basic, [note.id], basic, map, None) note.load() assert note["Front"] == "b123" assert note["Back"] == "note" @@ -306,7 +306,7 @@ def test_modelChange(): assert "note" in c1.q() assert c0.ord == 0 assert c1.ord == 1 - deck.models.change(basic, [note.id], basic, None, map) + col.models.change(basic, [note.id], basic, None, map) note.load() c0.load() c1.load() @@ -321,7 +321,7 @@ def test_modelChange(): if isWin: # The low precision timer on Windows reveals a race condition time.sleep(0.05) - deck.models.change(basic, [note.id], basic, None, map) + col.models.change(basic, [note.id], basic, None, map) note.load() c0.load() # the card was deleted @@ -335,29 +335,29 @@ def test_modelChange(): # an unmapped field becomes blank assert note["Front"] == "b123" assert note["Back"] == "note" - deck.models.change(basic, [note.id], basic, map, None) + col.models.change(basic, [note.id], basic, map, None) note.load() assert note["Front"] == "" assert note["Back"] == "note" # another note to try model conversion - note = deck.newNote() + note = col.newNote() note["Front"] = "f2" note["Back"] = "b2" - deck.addNote(note) - counts = deck.models.all_use_counts() + col.addNote(note) + counts = col.models.all_use_counts() assert next(c.use_count for c in counts if c.name == "Basic") == 2 assert next(c.use_count for c in counts if c.name == "Cloze") == 0 map = {0: 0, 1: 1} - deck.models.change(basic, [note.id], cloze, map, map) + col.models.change(basic, [note.id], cloze, map, map) note.load() assert note["Text"] == "f2" assert len(note.cards()) == 2 # back the other way, with deletion of second ord - deck.models.remTemplate(basic, basic["tmpls"][1]) - assert deck.db.scalar("select count() from cards where nid = ?", note.id) == 2 + col.models.remTemplate(basic, basic["tmpls"][1]) + assert col.db.scalar("select count() from cards where nid = ?", note.id) == 2 map = {0: 0} - deck.models.change(cloze, [note.id], basic, map, map) - assert deck.db.scalar("select count() from cards where nid = ?", note.id) == 1 + col.models.change(cloze, [note.id], basic, map, map) + assert col.db.scalar("select count() from cards where nid = ?", note.id) == 1 def test_req():