query_test.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package utils_test
  2. import (
  3. "encoding/json"
  4. "testing"
  5. "github.com/porter-dev/porter/internal/templater"
  6. "github.com/porter-dev/porter/internal/templater/utils"
  7. )
  8. type testType struct {
  9. Value interface{} `json:"value,omitempty"`
  10. }
  11. func TestQueryValues(t *testing.T) {
  12. vals := map[string]interface{}{
  13. "items": []interface{}{
  14. map[string]interface{}{
  15. "metadata": map[string]interface{}{
  16. "name": "a",
  17. "namespace": "velero",
  18. },
  19. "array": []interface{}{
  20. "1",
  21. "2",
  22. },
  23. },
  24. map[string]interface{}{
  25. "metadata": map[string]interface{}{
  26. "name": "b",
  27. "namespace": "velero",
  28. },
  29. "array": []interface{}{
  30. "3",
  31. "4",
  32. },
  33. },
  34. },
  35. }
  36. queries := make([]*templater.TemplateReaderQuery, 0)
  37. // should get turned into type []map[string]interface{} that can be converted to JSON
  38. // query, err := utils.NewQuery("test",
  39. // `
  40. // .items[].metadata |
  41. // { name: .name, namespace: .namespace }
  42. // `)
  43. query, err := utils.NewQuery("test", `.items`)
  44. if err != nil {
  45. t.Fatalf("%v\n", err)
  46. }
  47. queries = append(queries, query)
  48. res, _ := utils.QueryValues(vals, queries)
  49. test := &testType{
  50. Value: res["test"],
  51. }
  52. bytes, _ := json.Marshal(test)
  53. t.Errorf(string(bytes))
  54. }