Anki/tests/test_undo.py
Damien Elmes d3a3edb707 move models into the deck table
Like the previous change, models have been moved from a separate DB table to
an entry in the deck. We need them for many operations including reviewing,
and it's easier to keep them in memory than half on disk with a cache that
gets cleared every time we .reset(). This means they are easily serialized as
well - previously they were part Python and part JSON, which made access
confusing.

Because the data is all pulled from JSON now, the instance methods have been
moved to the model registry. Eg:
  model.addField(...) -> deck.models.addField(model, ...).

- IDs are now timestamped as with groups et al.

- The data field for plugins was also removed. Config info can be added to
  deck.conf; larger data should be stored externally.

- Upgrading needs to be updated for the new model structure.

- HexifyID() now accepts strings as well, as our IDs get converted to strings
  in the serialization process.
2011-08-27 22:27:09 +09:00

87 lines
2.2 KiB
Python

# coding: utf-8
import time
from tests.shared import assertException, getEmptyDeck
def test_op():
d = getEmptyDeck()
# should have no undo by default
assert not d.undoName()
# let's adjust a study option
assert d.qconf['repLim'] == 0
d.save("studyopts")
d.qconf['repLim'] = 10
# it should be listed as undoable
assert d.undoName() == "studyopts"
# with about 5 minutes until it's clobbered
assert time.time() - d._lastSave < 1
# undoing should restore the old value
d.undo()
assert not d.undoName()
assert d.qconf['repLim'] == 0
# an (auto)save will clear the undo
d.save("foo")
assert d.undoName() == "foo"
d.save()
assert not d.undoName()
# and a review will, too
d.save("add")
f = d.newFact()
f['Front'] = u"one"
d.addFact(f)
d.reset()
assert d.undoName() == "add"
c = d.sched.getCard()
d.sched.answerCard(c, 2)
assert d.undoName() == "Review"
def test_review():
d = getEmptyDeck()
f = d.newFact()
f['Front'] = u"one"
d.addFact(f)
d.reset()
assert not d.undoName()
# answer
assert d.sched.counts() == (1, 0, 0)
c = d.sched.getCard()
assert c.queue == 0
assert c.grade == 0
d.sched.answerCard(c, 2)
assert d.sched.counts() == (0, 1, 0)
assert c.queue == 1
assert c.grade == 1
# undo
assert d.undoName()
d.undo()
d.reset()
assert d.sched.counts() == (1, 0, 0)
c.load()
assert c.queue == 0
assert c.grade == 0
assert not d.undoName()
# we should be able to undo multiple answers too
f['Front'] = u"two"
d.addFact(f)
d.reset()
assert d.sched.counts() == (2, 0, 0)
c = d.sched.getCard()
d.sched.answerCard(c, 2)
c = d.sched.getCard()
d.sched.answerCard(c, 2)
assert d.sched.counts() == (0, 2, 0)
d.undo()
d.reset()
assert d.sched.counts() == (1, 1, 0)
d.undo()
d.reset()
assert d.sched.counts() == (2, 0, 0)
# performing a normal op will clear the review queue
c = d.sched.getCard()
d.sched.answerCard(c, 2)
assert d.undoName() == "Review"
d.save("foo")
assert d.undoName() == "foo"
d.undo()
assert not d.undoName()