import sys import os # 将当前目录添加到Python模块搜索路径 sys.path.append(os.path.dirname(os.path.abspath(__file__))) import db_adapter import json def test_users(): users = db_adapter.get_users() print(f"用户数量: {len(users)}") if users: print(f"第一个用户示例: {users[0]['username']}") return len(users) > 0 def test_game_states(): states = db_adapter.get_game_states() print(f"游戏状态数量: {len(states)}") if states: # 查看第一个状态的键 print(f"第一个游戏状态键: {list(states[0].keys())}") return True def test_custom_cards(): cards_data = db_adapter.get_custom_cards() cards = cards_data.get('cards', []) print(f"自定义卡牌数量: {len(cards)}") if cards: print(f"第一个卡牌标题: {cards[0]['title']}") return True def test_card_votes(): votes_data = db_adapter.get_card_votes() user_votes = votes_data.get('user_votes', {}) card_votes = votes_data.get('card_votes', {}) print(f"用户投票记录数量: {len(user_votes)}") print(f"卡牌投票统计数量: {len(card_votes)}") return True def test_sessions(): sessions_data = db_adapter.get_sessions() sessions = sessions_data.get('active_sessions', []) print(f"活跃会话数量: {len(sessions)}") return True def main(): print("开始测试SQLite数据库适配器...") tests = [ ("用户数据测试", test_users), ("游戏状态测试", test_game_states), ("自定义卡牌测试", test_custom_cards), ("卡牌投票测试", test_card_votes), ("会话数据测试", test_sessions) ] all_passed = True for test_name, test_func in tests: print(f"\n执行{test_name}...") try: result = test_func() if result: print(f"{test_name} 通过!") else: print(f"{test_name} 失败!") all_passed = False except Exception as e: print(f"{test_name} 出错: {str(e)}") all_passed = False print("\n测试总结:") if all_passed: print("所有测试通过!") else: print("有些测试失败,请检查日志") if __name__ == "__main__": main()