facet_total_fix_test.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package searcher
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. )
  6. // Test that verifies our facet Total fix logic
  7. func TestFacetTotalCorrection_Logic(t *testing.T) {
  8. // Test the logic we implemented in convertBleveResult
  9. // Case 1: Facet with terms - Total should be count of terms
  10. facetTerms := []*FacetTerm{
  11. {Term: "/api/users", Count: 500},
  12. {Term: "/api/posts", Count: 300},
  13. {Term: "/home", Count: 200},
  14. {Term: "/login", Count: 150},
  15. {Term: "/dashboard", Count: 100},
  16. }
  17. correctTotal := len(facetTerms)
  18. assert.Equal(t, 5, correctTotal, "Total should be the count of unique terms")
  19. // Case 2: Empty facet - Total should be 0
  20. emptyTerms := []*FacetTerm{}
  21. emptyTotal := len(emptyTerms)
  22. assert.Equal(t, 0, emptyTotal, "Total should be 0 when there are no terms")
  23. // Case 3: Nil terms - Total should be 0
  24. var nilTerms []*FacetTerm = nil
  25. nilTotal := 0
  26. if nilTerms != nil {
  27. nilTotal = len(nilTerms)
  28. }
  29. assert.Equal(t, 0, nilTotal, "Total should be 0 when terms are nil")
  30. }
  31. func TestFacetCorrection_PathExactVsPath(t *testing.T) {
  32. // This test verifies that we should use path_exact instead of path
  33. // for accurate unique page counting
  34. // path field (analyzed) might split "/api/users/123" into ["api", "users", "123"]
  35. // path_exact field (keyword) keeps "/api/users/123" as one term
  36. analyzedPathTerms := []string{"api", "users", "admin", "login", "dashboard"} // Wrong for counting unique URLs
  37. exactPathTerms := []string{"/api/users", "/api/admin", "/login", "/dashboard"} // Correct for counting unique URLs
  38. assert.Greater(t, len(analyzedPathTerms), len(exactPathTerms),
  39. "Analyzed path creates more terms due to tokenization, leading to incorrect counts")
  40. assert.Equal(t, 4, len(exactPathTerms),
  41. "path_exact gives accurate count of unique URLs")
  42. }