sort_metrics_test.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package golden
  4. import (
  5. "path/filepath"
  6. "testing"
  7. "github.com/stretchr/testify/require"
  8. "go.opentelemetry.io/collector/pdata/pcommon"
  9. )
  10. func TestSortAttributes(t *testing.T) {
  11. // Create a sample input map
  12. mp := pcommon.NewMap()
  13. mp.PutStr("name", "John")
  14. mp.PutInt("age", 30)
  15. mp.PutBool("isStudent", false)
  16. subMap := pcommon.NewMap()
  17. subMap.PutStr("city", "New York")
  18. subMap.CopyTo(mp.PutEmptyMap("location"))
  19. // Call the sortAttributeMap method
  20. sortAttributeMap(mp)
  21. // Verify the sorted keys
  22. expectedKeys := []string{"age", "isStudent", "location", "name"}
  23. actualKeys := []string{}
  24. mp.Range(func(key string, _ pcommon.Value) bool {
  25. actualKeys = append(actualKeys, key)
  26. return true
  27. })
  28. // Check if the keys are sorted correctly
  29. if len(expectedKeys) != len(actualKeys) {
  30. t.Errorf("Incorrect number of keys. Expected: %d, Actual: %d", len(expectedKeys), len(actualKeys))
  31. }
  32. for i, key := range expectedKeys {
  33. if key != actualKeys[i] {
  34. t.Errorf("Incorrect key at index %d. Expected: %s, Actual: %s", i, key, actualKeys[i])
  35. }
  36. }
  37. }
  38. func TestSortMetricsResourceAndScope(t *testing.T) {
  39. beforePath := filepath.Join("testdata", "sort-metrics", "before.yaml")
  40. afterPath := filepath.Join("testdata", "sort-metrics", "after.yaml")
  41. before, err := ReadMetrics(beforePath)
  42. require.NoError(t, err)
  43. sortResources(before)
  44. sortScopes(before)
  45. sortMetricDataPointSlices(before)
  46. after, err := ReadMetrics(afterPath)
  47. require.NoError(t, err)
  48. require.Equal(t, before, after)
  49. }