anki/pylib/tests/test_decks.py

97 lines
3.3 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
2019-11-13 15:49:09 +01:00
from anki.errors import DeckRenameError
2014-06-03 10:38:47 +02:00
from tests.shared import assertException, getEmptyCol
2019-12-25 05:18:34 +01:00
def test_basic():
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 col.decks.name(1)
# create a new col
parentId = col.decks.id("new deck")
assert parentId
assert len(col.decks.all_names_and_ids()) == 2
# should get the same id
assert col.decks.id("new deck") == parentId
# we start with the default col selected
assert col.decks.selected() == 1
col.reset()
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 = col.decks.id("new deck::child")
col.sched.reset()
# it should have been added to the active list
assert col.decks.selected() == parentId
assert col.decks.active() == [parentId, childId]
# we can select the child individually too
col.decks.select(childId)
assert col.decks.selected() == childId
assert col.decks.active() == [childId]
# parents with a different case should be handled correctly
col.decks.id("ONE")
m = col.models.current()
m["did"] = col.decks.id("one::two")
col.models.save(m, updateReqs=False)
n = col.newNote()
2019-12-25 05:18:34 +01:00
n["Front"] = "abc"
col.addNote(n)
2019-12-25 05:18:34 +01:00
def test_remove():
col = getEmptyCol()
# create a new col, and add a note/card to it
deck1 = col.decks.id("deck1")
note = col.newNote()
note["Front"] = "1"
2021-06-27 04:12:23 +02:00
note.note_type()["did"] = deck1
col.addNote(note)
c = note.cards()[0]
assert c.did == deck1
2021-06-27 07:12:22 +02:00
assert col.card_count() == 1
2021-06-27 04:12:23 +02:00
col.decks.remove([deck1])
2021-06-27 07:12:22 +02:00
assert col.card_count() == 0
# if we try to get it, we get the default
assert col.decks.name(c.did) == "[no deck]"
2019-12-25 05:18:34 +01:00
def test_rename():
col = getEmptyCol()
id = col.decks.id("hello::world")
# should be able to rename into a completely different branch, creating
# parents as necessary
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 = col.decks.id("tmp")
# automatically adjusted if a duplicate name
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
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
2019-11-13 15:49:09 +01:00
# over filtered
filteredId = col.decks.new_filtered("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"))