anki/pylib/tests/shared.py

76 lines
1.6 KiB
Python
Raw Normal View History

import os
import shutil
import tempfile
2020-03-22 05:41:01 +01:00
import time
from anki import Collection as aopen
2020-03-22 05:41:01 +01:00
# Between 2-4AM, shift the time back so test assumptions hold.
lt = time.localtime()
if lt.tm_hour >= 2 and lt.tm_hour < 4:
orig_time = time.time
def adjusted_time():
return orig_time() - 60 * 60 * 2
time.time = adjusted_time
else:
orig_time = None
2019-12-25 05:18:34 +01:00
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.
def getEmptyCol():
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.close(downgrade=False)
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
col = aopen(nam)
return col
2019-12-25 05:18:34 +01:00
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)
2019-12-25 05:18:34 +01:00
def getUpgradeDeckPath(name="anki12.anki"):
src = os.path.join(testDir, "support", name)
(fd, dst) = tempfile.mkstemp(suffix=".anki2")
shutil.copy(src, dst)
return dst
2019-12-25 05:18:34 +01:00
testDir = os.path.dirname(__file__)
2020-03-22 05:41:01 +01:00
def errorsAfterMidnight(func):
lt = time.localtime()
if lt.tm_hour < 4:
print("test disabled around cutoff", func)
else:
func()
def isNearCutoff():
return orig_time is not None