Skip to content

Commit

Permalink
finish fixing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
colincadams committed Jun 27, 2019
1 parent 6659817 commit 1681a35
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 71 deletions.
11 changes: 10 additions & 1 deletion opencensus/stats/aggregation_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

import copy
import logging
from functools import partial

from opencensus.metrics.export import point
from opencensus.metrics.export import value
Expand Down Expand Up @@ -88,6 +87,11 @@ def sum_data(self):
"""The current sum data"""
return self._sum_data

@property
def value_type(self):
"""The value type to use when creating the point"""
return self._value_type

def to_point(self, timestamp):
"""Get a Point conversion of this aggregation.
Expand Down Expand Up @@ -366,6 +370,11 @@ def value(self):
"""The current value recorded"""
return self._value

@property
def value_type(self):
"""The value type to use when creating the point"""
return self._value_type

def to_point(self, timestamp):
"""Get a Point conversion of this aggregation.
Expand Down
78 changes: 57 additions & 21 deletions tests/unit/stats/test_aggregation.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@

from opencensus.stats import aggregation as aggregation_module
from opencensus.stats import measure as measure_module
from opencensus.metrics.export import value


class TestBaseAggregation(unittest.TestCase):
def test_constructor_defaults(self):
def test_new_aggregation_data_defaults(self):
base_aggregation = aggregation_module.BaseAggregation()

self.assertEqual([], base_aggregation.buckets)

def test_constructor_explicit(self):
def test_new_aggregation_data_explicit(self):

buckets = ["test"]
base_aggregation = aggregation_module.BaseAggregation(buckets=buckets)
Expand All @@ -34,53 +35,88 @@ def test_constructor_explicit(self):


class TestSumAggregation(unittest.TestCase):
def test_constructor_defaults(self):
def test_new_aggregation_data_defaults(self):
measure = mock.Mock(spec=measure_module.MeasureInt)
sum_aggregation = aggregation_module.SumAggregation()
self.assertEqual(0, sum_aggregation.new_aggregation_data(measure).sum_data)
agg_data = sum_aggregation.new_aggregation_data(measure)
self.assertEqual(0, agg_data.sum_data)
self.assertEqual(value.ValueLong, agg_data.value_type)

def test_constructor_explicit(self):
def test_new_aggregation_data_explicit(self):
measure = mock.Mock(spec=measure_module.MeasureInt)
sum_aggregation = aggregation_module.SumAggregation(sum=1)
self.assertEqual(1, sum_aggregation.new_aggregation_data(measure).sum_data)
agg_data = sum_aggregation.new_aggregation_data(measure)
self.assertEqual(1, agg_data.sum_data)
self.assertEqual(value.ValueLong, agg_data.value_type)

def test_new_aggregation_data_float(self):
measure = mock.Mock(spec=measure_module.MeasureFloat)
sum_aggregation = aggregation_module.SumAggregation()
agg_data = sum_aggregation.new_aggregation_data(measure)
self.assertEqual(0, agg_data.sum_data)
self.assertEqual(value.ValueDouble, agg_data.value_type)

def test_new_aggregation_data_bad(self):
measure = mock.Mock(spec=measure_module.BaseMeasure)
sum_aggregation = aggregation_module.SumAggregation()
with self.assertRaises(ValueError):
agg_data = sum_aggregation.new_aggregation_data(measure)


class TestCountAggregation(unittest.TestCase):
def test_constructor_defaults(self):
def test_new_aggregation_data_defaults(self):
count_aggregation = aggregation_module.CountAggregation()
self.assertEqual(0, count_aggregation.new_aggregation_data().count_data)
agg_data = count_aggregation.new_aggregation_data()
self.assertEqual(0, agg_data.count_data)

def test_constructor_explicit(self):
def test_new_aggregation_data_explicit(self):
count_aggregation = aggregation_module.CountAggregation(count=4)
self.assertEqual(4, count_aggregation.new_aggregation_data().count_data)
agg_data = count_aggregation.new_aggregation_data()
self.assertEqual(4, agg_data.count_data)


