Skip to content
This repository was archived by the owner on Jul 31, 2023. It is now read-only.

Commit f50981b

Browse files
isturdyg-easy
authored andcommitted
Add some missing test coverage, and fix a bug in TestUtils::MakeViewData(). (#19)
1 parent e4b8acf commit f50981b

5 files changed

Lines changed: 186 additions & 1 deletion

File tree

opencensus/stats/BUILD

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,18 @@ cc_test(
206206
],
207207
)
208208

209+
cc_test(
210+
name = "view_data_test",
211+
srcs = ["internal/view_data_test.cc"],
212+
copts = TEST_COPTS,
213+
deps = [
214+
":core",
215+
":test_utils",
216+
"@com_google_absl//absl/time",
217+
"@com_google_googletest//:gtest_main",
218+
],
219+
)
220+
209221
# Benchmarks
210222
# ========================================================================= #
211223
cc_binary(

opencensus/stats/internal/distribution_test.cc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,5 +163,22 @@ TEST_F(DistributionTest, NegativeInfinity) {
163163
EXPECT_EQ(distribution.max(), 1);
164164
}
165165

166+
TEST_F(DistributionTest, DebugString) {
167+
BucketBoundaries buckets = BucketBoundaries::Explicit({0});
168+
Distribution distribution = MakeDistribution(&buckets);
169+
AddToDistribution(&distribution, -1);
170+
AddToDistribution(&distribution, 7);
171+
172+
const std::string s = distribution.DebugString();
173+
174+
EXPECT_PRED_FORMAT2(::testing::IsSubstring, "count: 2", s);
175+
EXPECT_PRED_FORMAT2(::testing::IsSubstring, "mean: 3", s);
176+
EXPECT_PRED_FORMAT2(::testing::IsSubstring, "sum of squared deviation: 32",
177+
s);
178+
EXPECT_PRED_FORMAT2(::testing::IsSubstring, "min: -1", s);
179+
EXPECT_PRED_FORMAT2(::testing::IsSubstring, "max: 7", s);
180+
EXPECT_PRED_FORMAT2(::testing::IsSubstring, "histogram counts: 1, 1", s);
181+
}
182+
166183
} // namespace stats
167184
} // namespace opencensus

