TensorFlow Serving C++ API Documentation
hashmap_source_adapter_test.cc
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 
16 #include "tensorflow_serving/servables/hashmap/hashmap_source_adapter.h"
17 
18 #include <memory>
19 #include <unordered_map>
20 #include <utility>
21 
22 #include <gmock/gmock.h>
23 #include <gtest/gtest.h>
24 #include "tensorflow/core/lib/core/errors.h"
25 #include "tensorflow/core/lib/core/status.h"
26 #include "tensorflow/core/lib/core/status_test_util.h"
27 #include "tensorflow/core/lib/io/path.h"
28 #include "tensorflow/core/lib/strings/strcat.h"
29 #include "tensorflow/core/platform/env.h"
30 #include "tensorflow/core/platform/test.h"
31 #include "tensorflow/core/platform/types.h"
32 #include "tensorflow_serving/core/loader.h"
33 #include "tensorflow_serving/core/servable_data.h"
34 #include "tensorflow_serving/servables/hashmap/hashmap_source_adapter.pb.h"
35 #include "tensorflow_serving/util/any_ptr.h"
36 
37 using ::testing::Pair;
38 using ::testing::UnorderedElementsAre;
39 
40 namespace tensorflow {
41 namespace serving {
42 namespace {
43 
44 using Hashmap = std::unordered_map<string, string>;
45 
46 // Writes the given hashmap to a file.
47 Status WriteHashmapToFile(const HashmapSourceAdapterConfig::Format format,
48  const string& file_name, const Hashmap& hashmap) {
49  std::unique_ptr<WritableFile> file;
50  TF_RETURN_IF_ERROR(Env::Default()->NewWritableFile(file_name, &file));
51  switch (format) {
52  case HashmapSourceAdapterConfig::SIMPLE_CSV: {
53  for (const auto& entry : hashmap) {
54  const string& key = entry.first;
55  const string& value = entry.second;
56  const string line = strings::StrCat(key, ",", value, "\n");
57  TF_RETURN_IF_ERROR(file->Append(line));
58  }
59  break;
60  }
61  default:
62  return errors::InvalidArgument("Unrecognized format enum value: ",
63  format);
64  }
65  TF_RETURN_IF_ERROR(file->Close());
66  return Status();
67 }
68 
69 TEST(HashmapSourceAdapter, Basic) {
70  const auto format = HashmapSourceAdapterConfig::SIMPLE_CSV;
71  const string file = io::JoinPath(testing::TmpDir(), "Basic");
72  TF_ASSERT_OK(
73  WriteHashmapToFile(format, file, {{"a", "apple"}, {"b", "banana"}}));
74 
75  HashmapSourceAdapterConfig config;
76  config.set_format(format);
77  auto adapter =
78  std::unique_ptr<HashmapSourceAdapter>(new HashmapSourceAdapter(config));
79  ServableData<std::unique_ptr<Loader>> loader_data =
80  adapter->AdaptOneVersion({{"", 0}, file});
81  TF_ASSERT_OK(loader_data.status());
82  std::unique_ptr<Loader> loader = loader_data.ConsumeDataOrDie();
83 
84  TF_ASSERT_OK(loader->Load());
85 
86  const Hashmap* hashmap = loader->servable().get<Hashmap>();
87  EXPECT_THAT(*hashmap,
88  UnorderedElementsAre(Pair("a", "apple"), Pair("b", "banana")));
89 
90  loader->Unload();
91 }
92 
93 } // namespace
94 } // namespace serving
95 } // namespace tensorflow