Anki/tests/test_models.py
Damien Elmes 9c247f45bd remove q/a cache, tags in fields, rewrite remaining ids, more
Anki used random 64bit IDs for cards, facts and fields. This had some nice
properties:
- merging data in syncs and imports was simply a matter of copying each way,
  as conflicts were astronomically unlikely
- it made it easy to identify identical cards and prevent them from being
  reimported
But there were some negatives too:
- they're more expensive to store
- javascript can't handle numbers > 2**53, which means AnkiMobile, iAnki and
  so on have to treat the ids as strings, which is slow
- simply copying data in a sync or import can lead to corruption, as while a
  duplicate id indicates the data was originally the same, it may have
  diverged. A more intelligent approach is necessary.
- sqlite was sorting the fields table based on the id, which meant the fields
  were spread across the table, and costly to fetch

So instead, we'll move to incremental ids. In the case of model changes we'll
declare that a schema change and force a full sync to avoid having to deal
with conflicts, and in the case of cards and facts, we'll need to update the
ids on one end to merge. Identical cards can be detected by checking to see if
their id is the same and their creation time is the same.

Creation time has been added back to cards and facts because it's necessary
for sync conflict merging. That means facts.pos is not required.

The graves table has been removed. It's not necessary for schema related
changes, and dead cards/facts can be represented as a card with queue=-4 and
created=0. Because we will record schema modification time and can ensure a
full sync propagates to all endpoints, it means we can remove the dead
cards/facts on schema change.

Tags have been removed from the facts table and are represented as a field
with ord=-1 and fmid=0. Combined with the locality improvement for fields, it
means that fetching fields is not much more expensive than using the q/a
cache.

Because of the above, removing the q/a cache is a possibility now. The q and a
columns on cards has been dropped. It will still be necessary to render the
q/a on fact add/edit, since we need to record media references. It would be
nice to avoid this in the future. Perhaps one way would be the ability to
assign a type to fields, like "image", "audio", or "latex". LaTeX needs
special consider anyway, as it was being rendered into the q/a cache.
2011-04-28 09:23:53 +09:00

95 lines
2.5 KiB
Python

# coding: utf-8
from tests.shared import getEmptyDeck
from anki.models import Model, Template, Field
from anki.utils import stripHTML
def test_modelDelete():
deck = getEmptyDeck()
f = deck.newFact()
f['Front'] = u'1'
f['Back'] = u'2'
deck.addFact(f)
assert deck.cardCount() == 1
deck.deleteModel(deck.conf['currentModelId'])
assert deck.cardCount() == 0
def test_modelCopy():
deck = getEmptyDeck()
m = deck.currentModel()
m2 = m.copy()
assert m2.name == "Basic copy"
assert m2.id != m.id
assert m2.fields[0].id != m.fields[0].id
assert m2.templates[0].id != m.templates[0].id
assert len(m2.fields) == 2
assert len(m.fields) == 2
assert len(m2.fields) == len(m.fields)
assert len(m.templates) == 2
assert len(m2.templates) == 2
def test_modelChange():
deck = getEmptyDeck()
m2 = deck.currentModel()
# taken from jp support plugin
m1 = Model(deck)
m1.name = "Japanese"
# field 1
fm = Field(deck)
fm.name = "Expression"
fm.conf['required'] = True
fm.conf['unique'] = True
m1.addField(fm)
# field2
fm = Field(deck)
fm.name = "Meaning"
m1.addField(fm)
# field3
fm = Field(deck)
fm.name = "Reading"
m1.addField(fm)
# template1
t = Template(deck)
t.name = "Recognition"
t.qfmt = "{{Expression}}"
t.afmt = "{{Reading}}<br>{{Meaning}}"
m1.addTemplate(t)
# template2
t = Template(deck)
t.name = "Recall"
t.qfmt = "{{Meaning}}"
t.afmt = "{{Expression}}<br>{{Reading}}"
#t.active = False
m1.addTemplate(t)
deck.addModel(m1)
# add some facts
f = deck.newFact()
f['Expression'] = u'e'
f['Meaning'] = u'm'
f['Reading'] = u'r'
deck.addFact(f)
f2 = deck.newFact()
f2['Expression'] = u'e2'
f2['Meaning'] = u'm2'
f2['Reading'] = u'r2'
deck.addFact(f2)
# convert to basic
assert deck.modelUseCount(m1) == 2
assert deck.modelUseCount(m2) == 0
assert deck.cardCount() == 4
assert deck.factCount() == 2
fmap = {m1.fields[0]: m2.fields[0],
m1.fields[1]: None,
m1.fields[2]: m2.fields[1]}
cmap = {m1.templates[0]: m2.templates[0],
m1.templates[1]: None}
deck.changeModel([f.id], m2, fmap, cmap)
assert deck.modelUseCount(m1) == 1
assert deck.modelUseCount(m2) == 1
assert deck.cardCount() == 3
assert deck.factCount() == 2
c = deck.getCard(deck.db.scalar("select id from cards where fid = ?", f.id))
assert stripHTML(c.q()) == u"e"
assert stripHTML(c.a()) == u"r"