ops_repo_error_where_test.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package repository
  2. import (
  3. "strings"
  4. "testing"
  5. "github.com/Wei-Shaw/sub2api/internal/service"
  6. )
  7. func TestBuildOpsErrorLogsWhere_QueryUsesQualifiedColumns(t *testing.T) {
  8. filter := &service.OpsErrorLogFilter{
  9. Query: "ACCESS_DENIED",
  10. }
  11. where, args := buildOpsErrorLogsWhere(filter)
  12. if where == "" {
  13. t.Fatalf("where should not be empty")
  14. }
  15. if len(args) != 1 {
  16. t.Fatalf("args len = %d, want 1", len(args))
  17. }
  18. if !strings.Contains(where, "e.request_id ILIKE $") {
  19. t.Fatalf("where should include qualified request_id condition: %s", where)
  20. }
  21. if !strings.Contains(where, "e.client_request_id ILIKE $") {
  22. t.Fatalf("where should include qualified client_request_id condition: %s", where)
  23. }
  24. if !strings.Contains(where, "e.error_message ILIKE $") {
  25. t.Fatalf("where should include qualified error_message condition: %s", where)
  26. }
  27. }
  28. func TestBuildOpsErrorLogsWhere_UserQueryUsesExistsSubquery(t *testing.T) {
  29. filter := &service.OpsErrorLogFilter{
  30. UserQuery: "admin@",
  31. }
  32. where, args := buildOpsErrorLogsWhere(filter)
  33. if where == "" {
  34. t.Fatalf("where should not be empty")
  35. }
  36. if len(args) != 1 {
  37. t.Fatalf("args len = %d, want 1", len(args))
  38. }
  39. if !strings.Contains(where, "EXISTS (SELECT 1 FROM users u WHERE u.id = e.user_id AND u.email ILIKE $") {
  40. t.Fatalf("where should include EXISTS user email condition: %s", where)
  41. }
  42. }