class TestLastValueAggregation(unittest.TestCase):
def test_constructor_defaults(self):
def test_new_aggregation_data_defaults(self):
measure = mock.Mock(spec=measure_module.MeasureInt)
last_value_aggregation = aggregation_module.LastValueAggregation()
self.assertEqual(0, last_value_aggregation.new_aggregation_data(measure).value)
agg_data = last_value_aggregation.new_aggregation_data(measure)
self.assertEqual(0, agg_data.value)
self.assertEqual(value.ValueLong, agg_data.value_type)

def test_constructor_explicit(self):
def test_new_aggregation_data_explicit(self):
measure = mock.Mock(spec=measure_module.MeasureInt)
last_value_aggregation = aggregation_module.LastValueAggregation(
value=6)
self.assertEqual(6, last_value_aggregation.new_aggregation_data(measure).value)
agg_data = last_value_aggregation.new_aggregation_data(measure)
self.assertEqual(6, agg_data.value)
self.assertEqual(value.ValueLong, agg_data.value_type)

def test_new_aggregation_data_float(self):
measure = mock.Mock(spec=measure_module.MeasureFloat)
last_value_aggregation = aggregation_module.LastValueAggregation()
agg_data = last_value_aggregation.new_aggregation_data(measure)
self.assertEqual(0, agg_data.value)
self.assertEqual(value.ValueDouble, agg_data.value_type)

def test_new_aggregation_data_bad(self):
measure = mock.Mock(spec=measure_module.BaseMeasure)
last_value_aggregation = aggregation_module.LastValueAggregation()
with self.assertRaises(ValueError):
agg_data = last_value_aggregation.new_aggregation_data(measure)


class TestDistributionAggregation(unittest.TestCase):
def test_constructor_defaults(self):
def test_new_aggregation_data_defaults(self):
distribution_aggregation = aggregation_module.DistributionAggregation()
agg_data = distribution_aggregation.new_aggregation_data()
self.assertEqual([], agg_data.bounds)

self.assertEqual([], distribution_aggregation.new_aggregation_data().bounds)

def test_constructor_explicit(self):
def test_new_aggregation_data_explicit(self):
boundaries = [1, 2]
distribution_aggregation = aggregation_module.DistributionAggregation(
boundaries=boundaries)

self.assertEqual(boundaries,
distribution_aggregation.new_aggregation_data().bounds)
agg_data = distribution_aggregation.new_aggregation_data()
self.assertEqual(boundaries, agg_data.bounds)

def test_init_bad_boundaries(self):
"""Check that boundaries must be sorted and unique."""
Expand Down
53 changes: 4 additions & 49 deletions tests/unit/stats/test_metric_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,54 +31,6 @@


class TestMetricUtils(unittest.TestCase):
def test_get_metric_type(self):
measure_int = mock.Mock(spec=measure.MeasureInt)
measure_float = mock.Mock(spec=measure.MeasureFloat)
agg_sum = mock.Mock(spec=aggregation.SumAggregation)
agg_count = mock.Mock(spec=aggregation.CountAggregation)
agg_dist = mock.Mock(spec=aggregation.DistributionAggregation)
agg_lv = mock.Mock(spec=aggregation.LastValueAggregation)

view_to_metric_type = {
(measure_int, agg_sum):
metric_descriptor.MetricDescriptorType.CUMULATIVE_INT64,
(measure_int, agg_count):
metric_descriptor.MetricDescriptorType.CUMULATIVE_INT64,
(measure_int, agg_dist):
metric_descriptor.MetricDescriptorType.CUMULATIVE_DISTRIBUTION,
(measure_int, agg_lv):
metric_descriptor.MetricDescriptorType.GAUGE_INT64,
(measure_float, agg_sum):
metric_descriptor.MetricDescriptorType.CUMULATIVE_DOUBLE,
(measure_float, agg_count):
metric_descriptor.MetricDescriptorType.CUMULATIVE_INT64,
(measure_float, agg_dist):
metric_descriptor.MetricDescriptorType.CUMULATIVE_DISTRIBUTION,
(measure_float, agg_lv):
metric_descriptor.MetricDescriptorType.GAUGE_DOUBLE,
}

