This repository has been archived by the owner on Dec 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
bug: fix year formatting in spanner::Date #1085
Merged
coryan
merged 2 commits into
googleapis:master
from
coryan:fix-year-formatting-for-dates
Nov 19, 2019
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
google/cloud/spanner/integration_tests/data_types_integration_test.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
// Copyright 2019 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include "google/cloud/spanner/client.h" | ||
#include "google/cloud/spanner/database.h" | ||
#include "google/cloud/spanner/database_admin_client.h" | ||
#include "google/cloud/spanner/mutations.h" | ||
#include "google/cloud/spanner/testing/database_environment.h" | ||
#include "google/cloud/internal/getenv.h" | ||
#include "google/cloud/testing_util/assert_ok.h" | ||
#include "google/cloud/testing_util/init_google_mock.h" | ||
#include <gmock/gmock.h> | ||
|
||
namespace google { | ||
namespace cloud { | ||
namespace spanner { | ||
inline namespace SPANNER_CLIENT_NS { | ||
namespace { | ||
|
||
class DataTypeIntegrationTest : public ::testing::Test { | ||
public: | ||
static void SetUpTestSuite() { | ||
client_ = google::cloud::internal::make_unique<Client>( | ||
MakeConnection(spanner_testing::DatabaseEnvironment::GetDatabase())); | ||
} | ||
|
||
void SetUp() override { | ||
auto commit_result = client_->Commit([](Transaction const&) { | ||
return Mutations{ | ||
MakeDeleteMutation("Singers", KeySet::All()), | ||
MakeDeleteMutation("DataTypes", KeySet::All()), | ||
}; | ||
}); | ||
EXPECT_STATUS_OK(commit_result); | ||
} | ||
|
||
static void TearDownTestSuite() { client_ = nullptr; } | ||
|
||
protected: | ||
static std::unique_ptr<Client> client_; | ||
}; | ||
|
||
std::unique_ptr<Client> DataTypeIntegrationTest::client_; | ||
|
||
TEST_F(DataTypeIntegrationTest, ReadWriteDate) { | ||
auto& client = *client_; | ||
using RowType = std::tuple<std::string, Date, std::string>; | ||
RowType read_back; | ||
auto commit_result = client_->Commit( | ||
[&client, &read_back](Transaction const& txn) -> StatusOr<Mutations> { | ||
auto result = client.ExecuteDml( | ||
txn, | ||
SqlStatement( | ||
"INSERT INTO DataTypes (Id, DateValue, StringValue) " | ||
"VALUES(@id, @date, @event)", | ||
{{"id", Value("ReadWriteDate-1")}, | ||
{"date", Value(Date(161, 3, 8))}, | ||
{"event", Value("Marcus Aurelius ascends to the throne")}})); | ||
if (!result) return std::move(result).status(); | ||
|
||
auto reader = client.ExecuteQuery( | ||
txn, SqlStatement("SELECT Id, DateValue, StringValue FROM DataTypes" | ||
" WHERE Id = @id", | ||
{{"id", Value("ReadWriteDate-1")}})); | ||
|
||
auto row = GetCurrentRow(StreamOf<RowType>(reader)); | ||
if (!row) return std::move(row).status(); | ||
read_back = *std::move(row); | ||
return Mutations{}; | ||
}); | ||
|
||
ASSERT_TRUE(commit_result.ok()); | ||
EXPECT_EQ("ReadWriteDate-1", std::get<0>(read_back)); | ||
EXPECT_EQ(Date(161, 3, 8), std::get<1>(read_back)); | ||
EXPECT_EQ("Marcus Aurelius ascends to the throne", std::get<2>(read_back)); | ||
} | ||
|
||
} // namespace | ||
} // namespace SPANNER_CLIENT_NS | ||
} // namespace spanner | ||
} // namespace cloud | ||
} // namespace google | ||
|
||
int main(int argc, char* argv[]) { | ||
::google::cloud::testing_util::InitGoogleMock(argc, argv); | ||
(void)::testing::AddGlobalTestEnvironment( | ||
new google::cloud::spanner_testing::DatabaseEnvironment()); | ||
|
||
return RUN_ALL_TESTS(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,7 +25,7 @@ namespace internal { | |
|
||
std::string DateToString(Date d) { | ||
std::array<char, sizeof "-9223372036854775808-01-01"> buf; | ||
std::snprintf(buf.data(), buf.size(), "%" PRId64 "-%02d-%02d", d.year(), | ||
std::snprintf(buf.data(), buf.size(), "%04" PRId64 "-%02d-%02d", d.year(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oops. Thanks. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no problem. |
||
d.month(), d.day()); | ||
return std::string(buf.data()); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps we should add a "> 9999" case to see how they handle their "YYYY: Four-digit year" edict.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Anything over 9999 results in an error. That is consistent with the documentation; which says the valid range is
0001-01-01
to9999-12-31