""" 測試 system_functions CRUD API """ import psycopg2 import json conn = psycopg2.connect( host="10.1.0.20", port=5433, database="hr_portal", user="admin", password="DC1qaz2wsx" ) cur = conn.cursor() print("=" * 60) print("Test system_functions API") print("=" * 60) # Test 1: 取得所有系統功能 print("\n[Test 1] Get all system functions") cur.execute(""" SELECT id, code, name, function_type, upper_function_id, "order" FROM system_functions ORDER BY "order"; """) results = cur.fetchall() print(f"Total: {len(results)} records") for r in results: indent = " " if r[4] > 0 else "" node_type = "NODE" if r[3] == 1 else "FUNC" print(f"{indent}[{r[0]}] {r[1]} - {r[2]} ({node_type})") # Test 2: 取得樹狀結構 print("\n[Test 2] Get tree structure") cur.execute(""" SELECT id, code, name, function_type FROM system_functions WHERE upper_function_id = 0 ORDER BY "order"; """) root_nodes = cur.fetchall() for node in root_nodes: node_type = "NODE" if node[3] == 1 else "FUNC" print(f"[{node[0]}] {node[2]} ({node_type})") # 取得子功能 cur.execute(""" SELECT id, code, name, function_type FROM system_functions WHERE upper_function_id = %s ORDER BY "order"; """, (node[0],)) children = cur.fetchall() for child in children: child_type = "NODE" if child[3] == 1 else "FUNC" print(f" ├── [{child[0]}] {child[2]} ({child_type})") # Test 3: 篩選功能類型 print("\n[Test 3] Filter by function_type=1 (nodes)") cur.execute(""" SELECT id, code, name FROM system_functions WHERE function_type = 1 ORDER BY "order"; """) nodes = cur.fetchall() print(f"Total nodes: {len(nodes)}") for n in nodes: print(f" [{n[0]}] {n[1]} - {n[2]}") # Test 4: 篩選系統管理功能 print("\n[Test 4] Filter by is_mana=true") cur.execute(""" SELECT id, code, name FROM system_functions WHERE is_mana = true ORDER BY "order"; """) mana_funcs = cur.fetchall() print(f"Total: {len(mana_funcs)} records") for f in mana_funcs: print(f" [{f[0]}] {f[1]} - {f[2]}") print("\n" + "=" * 60) print("All tests passed!") print("=" * 60) cur.close() conn.close()