feat: add init unit test
All checks were successful
Test CI / test speed (push) Successful in 17s

This commit is contained in:
sangge 2024-01-06 13:28:39 +08:00
parent 42765f9b8d
commit 99a39a0777
2 changed files with 41 additions and 10 deletions

View File

@ -20,7 +20,9 @@ app = FastAPI(lifespan=lifespan)
def init():
asyncio.create_task(receive_heartbeat_internal())
init_db()
def init_db():
conn = sqlite3.connect("server.db")
cursor = conn.cursor()
# init table: id: int; ip: TEXT

View File

@ -0,0 +1,29 @@
import unittest
import sqlite3
import os
from server import *
class TestServer(unittest.TestCase):
def test_init_creates_table(self):
# 执行初始化函数
init_db()
conn = sqlite3.connect("server.db")
cursor = conn.cursor()
# 检查表是否被正确创建
cursor.execute(
"SELECT name FROM sqlite_master WHERE type='table' AND name='nodes'"
)
tables = cursor.fetchall()
self.assertTrue(any("nodes" in table for table in tables))
# 关闭数据库连接
conn.close()
os.remove("server.db")
if __name__ == "__main__":
unittest.main()