opencensus/stats/internal/view_data_impl_test.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "gmock/gmock.h"
2121
#include "gtest/gtest.h"
2222
#include "opencensus/stats/aggregation.h"
23+
#include "opencensus/stats/aggregation_window.h"
2324
#include "opencensus/stats/bucket_boundaries.h"
2425
#include "opencensus/stats/distribution.h"
2526
#include "opencensus/stats/view_descriptor.h"
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
// Copyright 2018, OpenCensus Authors
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+
#include "opencensus/stats/view_data.h"
16+
17+
#include "absl/time/time.h"
18+
#include "gmock/gmock.h"
19+
#include "gtest/gtest.h"
20+
#include "opencensus/stats/aggregation.h"
21+
#include "opencensus/stats/aggregation_window.h"
22+
#include "opencensus/stats/bucket_boundaries.h"
23+
#include "opencensus/stats/distribution.h"
24+
#include "opencensus/stats/internal/view_data_impl.h"
25+
#include "opencensus/stats/testing/test_utils.h"
26+
#include "opencensus/stats/view_descriptor.h"
27+
28+
namespace opencensus {
29+
namespace stats {
30+
namespace {
31+
32+
TEST(ViewDataTest, CumulativeSum) {
33+
const auto descriptor =
34+
ViewDescriptor()
35+
.set_aggregation(Aggregation::Sum())
36+
.set_aggregation_window(AggregationWindow::Cumulative());
37+
ViewData data = testing::TestUtils::MakeViewData(descriptor, {{{}, 2.0}});
38+
EXPECT_EQ(Aggregation::Sum(), data.aggregation());
39+
EXPECT_EQ(AggregationWindow::Cumulative(), data.aggregation_window());
40+
ASSERT_EQ(ViewData::Type::kDouble, data.type());
41+
EXPECT_THAT(data.double_data(),
42+
::testing::UnorderedElementsAre(
43+
::testing::Pair(::testing::ElementsAre(), 2.0)));
44+
}
45+
46+
TEST(ViewDataTest, IntervalSum) {
47+
const auto descriptor =
48+
ViewDescriptor()
49+
.set_aggregation(Aggregation::Sum())
50+
.set_aggregation_window(
51+
AggregationWindow::Interval(absl::Minutes(1)));
52+
ViewData data = testing::TestUtils::MakeViewData(descriptor, {{{}, 2.0}});
53+
EXPECT_EQ(Aggregation::Sum(), data.aggregation());
54+
EXPECT_EQ(AggregationWindow::Type::kInterval,
55+
data.aggregation_window().type());
56+
EXPECT_EQ(absl::Minutes(1), data.aggregation_window().duration());
57+
ASSERT_EQ(ViewData::Type::kDouble, data.type());
58+
EXPECT_THAT(data.double_data(),
59+
::testing::UnorderedElementsAre(
60+
::testing::Pair(::testing::ElementsAre(), 2.0)));
61+
}
62+
63+
TEST(ViewDataTest, CumulativeCount) {
64+
const auto descriptor =
65+
ViewDescriptor()
66+
.set_aggregation(Aggregation::Count())
67+
.set_aggregation_window(AggregationWindow::Cumulative());
68+
ViewData data = testing::TestUtils::MakeViewData(descriptor, {{{}, 2.0}});
69+
EXPECT_EQ(Aggregation::Count(), data.aggregation());
70+
EXPECT_EQ(AggregationWindow::Cumulative(), data.aggregation_window());
71+
ASSERT_EQ(ViewData::Type::kInt64, data.type());
72+
EXPECT_THAT(data.int_data(), ::testing::UnorderedElementsAre(::testing::Pair(
73+
::testing::ElementsAre(), 1)));
74+
}
75+
76+
TEST(ViewDataTest, IntervalCount) {
77+
const auto window = AggregationWindow::Interval(absl::Minutes(1));
78+
const auto descriptor = ViewDescriptor()
79+
.set_aggregation(Aggregation::Count())
80+
.set_aggregation_window(window);
81+
ViewData data = testing::TestUtils::MakeViewData(descriptor, {{{}, 2.0}});
82+
EXPECT_EQ(Aggregation::Count(), data.aggregation());
83+
EXPECT_EQ(window, data.aggregation_window());
84+
ASSERT_EQ(ViewData::Type::kDouble, data.type());
85+
EXPECT_THAT(data.double_data(),
86+
::testing::UnorderedElementsAre(
87+
::testing::Pair(::testing::ElementsAre(), 1.0)));
88+
}
89+
90+
TEST(ViewDataTest, CumulativeDistribution) {
91+
const auto aggregation =
92+
Aggregation::Distribution(BucketBoundaries::Explicit({0}));
93+
const auto descriptor =
94+
ViewDescriptor()
95+
.set_aggregation(aggregation)
96+
.set_aggregation_window(AggregationWindow::Cumulative());
97+
ViewData data = testing::TestUtils::MakeViewData(descriptor, {{{}, 2.0}});
98+
EXPECT_EQ(aggregation, data.aggregation());
99+
EXPECT_EQ(AggregationWindow::Cumulative(), data.aggregation_window());
100+
ASSERT_EQ(ViewData::Type::kDistribution, data.type());
101+
EXPECT_EQ(data.distribution_data().size(), 1);
102+
}
103+
104+
TEST(ViewDataTest, IntervalDistribution) {
105+
const auto aggregation =
106+
Aggregation::Distribution(BucketBoundaries::Explicit({0}));
107+
const auto window = AggregationWindow::Interval(absl::Minutes(1));
108+
const auto descriptor = ViewDescriptor()
109+
.set_aggregation(aggregation)
110+
.set_aggregation_window(window);
111+
ViewData data = testing::TestUtils::MakeViewData(descriptor, {{{}, 2.0}});
112+
EXPECT_EQ(aggregation, data.aggregation());
113+
EXPECT_EQ(window, data.aggregation_window());
114+
ASSERT_EQ(ViewData::Type::kDistribution, data.type());
115+
EXPECT_EQ(data.distribution_data().size(), 1);
116+
}
117+
118+
TEST(ViewDataDeathTest, DoubleData) {
119+
const auto descriptor =
120+
ViewDescriptor()
121+
.set_aggregation(Aggregation::Sum())
122+
.set_aggregation_window(AggregationWindow::Cumulative());
123+
ViewData data = testing::TestUtils::MakeViewData(descriptor, {{{}, 1.0}});
124+
EXPECT_DEBUG_DEATH({ EXPECT_TRUE(data.int_data().empty()); }, "");
125+
EXPECT_DEBUG_DEATH({ EXPECT_TRUE(data.distribution_data().empty()); }, "");
126+
}
127+
128+
TEST(ViewDataDeathTest, IntData) {
129+
const auto descriptor =
130+
ViewDescriptor()
131+
.set_aggregation(Aggregation::Count())
132+
.set_aggregation_window(AggregationWindow::Cumulative());
133+
ViewData data = testing::TestUtils::MakeViewData(descriptor, {{{}, 1.0}});
134+
EXPECT_DEBUG_DEATH({ EXPECT_TRUE(data.double_data().empty()); }, "");
135+
EXPECT_DEBUG_DEATH({ EXPECT_TRUE(data.distribution_data().empty()); }, "");
136+
}
137+
138+
TEST(ViewDataDeathTest, DistributionData) {
139+
const auto descriptor =
140+
ViewDescriptor()
141+
.set_aggregation(
142+
Aggregation::Distribution(BucketBoundaries::Explicit({})))
143+
.set_aggregation_window(AggregationWindow::Cumulative());
144+
ViewData data = testing::TestUtils::MakeViewData(descriptor, {{{}, 1.0}});
145+
EXPECT_DEBUG_DEATH({ EXPECT_TRUE(data.double_data().empty()); }, "");
146+
EXPECT_DEBUG_DEATH({ EXPECT_TRUE(data.int_data().empty()); }, "");
147+
}
148+
149+
} // namespace
150+
} // namespace stats
151+
} // namespace opencensus

opencensus/stats/testing/test_utils.cc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ ViewData TestUtils::MakeViewData(
3131
for (const auto& value : values) {
3232
impl->Add(value.second, value.first, absl::UnixEpoch());
3333
}
34-
return ViewData(std::move(impl));
34+
if (impl->type() == ViewDataImpl::Type::kStatsObject) {
35+
return ViewData(absl::make_unique<ViewDataImpl>(*impl, absl::UnixEpoch()));
36+
} else {
37+
return ViewData(std::move(impl));
38+
}
3539
}
3640

3741
} // namespace testing

0 commit comments

Comments
 (0)