mirror of
https://github.com/ankitects/anki.git
synced 2025-09-20 15:02:21 -04:00

The approach of using incrementing id numbers works for syncing if we assume the server is canonical and all other clients rewrite their ids as necessary, but upon reflection it is not sufficient for merging decks in general, as we have no way of knowing whether objects with the same id are actually the same or not. So we need some way of uniquely identifying the object. One approach would be to go back to Anki 1.0's random 64bit numbers, but as outlined in a previous commit such large numbers can't be handled easy in some languages like Javascript, and they tend to be fragmented on disk which impacts performance. It's much better if we can keep content added at the same time in the same place on disk, so that operations like syncing which are mainly interested in newly added content can run faster. Another approach is to add a separate column containing the unique id, which is what Mnemosyne 2.0 will be doing. Unfortunately it means adding an index for that column, leading to slower inserts and larger deck files. And if the current sequential ids are kept, a bunch of code needs to be kept to ensure ids don't conflict when merging. To address the above, the plan is to use a millisecond timestamp as the id. This ensures disk order reflects creation order, allows us to merge the id and crt columns, avoids the need for a separate index, and saves us from worrying about rewriting ids. There is of course a small chance that the objects to be merged were created at exactly the same time, but this is extremely unlikely. This commit changes models. Other objects will follow.
91 lines
2.6 KiB
Python
91 lines
2.6 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().templates)
|
|
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['newCardOrder'] = NEW_CARDS_RANDOM
|
|
f = deck.newFact()
|
|
f['Front'] = u'1'
|
|
f['Back'] = u'2'
|
|
deck.addFact(f)
|
|
cards = deck.genCards(f, f.model().templates)
|
|
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") == 0
|
|
# add the fact back
|
|
deck.addFact(f)
|
|
assert deck.cardCount() == 1
|
|
cid = f.cards()[0].id
|
|
# delete again, this time with syncing enabled
|
|
deck.syncName = "abc"
|
|
deck.lastSync = time.time()
|
|
deck.delCards([cid])
|
|
assert deck.cardCount() == 0
|
|
assert deck.factCount() == 0
|
|
assert deck.db.scalar("select count() from graves") != 0
|
|
|
|
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.fact().id == 1
|
|
assert c.template()['ord'] == 0
|