Skip to content

Commit

Permalink
test(spanner): add mutations/row unit tests for FLOAT32 (#13880)
Browse files Browse the repository at this point in the history
  • Loading branch information
devbww authored Mar 29, 2024
1 parent 1ad6b64 commit 8bdc871
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
35 changes: 30 additions & 5 deletions google/cloud/spanner/mutations_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
#include "google/cloud/testing_util/is_proto_equal.h"
#include "absl/types/optional.h"
#include <google/protobuf/text_format.h>
#include <google/protobuf/util/message_differencer.h>
#include <gmock/gmock.h>
#include <cstdint>
#include <limits>
#include <sstream>
#include <string>
#include <tuple>
Expand All @@ -38,6 +38,7 @@ namespace spanner {
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
namespace {

using ::google::cloud::testing_util::IsProtoApproximatelyEqual;
using ::google::cloud::testing_util::IsProtoEqual;
using ::google::protobuf::TextFormat;
using ::testing::HasSubstr;
Expand Down Expand Up @@ -84,8 +85,35 @@ TEST(MutationsTest, InsertSimple) {
EXPECT_THAT(actual, IsProtoEqual(expected));
}

TEST(MutationsTest, InsertFloat32) {
auto builder = InsertMutationBuilder("table-name", {"col1", "col2"})
.EmplaceRow(1, 3.14F);
Mutation insert = builder.Build();
Mutation moved = std::move(builder).Build();
EXPECT_EQ(insert, moved);

auto actual = std::move(insert).as_proto();
auto constexpr kText = R"pb(
insert: {
table: "table-name"
columns: "col1"
columns: "col2"
values {
values { string_value: "1" }
values { number_value: 3.14 }
}
}
)pb";
google::spanner::v1::Mutation expected;
ASSERT_TRUE(TextFormat::ParseFromString(kText, &expected));

// Compare number_value using the (larger) float epsilon.
EXPECT_THAT(actual, IsProtoApproximatelyEqual(
expected, std::numeric_limits<float>::epsilon(),
std::numeric_limits<float>::epsilon()));
}

TEST(MutationsTest, InsertComplex) {
Mutation empty;
auto builder = InsertMutationBuilder("table-name", {"col1", "col2", "col3"})
.AddRow({Value(42), Value("foo"), Value(false)})
.EmplaceRow(absl::optional<std::int64_t>(), "bar",
Expand Down Expand Up @@ -146,7 +174,6 @@ TEST(MutationsTest, UpdateSimple) {
}

TEST(MutationsTest, UpdateComplex) {
Mutation empty;
auto builder = UpdateMutationBuilder("table-name", {"col_a", "col_b"})
.AddRow({Value(std::vector<std::string>{}), Value(7.0)})
.EmplaceRow(std::vector<std::string>{"a", "b"},
Expand Down Expand Up @@ -209,7 +236,6 @@ TEST(MutationsTest, InsertOrUpdateSimple) {
}

TEST(MutationsTest, InsertOrUpdateComplex) {
Mutation empty;
auto builder = InsertOrUpdateMutationBuilder("table-name", {"col_a", "col_b"})
.AddRow({Value(std::make_tuple("a", 7.0))})
.EmplaceRow(std::make_tuple("b", 8.0));
Expand Down Expand Up @@ -274,7 +300,6 @@ TEST(MutationsTest, ReplaceSimple) {
}

TEST(MutationsTest, ReplaceComplex) {
Mutation empty;
auto builder = ReplaceMutationBuilder("table-name", {"col_a", "col_b"})
.EmplaceRow("a", 7.0)
.AddRow({Value("b"), Value(8.0)});
Expand Down
17 changes: 17 additions & 0 deletions google/cloud/spanner/row_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,23 @@ TEST(RowStreamIterator, ForLoop) {
EXPECT_EQ(product, 30);
}

TEST(RowStreamIterator, RangeForLoopFloat32) {
std::vector<Row> rows;
rows.emplace_back(spanner_mocks::MakeRow({{"num", Value(2.1F)}}));
rows.emplace_back(spanner_mocks::MakeRow({{"num", Value(3.2F)}}));
rows.emplace_back(spanner_mocks::MakeRow({{"num", Value(5.4F)}}));

RowRange range(MakeRowStreamIteratorSource(rows));
float sum = 0;
for (auto const& row : range) {
ASSERT_STATUS_OK(row);
auto num = row->get<float>("num");
ASSERT_STATUS_OK(num);
sum += *num;
}
EXPECT_FLOAT_EQ(sum, 10.7F);
}

TEST(RowStreamIterator, RangeForLoop) {
std::vector<Row> rows;
rows.emplace_back(spanner_mocks::MakeRow({{"num", Value(2)}}));
Expand Down

0 comments on commit 8bdc871

Please sign in to comment.