OLD | NEW |
(Empty) | |
| 1 # Copyright 2016 Google Inc. All Rights Reserved. |
| 2 # |
| 3 # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 # you may not use this file except in compliance with the License. |
| 5 # You may obtain a copy of the License at |
| 6 # |
| 7 # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 # |
| 9 # Unless required by applicable law or agreed to in writing, software |
| 10 # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 # See the License for the specific language governing permissions and |
| 13 # limitations under the License. |
| 14 |
| 15 from __future__ import absolute_import |
| 16 |
| 17 import datetime |
| 18 import hashlib |
| 19 |
| 20 import unittest2 |
| 21 from expects import equal, expect, raise_error |
| 22 |
| 23 from apitools.base.py import encoding |
| 24 from google.api.control import distribution, timestamp, metric_value, messages |
| 25 from google.api.control import MetricKind |
| 26 |
| 27 |
| 28 class TestUpdateHash(unittest2.TestCase): |
| 29 NOTHING_ADDED = hashlib.md5().digest() |
| 30 |
| 31 def make_hash(self, mv): |
| 32 md5 = hashlib.md5() |
| 33 metric_value.update_hash(md5, mv) |
| 34 return md5.digest() |
| 35 |
| 36 def test_should_add_nothing_without_labels_or_currency(self): |
| 37 expect(self.make_hash(messages.MetricValue())).to( |
| 38 equal(self.NOTHING_ADDED)) |
| 39 |
| 40 def test_should_add_matching_hashes_for_matching_labels(self): |
| 41 a_dict = {'test': 'dict'} |
| 42 mv1 = metric_value.create(labels=a_dict) |
| 43 mv2 = metric_value.create(labels=a_dict) |
| 44 want = self.make_hash(mv1) |
| 45 got = self.make_hash(mv2) |
| 46 expect(got).to(equal(want)) |
| 47 |
| 48 def test_should_update_hash_for_when_currency_is_added(self): |
| 49 a_dict = {'test': 'dict'} |
| 50 mv1 = metric_value.create(labels=a_dict) |
| 51 mv2 = metric_value.create(labels=a_dict) |
| 52 mv2.moneyValue = messages.Money(currencyCode='JPY') |
| 53 want = self.make_hash(mv1) |
| 54 got = self.make_hash(mv2) |
| 55 expect(got).to_not(equal(want)) |
| 56 |
| 57 |
| 58 class TestSign(TestUpdateHash): |
| 59 |
| 60 def make_hash(self, mv): |
| 61 return metric_value.sign(mv) |
| 62 |
| 63 |
| 64 class TestMerge(unittest2.TestCase): |
| 65 A_FLOAT_VALUE = 1.0 |
| 66 EARLY = timestamp.to_rfc3339(datetime.datetime(1970, 1, 1, 10, 0, 0)) |
| 67 LATER = timestamp.to_rfc3339(datetime.datetime(1990, 1, 1, 10, 0, 0)) |
| 68 TEST_LABELS = { |
| 69 'key1': 'value1', |
| 70 'key2': 'value2', |
| 71 } |
| 72 |
| 73 def setUp(self): |
| 74 self.test_value = metric_value.create( |
| 75 labels=self.TEST_LABELS, |
| 76 doubleValue=self.A_FLOAT_VALUE) |
| 77 self.early_ending = metric_value.create( |
| 78 labels=self.TEST_LABELS, |
| 79 doubleValue=self.A_FLOAT_VALUE, |
| 80 endTime=self.EARLY) |
| 81 self.late_ending = metric_value.create( |
| 82 labels=self.TEST_LABELS, |
| 83 doubleValue=self.A_FLOAT_VALUE, |
| 84 endTime=self.LATER) |
| 85 self.test_value_with_money = metric_value.create( |
| 86 labels=self.TEST_LABELS, |
| 87 moneyValue=messages.Money(currencyCode='JPY', units=100, nanos=0)) |
| 88 |
| 89 def test_should_fail_for_metric_values_with_different_types(self): |
| 90 changed = metric_value.create(labels=self.TEST_LABELS, int64Value=1) |
| 91 for kind in (MetricKind.GAUGE, MetricKind.CUMULATIVE, MetricKind.DELTA): |
| 92 testf = lambda: metric_value.merge(kind, self.test_value, changed) |
| 93 expect(testf).to(raise_error(ValueError)) |
| 94 |
| 95 def test_should_fail_for_uninitialized_metric_values(self): |
| 96 no_init = metric_value.create() |
| 97 for kind in (MetricKind.GAUGE, MetricKind.CUMULATIVE, MetricKind.DELTA): |
| 98 testf = lambda: metric_value.merge(kind, no_init, no_init) |
| 99 expect(testf).to(raise_error(ValueError)) |
| 100 |
| 101 def test_should_fail_for_delta_metrics_with_unmergable_types(self): |
| 102 no_init = metric_value.create() |
| 103 unmergeables = [ |
| 104 metric_value.create(stringValue='a test string'), |
| 105 metric_value.create(boolValue=False), |
| 106 ] |
| 107 for mv in unmergeables: |
| 108 testf = lambda: metric_value.merge(MetricKind.DELTA, mv, mv) |
| 109 expect(testf).to(raise_error(ValueError)) |
| 110 |
| 111 def test_should_succeed_for_delta_metrics_with_the_money_type(self): |
| 112 v = self.test_value_with_money |
| 113 want = 2 * v.moneyValue.units |
| 114 got = metric_value.merge(MetricKind.DELTA, v, v) |
| 115 expect(got.moneyValue.units).to(equal(want)) |
| 116 |
| 117 def test_should_succeed_for_delta_metrics_with_the_double_type(self): |
| 118 v = self.test_value |
| 119 want = 2 * v.doubleValue |
| 120 got = metric_value.merge(MetricKind.DELTA, v, v) |
| 121 expect(got.doubleValue).to(equal(want)) |
| 122 |
| 123 def test_should_succeed_for_delta_metrics_with_the_int64_type(self): |
| 124 test_int = 4 |
| 125 v = metric_value.create(labels=self.TEST_LABELS, int64Value=test_int) |
| 126 want = 2 * test_int |
| 127 got = metric_value.merge(MetricKind.DELTA, v, v) |
| 128 expect(got.int64Value).to(equal(want)) |
| 129 |
| 130 def test_should_succeed_for_delta_metrics_with_the_distribution_type(self): |
| 131 test_distribution = distribution.create_explicit([0.1, 0.3, 0.5]) |
| 132 distribution.add_sample(0.4, test_distribution) |
| 133 v = metric_value.create(labels=self.TEST_LABELS, |
| 134 distributionValue=test_distribution) |
| 135 want = 2 * test_distribution.count |
| 136 got = metric_value.merge(MetricKind.DELTA, v, v) |
| 137 expect(got.distributionValue.count).to(equal(want)) |
| 138 |
| 139 def test_should_return_metric_value_with_latest_end_time_for_non_deltas(self
): |
| 140 for kind in (MetricKind.GAUGE, MetricKind.CUMULATIVE): |
| 141 got = metric_value.merge(kind, self.early_ending, self.late_ending) |
| 142 expect(got).to(equal(self.late_ending)) |
| 143 got = metric_value.merge(kind, self.late_ending, self.early_ending) |
| 144 expect(got).to(equal(self.late_ending)) |
| 145 |
| 146 def test_should_use_the_latest_end_time_delta_merges(self): |
| 147 got = metric_value.merge(MetricKind.DELTA, |
| 148 self.early_ending, |
| 149 self.late_ending) |
| 150 expect(got.endTime).to(equal(self.late_ending.endTime)) |
| 151 got = metric_value.merge(MetricKind.DELTA, |
| 152 self.late_ending, |
| 153 self.early_ending) |
| 154 expect(got.endTime).to(equal(self.late_ending.endTime)) |
| 155 |
| 156 def test_should_use_the_earliest_start_time_in_delta_merges(self): |
| 157 early_starting = metric_value.create( |
| 158 labels=self.TEST_LABELS, |
| 159 doubleValue=self.A_FLOAT_VALUE, |
| 160 startTime=self.EARLY) |
| 161 late_starting = metric_value.create( |
| 162 labels=self.TEST_LABELS, |
| 163 doubleValue=self.A_FLOAT_VALUE, |
| 164 startTime=self.LATER) |
| 165 got = metric_value.merge(MetricKind.DELTA, early_starting, |
| 166 late_starting) |
| 167 expect(got.startTime).to(equal(early_starting.startTime)) |
| 168 got = metric_value.merge(MetricKind.DELTA, late_starting, |
| 169 early_starting) |
| 170 expect(got.startTime).to(equal(early_starting.startTime)) |
OLD | NEW |