autocomplete_test.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package autocomplete
  2. import (
  3. "errors"
  4. "testing"
  5. )
  6. func TestSanitizeSearch(t *testing.T) {
  7. if got := SanitizeSearch(" ec2 "); got != "ec2" {
  8. t.Fatalf("SanitizeSearch() = %q, want %q", got, "ec2")
  9. }
  10. }
  11. func TestNormalizeLimit(t *testing.T) {
  12. got, err := NormalizeLimit(0)
  13. if err != nil || got != DefaultResultLimit {
  14. t.Fatalf("NormalizeLimit(0) = %d, %v", got, err)
  15. }
  16. got, err = NormalizeLimit(50)
  17. if err != nil || got != 50 {
  18. t.Fatalf("NormalizeLimit(50) = %d, %v", got, err)
  19. }
  20. _, err = NormalizeLimit(MaxResultLimit + 1)
  21. if err == nil || !errors.Is(err, ErrBadRequest) {
  22. t.Fatalf("expected ErrBadRequest, got %v", err)
  23. }
  24. }
  25. func TestFilterBySearch(t *testing.T) {
  26. got := FilterBySearch([]string{"AmazonEC2"}, "ec2")
  27. if len(got) != 1 || got[0] != "AmazonEC2" {
  28. t.Fatalf("FilterBySearch() = %v", got)
  29. }
  30. got = FilterBySearch([]string{"AmazonEC2", "S3"}, "")
  31. if len(got) != 2 {
  32. t.Fatalf("FilterBySearch empty search = %v", got)
  33. }
  34. }
  35. func TestMapValueFold(t *testing.T) {
  36. values := map[string]string{"Team": "platform"}
  37. if got, ok := MapValueFold(values, "Team"); !ok || got != "platform" {
  38. t.Fatalf("MapValueFold exact match = %q, %v", got, ok)
  39. }
  40. if got, ok := MapValueFold(values, "team"); !ok || got != "platform" {
  41. t.Fatalf("MapValueFold() = %q, %v", got, ok)
  42. }
  43. if _, ok := MapValueFold(values, "missing"); ok {
  44. t.Fatal("expected missing key")
  45. }
  46. }
  47. func TestUniqueSortedLimited(t *testing.T) {
  48. set := map[string]struct{}{"b": {}, "a": {}, "c": {}}
  49. got := UniqueSortedLimited(set, 2)
  50. if len(got) != 2 || got[0] != "a" || got[1] != "b" {
  51. t.Fatalf("UniqueSortedLimited() = %v", got)
  52. }
  53. all := UniqueSortedLimited(map[string]struct{}{"z": {}, "y": {}}, 5)
  54. if len(all) != 2 || all[0] != "y" || all[1] != "z" {
  55. t.Fatalf("UniqueSortedLimited no truncate = %v", all)
  56. }
  57. }
  58. func TestToSet(t *testing.T) {
  59. set := ToSet([]string{"a", "b", "a"})
  60. if len(set) != 2 {
  61. t.Fatalf("ToSet() = %v", set)
  62. }
  63. }
  64. func TestIsBadRequest(t *testing.T) {
  65. if !IsBadRequest(ErrBadRequest) {
  66. t.Fatal("expected ErrBadRequest")
  67. }
  68. if IsBadRequest(errors.New("other")) {
  69. t.Fatal("expected false for unrelated error")
  70. }
  71. }