From 3e2a1fb7b7068982b80f13af73d08eb05e3fde8c Mon Sep 17 00:00:00 2001 From: Carlos O'Ryan Date: Fri, 22 Jan 2021 13:35:06 +0000 Subject: [PATCH] feat: implement storage_unit_test example --- examples/CMakeLists.txt | 1 + .../hello_world_storage.cc | 2 +- examples/site/testing_storage/CMakeLists.txt | 50 +++++++++++ .../site/testing_storage/storage_unit_test.cc | 86 +++++++++++++++++++ 4 files changed, 138 insertions(+), 1 deletion(-) create mode 100644 examples/site/testing_storage/CMakeLists.txt create mode 100644 examples/site/testing_storage/storage_unit_test.cc diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 26751b58..25ff05ba 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -79,3 +79,4 @@ endif () add_subdirectory(site/testing_http) add_subdirectory(site/testing_pubsub) +add_subdirectory(site/testing_storage) diff --git a/examples/site/hello_world_storage/hello_world_storage.cc b/examples/site/hello_world_storage/hello_world_storage.cc index fddbb887..748cc9a5 100644 --- a/examples/site/hello_world_storage/hello_world_storage.cc +++ b/examples/site/hello_world_storage/hello_world_storage.cc @@ -31,7 +31,7 @@ void hello_world_storage(gcf::CloudEvent event) { // NOLINT BOOST_LOG_TRIVIAL(info) << "Event: " << event.id(); BOOST_LOG_TRIVIAL(info) << "Event Type: " << event.type(); BOOST_LOG_TRIVIAL(info) << "Bucket: " << payload.value("bucket", ""); - BOOST_LOG_TRIVIAL(info) << "File: " << payload.value("name", ""); + BOOST_LOG_TRIVIAL(info) << "Object: " << payload.value("name", ""); BOOST_LOG_TRIVIAL(info) << "Metageneration: " << payload.value("metageneration", ""); BOOST_LOG_TRIVIAL(info) << "Created: " << payload.value("timeCreated", ""); diff --git a/examples/site/testing_storage/CMakeLists.txt b/examples/site/testing_storage/CMakeLists.txt new file mode 100644 index 00000000..c3da5df6 --- /dev/null +++ b/examples/site/testing_storage/CMakeLists.txt @@ -0,0 +1,50 @@ +# ~~~ +# Copyright 2020 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. +# ~~~ + +if (BUILD_TESTING) + find_package(GTest CONFIG REQUIRED) + set(googleapis_functions_framework_examples_unit_tests # cmake-format: sort + storage_unit_test.cc) + + set(googleapis_functions_framework_examples_programs # cmake-format: sort + ) + + foreach (fname ${googleapis_functions_framework_examples_unit_tests}) + string(REPLACE "/" "_" target "${fname}") + string(REPLACE ".cc" "" target "${target}") + add_executable("${target}" ${fname}) + target_link_libraries( + ${target} + PRIVATE functions_framework_examples + googleapis_functions_framework + Boost::filesystem + Boost::log + GTest::gmock_main + GTest::gmock + GTest::gtest) + add_test(NAME ${target} COMMAND ${target}) + endforeach () + + foreach (fname ${googleapis_functions_framework_examples_programs}) + string(REPLACE "/" "_" target "${fname}") + string(REPLACE ".cc" "" target "${target}") + add_executable("${target}" ${fname}) + target_link_libraries( + ${target} + PRIVATE functions_framework_examples googleapis_functions_framework + Boost::filesystem Boost::log) + endforeach () +endif () diff --git a/examples/site/testing_storage/storage_unit_test.cc b/examples/site/testing_storage/storage_unit_test.cc new file mode 100644 index 00000000..af1393ab --- /dev/null +++ b/examples/site/testing_storage/storage_unit_test.cc @@ -0,0 +1,86 @@ +// Copyright 2020 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. + +// [START functions_storage_unit_test] +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace gcf = ::google::cloud::functions; +extern void hello_world_storage(gcf::CloudEvent event); + +namespace { + +using ::testing::HasSubstr; + +TEST(StorageUnitTest, Basic) { + auto core = boost::log::core::get(); + auto stream = boost::make_shared(); + auto be = [core, stream]() { + auto backend = + boost::make_shared(); + backend->add_stream(stream); + + // Enable auto-flushing after each log record written + backend->auto_flush(true); + using sink_t = boost::log::sinks::synchronous_sink< + boost::log::sinks::text_ostream_backend>; + auto be = boost::make_shared(backend); + core->add_sink(be); + return be; + }(); + + struct TestCases { + std::string name; + std::string expected; + } cases[]{ + {"object1.txt", "Object: object1.txt"}, + {"object/with/longer/name.txt", "Object: object/with/longer/name.txt"}, + }; + + auto const base = nlohmann::json::parse(R"js({ + "bucket": "some-bucket", + "name": "--set-later--", + "generation": "1587627537231057", + "contentType": "text/plain", + "timeCreated": "2020-04-23T07:38:57.230Z", + "updated": "2020-04-23T07:38:57.230Z" + })js"); + + for (auto const& test : cases) { + SCOPED_TRACE("Testing for " + test.expected); + gcf::CloudEvent event( + /*id=*/"test-id-0001", /*source=*/"https://test-source.example.com", + /*type=*/"google.cloud.pubsub.topic.v1.messagePublished"); + event.set_data_content_type("application/json"); + auto data = base; + data["name"] = test.name; + event.set_data(data.dump()); + stream->str({}); + EXPECT_NO_THROW(hello_world_storage(event)); + auto log_lines = stream->str(); + EXPECT_THAT(log_lines, HasSubstr(test.expected)); + } + + core->remove_sink(be); +} + +} // namespace +// [END functions_storage_unit_test]