|
17 | 17 | except ImportError: |
18 | 18 | from unittest import mock |
19 | 19 |
|
| 20 | +import datetime |
20 | 21 | import unittest |
21 | 22 |
|
22 | 23 | from opencensus.metrics.export import metric_descriptor |
| 24 | +from opencensus.metrics.export import point |
| 25 | +from opencensus.metrics.export import value |
23 | 26 | from opencensus.stats import aggregation |
| 27 | +from opencensus.stats import aggregation_data |
24 | 28 | from opencensus.stats import measure |
25 | 29 | from opencensus.stats import metric_utils |
26 | 30 | from opencensus.stats import view |
| 31 | +from opencensus.stats import view_data |
| 32 | +from opencensus.tags import tag_key |
| 33 | +from opencensus.tags import tag_value |
27 | 34 |
|
28 | 35 |
|
29 | 36 | class TestMetricUtils(unittest.TestCase): |
@@ -83,20 +90,84 @@ def test_get_metric_type_bad_measure(self): |
83 | 90 | with self.assertRaises(ValueError): |
84 | 91 | metric_utils.get_metric_type(base_measure, agg_lv) |
85 | 92 |
|
86 | | - def test_view_to_metric_descriptor(self): |
| 93 | + def do_test_view_data_to_metric(self, aggregation_type, aggregation_class, |
| 94 | + value_type, metric_descriptor_type): |
| 95 | + """Test that ViewDatas are converted correctly into Metrics. |
| 96 | +
|
| 97 | + This test doesn't check that the various aggregation data `to_point` |
| 98 | + methods handle the point conversion correctly, just that converted |
| 99 | + Point is included in the Metric, and the metric has the expected |
| 100 | + structure, descriptor, and labels. |
| 101 | + """ |
| 102 | + start_time = datetime.datetime(2019, 1, 25, 11, 12, 13) |
| 103 | + current_time = datetime.datetime(2019, 1, 25, 12, 13, 14) |
| 104 | + |
87 | 105 | mock_measure = mock.Mock(spec=measure.MeasureFloat) |
88 | | - mock_agg = mock.Mock(spec=aggregation.SumAggregation) |
89 | | - mock_agg.aggregation_type = aggregation.Type.SUM |
90 | | - test_view = view.View("name", "description", ["tk1", "tk2"], |
91 | | - mock_measure, mock_agg) |
92 | | - |
93 | | - md = metric_utils.view_to_metric_descriptor(test_view) |
94 | | - self.assertTrue(isinstance(md, metric_descriptor.MetricDescriptor)) |
95 | | - self.assertEqual(md.name, test_view.name) |
96 | | - self.assertEqual(md.description, test_view.description) |
97 | | - self.assertEqual(md.unit, test_view.measure.unit) |
98 | | - self.assertEqual( |
99 | | - md.type, metric_descriptor.MetricDescriptorType.CUMULATIVE_DOUBLE) |
100 | | - self.assertTrue( |
101 | | - all(lk.key == col |
102 | | - for lk, col in zip(md.label_keys, test_view.columns))) |
| 106 | + mock_aggregation = mock.Mock(spec=aggregation_class) |
| 107 | + mock_aggregation.aggregation_type = aggregation_type |
| 108 | + |
| 109 | + vv = view.View( |
| 110 | + name=mock.Mock(), |
| 111 | + description=mock.Mock(), |
| 112 | + columns=[tag_key.TagKey('k1'), tag_key.TagKey('k2')], |
| 113 | + measure=mock_measure, |
| 114 | + aggregation=mock_aggregation) |
| 115 | + |
| 116 | + vd = mock.Mock(spec=view_data.ViewData) |
| 117 | + vd.view = vv |
| 118 | + vd.start_time = start_time |
| 119 | + |
| 120 | + mock_point = mock.Mock(spec=point.Point) |
| 121 | + mock_point.value = mock.Mock(spec=value_type) |
| 122 | + |
| 123 | + mock_agg = mock.Mock(spec=aggregation_data.SumAggregationDataFloat) |
| 124 | + mock_agg.to_point.return_value = mock_point |
| 125 | + |
| 126 | + vd.tag_value_aggregation_data_map = { |
| 127 | + (tag_value.TagValue('v1'), tag_value.TagValue('v2')): mock_agg |
| 128 | + } |
| 129 | + |
| 130 | + metric = metric_utils.view_data_to_metric(vd, current_time) |
| 131 | + mock_agg.to_point.assert_called_once_with(current_time) |
| 132 | + |
| 133 | + self.assertEqual(metric.descriptor.name, vv.name) |
| 134 | + self.assertEqual(metric.descriptor.description, vv.description) |
| 135 | + self.assertEqual(metric.descriptor.unit, vv.measure.unit) |
| 136 | + self.assertEqual(metric.descriptor.type, metric_descriptor_type) |
| 137 | + self.assertListEqual( |
| 138 | + [lk.key for lk in metric.descriptor.label_keys], |
| 139 | + ['k1', 'k2']) |
| 140 | + |
| 141 | + self.assertEqual(len(metric.time_series), 1) |
| 142 | + [ts] = metric.time_series |
| 143 | + self.assertEqual(ts.start_timestamp, start_time) |
| 144 | + self.assertListEqual( |
| 145 | + [lv.value for lv in ts.label_values], |
| 146 | + ['v1', 'v2']) |
| 147 | + self.assertEqual(len(ts.points), 1) |
| 148 | + [pt] = ts.points |
| 149 | + self.assertEqual(pt, mock_point) |
| 150 | + |
| 151 | + def test_view_data_to_metric(self): |
| 152 | + args_list = [ |
| 153 | + [ |
| 154 | + aggregation.Type.SUM, |
| 155 | + aggregation.SumAggregation, |
| 156 | + value.ValueDouble, |
| 157 | + metric_descriptor.MetricDescriptorType.CUMULATIVE_DOUBLE |
| 158 | + ], |
| 159 | + [ |
| 160 | + aggregation.Type.COUNT, |
| 161 | + aggregation.CountAggregation, |
| 162 | + value.ValueLong, |
| 163 | + metric_descriptor.MetricDescriptorType.CUMULATIVE_INT64 |
| 164 | + ], |
| 165 | + [ |
| 166 | + aggregation.Type.DISTRIBUTION, |
| 167 | + aggregation.DistributionAggregation, |
| 168 | + value.ValueDistribution, |
| 169 | + metric_descriptor.MetricDescriptorType.CUMULATIVE_DISTRIBUTION |
| 170 | + ] |
| 171 | + ] |
| 172 | + for args in args_list: |
| 173 | + self.do_test_view_data_to_metric(*args) |
0 commit comments