Files
MyGoNavi/internal/app/methods_db_transaction_test.go
Syngnat 1ae2b74279 🐛 fix(sql-parser): 修复尾随注释导致事务与只读判定异常
- 过滤分号后的纯注释语句并保留数据库可执行版本注释
- 前后端按数据库方言统一处理双横线、井号与块注释
- 修复事务选择、只读保护、SQL 审计及 AI 风险分析误判
- 补充流式 SQL、事务执行与方言解析回归测试
2026-07-13 12:52:56 +08:00

67 lines
2.2 KiB
Go

package app
import "testing"
func TestShouldUseManagedSQLTransaction_UnsupportedTypesUsePlainExecution(t *testing.T) {
t.Parallel()
cases := []struct {
dbType string
query string
}{
{dbType: "trino", query: "UPDATE hive.default.orders SET status = 'done'"},
{dbType: "tdengine", query: "INSERT INTO meters(ts, current) VALUES (NOW, 10.2)"},
{dbType: "clickhouse", query: `INSERT INTO events FORMAT JSONEachRow {"id":1}`},
{dbType: "iotdb", query: "INSERT INTO root.ln.wf01.wt01(timestamp,status) VALUES(1,true)"},
}
for _, tc := range cases {
tc := tc
t.Run(tc.dbType, func(t *testing.T) {
t.Parallel()
if shouldUseManagedSQLTransaction(tc.dbType, tc.query) {
t.Fatalf("expected %s DML to skip SQL editor managed transactions", tc.dbType)
}
if shouldUseManagedSQLTransaction(tc.dbType, "BEGIN; "+tc.query+"; COMMIT;") {
t.Fatalf("expected %s explicit transactions to stay unmanaged", tc.dbType)
}
})
}
}
func TestShouldUseManagedSQLTransaction_OracleAnonymousBlockWithDMLUsesManagedTransaction(t *testing.T) {
t.Parallel()
query := `BEGIN
UPDATE users SET name = 'new' WHERE id = 1;
DELETE FROM audit_logs WHERE user_id = 1;
END;`
if !shouldUseManagedSQLTransaction("oracle", query) {
t.Fatal("expected Oracle anonymous block with DML to use SQL editor managed transaction")
}
}
func TestShouldUseManagedSQLTransaction_OracleReadOnlyAnonymousBlockStaysUnmanaged(t *testing.T) {
t.Parallel()
query := `BEGIN
NULL;
END;`
if shouldUseManagedSQLTransaction("oracle", query) {
t.Fatal("expected Oracle read-only anonymous block to stay unmanaged")
}
}
func TestShouldUseManagedSQLTransaction_UsesDialectCommentRules(t *testing.T) {
t.Parallel()
if !shouldUseManagedSQLTransaction("mysql", "DELETE FROM users WHERE id = 1; -- pending") {
t.Fatal("expected a valid MySQL trailing comment to preserve the managed transaction")
}
if shouldUseManagedSQLTransaction("mysql", "DELETE FROM users WHERE id = 1;--comment") {
t.Fatal("expected compact MySQL double-dash text to remain an executable statement")
}
if !shouldUseManagedSQLTransaction("postgres", "DELETE FROM users WHERE id = 1; /*! MySQL-only comment */") {
t.Fatal("expected PostgreSQL to ignore a MySQL-only block comment")
}
}