for (mm, ma), metric_type in view_to_metric_type.items():
self.assertEqual(metric_utils.get_metric_type(mm, ma), metric_type)

def test_get_metric_type_bad_aggregation(self):
base_agg = mock.Mock(spec=aggregation.BaseAggregation)
with self.assertRaises(ValueError):
metric_utils.get_metric_type(mock.Mock(), base_agg)

bad_agg = mock.Mock(spec=aggregation.SumAggregation)
with self.assertRaises(AssertionError):
metric_utils.get_metric_type(mock.Mock(), bad_agg)

def test_get_metric_type_bad_measure(self):
base_measure = mock.Mock(spec=measure.BaseMeasure)
agg_sum = mock.Mock(spec=aggregation.SumAggregation)
agg_lv = mock.Mock(spec=aggregation.LastValueAggregation)
with self.assertRaises(ValueError):
metric_utils.get_metric_type(base_measure, agg_sum)
with self.assertRaises(ValueError):
metric_utils.get_metric_type(base_measure, agg_lv)

def do_test_view_data_to_metric(self, aggregation_class,
value_type, metric_descriptor_type):
"""Test that ViewDatas are converted correctly into Metrics.
Expand All @@ -93,6 +45,7 @@ def do_test_view_data_to_metric(self, aggregation_class,

mock_measure = mock.Mock(spec=measure.MeasureFloat)
mock_aggregation = mock.Mock(spec=aggregation_class)
mock_aggregation.get_metric_type.return_value = metric_descriptor_type

vv = view.View(
name=mock.Mock(),
Expand Down Expand Up @@ -160,6 +113,8 @@ def test_view_data_to_metric(self):
def test_convert_view_without_labels(self):
mock_measure = mock.Mock(spec=measure.MeasureFloat)
mock_aggregation = mock.Mock(spec=aggregation.DistributionAggregation)
mock_aggregation.get_metric_type.return_value = \
metric_descriptor.MetricDescriptorType.CUMULATIVE_DISTRIBUTION

vd = mock.Mock(spec=view_data.ViewData)
vd.view = view.View(
Expand All @@ -173,7 +128,7 @@ def test_convert_view_without_labels(self):
mock_point = mock.Mock(spec=point.Point)
mock_point.value = mock.Mock(spec=value.ValueDistribution)

mock_agg = mock.Mock(spec=aggregation_data.SumAggregationData)
mock_agg = mock.Mock(spec=aggregation_data.DistributionAggregationData)
mock_agg.to_point.return_value = mock_point

vd.tag_value_aggregation_data_map = {tuple(): mock_agg}
Expand Down
1 change: 1 addition & 0 deletions tests/unit/stats/test_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ def test_get_metrics(self):
mm._measurement_map = {mock_measure: 1.0}

mock_view.aggregation = aggregation.DistributionAggregation()
mock_view.new_aggregation_data.return_value = mock_view.aggregation.new_aggregation_data()

tm = tag_map.TagMap()
tm.insert('k1', 'v1')
Expand Down
1 change: 1 addition & 0 deletions tests/unit/stats/test_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def test_constructor(self):
def test_view_to_metric_descriptor(self):
mock_measure = mock.Mock(spec=measure.MeasureFloat)
mock_agg = mock.Mock(spec=aggregation.SumAggregation)
mock_agg.get_metric_type.return_value = metric_descriptor.MetricDescriptorType.CUMULATIVE_DOUBLE
test_view = view.View("name", "description", ["tk1", "tk2"],
mock_measure, mock_agg)

Expand Down

0 comments on commit 1681a35

Please sign in to comment.