search_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package cache
  2. import (
  3. "testing"
  4. )
  5. // TestIsNumericQuery tests the isNumericQuery function
  6. func TestIsNumericQuery(t *testing.T) {
  7. tests := []struct {
  8. name string
  9. query string
  10. expected bool
  11. }{
  12. {
  13. name: "Pure number",
  14. query: "9005",
  15. expected: true,
  16. },
  17. {
  18. name: "Port with colon",
  19. query: ":9005",
  20. expected: true, // 4/5 = 80% are digits
  21. },
  22. {
  23. name: "IP address",
  24. query: "192.168.1.1",
  25. expected: true, // 9/11 = 81% are digits
  26. },
  27. {
  28. name: "Pure text",
  29. query: "nginx",
  30. expected: false,
  31. },
  32. {
  33. name: "Mixed with mostly text",
  34. query: "server9005",
  35. expected: false, // 4/10 = 40% are digits
  36. },
  37. {
  38. name: "Mixed with mostly numbers",
  39. query: "9005server",
  40. expected: false, // 4/10 = 40% are digits
  41. },
  42. {
  43. name: "Port number",
  44. query: "8080",
  45. expected: true,
  46. },
  47. {
  48. name: "Version number",
  49. query: "v1.2.3",
  50. expected: false, // 3/6 = 50% exactly, not > 50%
  51. },
  52. {
  53. name: "Empty string",
  54. query: "",
  55. expected: false,
  56. },
  57. }
  58. for _, tt := range tests {
  59. t.Run(tt.name, func(t *testing.T) {
  60. result := isNumericQuery(tt.query)
  61. if result != tt.expected {
  62. t.Errorf("isNumericQuery(%q) = %v, want %v", tt.query, result, tt.expected)
  63. }
  64. })
  65. }
  66. }
  67. // TestBuildQuery tests the buildQuery function structure
  68. func TestBuildQuery(t *testing.T) {
  69. indexer := &SearchIndexer{}
  70. tests := []struct {
  71. name string
  72. query string
  73. docType string
  74. validate func(t *testing.T, query interface{})
  75. }{
  76. {
  77. name: "Numeric query",
  78. query: "9005",
  79. docType: "",
  80. validate: func(t *testing.T, query interface{}) {
  81. if query == nil {
  82. t.Error("Expected non-nil query")
  83. }
  84. // The query should be built with numeric strategy
  85. // which prioritizes exact matches
  86. },
  87. },
  88. {
  89. name: "Text query",
  90. query: "nginx",
  91. docType: "",
  92. validate: func(t *testing.T, query interface{}) {
  93. if query == nil {
  94. t.Error("Expected non-nil query")
  95. }
  96. // The query should be built with text strategy
  97. // which includes fuzzy matching
  98. },
  99. },
  100. {
  101. name: "Numeric query with type filter",
  102. query: "9005",
  103. docType: "site",
  104. validate: func(t *testing.T, query interface{}) {
  105. if query == nil {
  106. t.Error("Expected non-nil query")
  107. }
  108. // The query should include type filter
  109. },
  110. },
  111. }
  112. for _, tt := range tests {
  113. t.Run(tt.name, func(t *testing.T) {
  114. query := indexer.buildQuery(tt.query, tt.docType)
  115. tt.validate(t, query)
  116. })
  117. }
  118. }
  119. // TestSearchStrategyDifference ensures numeric and text queries use different strategies
  120. func TestSearchStrategyDifference(t *testing.T) {
  121. // Test that numeric queries don't use fuzzy matching
  122. numericQuery := "9005"
  123. if !isNumericQuery(numericQuery) {
  124. t.Error("Expected '9005' to be detected as numeric")
  125. }
  126. // Test that text queries do use fuzzy matching
  127. textQuery := "nginx"
  128. if isNumericQuery(textQuery) {
  129. t.Error("Expected 'nginx' to be detected as text")
  130. }
  131. }