anki/tests/shared.py

47 lines
1.2 KiB
Python
Raw Normal View History

import tempfile, os, shutil
from anki import Collection as aopen
def assertException(exception, func):
found = False
try:
func()
except exception:
found = True
assert found
# Creating new decks is expensive. Just do it once, and then spin off
# copies from the master.
2018-01-14 07:58:12 +01:00
def getEmptyCol(schedVer=1):
2014-06-03 10:38:47 +02:00
if len(getEmptyCol.master) == 0:
(fd, nam) = tempfile.mkstemp(suffix=".anki2")
os.close(fd)
os.unlink(nam)
col = aopen(nam)
col.db.close()
2014-06-03 10:38:47 +02:00
getEmptyCol.master = nam
(fd, nam) = tempfile.mkstemp(suffix=".anki2")
2014-06-03 10:38:47 +02:00
shutil.copy(getEmptyCol.master, nam)
2018-01-14 07:58:12 +01:00
from anki.collection import _Collection
_Collection.defaultSchedulerVersion = schedVer
col = aopen(nam)
_Collection.defaultSchedulerVersion = 1
return col
2014-06-03 10:38:47 +02:00
getEmptyCol.master = ""
# Fallback for when the DB needs options passed in.
def getEmptyDeckWith(**kwargs):
(fd, nam) = tempfile.mkstemp(suffix=".anki2")
os.close(fd)
os.unlink(nam)
return aopen(nam, **kwargs)
def getUpgradeDeckPath(name="anki12.anki"):
src = os.path.join(testDir, "support", name)
(fd, dst) = tempfile.mkstemp(suffix=".anki2")
shutil.copy(src, dst)
return dst
testDir = os.path.dirname(__file__)