Anki/tests/test_cards.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

79 lines
2.3 KiB
Python

# coding: utf-8
import time
from anki.db import DB
from anki.consts import *
from anki.utils import hexifyID
from tests.shared import getEmptyDeck
def test_genCards():
deck = getEmptyDeck()
f = deck.newFact()
f['Front'] = u'1'
f['Back'] = u'2'
deck.addFact(f)
cards = deck.genCards(f, f.model()['tmpls'])
assert len(cards) == 1
assert cards[0].ord == 1
assert deck.cardCount() == 2
assert cards[0].due == f.id
# should work on random mode too
deck.qconf['newOrder'] = NEW_CARDS_RANDOM
f = deck.newFact()
f['Front'] = u'1'
f['Back'] = u'2'
deck.addFact(f)
cards = deck.genCards(f, f.model()['tmpls'])
assert deck.cardCount() == 4
c = deck.db.list("select due from cards where fid = ?", f.id)
assert c[0] == c[1]
def test_previewCards():
deck = getEmptyDeck()
f = deck.newFact()
f['Front'] = u'1'
f['Back'] = u'2'
# non-empty and active
cards = deck.previewCards(f, 0)
assert len(cards) == 1
assert cards[0].ord == 0
# all templates
cards = deck.previewCards(f, 2)
assert len(cards) == 2
# add the fact, and test existing preview
deck.addFact(f)
cards = deck.previewCards(f, 1)
assert len(cards) == 1
assert cards[0].ord == 0
# make sure we haven't accidentally added cards to the db
assert deck.cardCount() == 1
def test_delete():
deck = getEmptyDeck()
f = deck.newFact()
f['Front'] = u'1'
f['Back'] = u'2'
deck.addFact(f)
cid = f.cards()[0].id
deck.reset()
deck.sched.answerCard(deck.sched.getCard(), 2)
assert deck.db.scalar("select count() from revlog") == 1
deck.delCards([cid])
assert deck.cardCount() == 0
assert deck.factCount() == 0
assert deck.db.scalar("select count() from facts") == 0
assert deck.db.scalar("select count() from cards") == 0
assert deck.db.scalar("select count() from fsums") == 0
assert deck.db.scalar("select count() from revlog") == 0
assert deck.db.scalar("select count() from graves") == 2
def test_misc():
d = getEmptyDeck()
f = d.newFact()
f['Front'] = u'1'
f['Back'] = u'2'
d.addFact(f)
c = f.cards()[0]
id = d.conf['currentModelId']
assert c.cssClass() == "cm%s-0" % hexifyID(id)
assert c.template()['ord'] == 0