@@ -2,22 +2,48 @@ package cloudWatchConsts
22
33import (
44 "fmt"
5+ "maps"
56 "slices"
67 "sort"
8+ "strings"
79 "testing"
810
911 "github.com/stretchr/testify/assert"
1012)
1113
12- // test to check NamespaceMetricsMap is sorted alphabetically
13- func TestNamespaceMetricsMap (t * testing.T ) {
14+ // test to check NamespaceMetricsMap metrics are sorted alphabetically
15+ func TestNamespaceMetricsAlphabetized (t * testing.T ) {
1416 unsortedMetricNamespaces := namespacesWithUnsortedMetrics (NamespaceMetricsMap )
1517 if len (unsortedMetricNamespaces ) != 0 {
1618 assert .Fail (t , "NamespaceMetricsMap is not sorted alphabetically. Please replace the printed services" )
1719 printNamespacesThatNeedSorted (unsortedMetricNamespaces )
1820 }
1921}
2022
23+ func TestNamespaceMetricKeysAllHaveDimensions (t * testing.T ) {
24+ namespaceMetricsKeys := slices .Collect (maps .Keys (NamespaceMetricsMap ))
25+ namespaceDimensionKeys := slices .Collect (maps .Keys (NamespaceDimensionKeysMap ))
26+
27+ namespaceMetricsMissingKeys := findMetricKeysFromAMissingInB (namespaceDimensionKeys , namespaceMetricsKeys )
28+
29+ if len (namespaceMetricsMissingKeys ) != 0 {
30+ assert .Fail (t , "NamespaceMetricsMap is missing key(s) from NamespaceDimensionKeysMap." )
31+ fmt .Println (strings .Join (namespaceMetricsMissingKeys , "\n " ))
32+ }
33+ }
34+
35+ func TestNamespaceDimensionKeysAllHaveMetrics (t * testing.T ) {
36+ namespaceMetricsKeys := slices .Collect (maps .Keys (NamespaceMetricsMap ))
37+ namespaceDimensionKeys := slices .Collect (maps .Keys (NamespaceDimensionKeysMap ))
38+
39+ namespaceDimensionMissingKeys := findMetricKeysFromAMissingInB (namespaceMetricsKeys , namespaceDimensionKeys )
40+
41+ if len (namespaceDimensionMissingKeys ) != 0 {
42+ assert .Fail (t , "NamespaceDimensionKeysMap is missing key(s) from NamespaceMetricsMap." )
43+ fmt .Println (strings .Join (namespaceDimensionMissingKeys , "\n " ))
44+ }
45+ }
46+
2147func printNamespacesThatNeedSorted (unsortedMetricNamespaces []string ) {
2248 slices .Sort (unsortedMetricNamespaces )
2349
@@ -37,10 +63,8 @@ func printNamespacesThatNeedSorted(unsortedMetricNamespaces []string) {
3763// namespacesWithUnsortedMetrics returns which namespaces have unsorted metrics
3864func namespacesWithUnsortedMetrics (NamespaceMetricsMap map [string ][]string ) []string {
3965 // Extract keys from the map and sort them
40- keys := make ([]string , 0 , len (NamespaceMetricsMap ))
41- for k := range NamespaceMetricsMap {
42- keys = append (keys , k )
43- }
66+ keys := slices .Collect (maps .Keys (NamespaceMetricsMap ))
67+
4468 sort .Strings (keys )
4569
4670 var unsortedNamespace []string
@@ -62,3 +86,15 @@ func namespacesWithUnsortedMetrics(NamespaceMetricsMap map[string][]string) []st
6286
6387 return unsortedNamespace
6488}
89+
90+ func findMetricKeysFromAMissingInB (a []string , b []string ) []string {
91+ var missingKeys []string
92+
93+ for i := range a {
94+ if ! slices .Contains (b , a [i ]) {
95+ missingKeys = append (missingKeys , a [i ])
96+ }
97+ }
98+
99+ return missingKeys
100+ }
0 commit comments