@@ -0,0 +1,107 @@
|
||||
"""Тесты feed_quality.settings_store (recipes.db)."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import os
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
from sqlalchemy import create_engine, text
|
||||
|
||||
from app import create_app, db
|
||||
from app.models.feed_quality_settings import FeedQualitySettings
|
||||
from app.services.feed_quality.settings_store import (
|
||||
create_feed_quality_settings_table,
|
||||
default_settings,
|
||||
get_feed_quality_settings,
|
||||
get_legacy_json_path,
|
||||
import_feed_quality_settings_json,
|
||||
import_legacy_json_to_db,
|
||||
normalize_settings,
|
||||
save_feed_quality_settings,
|
||||
)
|
||||
from tests.helpers.zootech_test_helpers import ZootechTestConfig
|
||||
|
||||
|
||||
class FeedQualitySettingsStoreTests(unittest.TestCase):
|
||||
def setUp(self) -> None:
|
||||
self.app = create_app(ZootechTestConfig)
|
||||
self.ctx = self.app.app_context()
|
||||
self.ctx.push()
|
||||
db.create_all()
|
||||
|
||||
def tearDown(self) -> None:
|
||||
db.session.remove()
|
||||
db.drop_all()
|
||||
self.ctx.pop()
|
||||
os.environ.pop("WESP_FEED_QUALITY_SETTINGS_PATH", None)
|
||||
|
||||
def test_defaults(self) -> None:
|
||||
d = default_settings()
|
||||
self.assertTrue(d["loading"]["enabled"])
|
||||
self.assertEqual(d["loading"]["warning_pct"], 10.0)
|
||||
|
||||
def test_normalize_critical_not_below_warning(self) -> None:
|
||||
out = normalize_settings({"loading": {"warning_pct": 12, "critical_pct": 8}})
|
||||
self.assertEqual(out["loading"]["critical_pct"], 12.0)
|
||||
|
||||
def test_save_and_load_from_db(self) -> None:
|
||||
save_feed_quality_settings({"loading": {"enabled": False, "warning_pct": 11}})
|
||||
loaded = get_feed_quality_settings()
|
||||
self.assertFalse(loaded["loading"]["enabled"])
|
||||
self.assertEqual(loaded["loading"]["warning_pct"], 11.0)
|
||||
row = db.session.get(FeedQualitySettings, 1)
|
||||
self.assertIsNotNone(row)
|
||||
payload = json.loads(row.payload)
|
||||
self.assertFalse(payload["loading"]["enabled"])
|
||||
|
||||
def test_partial_save_preserves_other_loading_fields(self) -> None:
|
||||
save_feed_quality_settings({"loading": {"warning_pct": 12.5}})
|
||||
save_feed_quality_settings({"loading": {"enabled": False}})
|
||||
loaded = get_feed_quality_settings()
|
||||
self.assertFalse(loaded["loading"]["enabled"])
|
||||
self.assertEqual(loaded["loading"]["warning_pct"], 12.5)
|
||||
|
||||
def test_import_legacy_json_to_db(self) -> None:
|
||||
tmp = tempfile.mkdtemp()
|
||||
path = os.path.join(tmp, "legacy.json")
|
||||
os.environ["WESP_FEED_QUALITY_SETTINGS_PATH"] = path
|
||||
with open(path, "w", encoding="utf-8") as fh:
|
||||
json.dump({"loading": {"enabled": False, "warning_pct": 9}}, fh)
|
||||
self.assertTrue(import_legacy_json_to_db())
|
||||
loaded = get_feed_quality_settings()
|
||||
self.assertFalse(loaded["loading"]["enabled"])
|
||||
self.assertEqual(loaded["loading"]["warning_pct"], 9.0)
|
||||
|
||||
def test_import_json_via_bind(self) -> None:
|
||||
tmp = tempfile.mkdtemp()
|
||||
path = os.path.join(tmp, "legacy_bind.json")
|
||||
os.environ["WESP_FEED_QUALITY_SETTINGS_PATH"] = path
|
||||
with open(path, "w", encoding="utf-8") as fh:
|
||||
json.dump({"unloading": {"warning_pct": 7}}, fh)
|
||||
db.session.remove()
|
||||
db.drop_all()
|
||||
engine = create_engine(self.app.config["SQLALCHEMY_DATABASE_URI"])
|
||||
create_feed_quality_settings_table(engine)
|
||||
self.assertTrue(import_feed_quality_settings_json(engine))
|
||||
with engine.connect() as conn:
|
||||
row = conn.execute(
|
||||
text("SELECT payload FROM feed_quality_settings WHERE id = 1")
|
||||
).fetchone()
|
||||
payload = json.loads(row[0])
|
||||
self.assertEqual(payload["unloading"]["warning_pct"], 7.0)
|
||||
|
||||
def test_get_creates_defaults_when_empty(self) -> None:
|
||||
loaded = get_feed_quality_settings()
|
||||
self.assertTrue(loaded["loading"]["enabled"])
|
||||
row = db.session.get(FeedQualitySettings, 1)
|
||||
self.assertIsNotNone(row)
|
||||
|
||||
def test_legacy_path_env(self) -> None:
|
||||
os.environ["WESP_FEED_QUALITY_SETTINGS_PATH"] = "/tmp/custom_fq.json"
|
||||
self.assertTrue(str(get_legacy_json_path()).endswith("custom_fq.json"))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user