diff --git a/pylib/tests/shared.py b/pylib/tests/shared.py index df83eac73..a89252ada 100644 --- a/pylib/tests/shared.py +++ b/pylib/tests/shared.py @@ -1,9 +1,22 @@ import os import shutil import tempfile +import time from anki import Collection as aopen +# 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 + def assertException(exception, func): found = False @@ -48,3 +61,15 @@ def getUpgradeDeckPath(name="anki12.anki"): testDir = os.path.dirname(__file__) + + +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 diff --git a/pylib/tests/test_exporting.py b/pylib/tests/test_exporting.py index 1ab6b1ee1..db29d018e 100644 --- a/pylib/tests/test_exporting.py +++ b/pylib/tests/test_exporting.py @@ -6,6 +6,7 @@ import tempfile from anki import Collection as aopen from anki.exporting import * from anki.importing import Anki2Importer +from tests.shared import errorsAfterMidnight from tests.shared import getEmptyCol as getEmptyColOrig @@ -97,6 +98,7 @@ def test_export_ankipkg(): e.exportInto(newname) +@errorsAfterMidnight def test_export_anki_due(): setup1() deck = getEmptyCol() diff --git a/pylib/tests/test_find.py b/pylib/tests/test_find.py index 10758b56e..66f462c84 100644 --- a/pylib/tests/test_find.py +++ b/pylib/tests/test_find.py @@ -3,7 +3,7 @@ import pytest from anki.consts import * from anki.rsbackend import BuiltinSortKind -from tests.shared import getEmptyCol +from tests.shared import getEmptyCol, isNearCutoff class DummyCollection: @@ -189,19 +189,27 @@ def test_findCards(): assert len(deck.findCards("prop:ease>2")) == 1 assert len(deck.findCards("-prop:ease>2")) > 1 # recently failed - assert len(deck.findCards("rated:1:1")) == 0 - assert len(deck.findCards("rated:1:2")) == 0 - c = deck.sched.getCard() - deck.sched.answerCard(c, 2) - assert len(deck.findCards("rated:1:1")) == 0 - assert len(deck.findCards("rated:1:2")) == 1 - c = deck.sched.getCard() - deck.sched.answerCard(c, 1) - assert len(deck.findCards("rated:1:1")) == 1 - assert len(deck.findCards("rated:1:2")) == 1 - assert len(deck.findCards("rated:1")) == 2 - assert len(deck.findCards("rated:0:2")) == 0 - assert len(deck.findCards("rated:2:2")) == 1 + if not isNearCutoff(): + assert len(deck.findCards("rated:1:1")) == 0 + assert len(deck.findCards("rated:1:2")) == 0 + c = deck.sched.getCard() + deck.sched.answerCard(c, 2) + assert len(deck.findCards("rated:1:1")) == 0 + assert len(deck.findCards("rated:1:2")) == 1 + c = deck.sched.getCard() + deck.sched.answerCard(c, 1) + assert len(deck.findCards("rated:1:1")) == 1 + assert len(deck.findCards("rated:1:2")) == 1 + assert len(deck.findCards("rated:1")) == 2 + assert len(deck.findCards("rated:0:2")) == 0 + assert len(deck.findCards("rated:2:2")) == 1 + # added + assert len(deck.findCards("added:0")) == 0 + deck.db.execute("update cards set id = id - 86400*1000 where id = ?", id) + assert len(deck.findCards("added:1")) == deck.cardCount() - 1 + assert len(deck.findCards("added:2")) == deck.cardCount() + else: + print("some find tests disabled near cutoff") # empty field assert len(deck.findCards("front:")) == 0 f = deck.newNote() @@ -215,11 +223,6 @@ def test_findCards(): assert len(deck.findCards("-(tag:monkey OR tag:sheep)")) == 6 assert len(deck.findCards("tag:monkey or (tag:sheep sheep)")) == 2 assert len(deck.findCards("tag:monkey or (tag:sheep octopus)")) == 1 - # added - assert len(deck.findCards("added:0")) == 0 - deck.db.execute("update cards set id = id - 86400*1000 where id = ?", id) - assert len(deck.findCards("added:1")) == deck.cardCount() - 1 - assert len(deck.findCards("added:2")) == deck.cardCount() # flag with pytest.raises(Exception): deck.findCards("flag:12") diff --git a/pylib/tests/test_schedv2.py b/pylib/tests/test_schedv2.py index 0df6c2005..1feb94f4c 100644 --- a/pylib/tests/test_schedv2.py +++ b/pylib/tests/test_schedv2.py @@ -16,17 +16,6 @@ def getEmptyCol(): return col -# 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 - - def test_clock(): d = getEmptyCol() if (d.sched.dayCutoff - intTime()) < 10 * 60: