test: 全面迁移 pytest 并加入格式门禁
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
import unittest
|
||||
from datetime import datetime, timedelta, timezone
|
||||
|
||||
from sqlalchemy import create_engine
|
||||
@@ -9,8 +8,8 @@ from web.backend.models import Account, LoginTask, User
|
||||
from web.backend.routers.cookies import _order_cookie_tasks
|
||||
|
||||
|
||||
class CustomCookieOrderTests(unittest.TestCase):
|
||||
def setUp(self):
|
||||
class TestCustomCookieOrder:
|
||||
def setup_method(self):
|
||||
self.engine = create_engine("sqlite://")
|
||||
Base.metadata.create_all(self.engine)
|
||||
self.db = Session(self.engine)
|
||||
@@ -27,41 +26,32 @@ class CustomCookieOrderTests(unittest.TestCase):
|
||||
self.db.flush()
|
||||
# 完成时间特意与用户输入顺序相反。
|
||||
for index, name in enumerate(("account-a", "account-b", "account-c")):
|
||||
self.db.add(LoginTask(
|
||||
batch_id=f"batch-{name}",
|
||||
account_id=accounts[name].id,
|
||||
status="success",
|
||||
created_by=self.user.id,
|
||||
finished_at=now + timedelta(minutes=index),
|
||||
))
|
||||
self.db.add(
|
||||
LoginTask(
|
||||
batch_id=f"batch-{name}",
|
||||
account_id=accounts[name].id,
|
||||
status="success",
|
||||
created_by=self.user.id,
|
||||
finished_at=now + timedelta(minutes=index),
|
||||
)
|
||||
)
|
||||
self.db.commit()
|
||||
|
||||
def tearDown(self):
|
||||
def teardown_method(self):
|
||||
self.db.close()
|
||||
Base.metadata.drop_all(self.engine)
|
||||
self.engine.dispose()
|
||||
|
||||
def test_custom_cookie_order_follows_input_not_finished_time(self):
|
||||
selected_names = ["account-c", "account-a", "account-b"]
|
||||
tasks = (
|
||||
_order_cookie_tasks(
|
||||
self.db.query(LoginTask)
|
||||
.join(Account, LoginTask.account_id == Account.id)
|
||||
.filter(Account.username.in_(selected_names)),
|
||||
selected_names,
|
||||
)
|
||||
.all()
|
||||
)
|
||||
tasks = _order_cookie_tasks(
|
||||
self.db.query(LoginTask)
|
||||
.join(Account, LoginTask.account_id == Account.id)
|
||||
.filter(Account.username.in_(selected_names)),
|
||||
selected_names,
|
||||
).all()
|
||||
|
||||
account_names = {
|
||||
account.id: account.username
|
||||
for account in self.db.query(Account).all()
|
||||
account.id: account.username for account in self.db.query(Account).all()
|
||||
}
|
||||
self.assertEqual(
|
||||
[account_names[task.account_id] for task in tasks],
|
||||
selected_names,
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
assert [account_names[task.account_id] for task in tasks] == selected_names
|
||||
|
||||
Reference in New Issue
Block a user