2012-12-21 08:51:59 +01:00
|
|
|
# coding: utf-8
|
2020-01-02 23:52:10 +01:00
|
|
|
import pytest
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2020-02-02 10:40:30 +01:00
|
|
|
from anki.consts import *
|
2020-03-22 03:59:24 +01:00
|
|
|
from anki.rsbackend import BuiltinSortKind
|
2020-03-22 05:41:01 +01:00
|
|
|
from tests.shared import getEmptyCol, isNearCutoff
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2019-12-25 05:18:34 +01:00
|
|
|
|
2020-03-05 00:47:47 +01:00
|
|
|
class DummyCollection:
|
|
|
|
def weakref(self):
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
def test_findCards():
|
2020-07-17 05:21:01 +02:00
|
|
|
col = getEmptyCol()
|
|
|
|
note = col.newNote()
|
2020-07-17 05:18:09 +02:00
|
|
|
note["Front"] = "dog"
|
|
|
|
note["Back"] = "cat"
|
|
|
|
note.tags.append("monkey animal_1 * %")
|
2020-07-17 05:21:01 +02:00
|
|
|
col.addNote(note)
|
2020-07-17 17:30:29 +02:00
|
|
|
n1id = note.id
|
2020-07-17 05:18:09 +02:00
|
|
|
firstCardId = note.cards()[0].id
|
2020-07-17 05:21:01 +02:00
|
|
|
note = col.newNote()
|
2020-07-17 05:18:09 +02:00
|
|
|
note["Front"] = "goats are fun"
|
|
|
|
note["Back"] = "sheep"
|
|
|
|
note.tags.append("sheep goat horse animal11")
|
2020-07-17 05:21:01 +02:00
|
|
|
col.addNote(note)
|
2020-07-17 17:30:29 +02:00
|
|
|
n2id = note.id
|
2020-07-17 05:21:01 +02:00
|
|
|
note = col.newNote()
|
2020-07-17 05:18:09 +02:00
|
|
|
note["Front"] = "cat"
|
|
|
|
note["Back"] = "sheep"
|
2020-07-17 05:21:01 +02:00
|
|
|
col.addNote(note)
|
2020-07-17 05:18:09 +02:00
|
|
|
catCard = note.cards()[0]
|
2020-07-17 05:21:01 +02:00
|
|
|
m = col.models.current()
|
|
|
|
m = col.models.copy(m)
|
|
|
|
mm = col.models
|
2012-12-21 08:51:59 +01:00
|
|
|
t = mm.newTemplate("Reverse")
|
2019-12-25 05:18:34 +01:00
|
|
|
t["qfmt"] = "{{Back}}"
|
|
|
|
t["afmt"] = "{{Front}}"
|
2012-12-21 08:51:59 +01:00
|
|
|
mm.addTemplate(m, t)
|
|
|
|
mm.save(m)
|
2020-07-17 05:21:01 +02:00
|
|
|
note = col.newNote()
|
2020-07-17 05:18:09 +02:00
|
|
|
note["Front"] = "test"
|
|
|
|
note["Back"] = "foo bar"
|
2020-07-17 05:21:01 +02:00
|
|
|
col.addNote(note)
|
|
|
|
col.save()
|
2020-07-17 05:18:09 +02:00
|
|
|
latestCardIds = [c.id for c in note.cards()]
|
2012-12-21 08:51:59 +01:00
|
|
|
# tag searches
|
2020-07-17 05:21:01 +02:00
|
|
|
assert len(col.findCards("tag:*")) == 5
|
|
|
|
assert len(col.findCards("tag:\\*")) == 1
|
2020-11-15 09:39:10 +01:00
|
|
|
assert len(col.findCards("tag:%")) == 1
|
2020-11-19 09:28:19 +01:00
|
|
|
assert len(col.findCards("tag:sheep_goat")) == 0
|
|
|
|
assert len(col.findCards('"tag:sheep goat"')) == 0
|
|
|
|
assert len(col.findCards('"tag:* *"')) == 0
|
2020-07-17 05:21:01 +02:00
|
|
|
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
|
2012-12-21 08:51:59 +01:00
|
|
|
# text searches
|
2020-07-17 05:21:01 +02:00
|
|
|
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
|
2012-12-21 08:51:59 +01:00
|
|
|
# card states
|
2020-07-17 05:18:09 +02:00
|
|
|
c = note.cards()[0]
|
2020-02-02 10:40:30 +01:00
|
|
|
c.queue = c.type = CARD_TYPE_REV
|
2020-07-17 05:21:01 +02:00
|
|
|
assert col.findCards("is:review") == []
|
2012-12-21 08:51:59 +01:00
|
|
|
c.flush()
|
2020-07-17 05:21:01 +02:00
|
|
|
assert col.findCards("is:review") == [c.id]
|
|
|
|
assert col.findCards("is:due") == []
|
2019-12-25 05:18:34 +01:00
|
|
|
c.due = 0
|
2020-02-02 10:40:30 +01:00
|
|
|
c.queue = QUEUE_TYPE_REV
|
2012-12-21 08:51:59 +01:00
|
|
|
c.flush()
|
2020-07-17 05:21:01 +02:00
|
|
|
assert col.findCards("is:due") == [c.id]
|
|
|
|
assert len(col.findCards("-is:due")) == 4
|
2020-07-19 05:27:31 +02:00
|
|
|
c.queue = QUEUE_TYPE_SUSPENDED
|
2012-12-21 08:51:59 +01:00
|
|
|
# ensure this card gets a later mod time
|
|
|
|
c.flush()
|
2020-07-17 05:21:01 +02:00
|
|
|
col.db.execute("update cards set mod = mod + 1 where id = ?", c.id)
|
|
|
|
assert col.findCards("is:suspended") == [c.id]
|
2012-12-21 08:51:59 +01:00
|
|
|
# nids
|
2020-07-17 05:21:01 +02:00
|
|
|
assert col.findCards("nid:54321") == []
|
2020-07-17 05:21:12 +02:00
|
|
|
assert len(col.findCards(f"nid:{note.id}")) == 2
|
2020-07-17 17:30:29 +02:00
|
|
|
assert len(col.findCards(f"nid:{n1id},{n2id}")) == 2
|
2012-12-21 08:51:59 +01:00
|
|
|
# templates
|
2020-07-17 05:21:01 +02:00
|
|
|
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
|
2012-12-21 08:51:59 +01:00
|
|
|
# fields
|
2020-07-17 05:21:01 +02:00
|
|
|
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
|
2012-12-21 08:51:59 +01:00
|
|
|
# ordering
|
2020-07-17 05:21:01 +02:00
|
|
|
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
|
2020-03-22 03:59:24 +01:00
|
|
|
assert (
|
2020-07-17 05:21:01 +02:00
|
|
|
col.find_cards("", order=BuiltinSortKind.CARD_DUE, reverse=False)[0]
|
2020-03-22 03:59:24 +01:00
|
|
|
== firstCardId
|
|
|
|
)
|
|
|
|
assert (
|
2020-07-17 05:21:01 +02:00
|
|
|
col.find_cards("", order=BuiltinSortKind.CARD_DUE, reverse=True)[0]
|
2020-03-22 03:59:24 +01:00
|
|
|
!= firstCardId
|
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
# model
|
2020-07-17 05:21:01 +02:00
|
|
|
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
|
2012-12-21 08:51:59 +01:00
|
|
|
# full search
|
2020-07-17 05:21:01 +02:00
|
|
|
note = col.newNote()
|
2020-07-17 05:18:09 +02:00
|
|
|
note["Front"] = "hello<b>world</b>"
|
|
|
|
note["Back"] = "abc"
|
2020-07-17 05:21:01 +02:00
|
|
|
col.addNote(note)
|
2012-12-21 08:51:59 +01:00
|
|
|
# as it's the sort field, it matches
|
2020-07-17 05:21:01 +02:00
|
|
|
assert len(col.findCards("helloworld")) == 2
|
|
|
|
# assert len(col.findCards("helloworld", full=True)) == 2
|
2012-12-21 08:51:59 +01:00
|
|
|
# if we put it on the back, it won't
|
2020-07-17 05:18:09 +02:00
|
|
|
(note["Front"], note["Back"]) = (note["Back"], note["Front"])
|
|
|
|
note.flush()
|
2020-07-17 05:21:01 +02:00
|
|
|
assert len(col.findCards("helloworld")) == 0
|
|
|
|
# assert len(col.findCards("helloworld", full=True)) == 2
|
|
|
|
# assert len(col.findCards("back:helloworld", full=True)) == 2
|
2012-12-21 08:51:59 +01:00
|
|
|
# searching for an invalid special tag should not error
|
2020-01-02 23:52:10 +01:00
|
|
|
with pytest.raises(Exception):
|
2020-07-17 05:21:01 +02:00
|
|
|
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
|
2019-12-25 05:18:34 +01:00
|
|
|
)
|
2020-07-17 05:21:01 +02:00
|
|
|
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
|
2012-12-21 08:51:59 +01:00
|
|
|
# properties
|
2020-07-17 05:21:01 +02:00
|
|
|
id = col.db.scalar("select id from cards limit 1")
|
|
|
|
col.db.execute(
|
2012-12-21 08:51:59 +01:00
|
|
|
"update cards set queue=2, ivl=10, reps=20, due=30, factor=2200 "
|
2019-12-25 05:18:34 +01:00
|
|
|
"where id = ?",
|
|
|
|
id,
|
|
|
|
)
|
2020-07-17 05:21:01 +02:00
|
|
|
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
|
2012-12-21 08:51:59 +01:00
|
|
|
# due dates should work
|
2020-07-17 05:21:01 +02:00
|
|
|
assert len(col.findCards("prop:due=29")) == 0
|
|
|
|
assert len(col.findCards("prop:due=30")) == 1
|
2012-12-21 08:51:59 +01:00
|
|
|
# ease factors
|
2020-07-17 05:21:01 +02:00
|
|
|
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
|
2012-12-21 08:51:59 +01:00
|
|
|
# recently failed
|
2020-03-22 05:41:01 +01:00
|
|
|
if not isNearCutoff():
|
2021-01-10 16:25:52 +01:00
|
|
|
# rated
|
2020-07-17 05:21:01 +02:00
|
|
|
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:2:2")) == 1
|
2021-01-10 16:25:52 +01:00
|
|
|
assert len(col.findCards("rated:0")) == len(col.findCards("rated:1"))
|
|
|
|
|
2020-03-22 05:41:01 +01:00
|
|
|
# added
|
2020-07-17 05:21:01 +02:00
|
|
|
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()
|
2021-01-10 16:25:52 +01:00
|
|
|
assert len(col.findCards("added:0")) == len(col.findCards("added:1"))
|
2020-03-22 05:41:01 +01:00
|
|
|
else:
|
|
|
|
print("some find tests disabled near cutoff")
|
2012-12-21 08:51:59 +01:00
|
|
|
# empty field
|
2020-07-17 05:21:01 +02:00
|
|
|
assert len(col.findCards("front:")) == 0
|
|
|
|
note = col.newNote()
|
2020-07-17 05:18:09 +02:00
|
|
|
note["Front"] = ""
|
|
|
|
note["Back"] = "abc2"
|
2020-07-17 05:21:01 +02:00
|
|
|
assert col.addNote(note) == 1
|
|
|
|
assert len(col.findCards("front:")) == 1
|
2012-12-21 08:51:59 +01:00
|
|
|
# OR searches and nesting
|
2020-07-17 05:21:01 +02:00
|
|
|
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
|
2019-11-13 17:41:34 +01:00
|
|
|
# flag
|
2020-01-02 23:52:10 +01:00
|
|
|
with pytest.raises(Exception):
|
2020-07-17 05:21:01 +02:00
|
|
|
col.findCards("flag:12")
|
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_findReplace():
|
2020-07-17 05:21:01 +02:00
|
|
|
col = getEmptyCol()
|
|
|
|
note = col.newNote()
|
2020-07-17 05:18:09 +02:00
|
|
|
note["Front"] = "foo"
|
|
|
|
note["Back"] = "bar"
|
2020-07-17 05:21:01 +02:00
|
|
|
col.addNote(note)
|
|
|
|
note2 = col.newNote()
|
2020-07-17 05:18:35 +02:00
|
|
|
note2["Front"] = "baz"
|
|
|
|
note2["Back"] = "foo"
|
2020-07-17 05:21:01 +02:00
|
|
|
col.addNote(note2)
|
2020-07-17 05:18:35 +02:00
|
|
|
nids = [note.id, note2.id]
|
2012-12-21 08:51:59 +01:00
|
|
|
# should do nothing
|
2020-07-17 05:21:01 +02:00
|
|
|
assert col.findReplace(nids, "abc", "123") == 0
|
2012-12-21 08:51:59 +01:00
|
|
|
# global replace
|
2020-07-17 05:21:01 +02:00
|
|
|
assert col.findReplace(nids, "foo", "qux") == 2
|
2020-07-17 05:18:09 +02:00
|
|
|
note.load()
|
|
|
|
assert note["Front"] == "qux"
|
2020-07-17 05:18:35 +02:00
|
|
|
note2.load()
|
|
|
|
assert note2["Back"] == "qux"
|
2012-12-21 08:51:59 +01:00
|
|
|
# single field replace
|
2020-07-17 05:21:01 +02:00
|
|
|
assert col.findReplace(nids, "qux", "foo", field="Front") == 1
|
2020-07-17 05:18:09 +02:00
|
|
|
note.load()
|
|
|
|
assert note["Front"] == "foo"
|
2020-07-17 05:18:35 +02:00
|
|
|
note2.load()
|
|
|
|
assert note2["Back"] == "qux"
|
2012-12-21 08:51:59 +01:00
|
|
|
# regex replace
|
2020-07-17 05:21:01 +02:00
|
|
|
assert col.findReplace(nids, "B.r", "reg") == 0
|
2020-07-17 05:18:09 +02:00
|
|
|
note.load()
|
|
|
|
assert note["Back"] != "reg"
|
2020-07-17 05:21:01 +02:00
|
|
|
assert col.findReplace(nids, "B.r", "reg", regex=True) == 1
|
2020-07-17 05:18:09 +02:00
|
|
|
note.load()
|
|
|
|
assert note["Back"] == "reg"
|
2019-12-25 05:18:34 +01:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def test_findDupes():
|
2020-07-17 05:21:01 +02:00
|
|
|
col = getEmptyCol()
|
|
|
|
note = col.newNote()
|
2020-07-17 05:18:09 +02:00
|
|
|
note["Front"] = "foo"
|
|
|
|
note["Back"] = "bar"
|
2020-07-17 05:21:01 +02:00
|
|
|
col.addNote(note)
|
|
|
|
note2 = col.newNote()
|
2020-07-17 05:18:35 +02:00
|
|
|
note2["Front"] = "baz"
|
|
|
|
note2["Back"] = "bar"
|
2020-07-17 05:21:01 +02:00
|
|
|
col.addNote(note2)
|
2020-07-17 17:33:58 +02:00
|
|
|
note3 = col.newNote()
|
|
|
|
note3["Front"] = "quux"
|
|
|
|
note3["Back"] = "bar"
|
|
|
|
col.addNote(note3)
|
2020-07-17 17:34:39 +02:00
|
|
|
note4 = col.newNote()
|
|
|
|
note4["Front"] = "quuux"
|
|
|
|
note4["Back"] = "nope"
|
|
|
|
col.addNote(note4)
|
2020-07-17 05:21:01 +02:00
|
|
|
r = col.findDupes("Back")
|
2012-12-21 08:51:59 +01:00
|
|
|
assert r[0][0] == "bar"
|
|
|
|
assert len(r[0][1]) == 3
|
|
|
|
# valid search
|
2020-07-17 05:21:01 +02:00
|
|
|
r = col.findDupes("Back", "bar")
|
2012-12-21 08:51:59 +01:00
|
|
|
assert r[0][0] == "bar"
|
|
|
|
assert len(r[0][1]) == 3
|
|
|
|
# excludes everything
|
2020-07-17 05:21:01 +02:00
|
|
|
r = col.findDupes("Back", "invalid")
|
2012-12-21 08:51:59 +01:00
|
|
|
assert not r
|
|
|
|
# front isn't dupe
|
2020-07-17 05:21:01 +02:00
|
|
|
assert col.findDupes("Front") == []
|