Files
routine/tests/test_storage.py

320 lines
14 KiB
Python

"""Where the routine and history are kept (routine/storage.py, routine/paths.py), on fake data in
temporary folders: moving a file safely, the first run, existing users, renamed config keys.
Run: python3 -m unittest tests.test_storage"""
import datetime as dt
import json
import os
import sys
import tempfile
import unittest
from unittest import mock
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, ROOT)
from routine import model, paths, storage # noqa: E402
from routine.store import Store # noqa: E402
FAKE = {"rev": 5, "dayStartsAt": 4, "blocks": [
{"id": "am", "label": "Morning", "start": 7, "end": 9, "color": "#FFEEAA", "items": [
{"id": "water", "label": "glass of water"},
{"id": "plan", "label": "plan the day", "items": [
{"id": "list", "label": "write a list", "items": [{"id": "short", "label": "keep it short"}]},
{"id": "tip", "label": "five minutes is enough", "hint": True}]},
{"id": "quiet", "label": "sit quietly", "calm": True}]},
{"id": "pm", "label": "Evening", "start": 19, "end": 22, "color": "#CCBBFF", "items": [
{"id": "tidy", "label": "tidy up"}]}]}
class Base(unittest.TestCase):
def setUp(self):
self._tmp = tempfile.TemporaryDirectory()
self.tmp = self._tmp.name
self.addCleanup(self._tmp.cleanup)
p = mock.patch.object(paths, "DATA", os.path.join(self.tmp, "data"))
p.start()
self.addCleanup(p.stop)
def path(self, *parts):
return os.path.join(self.tmp, *parts)
def write(self, path, text):
os.makedirs(os.path.dirname(path), exist_ok=True)
with open(path, "w") as f:
f.write(text)
def read(self, path):
with open(path) as f:
return f.read()
class Move(Base):
def test_moves_and_removes_the_original(self):
src, dst = self.path("a", "schedule.json"), self.path("b", "deep", "schedule.json")
self.write(src, json.dumps(FAKE))
self.assertEqual(storage.move_file(src, dst, check=storage.check_schedule), "moved")
self.assertFalse(os.path.exists(src))
self.assertEqual(json.loads(self.read(dst)), FAKE)
self.assertEqual(os.listdir(self.path("b", "deep")), ["schedule.json"]) # no .partial left behind
def test_same_place_does_nothing(self):
src = self.path("a", "x.json")
self.write(src, "{}")
self.assertEqual(storage.move_file(src, src), "same")
self.assertTrue(os.path.exists(src))
def test_no_original_only_changes_the_place(self):
self.assertEqual(storage.move_file(self.path("a", "x.json"), self.path("b", "x.json")), "nothing")
self.assertTrue(os.path.isdir(self.path("b")))
def test_existing_target_is_never_overwritten_without_asking(self):
src, dst = self.path("a", "x.json"), self.path("b", "x.json")
self.write(src, "mine")
self.write(dst, "theirs")
with self.assertRaises(storage.TargetTaken):
storage.move_file(src, dst)
self.assertEqual(self.read(src), "mine")
self.assertEqual(self.read(dst), "theirs")
def test_use_that_one_leaves_both_alone(self):
src, dst = self.path("a", "x.json"), self.path("b", "x.json")
self.write(src, "mine")
self.write(dst, "theirs")
self.assertEqual(storage.move_file(src, dst, "use"), "used")
self.assertEqual(self.read(src), "mine")
self.assertEqual(self.read(dst), "theirs")
def test_use_that_one_refuses_a_broken_file(self):
src, dst = self.path("a", "schedule.json"), self.path("b", "schedule.json")
self.write(src, json.dumps(FAKE))
self.write(dst, "not json")
with self.assertRaises(storage.MoveError):
storage.move_file(src, dst, "use", storage.check_schedule)
self.assertEqual(json.loads(self.read(src)), FAKE)
def test_replace_keeps_theirs_beside(self):
src, dst = self.path("a", "x.json"), self.path("b", "x.json")
self.write(src, "mine")
self.write(dst, "theirs")
self.assertEqual(storage.move_file(src, dst, "replace"), "moved")
self.assertFalse(os.path.exists(src))
self.assertEqual(self.read(dst), "mine")
kept = [f for f in os.listdir(self.path("b")) if f != "x.json"]
self.assertEqual(len(kept), 1)
self.assertTrue(kept[0].startswith("x.replaced-") and kept[0].endswith(".json"), kept)
self.assertEqual(self.read(self.path("b", kept[0])), "theirs")
def test_failed_copy_leaves_the_original(self):
src, dst = self.path("a", "x.json"), self.path("b", "x.json")
self.write(src, "mine")
def broken(a, b):
with open(b, "w") as f:
f.write("mi") # a short write, then the disk fills up
raise OSError(28, "No space left on device")
with self.assertRaises(storage.MoveError):
storage.move_file(src, dst, copy=broken)
self.assertEqual(self.read(src), "mine")
self.assertFalse(os.path.exists(dst))
self.assertEqual(os.listdir(self.path("b")), [])
def test_copy_that_differs_is_caught(self):
src, dst = self.path("a", "x.json"), self.path("b", "x.json")
self.write(src, "mine")
self.write(dst, "theirs")
def garbled(a, b):
with open(b, "w") as f:
f.write("mime")
with self.assertRaises(storage.MoveError):
storage.move_file(src, dst, "replace", copy=garbled)
self.assertEqual(self.read(src), "mine")
self.assertEqual(self.read(dst), "theirs") # theirs is back where it was
self.assertEqual(os.listdir(self.path("b")), ["x.json"])
def test_failing_check_leaves_both(self):
src, dst = self.path("a", "routine.db"), self.path("b", "routine.db")
self.write(src, "not a database")
with self.assertRaises(storage.MoveError):
storage.move_file(src, dst, check=storage.check_db)
self.assertEqual(self.read(src), "not a database")
self.assertFalse(os.path.exists(dst))
def test_database_moves_closed_and_reopens(self):
src, dst = self.path("a", "routine.db"), self.path("b", "routine.db")
st = Store(src)
day = model.new_day(FAKE, dt.date(2026, 3, 2))
model.set_state(day, "am/water", model.DONE)
st.put(day)
st.set_meta("seq", 12)
st.close()
self.assertEqual(storage.move_file(src, dst, check=storage.check_db), "moved")
self.assertFalse(os.path.exists(src))
st.open(dst)
self.assertEqual(st.path, dst)
self.assertEqual(model.state(st.get("2026-03-02"), "am/water"), model.DONE)
self.assertEqual(st.meta("seq"), "12")
st.close()
def test_database_in_use_is_not_moved(self):
src, dst = self.path("a", "routine.db"), self.path("b", "routine.db")
st = Store(src)
st.db.execute("PRAGMA journal_mode=WAL")
st.put(model.new_day(FAKE, dt.date(2026, 3, 2)))
with self.assertRaises(storage.MoveError): # still open, its -wal beside it
storage.move_file(src, dst, check=storage.check_db)
self.assertFalse(os.path.exists(dst))
st.close() # close() folds the log in
self.assertFalse(os.path.exists(src + "-wal"))
self.assertEqual(storage.move_file(src, dst, check=storage.check_db), "moved")
class FirstRun(Base):
def cfg(self):
return {"schedule_dir": self.path("sched"), "db_dir": self.path("hist")}
def template(self):
p = self.path("repo", "schedule.json")
self.write(p, json.dumps({"rev": 0, "dayStartsAt": 4, "blocks": [
{"id": "t", "label": "Template", "start": 7, "end": 8, "color": "#FFFFFF", "items": [{"label": "x"}]}]}))
return p
def test_fresh_start_copies_the_template(self):
cfg = self.cfg()
sch, how = storage.load_schedule(cfg, self.template())
self.assertEqual(how, "template")
self.assertEqual(sch["blocks"][0]["id"], "t")
self.assertEqual(json.loads(self.read(paths.schedule_path(cfg))), sch)
def test_the_users_copy_wins_whatever_its_rev(self):
cfg = self.cfg()
mine = dict(FAKE, rev=0)
self.write(paths.schedule_path(cfg), json.dumps(mine))
tpl = self.template()
with open(tpl) as f:
t = json.load(f)
t["rev"] = 99
self.write(tpl, json.dumps(t))
sch, how = storage.load_schedule(cfg, tpl)
self.assertEqual((how, sch), ("yours", mine))
self.assertEqual(model.load_schedule(paths.schedule_path(cfg), tpl), mine)
def history(self, cfg, days=3):
st = Store(paths.db_path(cfg))
for k in range(days):
st.put(model.new_day(FAKE, dt.date(2026, 3, 1) + dt.timedelta(days=k)))
st.close()
def test_existing_user_adopts_the_saved_copy_not_the_template(self):
cfg = self.cfg()
self.history(cfg)
self.write(os.path.join(paths.DATA, "schedule.from-repo.json"), json.dumps(FAKE))
sch, how = storage.load_schedule(cfg, self.template())
self.assertEqual((how, sch), ("adopted", FAKE))
self.assertEqual(json.loads(self.read(paths.schedule_path(cfg))), FAKE)
self.assertTrue(os.path.exists(os.path.join(paths.DATA, "schedule.from-repo.json"))) # kept
sch, how = storage.load_schedule(cfg, self.template()) # once only
self.assertEqual(how, "yours")
def test_existing_user_without_a_saved_copy_gets_the_routine_back_from_the_history(self):
cfg = self.cfg()
self.history(cfg)
sch, how = storage.load_schedule(cfg, self.template())
self.assertEqual(how, "rebuilt")
self.assertEqual(sch["rev"], FAKE["rev"])
# the same tasks, ids, nesting, hints and calm marks: the history carries on
day = dt.date(2026, 3, 9)
self.assertEqual(model.snapshot(sch, day), model.snapshot(FAKE, day))
plan = sch["blocks"][0]["items"][1]
self.assertEqual([x["id"] for x in plan["items"]], ["list", "tip"])
self.assertEqual(plan["items"][0]["items"][0]["id"], "short")
self.assertTrue(plan["items"][1]["hint"])
self.assertTrue(sch["blocks"][0]["items"][2]["calm"])
def test_rebuild_uses_the_most_recent_day(self):
cfg = self.cfg()
self.history(cfg)
newer = json.loads(json.dumps(FAKE))
newer["rev"] = 6
newer["blocks"][1]["items"].append({"id": "read", "label": "read a little"})
st = Store(paths.db_path(cfg))
st.put(model.new_day(newer, dt.date(2026, 3, 10)))
st.close()
sch, how = storage.load_schedule(cfg, self.template())
self.assertEqual(how, "rebuilt")
self.assertEqual([x["id"] for x in sch["blocks"][1]["items"]], ["tidy", "read"])
def test_unreadable_copy_is_set_aside_not_lost(self):
cfg = self.cfg()
self.history(cfg)
self.write(paths.schedule_path(cfg), "{ half a fi")
sch, how = storage.load_schedule(cfg, self.template())
self.assertEqual(how, "rebuilt") # not the template: there's a history
aside = [f for f in os.listdir(self.path("sched")) if "unreadable" in f]
self.assertEqual(len(aside), 1)
self.assertEqual(self.read(self.path("sched", aside[0])), "{ half a fi")
def test_locations(self):
self.assertEqual(paths.schedule_path({}), os.path.join(paths.DATA, "schedule.json"))
self.assertEqual(paths.db_path({"db_dir": ""}), os.path.join(paths.DATA, "routine.db"))
self.assertEqual(paths.db_path({"db_dir": self.path("x")}), self.path("x", "routine.db"))
class Config(Base):
def setUp(self):
super().setUp()
p = mock.patch.dict(paths.RENAMED, {"old_url": "sync_url", "old_token": "sync_token"})
p.start()
self.addCleanup(p.stop)
def test_renamed_keys_move_once(self):
old, new = list(paths.RENAMED.items())[0]
saved = {old: "value", "mode": "dark"}
self.assertEqual(paths.migrate_config(saved), [old] + [])
self.assertEqual(saved, {new: "value", "mode": "dark"})
self.assertEqual(paths.migrate_config(saved), [])
def test_new_key_wins_over_an_old_one(self):
old, new = list(paths.RENAMED.items())[0]
saved = {old: "stale", new: "fresh"}
paths.migrate_config(saved)
self.assertEqual(saved, {new: "fresh"})
def test_load_config_writes_the_new_names_back(self):
conf = self.path("conf", "config.json")
old = list(paths.RENAMED)
self.write(conf, json.dumps({old[0]: "http://example.invalid:1", old[1]: "fake-token"}))
with mock.patch.object(paths, "CONFIG", conf), mock.patch.object(paths, "CONFIG_DIR", self.path("conf")), \
mock.patch("sys.stderr"):
cfg = paths.load_config()
self.assertEqual(cfg["sync_url"], "http://example.invalid:1")
self.assertEqual(cfg["sync_token"], "fake-token")
on_disk = json.loads(self.read(conf))
self.assertFalse(set(old) & set(on_disk))
self.assertEqual(on_disk["sync_token"], "fake-token")
class Template(unittest.TestCase):
def test_the_shipped_template_shows_everything(self):
with open(paths.SCHEDULE) as f:
t = json.load(f)
self.assertEqual(t["rev"], 0) # any routine of the user's own outranks it
flat = []
def walk(items, depth):
for x in items:
flat.append((x, depth))
walk(x.get("items", []), depth + 1)
for b in t["blocks"]:
walk(b["items"], 0)
self.assertTrue(any(b.get("days") for b in t["blocks"]))
self.assertTrue(any(x.get("days") for x, _ in flat))
self.assertTrue(any(x.get("hint") for x, _ in flat))
self.assertTrue(any(x.get("calm") for x, _ in flat))
self.assertGreaterEqual(max(d for _, d in flat), 2)
for k in range(7): # every day of the week makes a valid day
model.new_day(t, dt.date(2026, 3, 1) + dt.timedelta(days=k))
if __name__ == "__main__":
unittest.main()