autocomplete_parser_test.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package asset
  2. import (
  3. "testing"
  4. "github.com/opencost/opencost/core/pkg/autocomplete"
  5. coreasset "github.com/opencost/opencost/core/pkg/autocomplete/asset"
  6. "github.com/opencost/opencost/core/pkg/util/httputil"
  7. )
  8. func TestParseAssetAutocompleteRequest(t *testing.T) {
  9. windowStr := "2023-01-01T00:00:00Z,2023-01-02T00:00:00Z"
  10. tests := map[string]struct {
  11. values map[string][]string
  12. wantErr bool
  13. }{
  14. "missing window": {
  15. values: map[string][]string{"field": {"cluster"}},
  16. wantErr: true,
  17. },
  18. "valid with default window": {
  19. values: map[string][]string{
  20. "field": {"cluster"},
  21. },
  22. wantErr: false,
  23. },
  24. "valid": {
  25. values: map[string][]string{
  26. "window": {windowStr},
  27. "field": {"label:App"},
  28. "search": {" foo "},
  29. },
  30. wantErr: false,
  31. },
  32. "assettype alias": {
  33. values: map[string][]string{
  34. "window": {windowStr},
  35. "field": {"assettype"},
  36. },
  37. wantErr: false,
  38. },
  39. }
  40. for name, tt := range tests {
  41. t.Run(name, func(t *testing.T) {
  42. qp := httputil.NewQueryParams(tt.values)
  43. opts := autocomplete.ParseOptions{DefaultTenantID: "tenant-1"}
  44. if name == "valid with default window" {
  45. opts.DefaultWindow = "30d"
  46. }
  47. got, err := coreasset.ParseRequest(qp, opts)
  48. if (err != nil) != tt.wantErr {
  49. t.Fatalf("ParseRequest() error = %v, wantErr %v", err, tt.wantErr)
  50. }
  51. if tt.wantErr {
  52. return
  53. }
  54. if got.Field == "assettype" {
  55. t.Fatalf("field should be normalized from assettype")
  56. }
  57. if name == "valid" && got.Search != "foo" {
  58. t.Fatalf("search = %q, want foo", got.Search)
  59. }
  60. if name == "assettype alias" && got.Field != "type" {
  61. t.Fatalf("field = %q, want type", got.Field)
  62. }
  63. })
  64. }
  65. }
  66. func TestValidateAutocompleteField_assettype(t *testing.T) {
  67. got, err := coreasset.ValidateField("assettype")
  68. if err != nil || got != "type" {
  69. t.Fatalf("ValidateField(assettype) = %q, %v", got, err)
  70. }
  71. }