1+ import unittest
2+ import json
3+ import os
4+ from prometheus_api_client import MetricRangeDataFrame
5+
6+
7+ class TestMetricRangeDataFrame (unittest .TestCase ):
8+ def setUp (self ):
9+ """
10+ read metrics stored as jsons in './tests/metrics'
11+ """
12+ self .raw_metrics_list = list ()
13+ self .raw_metrics_labels = list ()
14+ for (dir_path , _ , file_names ) in os .walk ("./tests/metrics" ):
15+ for fname in file_names :
16+ with open (os .path .join (dir_path , fname ), "rb" ) as f :
17+ metric_jsons = json .load (f )
18+
19+ # save json list
20+ self .raw_metrics_list .extend ([metric_jsons ])
21+
22+ # save label configs
23+ labels = set ()
24+ for i in metric_jsons :
25+ labels .update (set (i ["metric" ].keys ()))
26+ self .raw_metrics_labels .append (labels )
27+
28+ def test_setup (self ):
29+ """
30+ Check if setup was done correctly
31+ """
32+ self .assertEqual (
33+ 8 , len (self .raw_metrics_list ), "incorrect number json files read (incorrect test setup)"
34+ )
35+
36+ def test_init_shape (self ):
37+ """
38+ Test if dataframe initialized is of correct shape
39+ """
40+ # check shape
41+ # each metric json contains number of timestamps equal to number entries * number of timestamps in each series
42+ # we're assuming each series has the same number of timestamps
43+ # 3 labels
44+ for current_metric_list in self .raw_metrics_list :
45+ df = MetricRangeDataFrame (current_metric_list )
46+ num_values = sum ([len (v ["values" ]) for v in current_metric_list ])
47+ self .assertEqual (
48+ (len (df .index .values ), df .shape [1 ]), # shape[1] = 4xlabels + value
49+ (num_values , 5 ),
50+ "incorrect dataframe shape" ,
51+ )
52+
53+ def test_init_timestamps (self ):
54+ """
55+ Test if dataframe contains the correct timestamp indices
56+ """
57+ # check that the timestamp indices in each series are the same
58+ for curr_metric_list in self .raw_metrics_list :
59+ self .assertEqual (
60+ set (MetricRangeDataFrame (curr_metric_list ).index .values ),
61+ set ([v [0 ] for s in curr_metric_list for v in s ["values" ] ])
62+ )
63+
64+ def test_init_columns (self ):
65+ """
66+ Test if dataframe initialized has correct columns
67+ """
68+ for curr_metric_labels , curr_metric_list in zip (self .raw_metrics_labels , self .raw_metrics_list ):
69+ self .assertEqual (
70+ curr_metric_labels .union ({"value" }),
71+ set (MetricRangeDataFrame (curr_metric_list ).columns ),
72+ "incorrect dataframe columns" ,
73+ )
74+
75+ def test_init_single_metric (self ):
76+ """
77+ Test if dataframe initialized is of correct shape when
78+ 1. json object is passed as data
79+ 2. list with single json object is passed as data
80+ """
81+ # check shape when single json passed
82+ num_vals = len (self .raw_metrics_list [0 ][0 ]["values" ])
83+ self .assertEqual (
84+ (num_vals , 5 ),
85+ MetricRangeDataFrame (self .raw_metrics_list [0 ][0 ]).shape ,
86+ "incorrect dataframe shape when initialized with json" ,
87+ )
88+ # check shape when list with single json passed
89+ self .assertEqual (
90+ (num_vals , 5 ),
91+ MetricRangeDataFrame ([self .raw_metrics_list [0 ][0 ]]).shape ,
92+ "incorrect dataframe shape when initialized with single json list" ,
93+ )
94+
95+
96+ if __name__ == "__main__" :
97+ unittest .main ()
0 commit comments