1 /**
2 * Copyright 2019 Huawei Technologies Co., Ltd
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 #include <cstring>
17 #include <iostream>
18 #include <memory>
19 #include <string>
20 #include "minddata/dataset/core/client.h"
21 #include "minddata/dataset/engine/datasetops/rename_op.h"
22 #include "minddata/dataset/engine/jagged_connector.h"
23 #include "common/common.h"
24 #include "utils/ms_utils.h"
25
26 #include "gtest/gtest.h"
27 #include "minddata/dataset/core/global_context.h"
28 #include "utils/log_adapter.h"
29
30 namespace common = mindspore::common;
31
32 using namespace mindspore::dataset;
33 using mindspore::LogStream;
34 using mindspore::ExceptionType::NoExceptionType;
35 using mindspore::MsLogLevel::INFO;
36
37 class MindDataTestRenameOp : public UT::DatasetOpTesting {};
38
TEST_F(MindDataTestRenameOp,TestRenameOpDefault)39 TEST_F(MindDataTestRenameOp, TestRenameOpDefault) {
40 // Tree:
41 //
42 //
43 // OpId(2) RenameOp
44 // |
45 // OpId(0) TFReaderOp
46 // Start with an empty execution tree
47 Status rc;
48 MS_LOG(INFO) << "UT test TestRenameBasic.";
49 auto my_tree = std::make_shared<ExecutionTree>();
50 // Creating TFReaderOp
51
52 std::string dataset_path = datasets_root_path_ + "/test_tf_file_3_images/train-0000-of-0001.data";
53 std::shared_ptr<ConfigManager> config_manager = GlobalContext::config_manager();
54 int32_t op_connector_size = config_manager->op_connector_size();
55 int32_t num_workers = 1;
56 int32_t worker_connector_size = 16;
57 std::vector<std::string> columns_to_load = {};
58 std::vector<std::string> files = {dataset_path};
59 std::unique_ptr<DataSchema> schema = std::make_unique<DataSchema>();
60 std::shared_ptr<TFReaderOp> my_tfreader_op =
61 std::make_shared<TFReaderOp>(num_workers, worker_connector_size, 0, files, std::move(schema), op_connector_size,
62 columns_to_load, false, 1, 0, false);
63 rc = my_tfreader_op->Init();
64 EXPECT_TRUE(rc.IsOk());
65 rc = my_tree->AssociateNode(my_tfreader_op);
66 EXPECT_TRUE(rc.IsOk());
67
68 // Creating DatasetOp
69 std::vector<std::string> in_col_names = {"label"};
70 std::vector<std::string> out_col_names = {"label1"};
71
72 std::shared_ptr<RenameOp> rename_op = std::make_shared<RenameOp>(in_col_names, out_col_names);
73
74 rc = my_tree->AssociateNode(rename_op);
75 EXPECT_TRUE(rc.IsOk());
76 rc = rename_op->AddChild(std::move(my_tfreader_op));
77 EXPECT_TRUE(rc.IsOk());
78 rc = my_tree->AssignRoot(rename_op);
79 EXPECT_TRUE(rc.IsOk());
80 rc = my_tree->Prepare();
81 EXPECT_TRUE(rc.IsOk());
82
83 // Launch the tree execution to kick off threads and start running the pipeline
84 MS_LOG(INFO) << "Launching my tree.";
85 rc = my_tree->Launch();
86 EXPECT_TRUE(rc.IsOk());
87
88 // Simulate a parse of data from our pipeline.
89 std::shared_ptr<DatasetOp> root_node = my_tree->root();
90
91 DatasetIterator di(my_tree);
92 TensorRow tensor_list;
93 rc = di.FetchNextTensorRow(&tensor_list);
94 EXPECT_TRUE(rc.IsOk());
95
96 int row_count = 0;
97 while (!tensor_list.empty()) {
98 MS_LOG(INFO) << "Row display for row #: " << row_count << ".";
99
100 // Display the tensor by calling the printer on it
101 for (int i = 0; i < tensor_list.size(); i++) {
102 std::ostringstream ss;
103 ss << "(" << tensor_list[i] << "): " << *tensor_list[i] << std::endl;
104 MS_LOG(INFO) << "Tensor print: " << ss.str() << ".";
105 }
106 rc = di.FetchNextTensorRow(&tensor_list);
107 EXPECT_TRUE(rc.IsOk());
108 row_count++;
109 }
110 ASSERT_EQ(row_count, 3); // Should be 3 rows fetched
111 }
112