anki/pylib/tests/test_schedv3.py

1181 lines
33 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
import copy
import os
import time
Move away from Bazel (#2202) (for upgrading users, please see the notes at the bottom) Bazel brought a lot of nice things to the table, such as rebuilds based on content changes instead of modification times, caching of build products, detection of incorrect build rules via a sandbox, and so on. Rewriting the build in Bazel was also an opportunity to improve on the Makefile-based build we had prior, which was pretty poor: most dependencies were external or not pinned, and the build graph was poorly defined and mostly serialized. It was not uncommon for fresh checkouts to fail due to floating dependencies, or for things to break when trying to switch to an older commit. For day-to-day development, I think Bazel served us reasonably well - we could generally switch between branches while being confident that builds would be correct and reasonably fast, and not require full rebuilds (except on Windows, where the lack of a sandbox and the TS rules would cause build breakages when TS files were renamed/removed). Bazel achieves that reliability by defining rules for each programming language that define how source files should be turned into outputs. For the rules to work with Bazel's sandboxing approach, they often have to reimplement or partially bypass the standard tools that each programming language provides. The Rust rules call Rust's compiler directly for example, instead of using Cargo, and the Python rules extract each PyPi package into a separate folder that gets added to sys.path. These separate language rules allow proper declaration of inputs and outputs, and offer some advantages such as caching of build products and fine-grained dependency installation. But they also bring some downsides: - The rules don't always support use-cases/platforms that the standard language tools do, meaning they need to be patched to be used. I've had to contribute a number of patches to the Rust, Python and JS rules to unblock various issues. - The dependencies we use with each language sometimes make assumptions that do not hold in Bazel, meaning they either need to be pinned or patched, or the language rules need to be adjusted to accommodate them. I was hopeful that after the initial setup work, things would be relatively smooth-sailing. Unfortunately, that has not proved to be the case. Things frequently broke when dependencies or the language rules were updated, and I began to get frustrated at the amount of Anki development time I was instead spending on build system upkeep. It's now about 2 years since switching to Bazel, and I think it's time to cut losses, and switch to something else that's a better fit. The new build system is based on a small build tool called Ninja, and some custom Rust code in build/. This means that to build Anki, Bazel is no longer required, but Ninja and Rust need to be installed on your system. Python and Node toolchains are automatically downloaded like in Bazel. This new build system should result in faster builds in some cases: - Because we're using cargo to build now, Rust builds are able to take advantage of pipelining and incremental debug builds, which we didn't have with Bazel. It's also easier to override the default linker on Linux/macOS, which can further improve speeds. - External Rust crates are now built with opt=1, which improves performance of debug builds. - Esbuild is now used to transpile TypeScript, instead of invoking the TypeScript compiler. This results in faster builds, by deferring typechecking to test/check time, and by allowing more work to happen in parallel. As an example of the differences, when testing with the mold linker on Linux, adding a new message to tags.proto (which triggers a recompile of the bulk of the Rust and TypeScript code) results in a compile that goes from about 22s on Bazel to about 7s in the new system. With the standard linker, it's about 9s. Some other changes of note: - Our Rust workspace now uses cargo-hakari to ensure all packages agree on available features, preventing unnecessary rebuilds. - pylib/anki is now a PEP420 implicit namespace, avoiding the need to merge source files and generated files into a single folder for running. By telling VSCode about the extra search path, code completion now works with generated files without needing to symlink them into the source folder. - qt/aqt can't use PEP420 as it's difficult to get rid of aqt/__init__.py. Instead, the generated files are now placed in a separate _aqt package that's added to the path. - ts/lib is now exposed as @tslib, so the source code and generated code can be provided under the same namespace without a merging step. - MyPy and PyLint are now invoked once for the entire codebase. - dprint will be used to format TypeScript/json files in the future instead of the slower prettier (currently turned off to avoid causing conflicts). It can automatically defer to prettier when formatting Svelte files. - svelte-check is now used for typechecking our Svelte code, which revealed a few typing issues that went undetected with the old system. - The Jest unit tests now work on Windows as well. If you're upgrading from Bazel, updated usage instructions are in docs/development.md and docs/build.md. A summary of the changes: - please remove node_modules and .bazel - install rustup (https://rustup.rs/) - install rsync if not already installed (on windows, use pacman - see docs/windows.md) - install Ninja (unzip from https://github.com/ninja-build/ninja/releases/tag/v1.11.1 and place on your path, or from your distro/homebrew if it's 1.10+) - update .vscode/settings.json from .vscode.dist
2022-11-27 06:24:20 +01:00
from typing import Callable, Dict
import pytest
from anki import hooks
2020-02-02 10:19:45 +01:00
from anki.consts import *
2020-02-20 03:48:46 +01:00
from anki.lang import without_unicode_isolation
from anki.scheduler import UnburyDeck
PEP8 for rest of pylib (#1451) * PEP8 dbproxy.py * PEP8 errors.py * PEP8 httpclient.py * PEP8 lang.py * PEP8 latex.py * Add decorator to deprectate key words * Make replacement for deprecated attribute optional * Use new helper `_print_replacement_warning()` * PEP8 media.py * PEP8 rsbackend.py * PEP8 sound.py * PEP8 stdmodels.py * PEP8 storage.py * PEP8 sync.py * PEP8 tags.py * PEP8 template.py * PEP8 types.py * Fix DeprecatedNamesMixinForModule The class methods need to be overridden with instance methods, so every module has its own dicts. * Use `# pylint: disable=invalid-name` instead of id * PEP8 utils.py * Only decorate `__getattr__` with `@no_type_check` * Fix mypy issue with snakecase Importing it from `anki._vendor` raises attribute errors. * Format * Remove inheritance of DeprecatedNamesMixin There's almost no shared code now and overriding classmethods with instance methods raises mypy issues. * Fix traceback frames of deprecation warnings * remove fn/TimedLog (dae) Neither Anki nor add-ons appear to have been using it * fix some issues with stringcase use (dae) - the wheel was depending on the PyPI version instead of our vendored version - _vendor:stringcase should not have been listed in the anki py_library. We already include the sources in py_srcs, and need to refer to them directly. By listing _vendor:stringcase as well, we were making a top-level stringcase library available, which would have only worked for distributing because the wheel definition was also incorrect. - mypy errors are what caused me to mistakenly add the above - they were because the type: ignore at the top of stringcase.py was causing mypy to completely ignore the file, so it was not aware of any attributes it contained.
2021-10-25 06:50:13 +02:00
from anki.utils import int_time
from tests.shared import getEmptyCol as getEmptyColOrig
def getEmptyCol():
col = getEmptyColOrig()
return col
2013-10-01 21:04:06 +02:00
def test_clock():
col = getEmptyCol()
PEP8 for rest of pylib (#1451) * PEP8 dbproxy.py * PEP8 errors.py * PEP8 httpclient.py * PEP8 lang.py * PEP8 latex.py * Add decorator to deprectate key words * Make replacement for deprecated attribute optional * Use new helper `_print_replacement_warning()` * PEP8 media.py * PEP8 rsbackend.py * PEP8 sound.py * PEP8 stdmodels.py * PEP8 storage.py * PEP8 sync.py * PEP8 tags.py * PEP8 template.py * PEP8 types.py * Fix DeprecatedNamesMixinForModule The class methods need to be overridden with instance methods, so every module has its own dicts. * Use `# pylint: disable=invalid-name` instead of id * PEP8 utils.py * Only decorate `__getattr__` with `@no_type_check` * Fix mypy issue with snakecase Importing it from `anki._vendor` raises attribute errors. * Format * Remove inheritance of DeprecatedNamesMixin There's almost no shared code now and overriding classmethods with instance methods raises mypy issues. * Fix traceback frames of deprecation warnings * remove fn/TimedLog (dae) Neither Anki nor add-ons appear to have been using it * fix some issues with stringcase use (dae) - the wheel was depending on the PyPI version instead of our vendored version - _vendor:stringcase should not have been listed in the anki py_library. We already include the sources in py_srcs, and need to refer to them directly. By listing _vendor:stringcase as well, we were making a top-level stringcase library available, which would have only worked for distributing because the wheel definition was also incorrect. - mypy errors are what caused me to mistakenly add the above - they were because the type: ignore at the top of stringcase.py was causing mypy to completely ignore the file, so it was not aware of any attributes it contained.
2021-10-25 06:50:13 +02:00
if (col.sched.day_cutoff - int_time()) < 10 * 60:
2013-10-01 21:04:06 +02:00
raise Exception("Unit tests will fail around the day rollover.")
2019-12-25 05:18:34 +01:00
def test_basics():
col = getEmptyCol()
assert not col.sched.getCard()
2019-12-25 05:18:34 +01:00
def test_new():
col = getEmptyCol()
assert col.sched.newCount == 0
# add a note
note = col.newNote()
note["Front"] = "one"
note["Back"] = "two"
col.addNote(note)
assert col.sched.newCount == 1
# fetch it
c = col.sched.getCard()
assert c
2020-02-02 10:19:45 +01:00
assert c.queue == QUEUE_TYPE_NEW
assert c.type == CARD_TYPE_NEW
# if we answer it, it should become a learn card
PEP8 for rest of pylib (#1451) * PEP8 dbproxy.py * PEP8 errors.py * PEP8 httpclient.py * PEP8 lang.py * PEP8 latex.py * Add decorator to deprectate key words * Make replacement for deprecated attribute optional * Use new helper `_print_replacement_warning()` * PEP8 media.py * PEP8 rsbackend.py * PEP8 sound.py * PEP8 stdmodels.py * PEP8 storage.py * PEP8 sync.py * PEP8 tags.py * PEP8 template.py * PEP8 types.py * Fix DeprecatedNamesMixinForModule The class methods need to be overridden with instance methods, so every module has its own dicts. * Use `# pylint: disable=invalid-name` instead of id * PEP8 utils.py * Only decorate `__getattr__` with `@no_type_check` * Fix mypy issue with snakecase Importing it from `anki._vendor` raises attribute errors. * Format * Remove inheritance of DeprecatedNamesMixin There's almost no shared code now and overriding classmethods with instance methods raises mypy issues. * Fix traceback frames of deprecation warnings * remove fn/TimedLog (dae) Neither Anki nor add-ons appear to have been using it * fix some issues with stringcase use (dae) - the wheel was depending on the PyPI version instead of our vendored version - _vendor:stringcase should not have been listed in the anki py_library. We already include the sources in py_srcs, and need to refer to them directly. By listing _vendor:stringcase as well, we were making a top-level stringcase library available, which would have only worked for distributing because the wheel definition was also incorrect. - mypy errors are what caused me to mistakenly add the above - they were because the type: ignore at the top of stringcase.py was causing mypy to completely ignore the file, so it was not aware of any attributes it contained.
2021-10-25 06:50:13 +02:00
t = int_time()
col.sched.answerCard(c, 1)
2020-02-02 10:28:04 +01:00
assert c.queue == QUEUE_TYPE_LRN
assert c.type == CARD_TYPE_LRN
assert c.due >= t
2014-06-03 11:17:02 +02:00
# disabled for now, as the learn fudging makes this randomly fail
# # the default order should ensure siblings are not seen together, and
# # should show all cards
# m = col.models.current(); mm = col.models
2021-06-27 05:49:58 +02:00
# t = mm.new_template("Reverse")
2014-06-03 11:17:02 +02:00
# t['qfmt'] = "{{Back}}"
# t['afmt'] = "{{Front}}"
2021-06-27 05:49:58 +02:00
# mm.add_template(m, t)
2014-06-03 11:17:02 +02:00
# mm.save(m)
# note = col.newNote()
# note['Front'] = u"2"; note['Back'] = u"2"
# col.addNote(note)
# note = col.newNote()
# note['Front'] = u"3"; note['Back'] = u"3"
# col.addNote(note)
# col.reset()
2014-06-03 11:17:02 +02:00
# qs = ("2", "3", "2", "3")
# for n in range(4):
# c = col.sched.getCard()
2021-06-27 04:12:23 +02:00
# assert qs[n] in c.question()
# col.sched.answerCard(c, 2)
2019-12-25 05:18:34 +01:00
def test_newLimits():
col = getEmptyCol()
# add some notes
deck2 = col.decks.id("Default::foo")
for i in range(30):
note = col.newNote()
note["Front"] = str(i)
if i > 4:
2021-06-27 04:12:23 +02:00
note.note_type()["did"] = deck2
col.addNote(note)
# give the child deck a different configuration
c2 = col.decks.add_config_returning_id("new conf")
2021-06-27 06:02:48 +02:00
col.decks.set_config_id_for_deck_dict(col.decks.get(deck2), c2)
# both confs have defaulted to a limit of 20
assert col.sched.newCount == 20
# first card we get comes from parent
c = col.sched.getCard()
assert c.did == 1
# limit the parent to 10 cards, meaning we get 10 in total
2021-06-27 06:02:48 +02:00
conf1 = col.decks.config_dict_for_deck_id(1)
2019-12-25 05:18:34 +01:00
conf1["new"]["perDay"] = 10
col.decks.save(conf1)
assert col.sched.newCount == 10
# if we limit child to 4, we should get 9
2021-06-27 06:02:48 +02:00
conf2 = col.decks.config_dict_for_deck_id(deck2)
2019-12-25 05:18:34 +01:00
conf2["new"]["perDay"] = 4
col.decks.save(conf2)
assert col.sched.newCount == 9
2019-12-25 05:18:34 +01:00
def test_newBoxes():
col = getEmptyCol()
note = col.newNote()
note["Front"] = "one"
col.addNote(note)
c = col.sched.getCard()
conf = col.sched._cardConf(c)
conf["new"]["delays"] = [1, 2, 3, 4, 5]
col.decks.save(conf)
col.sched.answerCard(c, 2)
# should handle gracefully
conf["new"]["delays"] = [1]
col.decks.save(conf)
col.sched.answerCard(c, 2)
2019-12-25 05:18:34 +01:00
def test_learn():
col = getEmptyCol()
# add a note
note = col.newNote()
note["Front"] = "one"
note["Back"] = "two"
col.addNote(note)
# set as a new card and rebuild queues
col.db.execute(f"update cards set queue={QUEUE_TYPE_NEW}, type={CARD_TYPE_NEW}")
# sched.getCard should return it, since it's due in the past
c = col.sched.getCard()
assert c
conf = col.sched._cardConf(c)
conf["new"]["delays"] = [0.5, 3, 10]
col.decks.save(conf)
# fail it
col.sched.answerCard(c, 1)
# it should have three reps left to graduation
2019-12-25 05:18:34 +01:00
assert c.left % 1000 == 3
2020-07-19 11:24:58 +02:00
# it should be due in 30 seconds
t = round(c.due - time.time())
assert t >= 25 and t <= 40
# pass it once
col.sched.answerCard(c, 3)
2020-07-19 11:24:58 +02:00
# it should be due in 3 minutes
dueIn = c.due - time.time()
assert 178 <= dueIn <= 180 * 1.25
2019-12-25 05:18:34 +01:00
assert c.left % 1000 == 2
# check log is accurate
log = col.db.first("select * from revlog order by id desc")
assert log[3] == 3
assert log[4] == -180
assert log[5] == -30
# pass again
col.sched.answerCard(c, 3)
2020-07-19 11:24:58 +02:00
# it should be due in 10 minutes
dueIn = c.due - time.time()
assert 598 <= dueIn <= 600 * 1.25
2019-12-25 05:18:34 +01:00
assert c.left % 1000 == 1
# the next pass should graduate the card
2020-02-02 10:28:04 +01:00
assert c.queue == QUEUE_TYPE_LRN
assert c.type == CARD_TYPE_LRN
col.sched.answerCard(c, 3)
2020-02-02 10:40:30 +01:00
assert c.queue == QUEUE_TYPE_REV
assert c.type == CARD_TYPE_REV
# should be due tomorrow, with an interval of 1
assert c.due == col.sched.today + 1
assert c.ivl == 1
# or normal removal
2020-07-19 05:27:31 +02:00
c.type = CARD_TYPE_NEW
c.queue = QUEUE_TYPE_LRN
c.flush()
col.sched.answerCard(c, 4)
2020-02-02 10:40:30 +01:00
assert c.type == CARD_TYPE_REV
assert c.queue == QUEUE_TYPE_REV
# revlog should have been updated each time
assert col.db.scalar("select count() from revlog where type = 0") == 5
2019-12-25 05:18:34 +01:00
def test_relearn():
col = getEmptyCol()
note = col.newNote()
note["Front"] = "one"
col.addNote(note)
c = note.cards()[0]
c.ivl = 100
c.due = col.sched.today
2020-02-02 10:40:30 +01:00
c.queue = CARD_TYPE_REV
c.type = QUEUE_TYPE_REV
c.flush()
# fail the card
c = col.sched.getCard()
col.sched.answerCard(c, 1)
2020-02-02 10:28:04 +01:00
assert c.queue == QUEUE_TYPE_LRN
2020-01-31 05:38:39 +01:00
assert c.type == CARD_TYPE_RELEARNING
assert c.ivl == 1
# immediately graduate it
col.sched.answerCard(c, 4)
2020-02-02 10:40:30 +01:00
assert c.queue == CARD_TYPE_REV and c.type == QUEUE_TYPE_REV
2019-12-09 04:00:15 +01:00
assert c.ivl == 2
assert c.due == col.sched.today + c.ivl
2019-12-25 05:18:34 +01:00
2018-02-02 05:30:53 +01:00
def test_relearn_no_steps():
col = getEmptyCol()
note = col.newNote()
note["Front"] = "one"
col.addNote(note)
c = note.cards()[0]
2018-02-02 05:30:53 +01:00
c.ivl = 100
c.due = col.sched.today
2020-02-02 10:40:30 +01:00
c.queue = CARD_TYPE_REV
c.type = QUEUE_TYPE_REV
2018-02-02 05:30:53 +01:00
c.flush()
2021-06-27 06:02:48 +02:00
conf = col.decks.config_dict_for_deck_id(1)
2019-12-25 05:18:34 +01:00
conf["lapse"]["delays"] = []
col.decks.save(conf)
2018-02-02 05:30:53 +01:00
# fail the card
c = col.sched.getCard()
col.sched.answerCard(c, 1)
2020-02-02 10:40:30 +01:00
assert c.queue == CARD_TYPE_REV and c.type == QUEUE_TYPE_REV
2018-02-02 05:30:53 +01:00
2019-12-25 05:18:34 +01:00
def test_learn_collapsed():
col = getEmptyCol()
# add 2 notes
note = col.newNote()
note["Front"] = "1"
col.addNote(note)
note = col.newNote()
note["Front"] = "2"
col.addNote(note)
# set as a new card and rebuild queues
col.db.execute(f"update cards set queue={QUEUE_TYPE_NEW}, type={CARD_TYPE_NEW}")
# should get '1' first
c = col.sched.getCard()
2021-06-27 04:12:23 +02:00
assert c.question().endswith("1")
# pass it so it's due in 10 minutes
col.sched.answerCard(c, 3)
# get the other card
c = col.sched.getCard()
2021-06-27 04:12:23 +02:00
assert c.question().endswith("2")
# fail it so it's due in 1 minute
col.sched.answerCard(c, 1)
# we shouldn't get the same card again
c = col.sched.getCard()
2021-06-27 04:12:23 +02:00
assert not c.question().endswith("2")
2019-12-25 05:18:34 +01:00
def test_learn_day():
col = getEmptyCol()
# add a note
note = col.newNote()
note["Front"] = "one"
col.addNote(note)
note = col.newNote()
note["Front"] = "two"
col.addNote(note)
c = col.sched.getCard()
conf = col.sched._cardConf(c)
conf["new"]["delays"] = [1, 10, 1440, 2880]
col.decks.save(conf)
# pass it
col.sched.answerCard(c, 3)
# two reps to graduate, 1 more today
2019-12-25 05:18:34 +01:00
assert c.left % 1000 == 3
assert col.sched.counts() == (1, 1, 0)
c.load()
ni = col.sched.nextIvl
assert ni(c, 3) == 86400
# answer the other dummy card
col.sched.answerCard(col.sched.getCard(), 4)
# answering the first one will place it in queue 3
c = col.sched.getCard()
col.sched.answerCard(c, 3)
assert c.due == col.sched.today + 1
2020-01-31 05:38:39 +01:00
assert c.queue == QUEUE_TYPE_DAY_LEARN_RELEARN
assert not col.sched.getCard()
# for testing, move it back a day
c.due -= 1
c.flush()
revert some interday learning changes in v3 Interday learning cards are now counted in the learning count again, and are no longer subject to the daily review limit. The thinking behind the original change was that interday learning cards are scheduled more like reviews, and counting them in the review count would allow the learning count to focus on intraday learning - the red number reflecting the fact that they are the most fragile memories. And counting them together made it practical to apply the review limit to both at once. Since the release, there have been a number of users expecting to see interday learning cards included in the learning count (the latest being https://forums.ankiweb.net/t/feedback-and-a-feature-adjustment-request-for-2-1-45/12308), and a good argument can be made for that too - they are, after all, listed in the learning steps, and do tend to be harder than reviews. Short of introducing another count to keep track of interday and intraday learning separately, moving back to the old behaviour seems like the best move. This also means it is not really practical to apply the review limit to interday learning cards anymore, as the limit would be split between two different numbers, and how much each number is capped would depend on the order cards are introduced. The scheduler could figure this out, but the deck list code does not know card order, and would need significant changes to be able to produce numbers that matched the scheduler. And even if we ignore implementation complexities, I think it would be more difficult for users to reason about - the influence of the review limit on new cards is confusing enough as it is.
2021-08-19 08:40:12 +02:00
assert col.sched.counts() == (0, 1, 0)
c = col.sched.getCard()
# nextIvl should work
2019-12-25 05:18:34 +01:00
assert ni(c, 3) == 86400 * 2
# if we fail it, it should be back in the correct queue
col.sched.answerCard(c, 1)
2020-02-02 10:28:04 +01:00
assert c.queue == QUEUE_TYPE_LRN
2023-09-24 06:19:25 +02:00
col.undo()
c = col.sched.getCard()
col.sched.answerCard(c, 3)
# simulate the passing of another two days
c.due -= 2
c.flush()
# the last pass should graduate it into a review card
assert ni(c, 3) == 86400
col.sched.answerCard(c, 3)
2020-02-02 10:40:30 +01:00
assert c.queue == CARD_TYPE_REV and c.type == QUEUE_TYPE_REV
# if the lapse step is tomorrow, failing it should handle the counts
# correctly
c.due = 0
c.flush()
assert col.sched.counts() == (0, 0, 1)
conf = col.sched._cardConf(c)
conf["lapse"]["delays"] = [1440]
col.decks.save(conf)
c = col.sched.getCard()
col.sched.answerCard(c, 1)
2020-01-31 05:38:39 +01:00
assert c.queue == QUEUE_TYPE_DAY_LEARN_RELEARN
assert col.sched.counts() == (0, 0, 0)
2019-12-25 05:18:34 +01:00
def test_reviews():
col = getEmptyCol()
# add a note
note = col.newNote()
note["Front"] = "one"
note["Back"] = "two"
col.addNote(note)
# set the card up as a review card, due 8 days ago
c = note.cards()[0]
2020-02-02 10:40:30 +01:00
c.type = CARD_TYPE_REV
c.queue = QUEUE_TYPE_REV
c.due = col.sched.today - 8
2017-02-08 08:28:05 +01:00
c.factor = STARTING_FACTOR
c.reps = 3
c.lapses = 1
c.ivl = 100
2021-06-27 04:12:23 +02:00
c.start_timer()
c.flush()
# save it for later use as well
cardcopy = copy.copy(c)
# try with an ease of 2
##################################################
c = copy.copy(cardcopy)
c.flush()
col.sched.answerCard(c, 2)
2020-02-02 10:40:30 +01:00
assert c.queue == QUEUE_TYPE_REV
# the new interval should be (100) * 1.2 = 120
assert c.due == col.sched.today + c.ivl
# factor should have been decremented
assert c.factor == 2350
# check counters
assert c.lapses == 1
assert c.reps == 4
# ease 3
##################################################
c = copy.copy(cardcopy)
c.flush()
col.sched.answerCard(c, 3)
# the new interval should be (100 + 8/2) * 2.5 = 260
assert c.due == col.sched.today + c.ivl
# factor should have been left alone
2017-02-08 08:28:05 +01:00
assert c.factor == STARTING_FACTOR
# ease 4
##################################################
c = copy.copy(cardcopy)
c.flush()
col.sched.answerCard(c, 4)
# the new interval should be (100 + 8) * 2.5 * 1.3 = 351
assert c.due == col.sched.today + c.ivl
# factor should have been increased
assert c.factor == 2650
# leech handling
##################################################
2021-06-27 06:02:48 +02:00
conf = col.decks.get_config(1)
2020-06-09 06:11:46 +02:00
conf["lapse"]["leechAction"] = LEECH_SUSPEND
col.decks.save(conf)
c = copy.copy(cardcopy)
c.lapses = 7
c.flush()
2019-12-25 05:18:34 +01:00
col.sched.answerCard(c, 1)
2020-01-31 04:48:14 +01:00
assert c.queue == QUEUE_TYPE_SUSPENDED
c.load()
2020-01-31 04:48:14 +01:00
assert c.queue == QUEUE_TYPE_SUSPENDED
assert "leech" in c.note().tags
2019-12-25 05:18:34 +01:00
def review_limits_setup() -> tuple[anki.collection.Collection, Dict]:
col = getEmptyCol()
parent = col.decks.get(col.decks.id("parent"))
child = col.decks.get(col.decks.id("parent::child"))
pconf = col.decks.get_config(col.decks.add_config_returning_id("parentConf"))
cconf = col.decks.get_config(col.decks.add_config_returning_id("childConf"))
2019-12-25 05:18:34 +01:00
pconf["rev"]["perDay"] = 5
col.decks.update_config(pconf)
2021-06-27 06:02:48 +02:00
col.decks.set_config_id_for_deck_dict(parent, pconf["id"])
2019-12-25 05:18:34 +01:00
cconf["rev"]["perDay"] = 10
col.decks.update_config(cconf)
2021-06-27 06:02:48 +02:00
col.decks.set_config_id_for_deck_dict(child, cconf["id"])
m = col.models.current()
2019-12-25 05:18:34 +01:00
m["did"] = child["id"]
col.models.save(m, updateReqs=False)
# add some cards
for i in range(20):
note = col.newNote()
note["Front"] = "one"
note["Back"] = "two"
col.addNote(note)
# make them reviews
c = note.cards()[0]
2020-02-02 10:40:30 +01:00
c.queue = CARD_TYPE_REV
c.type = QUEUE_TYPE_REV
c.due = 0
c.flush()
return col, child
def test_review_limits():
col, child = review_limits_setup()
tree = col.sched.deck_due_tree().children
# (('parent', 1514457677462, 5, 0, 0, (('child', 1514457677463, 5, 0, 0, ()),)))
assert tree[0].review_count == 5 # parent
assert tree[0].children[0].review_count == 10 # child
# .counts() should match
col.decks.select(child["id"])
col.sched.reset()
assert col.sched.counts() == (0, 0, 10)
# answering a card in the child should decrement parent count
c = col.sched.getCard()
col.sched.answerCard(c, 3)
assert col.sched.counts() == (0, 0, 9)
tree = col.sched.deck_due_tree().children
assert tree[0].review_count == 4 # parent
assert tree[0].children[0].review_count == 9 # child
def test_button_spacing():
col = getEmptyCol()
note = col.newNote()
note["Front"] = "one"
col.addNote(note)
# 1 day ivl review card due now
c = note.cards()[0]
2020-02-02 10:40:30 +01:00
c.type = CARD_TYPE_REV
c.queue = QUEUE_TYPE_REV
c.due = col.sched.today
c.reps = 1
c.ivl = 1
2021-06-27 04:12:23 +02:00
c.start_timer()
c.flush()
ni = col.sched.nextIvlStr
2020-02-20 03:48:46 +01:00
wo = without_unicode_isolation
assert wo(ni(c, 2)) == "2d"
assert wo(ni(c, 3)) == "3d"
assert wo(ni(c, 4)) == "4d"
2018-07-11 13:23:22 +02:00
# if hard factor is <= 1, then hard may not increase
2021-06-27 06:02:48 +02:00
conf = col.decks.config_dict_for_deck_id(1)
2019-12-25 05:18:34 +01:00
conf["rev"]["hardFactor"] = 1
col.decks.save(conf)
2020-02-20 03:48:46 +01:00
assert wo(ni(c, 2)) == "1d"
2018-07-11 13:23:22 +02:00
2019-12-25 05:18:34 +01:00
def test_nextIvl():
col = getEmptyCol()
note = col.newNote()
note["Front"] = "one"
note["Back"] = "two"
col.addNote(note)
2021-06-27 06:02:48 +02:00
conf = col.decks.config_dict_for_deck_id(1)
2019-12-25 05:18:34 +01:00
conf["new"]["delays"] = [0.5, 3, 10]
conf["lapse"]["delays"] = [1, 5, 9]
col.decks.save(conf)
c = col.sched.getCard()
# new cards
##################################################
ni = col.sched.nextIvl
assert ni(c, 1) == 30
2019-12-25 05:18:34 +01:00
assert ni(c, 2) == (30 + 180) // 2
assert ni(c, 3) == 180
2019-12-25 05:18:34 +01:00
assert ni(c, 4) == 4 * 86400
col.sched.answerCard(c, 1)
# cards in learning
##################################################
assert ni(c, 1) == 30
2019-12-25 05:18:34 +01:00
assert ni(c, 2) == (30 + 180) // 2
assert ni(c, 3) == 180
2019-12-25 05:18:34 +01:00
assert ni(c, 4) == 4 * 86400
col.sched.answerCard(c, 3)
assert ni(c, 1) == 30
assert ni(c, 2) == 180
assert ni(c, 3) == 600
2019-12-25 05:18:34 +01:00
assert ni(c, 4) == 4 * 86400
col.sched.answerCard(c, 3)
# normal graduation is tomorrow
2019-12-25 05:18:34 +01:00
assert ni(c, 3) == 1 * 86400
assert ni(c, 4) == 4 * 86400
# lapsed cards
##################################################
c.type = CARD_TYPE_RELEARNING
c.ivl = 100
2017-02-08 08:28:05 +01:00
c.factor = STARTING_FACTOR
c.flush()
assert ni(c, 1) == 60
2019-12-25 05:18:34 +01:00
assert ni(c, 3) == 100 * 86400
assert ni(c, 4) == 101 * 86400
# review cards
##################################################
c.type = CARD_TYPE_REV
2020-02-02 10:40:30 +01:00
c.queue = QUEUE_TYPE_REV
c.ivl = 100
2017-02-08 08:28:05 +01:00
c.factor = STARTING_FACTOR
c.flush()
# failing it should put it at 60s
assert ni(c, 1) == 60
# or 1 day if relearn is false
conf["lapse"]["delays"] = []
col.decks.save(conf)
2019-12-25 05:18:34 +01:00
assert ni(c, 1) == 1 * 86400
# (* 100 1.2 86400)10368000.0
assert ni(c, 2) == 10368000
# (* 100 2.5 86400)21600000.0
assert ni(c, 3) == 21600000
# (* 100 2.5 1.3 86400)28080000.0
assert ni(c, 4) == 28080000
assert without_unicode_isolation(col.sched.nextIvlStr(c, 4)) == "10.8mo"
2019-12-25 05:18:34 +01:00
def test_bury():
col = getEmptyCol()
note = col.newNote()
note["Front"] = "one"
col.addNote(note)
c = note.cards()[0]
note = col.newNote()
note["Front"] = "two"
col.addNote(note)
c2 = note.cards()[0]
# burying
2020-08-31 08:14:04 +02:00
col.sched.bury_cards([c.id], manual=True) # pylint: disable=unexpected-keyword-arg
c.load()
2020-01-31 04:21:20 +01:00
assert c.queue == QUEUE_TYPE_MANUALLY_BURIED
2020-08-31 08:14:04 +02:00
col.sched.bury_cards(
[c2.id], manual=False
) # pylint: disable=unexpected-keyword-arg
c2.load()
2020-01-31 04:40:51 +01:00
assert c2.queue == QUEUE_TYPE_SIBLING_BURIED
assert not col.sched.getCard()
col.sched.unbury_deck(deck_id=col.decks.get_current_id(), mode=UnburyDeck.USER_ONLY)
2019-12-25 05:18:34 +01:00
c.load()
2020-02-02 10:19:45 +01:00
assert c.queue == QUEUE_TYPE_NEW
2019-12-25 05:18:34 +01:00
c2.load()
2020-01-31 04:40:51 +01:00
assert c2.queue == QUEUE_TYPE_SIBLING_BURIED
col.sched.unbury_deck(
deck_id=col.decks.get_current_id(), mode=UnburyDeck.SCHED_ONLY
)
2019-12-25 05:18:34 +01:00
c2.load()
2020-02-02 10:19:45 +01:00
assert c2.queue == QUEUE_TYPE_NEW
2020-08-31 08:14:04 +02:00
col.sched.bury_cards([c.id, c2.id])
col.sched.unbury_deck(deck_id=col.decks.get_current_id())
assert col.sched.counts() == (2, 0, 0)
2019-12-25 05:18:34 +01:00
def test_suspend():
col = getEmptyCol()
note = col.newNote()
note["Front"] = "one"
col.addNote(note)
c = note.cards()[0]
# suspending
assert col.sched.getCard()
2020-08-31 08:14:04 +02:00
col.sched.suspend_cards([c.id])
assert not col.sched.getCard()
# unsuspending
2020-08-31 08:14:04 +02:00
col.sched.unsuspend_cards([c.id])
assert col.sched.getCard()
# should cope with rev cards being relearnt
2019-12-25 05:18:34 +01:00
c.due = 0
c.ivl = 100
2020-02-02 10:40:30 +01:00
c.type = CARD_TYPE_REV
c.queue = QUEUE_TYPE_REV
2019-12-25 05:18:34 +01:00
c.flush()
c = col.sched.getCard()
col.sched.answerCard(c, 1)
assert c.due >= time.time()
due = c.due
2020-02-02 10:28:04 +01:00
assert c.queue == QUEUE_TYPE_LRN
2020-01-31 05:38:39 +01:00
assert c.type == CARD_TYPE_RELEARNING
2020-08-31 08:14:04 +02:00
col.sched.suspend_cards([c.id])
col.sched.unsuspend_cards([c.id])
c.load()
2020-02-02 10:28:04 +01:00
assert c.queue == QUEUE_TYPE_LRN
2020-01-31 05:38:39 +01:00
assert c.type == CARD_TYPE_RELEARNING
assert c.due == due
# should cope with cards in cram decks
c.due = 1
c.flush()
did = col.decks.new_filtered("tmp")
col.sched.rebuild_filtered_deck(did)
c.load()
assert c.due != 1
assert c.did != 1
2020-08-31 08:14:04 +02:00
col.sched.suspend_cards([c.id])
c.load()
assert c.due != 1
assert c.did != 1
assert c.odue == 1
2019-12-25 05:18:34 +01:00
def test_filt_reviewing_early_normal():
col = getEmptyCol()
note = col.newNote()
note["Front"] = "one"
col.addNote(note)
c = note.cards()[0]
c.ivl = 100
2020-02-02 10:40:30 +01:00
c.queue = CARD_TYPE_REV
c.type = QUEUE_TYPE_REV
# due in 25 days, so it's been waiting 75 days
c.due = col.sched.today + 25
c.mod = 1
2017-02-08 08:28:05 +01:00
c.factor = STARTING_FACTOR
2021-06-27 04:12:23 +02:00
c.start_timer()
c.flush()
assert col.sched.counts() == (0, 0, 0)
# create a dynamic deck and refresh it
did = col.decks.new_filtered("Cram")
col.sched.rebuild_filtered_deck(did)
2017-12-24 10:27:19 +01:00
# should appear as normal in the deck list
assert sorted(col.sched.deck_due_tree().children)[0].review_count == 1
# and should appear in the counts
assert col.sched.counts() == (0, 0, 1)
# grab it and check estimates
c = col.sched.getCard()
assert col.sched.answerButtons(c) == 4
assert col.sched.nextIvl(c, 1) == 600
2023-09-24 06:19:25 +02:00
assert col.sched.nextIvl(c, 2) == round(75 * 1.2) * 86400
assert col.sched.nextIvl(c, 3) == round(75 * 2.5) * 86400
assert col.sched.nextIvl(c, 4) == round(75 * 2.5 * 1.15) * 86400
# answer 'good'
col.sched.answerCard(c, 3)
assert c.due == col.sched.today + c.ivl
assert not c.odue
# should not be in learning
2020-02-02 10:40:30 +01:00
assert c.queue == QUEUE_TYPE_REV
# should be logged as a cram rep
assert col.db.scalar("select type from revlog order by id desc limit 1") == 3
# due in 75 days, so it's been waiting 25 days
c.ivl = 100
c.due = col.sched.today + 75
c.flush()
col.sched.rebuild_filtered_deck(did)
c = col.sched.getCard()
assert col.sched.nextIvl(c, 2) == 100 * 1.2 / 2 * 86400
assert col.sched.nextIvl(c, 3) == 100 * 86400
2023-09-24 06:19:25 +02:00
assert col.sched.nextIvl(c, 4) == round(100 * (1.3 - (1.3 - 1) / 2)) * 86400
2019-12-25 05:18:34 +01:00
def test_filt_keep_lrn_state():
col = getEmptyCol()
note = col.newNote()
note["Front"] = "one"
col.addNote(note)
# fail the card outside filtered deck
c = col.sched.getCard()
conf = col.sched._cardConf(c)
conf["new"]["delays"] = [1, 10, 61]
col.decks.save(conf)
col.sched.answerCard(c, 1)
2020-02-02 10:28:04 +01:00
assert c.type == CARD_TYPE_LRN and c.queue == QUEUE_TYPE_LRN
assert c.left % 1000 == 3
col.sched.answerCard(c, 3)
2020-02-02 10:28:04 +01:00
assert c.type == CARD_TYPE_LRN and c.queue == QUEUE_TYPE_LRN
# create a dynamic deck and refresh it
did = col.decks.new_filtered("Cram")
col.sched.rebuild_filtered_deck(did)
# card should still be in learning state
c.load()
2020-02-02 10:28:04 +01:00
assert c.type == CARD_TYPE_LRN and c.queue == QUEUE_TYPE_LRN
assert c.left % 1000 == 2
# should be able to advance learning steps
col.sched.answerCard(c, 3)
# should be due at least an hour in the future
PEP8 for rest of pylib (#1451) * PEP8 dbproxy.py * PEP8 errors.py * PEP8 httpclient.py * PEP8 lang.py * PEP8 latex.py * Add decorator to deprectate key words * Make replacement for deprecated attribute optional * Use new helper `_print_replacement_warning()` * PEP8 media.py * PEP8 rsbackend.py * PEP8 sound.py * PEP8 stdmodels.py * PEP8 storage.py * PEP8 sync.py * PEP8 tags.py * PEP8 template.py * PEP8 types.py * Fix DeprecatedNamesMixinForModule The class methods need to be overridden with instance methods, so every module has its own dicts. * Use `# pylint: disable=invalid-name` instead of id * PEP8 utils.py * Only decorate `__getattr__` with `@no_type_check` * Fix mypy issue with snakecase Importing it from `anki._vendor` raises attribute errors. * Format * Remove inheritance of DeprecatedNamesMixin There's almost no shared code now and overriding classmethods with instance methods raises mypy issues. * Fix traceback frames of deprecation warnings * remove fn/TimedLog (dae) Neither Anki nor add-ons appear to have been using it * fix some issues with stringcase use (dae) - the wheel was depending on the PyPI version instead of our vendored version - _vendor:stringcase should not have been listed in the anki py_library. We already include the sources in py_srcs, and need to refer to them directly. By listing _vendor:stringcase as well, we were making a top-level stringcase library available, which would have only worked for distributing because the wheel definition was also incorrect. - mypy errors are what caused me to mistakenly add the above - they were because the type: ignore at the top of stringcase.py was causing mypy to completely ignore the file, so it was not aware of any attributes it contained.
2021-10-25 06:50:13 +02:00
assert c.due - int_time() > 60 * 60
# emptying the deck preserves learning state
2020-09-03 09:43:07 +02:00
col.sched.empty_filtered_deck(did)
c.load()
2020-02-02 10:28:04 +01:00
assert c.type == CARD_TYPE_LRN and c.queue == QUEUE_TYPE_LRN
assert c.left % 1000 == 1
PEP8 for rest of pylib (#1451) * PEP8 dbproxy.py * PEP8 errors.py * PEP8 httpclient.py * PEP8 lang.py * PEP8 latex.py * Add decorator to deprectate key words * Make replacement for deprecated attribute optional * Use new helper `_print_replacement_warning()` * PEP8 media.py * PEP8 rsbackend.py * PEP8 sound.py * PEP8 stdmodels.py * PEP8 storage.py * PEP8 sync.py * PEP8 tags.py * PEP8 template.py * PEP8 types.py * Fix DeprecatedNamesMixinForModule The class methods need to be overridden with instance methods, so every module has its own dicts. * Use `# pylint: disable=invalid-name` instead of id * PEP8 utils.py * Only decorate `__getattr__` with `@no_type_check` * Fix mypy issue with snakecase Importing it from `anki._vendor` raises attribute errors. * Format * Remove inheritance of DeprecatedNamesMixin There's almost no shared code now and overriding classmethods with instance methods raises mypy issues. * Fix traceback frames of deprecation warnings * remove fn/TimedLog (dae) Neither Anki nor add-ons appear to have been using it * fix some issues with stringcase use (dae) - the wheel was depending on the PyPI version instead of our vendored version - _vendor:stringcase should not have been listed in the anki py_library. We already include the sources in py_srcs, and need to refer to them directly. By listing _vendor:stringcase as well, we were making a top-level stringcase library available, which would have only worked for distributing because the wheel definition was also incorrect. - mypy errors are what caused me to mistakenly add the above - they were because the type: ignore at the top of stringcase.py was causing mypy to completely ignore the file, so it was not aware of any attributes it contained.
2021-10-25 06:50:13 +02:00
assert c.due - int_time() > 60 * 60
2019-12-25 05:18:34 +01:00
def test_preview():
# add cards
col = getEmptyCol()
note = col.newNote()
note["Front"] = "one"
col.addNote(note)
c = note.cards()[0]
orig = copy.copy(c)
note2 = col.newNote()
note2["Front"] = "two"
col.addNote(note2)
# cram deck
did = col.decks.new_filtered("Cram")
cram = col.decks.get(did)
2019-12-25 05:18:34 +01:00
cram["resched"] = False
col.decks.save(cram)
col.sched.rebuild_filtered_deck(did)
# grab the first card
c = col.sched.getCard()
2023-09-24 06:19:25 +02:00
passing_grade = 4
assert col.sched.answerButtons(c) == passing_grade
assert col.sched.nextIvl(c, 1) == 60
assert col.sched.nextIvl(c, passing_grade) == 0
# failing it will push its due time back
due = c.due
col.sched.answerCard(c, 1)
assert c.due != due
# the other card should come next
c2 = col.sched.getCard()
assert c2.id != c.id
# passing it will remove it
col.sched.answerCard(c2, passing_grade)
2020-02-02 10:19:45 +01:00
assert c2.queue == QUEUE_TYPE_NEW
assert c2.reps == 0
2020-02-02 10:19:45 +01:00
assert c2.type == CARD_TYPE_NEW
# the other card should appear again
c = col.sched.getCard()
assert c.id == orig.id
# emptying the filtered deck should restore card
2020-09-03 09:43:07 +02:00
col.sched.empty_filtered_deck(did)
c.load()
2020-02-02 10:19:45 +01:00
assert c.queue == QUEUE_TYPE_NEW
assert c.reps == 0
2020-02-02 10:19:45 +01:00
assert c.type == CARD_TYPE_NEW
2019-12-25 05:18:34 +01:00
def test_ordcycle():
col = getEmptyCol()
# add two more templates and set second active
m = col.models.current()
mm = col.models
2021-06-27 05:49:58 +02:00
t = mm.new_template("Reverse")
2019-12-25 05:18:34 +01:00
t["qfmt"] = "{{Back}}"
t["afmt"] = "{{Front}}"
2021-06-27 05:49:58 +02:00
mm.add_template(m, t)
t = mm.new_template("f2")
t["qfmt"] = "{{Front}}2"
2019-12-25 05:18:34 +01:00
t["afmt"] = "{{Back}}"
2021-06-27 05:49:58 +02:00
mm.add_template(m, t)
mm.save(m)
# create a new note; it should have 3 cards
note = col.newNote()
note["Front"] = "1"
note["Back"] = "1"
col.addNote(note)
2021-06-27 07:12:22 +02:00
assert col.card_count() == 3
conf = col.decks.get_config(1)
conf["new"]["bury"] = False
col.decks.save(conf)
# ordinals should arrive in order
for i in range(3):
c = col.sched.getCard()
assert c.ord == i
col.sched.answerCard(c, 4)
2019-12-25 05:18:34 +01:00
def test_counts_idx_new():
col = getEmptyCol()
note = col.newNote()
note["Front"] = "one"
note["Back"] = "two"
col.addNote(note)
note = col.newNote()
note["Front"] = "two"
note["Back"] = "two"
col.addNote(note)
assert col.sched.counts() == (2, 0, 0)
c = col.sched.getCard()
# getCard does not decrement counts
assert col.sched.counts() == (2, 0, 0)
assert col.sched.countIdx(c) == 0
# answer to move to learn queue
col.sched.answerCard(c, 1)
assert col.sched.counts() == (1, 1, 0)
assert col.sched.countIdx(c) == 1
# fetching next will not decrement the count
c = col.sched.getCard()
assert col.sched.counts() == (1, 1, 0)
assert col.sched.countIdx(c) == 0
def test_repCounts():
col = getEmptyCol()
note = col.newNote()
note["Front"] = "one"
col.addNote(note)
note = col.newNote()
note["Front"] = "two"
col.addNote(note)
# lrnReps should be accurate on pass/fail
assert col.sched.counts() == (2, 0, 0)
col.sched.answerCard(col.sched.getCard(), 1)
assert col.sched.counts() == (1, 1, 0)
col.sched.answerCard(col.sched.getCard(), 1)
assert col.sched.counts() == (0, 2, 0)
col.sched.answerCard(col.sched.getCard(), 3)
assert col.sched.counts() == (0, 2, 0)
col.sched.answerCard(col.sched.getCard(), 1)
assert col.sched.counts() == (0, 2, 0)
col.sched.answerCard(col.sched.getCard(), 3)
assert col.sched.counts() == (0, 1, 0)
col.sched.answerCard(col.sched.getCard(), 4)
assert col.sched.counts() == (0, 0, 0)
note = col.newNote()
note["Front"] = "three"
col.addNote(note)
note = col.newNote()
note["Front"] = "four"
col.addNote(note)
# initial pass and immediate graduate should be correct too
assert col.sched.counts() == (2, 0, 0)
col.sched.answerCard(col.sched.getCard(), 3)
assert col.sched.counts() == (1, 1, 0)
col.sched.answerCard(col.sched.getCard(), 4)
assert col.sched.counts() == (0, 1, 0)
col.sched.answerCard(col.sched.getCard(), 4)
assert col.sched.counts() == (0, 0, 0)
# and failing a review should too
note = col.newNote()
note["Front"] = "five"
col.addNote(note)
c = note.cards()[0]
2020-02-02 10:40:30 +01:00
c.type = CARD_TYPE_REV
c.queue = QUEUE_TYPE_REV
c.due = col.sched.today
c.flush()
note = col.newNote()
note["Front"] = "six"
col.addNote(note)
assert col.sched.counts() == (1, 0, 1)
col.sched.answerCard(col.sched.getCard(), 1)
assert col.sched.counts() == (1, 1, 0)
2019-12-25 05:18:34 +01:00
def test_timing():
col = getEmptyCol()
# add a few review cards, due today
for i in range(5):
note = col.newNote()
2021-10-02 15:51:42 +02:00
note["Front"] = f"num{str(i)}"
col.addNote(note)
c = note.cards()[0]
2020-02-02 10:40:30 +01:00
c.type = CARD_TYPE_REV
c.queue = QUEUE_TYPE_REV
c.due = 0
c.flush()
# fail the first one
c = col.sched.getCard()
col.sched.answerCard(c, 1)
# the next card should be another review
c2 = col.sched.getCard()
2020-02-02 10:40:30 +01:00
assert c2.queue == QUEUE_TYPE_REV
2017-12-24 10:27:19 +01:00
# if the failed card becomes due, it should show first
PEP8 for rest of pylib (#1451) * PEP8 dbproxy.py * PEP8 errors.py * PEP8 httpclient.py * PEP8 lang.py * PEP8 latex.py * Add decorator to deprectate key words * Make replacement for deprecated attribute optional * Use new helper `_print_replacement_warning()` * PEP8 media.py * PEP8 rsbackend.py * PEP8 sound.py * PEP8 stdmodels.py * PEP8 storage.py * PEP8 sync.py * PEP8 tags.py * PEP8 template.py * PEP8 types.py * Fix DeprecatedNamesMixinForModule The class methods need to be overridden with instance methods, so every module has its own dicts. * Use `# pylint: disable=invalid-name` instead of id * PEP8 utils.py * Only decorate `__getattr__` with `@no_type_check` * Fix mypy issue with snakecase Importing it from `anki._vendor` raises attribute errors. * Format * Remove inheritance of DeprecatedNamesMixin There's almost no shared code now and overriding classmethods with instance methods raises mypy issues. * Fix traceback frames of deprecation warnings * remove fn/TimedLog (dae) Neither Anki nor add-ons appear to have been using it * fix some issues with stringcase use (dae) - the wheel was depending on the PyPI version instead of our vendored version - _vendor:stringcase should not have been listed in the anki py_library. We already include the sources in py_srcs, and need to refer to them directly. By listing _vendor:stringcase as well, we were making a top-level stringcase library available, which would have only worked for distributing because the wheel definition was also incorrect. - mypy errors are what caused me to mistakenly add the above - they were because the type: ignore at the top of stringcase.py was causing mypy to completely ignore the file, so it was not aware of any attributes it contained.
2021-10-25 06:50:13 +02:00
c.due = int_time() - 1
2017-12-24 10:27:19 +01:00
c.flush()
c = col.sched.getCard()
2020-02-02 10:28:04 +01:00
assert c.queue == QUEUE_TYPE_LRN
2019-12-25 05:18:34 +01:00
def test_collapse():
col = getEmptyCol()
# add a note
note = col.newNote()
note["Front"] = "one"
col.addNote(note)
# and another, so we don't get the same twice in a row
note = col.newNote()
note["Front"] = "two"
col.addNote(note)
# first note
c = col.sched.getCard()
col.sched.answerCard(c, 1)
# second note
c2 = col.sched.getCard()
assert c2.nid != c.nid
col.sched.answerCard(c2, 1)
# first should become available again, despite it being due in the future
c3 = col.sched.getCard()
PEP8 for rest of pylib (#1451) * PEP8 dbproxy.py * PEP8 errors.py * PEP8 httpclient.py * PEP8 lang.py * PEP8 latex.py * Add decorator to deprectate key words * Make replacement for deprecated attribute optional * Use new helper `_print_replacement_warning()` * PEP8 media.py * PEP8 rsbackend.py * PEP8 sound.py * PEP8 stdmodels.py * PEP8 storage.py * PEP8 sync.py * PEP8 tags.py * PEP8 template.py * PEP8 types.py * Fix DeprecatedNamesMixinForModule The class methods need to be overridden with instance methods, so every module has its own dicts. * Use `# pylint: disable=invalid-name` instead of id * PEP8 utils.py * Only decorate `__getattr__` with `@no_type_check` * Fix mypy issue with snakecase Importing it from `anki._vendor` raises attribute errors. * Format * Remove inheritance of DeprecatedNamesMixin There's almost no shared code now and overriding classmethods with instance methods raises mypy issues. * Fix traceback frames of deprecation warnings * remove fn/TimedLog (dae) Neither Anki nor add-ons appear to have been using it * fix some issues with stringcase use (dae) - the wheel was depending on the PyPI version instead of our vendored version - _vendor:stringcase should not have been listed in the anki py_library. We already include the sources in py_srcs, and need to refer to them directly. By listing _vendor:stringcase as well, we were making a top-level stringcase library available, which would have only worked for distributing because the wheel definition was also incorrect. - mypy errors are what caused me to mistakenly add the above - they were because the type: ignore at the top of stringcase.py was causing mypy to completely ignore the file, so it was not aware of any attributes it contained.
2021-10-25 06:50:13 +02:00
assert c3.due > int_time()
col.sched.answerCard(c3, 4)
# answer other
c4 = col.sched.getCard()
col.sched.answerCard(c4, 4)
assert not col.sched.getCard()
2019-12-25 05:18:34 +01:00
def test_deckDue():
col = getEmptyCol()
# add a note with default deck
note = col.newNote()
note["Front"] = "one"
col.addNote(note)
# and one that's a child
note = col.newNote()
note["Front"] = "two"
2021-06-27 04:12:23 +02:00
default1 = note.note_type()["did"] = col.decks.id("Default::1")
col.addNote(note)
# make it a review card
c = note.cards()[0]
2020-02-02 10:40:30 +01:00
c.queue = QUEUE_TYPE_REV
c.due = 0
c.flush()
# add one more with a new deck
note = col.newNote()
note["Front"] = "two"
2021-06-27 04:12:23 +02:00
note.note_type()["did"] = col.decks.id("foo::bar")
col.addNote(note)
# and one that's a sibling
note = col.newNote()
note["Front"] = "three"
2021-06-27 04:12:23 +02:00
note.note_type()["did"] = col.decks.id("foo::baz")
col.addNote(note)
assert len(col.decks.all_names_and_ids()) == 5
tree = col.sched.deck_due_tree().children
assert tree[0].name == "Default"
# sum of child and parent
assert tree[0].deck_id == 1
assert tree[0].review_count == 1
assert tree[0].new_count == 1
# child count is just review
child = tree[0].children[0]
assert child.name == "1"
assert child.deck_id == default1
assert child.review_count == 1
assert child.new_count == 0
# code should not fail if a card has an invalid deck
2019-12-25 05:18:34 +01:00
c.did = 12345
c.flush()
col.sched.deck_due_tree()
2019-12-25 05:18:34 +01:00
def test_deckTree():
col = getEmptyCol()
col.decks.id("new::b::c")
col.decks.id("new2")
# new should not appear twice in tree
names = [x.name for x in col.sched.deck_due_tree().children]
names.remove("new")
assert "new" not in names
2019-12-25 05:18:34 +01:00
def test_deckFlow():
col = getEmptyCol()
# add a note with default deck
note = col.newNote()
note["Front"] = "one"
col.addNote(note)
# and one that's a child
note = col.newNote()
note["Front"] = "two"
2021-06-27 04:12:23 +02:00
note.note_type()["did"] = col.decks.id("Default::2")
col.addNote(note)
# and another that's higher up
note = col.newNote()
note["Front"] = "three"
2021-06-27 04:12:23 +02:00
default1 = note.note_type()["did"] = col.decks.id("Default::1")
col.addNote(note)
assert col.sched.counts() == (3, 0, 0)
V3 parent limits (#1638) * avoid repinning Rust deps by default * add id_tree dependency * Respect intermediate child limits in v3 * Test new behaviour of v3 counts * Rework v3 queue building to respect parent limits * Add missing did field to SQL query * Fix `LimitTreeMap::is_exhausted()` * Rework tree building logic https://github.com/ankitects/anki/pull/1638#discussion_r798328734 * Add timer for build_queues() * `is_exhausted()` -> `limit_reached()` * Move context and limits into `QueueBuilder` This allows for moving more logic into QueueBuilder, so less passing around of arguments. Unfortunately, some tests will require additional work to set up. * Fix stop condition in new_cards_by_position * Fix order gather order of new cards by deck * Add scheduler/queue/builder/burying.rs * Fix bad tree due to unsorted child decks * Fix comment * Fix `cap_new_to_review_rec()` * Add test for new card gathering * Always sort `child_decks()` * Fix deck removal in `cap_new_to_review_rec()` * Fix sibling ordering in new card gathering * Remove limits for deck total count with children * Add random gather order * Remove bad sibling order handling All routines ensure ascending order now. Also do some other minor refactoring. * Remove queue truncating All routines stop now as soon as the root limit is reached. * Move deck fetching into `QueueBuilder::new()` * Rework new card gather and sort options https://github.com/ankitects/anki/pull/1638#issuecomment-1032173013 * Disable new sort order choices ... depending on set gather order. * Use enum instead of numbers * Ensure valid sort order setting * Update new gather and sort order tooltips * Warn about random insertion order with v3 * Revert "Add timer for build_queues()" This reverts commit c9f5fc6ebe87953c17a0c842990b009b5596c69c. * Update rslib/src/storage/card/mod.rs (dae) * minor wording tweaks to the tooltips (dae) + move legacy strings to bottom + consistent capitalization (our leech action still needs fixing, but that will require introducing a new 'suspend card' string as the existing one is used elsewhere as well)
2022-02-10 00:55:43 +01:00
# should get top level one first, then ::1, then ::2
for i in "one", "three", "two":
c = col.sched.getCard()
assert c.note()["Front"] == i
col.sched.answerCard(c, 3)
2019-12-25 05:18:34 +01:00
def test_reorder():
col = getEmptyCol()
# add a note with default deck
note = col.newNote()
note["Front"] = "one"
col.addNote(note)
note2 = col.newNote()
note2["Front"] = "two"
col.addNote(note2)
assert note2.cards()[0].due == 2
2019-12-25 05:18:34 +01:00
found = False
# 50/50 chance of being reordered
for i in range(20):
col.sched.randomize_cards(1)
if note.cards()[0].due != note.id:
2019-12-25 05:18:34 +01:00
found = True
break
assert found
col.sched.order_cards(1)
assert note.cards()[0].due == 1
# shifting
2020-07-17 17:33:58 +02:00
note3 = col.newNote()
note3["Front"] = "three"
col.addNote(note3)
2020-07-17 17:34:39 +02:00
note4 = col.newNote()
note4["Front"] = "four"
col.addNote(note4)
assert note.cards()[0].due == 1
assert note2.cards()[0].due == 2
2020-07-17 17:33:58 +02:00
assert note3.cards()[0].due == 3
2020-07-17 17:34:39 +02:00
assert note4.cards()[0].due == 4
2021-03-18 02:46:11 +01:00
col.sched.reposition_new_cards(
[note3.cards()[0].id, note4.cards()[0].id],
starting_from=1,
shift_existing=True,
step_size=1,
randomize=False,
)
assert note.cards()[0].due == 3
assert note2.cards()[0].due == 4
2020-07-17 17:33:58 +02:00
assert note3.cards()[0].due == 1
2020-07-17 17:34:39 +02:00
assert note4.cards()[0].due == 2
2019-12-25 05:18:34 +01:00
def test_forget():
col = getEmptyCol()
note = col.newNote()
note["Front"] = "one"
col.addNote(note)
c = note.cards()[0]
2020-02-02 10:40:30 +01:00
c.queue = QUEUE_TYPE_REV
c.type = CARD_TYPE_REV
2019-12-25 05:18:34 +01:00
c.ivl = 100
c.due = 0
c.flush()
assert col.sched.counts() == (0, 0, 1)
col.sched.forgetCards([c.id])
assert col.sched.counts() == (1, 0, 0)
2019-12-25 05:18:34 +01:00
def test_resched():
col = getEmptyCol()
note = col.newNote()
note["Front"] = "one"
col.addNote(note)
c = note.cards()[0]
col.sched.set_due_date([c.id], "0")
c.load()
assert c.due == col.sched.today
assert c.ivl == 1
2020-02-02 10:40:30 +01:00
assert c.queue == QUEUE_TYPE_REV and c.type == CARD_TYPE_REV
# make it due tomorrow
col.sched.set_due_date([c.id], "1")
c.load()
assert c.due == col.sched.today + 1
assert c.ivl == 1
2019-12-25 05:18:34 +01:00
def test_norelearn():
col = getEmptyCol()
# add a note
note = col.newNote()
note["Front"] = "one"
col.addNote(note)
c = note.cards()[0]
2020-02-02 10:40:30 +01:00
c.type = CARD_TYPE_REV
c.queue = QUEUE_TYPE_REV
c.due = 0
2017-02-08 08:28:05 +01:00
c.factor = STARTING_FACTOR
c.reps = 3
c.lapses = 1
c.ivl = 100
2021-06-27 04:12:23 +02:00
c.start_timer()
c.flush()
col.sched.answerCard(c, 1)
col.sched._cardConf(c)["lapse"]["delays"] = []
col.sched.answerCard(c, 1)
2019-12-25 05:18:34 +01:00
def test_failmult():
col = getEmptyCol()
note = col.newNote()
note["Front"] = "one"
note["Back"] = "two"
col.addNote(note)
c = note.cards()[0]
2020-02-02 10:40:30 +01:00
c.type = CARD_TYPE_REV
c.queue = QUEUE_TYPE_REV
c.ivl = 100
c.due = col.sched.today - c.ivl
2017-02-08 08:28:05 +01:00
c.factor = STARTING_FACTOR
c.reps = 3
c.lapses = 1
2021-06-27 04:12:23 +02:00
c.start_timer()
c.flush()
conf = col.sched._cardConf(c)
conf["lapse"]["mult"] = 0.5
col.decks.save(conf)
c = col.sched.getCard()
col.sched.answerCard(c, 1)
assert c.ivl == 50
col.sched.answerCard(c, 1)
assert c.ivl == 25
2018-01-14 07:58:12 +01:00
2019-12-25 05:18:34 +01:00
# cards with a due date earlier than the collection should retain
# their due date when removed
def test_negativeDueFilter():
col = getEmptyCol()
# card due prior to collection date
note = col.newNote()
note["Front"] = "one"
note["Back"] = "two"
col.addNote(note)
c = note.cards()[0]
c.due = -5
2020-02-02 10:40:30 +01:00
c.queue = QUEUE_TYPE_REV
c.ivl = 5
c.flush()
# into and out of filtered deck
did = col.decks.new_filtered("Cram")
col.sched.rebuild_filtered_deck(did)
2020-09-03 09:43:07 +02:00
col.sched.empty_filtered_deck(did)
c.load()
assert c.due == -5
# hard on the first step should be the average of again and good,
# and it should be logged properly
def test_initial_repeat():
col = getEmptyCol()
note = col.newNote()
note["Front"] = "one"
note["Back"] = "two"
col.addNote(note)
c = col.sched.getCard()
col.sched.answerCard(c, 2)
# should be due in ~ 5.5 mins
expected = time.time() + 5.5 * 60
assert expected - 10 < c.due < expected * 1.25
ivl = col.db.scalar("select ivl from revlog")
assert ivl == -5.5 * 60