feat: add unit tests for InjectAndConditions function

This commit is contained in:
wizardchen
2026-03-16 19:21:35 +08:00
parent 795f615b5e
commit 452db9f5c3
2 changed files with 57 additions and 4 deletions
+37
View File
@@ -411,3 +411,40 @@ func ExampleValidateSQL() {
// Error: High-risk SQL injection pattern detected
// Example 3 - Valid: false, Error count: 2
}
func TestInjectAndConditions(t *testing.T) {
tests := []struct {
name string
sql string
filter string
want string
}{
{
name: "existing WHERE with ORDER BY",
sql: "SELECT id, title FROM knowledges WHERE parse_status = 'completed' ORDER BY created_at DESC LIMIT 10",
filter: "knowledges.tenant_id = 123",
want: "SELECT id, title FROM knowledges WHERE knowledges.tenant_id = 123 AND (parse_status = 'completed') ORDER BY created_at DESC LIMIT 10",
},
{
name: "existing WHERE without tail clauses",
sql: "SELECT id FROM knowledges WHERE enable_status = 'enabled'",
filter: "knowledges.deleted_at IS NULL",
want: "SELECT id FROM knowledges WHERE knowledges.deleted_at IS NULL AND (enable_status = 'enabled')",
},
{
name: "no WHERE with ORDER BY",
sql: "SELECT id FROM knowledges ORDER BY created_at DESC",
filter: "knowledges.tenant_id = 123",
want: "SELECT id FROM knowledges WHERE knowledges.tenant_id = 123 ORDER BY created_at DESC",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := InjectAndConditions(tt.sql, tt.filter)
if got != tt.want {
t.Fatalf("InjectAndConditions() = %q, want %q", got, tt.want)
}
})
}
}