anki/pylib/tests/test_cards.py

96 lines
2.8 KiB
Python
Raw Normal View History

2021-04-13 10:45:05 +02:00
# Copyright: Ankitects Pty Ltd and contributors
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
# coding: utf-8
2014-06-03 10:38:47 +02:00
from tests.shared import getEmptyCol
2019-12-25 05:18:34 +01:00
def test_delete():
col = getEmptyCol()
note = col.newNote()
note["Front"] = "1"
note["Back"] = "2"
col.addNote(note)
cid = note.cards()[0].id
col.sched.answerCard(col.sched.getCard(), 2)
col.remove_cards_and_orphaned_notes([cid])
2021-06-27 07:12:22 +02:00
assert col.card_count() == 0
assert col.note_count() == 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
2019-12-25 05:18:34 +01:00
def test_misc():
col = getEmptyCol()
note = col.newNote()
note["Front"] = "1"
note["Back"] = "2"
col.addNote(note)
c = note.cards()[0]
id = col.models.current()["id"]
2019-12-25 05:18:34 +01:00
assert c.template()["ord"] == 0
def test_genrem():
col = getEmptyCol()
note = col.newNote()
note["Front"] = "1"
note["Back"] = ""
col.addNote(note)
assert len(note.cards()) == 1
m = col.models.current()
mm = col.models
# adding a new template should automatically create cards
2021-06-27 05:49:58 +02:00
t = mm.new_template("rev")
t["qfmt"] = "{{Front}}2"
2019-12-25 05:18:34 +01:00
t["afmt"] = ""
2021-06-27 05:49:58 +02:00
mm.add_template(m, t)
mm.save(m, templates=True)
assert len(note.cards()) == 2
# if the template is changed to remove cards, they'll be removed
t = m["tmpls"][1]
2019-12-25 05:18:34 +01:00
t["qfmt"] = "{{Back}}"
mm.save(m, templates=True)
2021-01-31 09:54:13 +01:00
rep = col._backend.get_empty_cards()
rep = col._backend.get_empty_cards()
for n in rep.notes:
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()
note["Back"] = "1"
note.flush()
assert len(note.cards()) == 2
2019-12-25 05:18:34 +01:00
def test_gendeck():
col = getEmptyCol()
2021-06-27 05:49:58 +02:00
cloze = col.models.by_name("Cloze")
note = col.new_note(cloze)
note["Text"] = "{{c1::one}}"
col.addNote(note)
2021-06-27 07:12:22 +02:00
assert col.card_count() == 1
assert note.cards()[0].did == 1
# set the model to a new default col
newId = col.decks.id("new")
col.set_aux_notetype_config(cloze["id"], "lastDeck", newId)
col.models.save(cloze, updateReqs=False)
# a newly generated card should share the first card's col
note["Text"] += "{{c2::two}}"
note.flush()
assert note.cards()[1].did == 1
# and same with multiple cards
note["Text"] += "{{c3::three}}"
note.flush()
assert note.cards()[2].did == 1
# if one of the cards is in a different col, it should revert to the
# model default
c = note.cards()[1]
c.did = newId
c.flush()
note["Text"] += "{{c4::four}}"
note.flush()
assert note.cards()[3].did == newId