ops_repo_system_logs_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package repository
  2. import (
  3. "strings"
  4. "testing"
  5. "time"
  6. "github.com/Wei-Shaw/sub2api/internal/service"
  7. )
  8. func TestBuildOpsSystemLogsWhere_WithClientRequestIDAndUserID(t *testing.T) {
  9. start := time.Date(2026, 2, 1, 0, 0, 0, 0, time.UTC)
  10. end := time.Date(2026, 2, 2, 0, 0, 0, 0, time.UTC)
  11. userID := int64(12)
  12. apiKeyID := int64(56)
  13. accountID := int64(34)
  14. filter := &service.OpsSystemLogFilter{
  15. StartTime: &start,
  16. EndTime: &end,
  17. Host: "api-node-1",
  18. Level: "warn",
  19. Component: "http.access",
  20. RequestID: "req-1",
  21. ClientRequestID: "creq-1",
  22. UserID: &userID,
  23. APIKeyID: &apiKeyID,
  24. AccountID: &accountID,
  25. Platform: "openai",
  26. Model: "gpt-5",
  27. Query: "timeout",
  28. }
  29. where, args, hasConstraint := buildOpsSystemLogsWhere(filter)
  30. if !hasConstraint {
  31. t.Fatalf("expected hasConstraint=true")
  32. }
  33. if where == "" {
  34. t.Fatalf("where should not be empty")
  35. }
  36. if len(args) != 13 {
  37. t.Fatalf("args len = %d, want 13", len(args))
  38. }
  39. if !contains(where, "l.host = $") {
  40. t.Fatalf("where should include host condition: %s", where)
  41. }
  42. if !contains(where, "COALESCE(l.client_request_id,'') = $") {
  43. t.Fatalf("where should include client_request_id condition: %s", where)
  44. }
  45. if !contains(where, "l.user_id = $") {
  46. t.Fatalf("where should include user_id condition: %s", where)
  47. }
  48. if !contains(where, "l.api_key_id = $") {
  49. t.Fatalf("where should include api_key_id condition: %s", where)
  50. }
  51. }
  52. func TestBuildOpsSystemLogsCleanupWhere_RequireConstraint(t *testing.T) {
  53. where, args, hasConstraint := buildOpsSystemLogsCleanupWhere(&service.OpsSystemLogCleanupFilter{})
  54. if hasConstraint {
  55. t.Fatalf("expected hasConstraint=false")
  56. }
  57. if where == "" {
  58. t.Fatalf("where should not be empty")
  59. }
  60. if len(args) != 0 {
  61. t.Fatalf("args len = %d, want 0", len(args))
  62. }
  63. }
  64. func TestBuildOpsSystemLogsCleanupWhere_WithClientRequestIDAndUserID(t *testing.T) {
  65. userID := int64(9)
  66. apiKeyID := int64(10)
  67. filter := &service.OpsSystemLogCleanupFilter{
  68. Host: "api-node-2",
  69. ClientRequestID: "creq-9",
  70. UserID: &userID,
  71. APIKeyID: &apiKeyID,
  72. }
  73. where, args, hasConstraint := buildOpsSystemLogsCleanupWhere(filter)
  74. if !hasConstraint {
  75. t.Fatalf("expected hasConstraint=true")
  76. }
  77. if len(args) != 4 {
  78. t.Fatalf("args len = %d, want 4", len(args))
  79. }
  80. if !contains(where, "l.host = $") {
  81. t.Fatalf("where should include host condition: %s", where)
  82. }
  83. if !contains(where, "COALESCE(l.client_request_id,'') = $") {
  84. t.Fatalf("where should include client_request_id condition: %s", where)
  85. }
  86. if !contains(where, "l.user_id = $") {
  87. t.Fatalf("where should include user_id condition: %s", where)
  88. }
  89. if !contains(where, "l.api_key_id = $") {
  90. t.Fatalf("where should include api_key_id condition: %s", where)
  91. }
  92. }
  93. func contains(s string, sub string) bool {
  94. return strings.Contains(s, sub)
  95. }