autocomplete_parser_test.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package allocation
  2. import (
  3. "testing"
  4. "github.com/opencost/opencost/core/pkg/autocomplete"
  5. coreallocation "github.com/opencost/opencost/core/pkg/autocomplete/allocation"
  6. "github.com/opencost/opencost/core/pkg/util/httputil"
  7. )
  8. func TestParseAllocationAutocompleteRequest(t *testing.T) {
  9. windowStr := "2023-01-01T00:00:00Z,2023-01-02T00:00:00Z"
  10. qp := httputil.NewQueryParams(map[string][]string{
  11. "window": {windowStr},
  12. "field": {"account"},
  13. "search": {" ns "},
  14. })
  15. got, err := coreallocation.ParseRequest(qp, autocomplete.ParseOptions{
  16. DefaultWindow: "30d",
  17. })
  18. if err != nil {
  19. t.Fatalf("ParseRequest() error = %v", err)
  20. }
  21. if got.Field != "account" || got.Search != "ns" {
  22. t.Fatalf("unexpected request: %+v", got)
  23. }
  24. }
  25. func TestValidateAutocompleteField_account(t *testing.T) {
  26. got, err := coreallocation.ValidateField("account")
  27. if err != nil || got != "account" {
  28. t.Fatalf("ValidateField(account) = %q, %v", got, err)
  29. }
  30. }
  31. func TestRouteAllocationAutocompleteField(t *testing.T) {
  32. route, key, err := coreallocation.RouteField("namespacelabel:Team")
  33. if err != nil || route != coreallocation.RouteNamespaceLabelValue || key != "Team" {
  34. t.Fatalf("RouteField() = %v, %q, %v", route, key, err)
  35. }
  36. }