test: 补上迁移对照、启动检查和调度去重测试
This commit is contained in:
@@ -28,4 +28,4 @@ class SystemConfig(Base):
|
||||
admin_sessions_valid_after: Mapped[datetime] = mapped_column(
|
||||
DateTime(timezone=True), nullable=False, server_default=text("CURRENT_TIMESTAMP")
|
||||
)
|
||||
key_check: Mapped[str] = mapped_column(Text, nullable=False, default="", server_default="")
|
||||
key_check: Mapped[str] = mapped_column(Text, nullable=False, default="", server_default=text("''"))
|
||||
|
||||
+13
-13
@@ -43,7 +43,7 @@ class FinishedCourse(Base):
|
||||
|
||||
id: Mapped[int] = mapped_column(primary_key=True)
|
||||
employee_id: Mapped[str] = mapped_column(String(64), nullable=False)
|
||||
knowledge_id: Mapped[str] = mapped_column(String(64), nullable=False, default="", server_default="")
|
||||
knowledge_id: Mapped[str] = mapped_column(String(64), nullable=False, default="", server_default=text("''"))
|
||||
|
||||
|
||||
class Account(Base):
|
||||
@@ -55,14 +55,14 @@ class Account(Base):
|
||||
refresh_paused: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False, server_default=sql_false())
|
||||
last_refresh_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
|
||||
credential_valid: Mapped[bool | None] = mapped_column(Boolean, nullable=True)
|
||||
month_hours: Mapped[str] = mapped_column(String(32), nullable=False, default="", server_default="")
|
||||
hours_month: Mapped[str] = mapped_column(String(7), nullable=False, default="", server_default="")
|
||||
month_hours: Mapped[str] = mapped_column(String(32), nullable=False, default="", server_default=text("''"))
|
||||
hours_month: Mapped[str] = mapped_column(String(7), nullable=False, default="", server_default=text("''"))
|
||||
monthly_target: Mapped[int] = mapped_column(nullable=False, default=600, server_default="600")
|
||||
proxy_scheme: Mapped[str] = mapped_column(String(16), nullable=False, default="", server_default="")
|
||||
proxy_host: Mapped[str] = mapped_column(String(255), nullable=False, default="", server_default="")
|
||||
proxy_scheme: Mapped[str] = mapped_column(String(16), nullable=False, default="", server_default=text("''"))
|
||||
proxy_host: Mapped[str] = mapped_column(String(255), nullable=False, default="", server_default=text("''"))
|
||||
proxy_port: Mapped[int | None] = mapped_column(nullable=True)
|
||||
proxy_username: Mapped[str] = mapped_column(String(128), nullable=False, default="", server_default="")
|
||||
proxy_password_cipher: Mapped[str] = mapped_column(Text, nullable=False, default="", server_default="")
|
||||
proxy_username: Mapped[str] = mapped_column(String(128), nullable=False, default="", server_default=text("''"))
|
||||
proxy_password_cipher: Mapped[str] = mapped_column(Text, nullable=False, default="", server_default=text("''"))
|
||||
study_start_day: Mapped[int] = mapped_column(nullable=False, default=8, server_default="8")
|
||||
study_start_hour: Mapped[int] = mapped_column(nullable=False, default=0, server_default="0")
|
||||
study_start_minute: Mapped[int] = mapped_column(nullable=False, default=0, server_default="0")
|
||||
@@ -70,7 +70,7 @@ class Account(Base):
|
||||
course_workers: Mapped[int] = mapped_column(nullable=False, default=1, server_default="1")
|
||||
use_proxy_pool: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False, server_default=sql_false())
|
||||
study_paused: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False, server_default=sql_false())
|
||||
study_stop_month: Mapped[str] = mapped_column(String(7), nullable=False, default="", server_default="")
|
||||
study_stop_month: Mapped[str] = mapped_column(String(7), nullable=False, default="", server_default=text("''"))
|
||||
study_ignore_window: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False, server_default=sql_false())
|
||||
study_schedule_enabled: Mapped[bool] = mapped_column(Boolean, nullable=False, default=True, server_default=sql_true())
|
||||
sessions_valid_after: Mapped[datetime] = mapped_column(
|
||||
@@ -92,14 +92,14 @@ class ProxyNode(Base):
|
||||
scheme: Mapped[str] = mapped_column(String(16), nullable=False)
|
||||
host: Mapped[str] = mapped_column(String(255), nullable=False)
|
||||
port: Mapped[int] = mapped_column(nullable=False)
|
||||
country: Mapped[str] = mapped_column(String(32), nullable=False, default="", server_default="")
|
||||
anonymity: Mapped[str] = mapped_column(String(32), nullable=False, default="", server_default="")
|
||||
source: Mapped[str] = mapped_column(String(64), nullable=False, default="", server_default="")
|
||||
country: Mapped[str] = mapped_column(String(32), nullable=False, default="", server_default=text("''"))
|
||||
anonymity: Mapped[str] = mapped_column(String(32), nullable=False, default="", server_default=text("''"))
|
||||
source: Mapped[str] = mapped_column(String(64), nullable=False, default="", server_default=text("''"))
|
||||
status: Mapped[str] = mapped_column(String(16), nullable=False, default="new", server_default="new")
|
||||
latency_ms: Mapped[int | None] = mapped_column(nullable=True)
|
||||
last_checked_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
|
||||
last_ok_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
|
||||
fail_reason: Mapped[str] = mapped_column(String(200), nullable=False, default="", server_default="")
|
||||
fail_reason: Mapped[str] = mapped_column(String(200), nullable=False, default="", server_default=text("''"))
|
||||
assigns: Mapped[int] = mapped_column(nullable=False, default=0, server_default="0")
|
||||
|
||||
|
||||
@@ -110,7 +110,7 @@ class ProxyPoolSync(Base):
|
||||
fetched_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
|
||||
checked_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
|
||||
running: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False, server_default=sql_false())
|
||||
message: Mapped[str] = mapped_column(String(200), nullable=False, default="", server_default="")
|
||||
message: Mapped[str] = mapped_column(String(200), nullable=False, default="", server_default=text("''"))
|
||||
alive_count: Mapped[int] = mapped_column(nullable=False, default=0, server_default="0")
|
||||
dead_count: Mapped[int] = mapped_column(nullable=False, default=0, server_default="0")
|
||||
new_count: Mapped[int] = mapped_column(nullable=False, default=0, server_default="0")
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
import asyncio
|
||||
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from euniv.autostudy import AutoStudyWorker, is_wanted
|
||||
from euniv.crypto import encrypt_secret
|
||||
from euniv.db import SessionLocal, engine
|
||||
from euniv.models import Account, Course
|
||||
from euniv.study_window import month_key
|
||||
|
||||
|
||||
def test_schema_matches_models() -> None:
|
||||
from alembic.autogenerate import compare_metadata
|
||||
from alembic.migration import MigrationContext
|
||||
|
||||
import admin.models # noqa: F401
|
||||
from euniv.models import Base
|
||||
|
||||
with engine.connect() as connection:
|
||||
context = MigrationContext.configure(
|
||||
connection,
|
||||
opts={"compare_server_default": True, "render_as_batch": True},
|
||||
)
|
||||
diff = compare_metadata(context, Base.metadata)
|
||||
assert diff == []
|
||||
|
||||
|
||||
def test_process_starts_and_health_reads_database() -> None:
|
||||
from euniv.api import app
|
||||
|
||||
with TestClient(app) as client:
|
||||
response = client.get("/api/health")
|
||||
assert response.status_code == 200
|
||||
body = response.json()
|
||||
assert body == {"ok": True, "database": True, "study": False}
|
||||
|
||||
|
||||
def test_tick_does_not_relaunch_and_stops_when_hours_met() -> None:
|
||||
launched: list[str] = []
|
||||
|
||||
async def hang(self, job, _course_list, _generation) -> None:
|
||||
launched.append(job[0])
|
||||
await asyncio.Event().wait()
|
||||
|
||||
with SessionLocal() as db:
|
||||
db.add(
|
||||
Account(
|
||||
employee_id="u-tick",
|
||||
password_cipher=encrypt_secret("pw"),
|
||||
credential_cipher=encrypt_secret('{"token":"x"}'),
|
||||
credential_valid=True,
|
||||
monthly_target=60,
|
||||
month_hours="0",
|
||||
hours_month=month_key(),
|
||||
study_ignore_window=True,
|
||||
study_schedule_enabled=True,
|
||||
)
|
||||
)
|
||||
db.add(Course(path="/kng/view/video/tick.html", title="定时课", kind="视频"))
|
||||
db.commit()
|
||||
|
||||
async def main() -> None:
|
||||
worker = AutoStudyWorker()
|
||||
worker._loop = asyncio.get_running_loop()
|
||||
worker._run_account = hang.__get__(worker, AutoStudyWorker)
|
||||
worker._tick()
|
||||
await asyncio.sleep(0)
|
||||
first = worker._tasks["u-tick"]
|
||||
assert launched == ["u-tick"]
|
||||
assert is_wanted("u-tick")
|
||||
worker._tick()
|
||||
await asyncio.sleep(0)
|
||||
assert worker._tasks["u-tick"] is first
|
||||
assert launched == ["u-tick"]
|
||||
with SessionLocal() as db:
|
||||
row = db.get(Account, "u-tick")
|
||||
assert row is not None
|
||||
row.month_hours = "60"
|
||||
row.hours_month = month_key()
|
||||
db.commit()
|
||||
worker._tick()
|
||||
assert "u-tick" not in launched[1:]
|
||||
assert launched == ["u-tick"]
|
||||
assert not is_wanted("u-tick")
|
||||
assert worker._tasks["u-tick"] is first
|
||||
first.cancel()
|
||||
await asyncio.gather(first, return_exceptions=True)
|
||||
|
||||
asyncio.run(main())
|
||||
Reference in New Issue
Block a user