TensorFlow Serving C++ API Documentation
proto_util_test.cc
1 /* Copyright 2022 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 #include "tensorflow_serving/util/proto_util.h"
16 
17 #include <memory>
18 
19 #include <gtest/gtest.h>
20 #include "tensorflow/core/lib/core/status_test_util.h"
21 #include "tensorflow/core/lib/io/path.h"
22 #include "tensorflow/core/platform/test.h"
23 #include "tensorflow_serving/apis/status.pb.h"
24 
25 namespace tensorflow {
26 namespace serving {
27 namespace {
28 
29 TEST(ProtoUtilTest, ParseProtoTextFile_Errors) {
30  StatusProto status_proto;
31  auto status = ParseProtoTextFile<StatusProto>("missing.txt", &status_proto);
32  ASSERT_TRUE(errors::IsNotFound(status));
33 
34  const string kProtoFile = io::JoinPath(testing::TmpDir(), "corrupt.txt");
35  std::unique_ptr<WritableFile> file;
36  TF_ASSERT_OK(Env::Default()->NewWritableFile(kProtoFile, &file));
37  TF_ASSERT_OK(file->Append("corrupt proto content"));
38  TF_ASSERT_OK(file->Close());
39  status = ParseProtoTextFile<StatusProto>(kProtoFile, &status_proto);
40  ASSERT_TRUE(errors::IsInvalidArgument(status));
41 }
42 
43 TEST(ProtoUtilTest, ParseProtoTextFile_EmptyFile_Success) {
44  StatusProto status_proto;
45  const string kProtoFile = io::JoinPath(testing::TmpDir(), "proto1.txt");
46  std::unique_ptr<WritableFile> file;
47  TF_ASSERT_OK(Env::Default()->NewWritableFile(kProtoFile, &file));
48  TF_ASSERT_OK(file->Append(" "));
49  TF_ASSERT_OK(file->Close());
50  auto status = ParseProtoTextFile<StatusProto>(kProtoFile, &status_proto);
51  TF_ASSERT_OK(status);
52 }
53 
54 TEST(ProtoUtilTest, ParseProtoTextFile_Success) {
55  StatusProto status_proto;
56  const string kProtoFile = io::JoinPath(testing::TmpDir(), "proto2.txt");
57  std::unique_ptr<WritableFile> file;
58  TF_ASSERT_OK(Env::Default()->NewWritableFile(kProtoFile, &file));
59  TF_ASSERT_OK(file->Append("error_message: 'hello'"));
60  TF_ASSERT_OK(file->Close());
61  auto status = ParseProtoTextFile<StatusProto>(kProtoFile, &status_proto);
62  TF_ASSERT_OK(status);
63  ASSERT_EQ("hello", status_proto.error_message());
64 }
65 
66 } // namespace
67 } // namespace serving
68 } // namespace tensorflow