2012-12-21 08:51:59 +01:00
|
|
|
# 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
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2019-12-25 05:18:34 +01:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
def test_basic():
|
2020-07-17 05:21:01 +02:00
|
|
|
col = getEmptyCol()
|
|
|
|
# we start with a standard col
|
|
|
|
assert len(col.decks.all_names_and_ids()) == 1
|
2012-12-21 08:51:59 +01:00
|
|
|
# it should have an id of 1
|
2020-07-17 05:21:01 +02:00
|
|
|
assert col.decks.name(1)
|
|
|
|
# create a new col
|
|
|
|
parentId = col.decks.id("new deck")
|
2012-12-21 08:51:59 +01:00
|
|
|
assert parentId
|
2020-07-17 05:21:01 +02:00
|
|
|
assert len(col.decks.all_names_and_ids()) == 2
|
2012-12-21 08:51:59 +01:00
|
|
|
# should get the same id
|
2020-07-17 05:21:01 +02:00
|
|
|
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]
|
2012-12-21 08:51:59 +01:00
|
|
|
# let's create a child
|
2020-07-17 05:21:01 +02:00
|
|
|
childId = col.decks.id("new deck::child")
|
|
|
|
col.sched.reset()
|
2012-12-21 08:51:59 +01:00
|
|
|
# it should have been added to the active list
|
2020-07-17 05:21:01 +02:00
|
|
|
assert col.decks.selected() == parentId
|
|
|
|
assert col.decks.active() == [parentId, childId]
|
2012-12-21 08:51:59 +01:00
|
|
|
# we can select the child individually too
|
2020-07-17 05:21:01 +02:00
|
|
|
col.decks.select(childId)
|
|
|
|
assert col.decks.selected() == childId
|
|
|
|
assert col.decks.active() == [childId]
|
2012-12-21 08:51:59 +01:00
|
|
|
# parents with a different case should be handled correctly
|
2020-07-17 05:21:01 +02:00
|
|
|
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"
|
2020-07-17 05:21:01 +02:00
|
|
|
col.addNote(n)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2019-12-25 05:18:34 +01:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
def test_remove():
|
2020-07-17 05:21:01 +02:00
|
|
|
col = getEmptyCol()
|
|
|
|
# create a new col, and add a note/card to it
|
2020-07-17 05:23:33 +02:00
|
|
|
deck1 = col.decks.id("deck1")
|
2020-07-17 05:21:01 +02:00
|
|
|
note = col.newNote()
|
2020-07-17 05:18:09 +02:00
|
|
|
note["Front"] = "1"
|
2020-07-17 05:23:33 +02:00
|
|
|
note.model()["did"] = deck1
|
2020-07-17 05:21:01 +02:00
|
|
|
col.addNote(note)
|
2020-07-17 05:18:09 +02:00
|
|
|
c = note.cards()[0]
|
2020-07-17 05:23:33 +02:00
|
|
|
assert c.did == deck1
|
2020-07-17 05:21:01 +02:00
|
|
|
assert col.cardCount() == 1
|
2020-07-17 05:23:33 +02:00
|
|
|
col.decks.rem(deck1)
|
2020-07-17 05:21:01 +02:00
|
|
|
assert col.cardCount() == 0
|
2020-05-02 04:41:53 +02:00
|
|
|
# if we try to get it, we get the default
|
2020-07-17 05:21:01 +02:00
|
|
|
assert col.decks.name(c.did) == "[no deck]"
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2019-12-25 05:18:34 +01:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
def test_rename():
|
2020-07-17 05:21:12 +02:00
|
|
|
col = getEmptyCol()
|
|
|
|
id = col.decks.id("hello::world")
|
2012-12-21 08:51:59 +01:00
|
|
|
# should be able to rename into a completely different branch, creating
|
|
|
|
# parents as necessary
|
2020-07-17 05:21:12 +02:00
|
|
|
col.decks.rename(col.decks.get(id), "foo::bar")
|
|
|
|
names = [n.name for n in col.decks.all_names_and_ids()]
|
2020-05-15 13:21:10 +02:00
|
|
|
assert "foo" in names
|
|
|
|
assert "foo::bar" in names
|
|
|
|
assert "hello::world" not in names
|
2020-07-17 05:21:01 +02:00
|
|
|
# create another col
|
2020-07-17 05:21:12 +02:00
|
|
|
id = col.decks.id("tmp")
|
2020-05-17 11:07:15 +02:00
|
|
|
# automatically adjusted if a duplicate name
|
2020-07-17 05:21:12 +02:00
|
|
|
col.decks.rename(col.decks.get(id), "FOO")
|
|
|
|
names = [n.name for n in col.decks.all_names_and_ids()]
|
2020-05-17 11:07:15 +02:00
|
|
|
assert "FOO+" in names
|
2012-12-21 08:51:59 +01:00
|
|
|
# when renaming, the children should be renamed too
|
2020-07-17 05:21:12 +02:00
|
|
|
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()]
|
2012-12-21 08:51:59 +01:00
|
|
|
for n in "yo", "yo::two", "yo::two::three":
|
2020-05-15 13:21:10 +02:00
|
|
|
assert n in names
|
2019-11-13 15:49:09 +01:00
|
|
|
# over filtered
|
2020-09-03 10:02:47 +02:00
|
|
|
filteredId = col.decks.new_filtered("filtered")
|
2020-07-17 05:21:12 +02:00
|
|
|
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"))
|
2019-11-13 15:49:09 +01:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def test_renameForDragAndDrop():
|
2020-07-17 05:21:12 +02:00
|
|
|
col = getEmptyCol()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def deckNames():
|
2020-07-17 05:21:12 +02:00
|
|
|
return [n.name for n in col.decks.all_names_and_ids(skip_empty_default=True)]
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2020-07-17 05:21:12 +02:00
|
|
|
languages_did = col.decks.id("Languages")
|
|
|
|
chinese_did = col.decks.id("Chinese")
|
|
|
|
hsk_did = col.decks.id("Chinese::HSK")
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
# Renaming also renames children
|
2020-07-17 05:21:12 +02:00
|
|
|
col.decks.renameForDragAndDrop(chinese_did, languages_did)
|
2019-12-25 05:18:34 +01:00
|
|
|
assert deckNames() == ["Languages", "Languages::Chinese", "Languages::Chinese::HSK"]
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2021-01-30 11:37:29 +01:00
|
|
|
# Dragging a deck onto itself is a no-op
|
2020-07-17 05:21:12 +02:00
|
|
|
col.decks.renameForDragAndDrop(languages_did, languages_did)
|
2019-12-25 05:18:34 +01:00
|
|
|
assert deckNames() == ["Languages", "Languages::Chinese", "Languages::Chinese::HSK"]
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2021-01-30 11:37:29 +01:00
|
|
|
# Dragging a deck onto its parent is a no-op
|
2020-07-17 05:21:12 +02:00
|
|
|
col.decks.renameForDragAndDrop(hsk_did, chinese_did)
|
2019-12-25 05:18:34 +01:00
|
|
|
assert deckNames() == ["Languages", "Languages::Chinese", "Languages::Chinese::HSK"]
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2021-01-30 11:37:29 +01:00
|
|
|
# Dragging a deck onto a descendant is a no-op
|
2020-07-17 05:21:12 +02:00
|
|
|
col.decks.renameForDragAndDrop(languages_did, hsk_did)
|
2019-12-25 05:18:34 +01:00
|
|
|
assert deckNames() == ["Languages", "Languages::Chinese", "Languages::Chinese::HSK"]
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
# Can drag a grandchild onto its grandparent. It becomes a child
|
2020-07-17 05:21:12 +02:00
|
|
|
col.decks.renameForDragAndDrop(hsk_did, languages_did)
|
2019-12-25 05:18:34 +01:00
|
|
|
assert deckNames() == ["Languages", "Languages::Chinese", "Languages::HSK"]
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2021-01-30 11:37:29 +01:00
|
|
|
# Can drag a deck onto its sibling
|
2020-07-17 05:21:12 +02:00
|
|
|
col.decks.renameForDragAndDrop(hsk_did, chinese_did)
|
2019-12-25 05:18:34 +01:00
|
|
|
assert deckNames() == ["Languages", "Languages::Chinese", "Languages::Chinese::HSK"]
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2021-01-30 11:37:29 +01:00
|
|
|
# Can drag a deck back to the top level
|
2020-07-17 05:21:12 +02:00
|
|
|
col.decks.renameForDragAndDrop(chinese_did, None)
|
2019-12-25 05:18:34 +01:00
|
|
|
assert deckNames() == ["Chinese", "Chinese::HSK", "Languages"]
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2020-07-17 05:21:01 +02:00
|
|
|
# Dragging a top level col to the top level is a no-op
|
2020-07-17 05:21:12 +02:00
|
|
|
col.decks.renameForDragAndDrop(chinese_did, None)
|
2019-12-25 05:18:34 +01:00
|
|
|
assert deckNames() == ["Chinese", "Chinese::HSK", "Languages"]
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2020-05-17 11:07:15 +02:00
|
|
|
# decks are renamed if necessary
|
2020-07-17 05:21:12 +02:00
|
|
|
new_hsk_did = col.decks.id("hsk")
|
|
|
|
col.decks.renameForDragAndDrop(new_hsk_did, chinese_did)
|
2020-05-17 11:07:15 +02:00
|
|
|
assert deckNames() == ["Chinese", "Chinese::HSK", "Chinese::hsk+", "Languages"]
|
2020-07-17 05:21:12 +02:00
|
|
|
col.decks.rem(new_hsk_did)
|
2019-11-13 16:29:54 +01:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
# '' is a convenient alias for the top level DID
|
2020-07-17 05:21:12 +02:00
|
|
|
col.decks.renameForDragAndDrop(hsk_did, "")
|
2019-12-25 05:18:34 +01:00
|
|
|
assert deckNames() == ["Chinese", "HSK", "Languages"]
|