73 lines
2.5 KiB
Python
73 lines
2.5 KiB
Python
import os
|
|
import sqlite3
|
|
import tempfile
|
|
import unittest
|
|
|
|
from app.services.auto_update_db import load_auto_update_dict, merge_auto_update_settings
|
|
|
|
|
|
class AutoUpdateDbTests(unittest.TestCase):
|
|
def setUp(self) -> None:
|
|
self._root = tempfile.mkdtemp(prefix="wesp-auto-update-db-")
|
|
data = os.path.join(self._root, "data")
|
|
os.makedirs(data, exist_ok=True)
|
|
self._db = os.path.join(data, "recipes.db")
|
|
conn = sqlite3.connect(self._db)
|
|
conn.execute(
|
|
"""
|
|
CREATE TABLE auto_update_settings (
|
|
id INTEGER NOT NULL PRIMARY KEY,
|
|
enabled INTEGER NOT NULL DEFAULT 0,
|
|
auto_install INTEGER NOT NULL DEFAULT 0,
|
|
gitea_url VARCHAR(512) NOT NULL DEFAULT '',
|
|
gitea_owner VARCHAR(255) NOT NULL DEFAULT '',
|
|
gitea_repo VARCHAR(255) NOT NULL DEFAULT '',
|
|
repository_url VARCHAR(512) NOT NULL DEFAULT '',
|
|
check_interval_sec INTEGER NOT NULL DEFAULT 3600,
|
|
CHECK (id = 1)
|
|
)
|
|
"""
|
|
)
|
|
conn.commit()
|
|
conn.close()
|
|
|
|
def test_merge_inserts_and_updates_partially(self) -> None:
|
|
merge_auto_update_settings(
|
|
self._root,
|
|
{
|
|
"enabled": True,
|
|
"gitea_owner": "farm",
|
|
"gitea_repo": "wesp",
|
|
"check_interval_sec": 120,
|
|
},
|
|
)
|
|
au = load_auto_update_dict(self._root)
|
|
self.assertIsNotNone(au)
|
|
assert au is not None
|
|
self.assertTrue(au["enabled"])
|
|
self.assertFalse(au["auto_install"])
|
|
self.assertEqual(au["gitea_owner"], "farm")
|
|
self.assertEqual(au["gitea_repo"], "wesp")
|
|
self.assertEqual(au["check_interval_sec"], 120)
|
|
|
|
merge_auto_update_settings(
|
|
self._root,
|
|
{"auto_install": True, "gitea_url": "https://git.example.com"},
|
|
)
|
|
au2 = load_auto_update_dict(self._root)
|
|
self.assertIsNotNone(au2)
|
|
assert au2 is not None
|
|
self.assertTrue(au2["enabled"])
|
|
self.assertTrue(au2["auto_install"])
|
|
self.assertEqual(au2["gitea_url"], "https://git.example.com")
|
|
self.assertEqual(au2["gitea_owner"], "farm")
|
|
|
|
def test_merge_missing_db_raises(self) -> None:
|
|
empty_root = tempfile.mkdtemp(prefix="wesp-no-db-")
|
|
with self.assertRaises(FileNotFoundError):
|
|
merge_auto_update_settings(empty_root, {"enabled": True})
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|