Repository: incubator-hawq
Updated Branches:
  refs/heads/master 76260cdf4 -> dbf32f1b7


HAWQ-822. Add string replacement utility in feature test framework to support 
convert from source to sql and ans files


Project: http://git-wip-us.apache.org/repos/asf/incubator-hawq/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-hawq/commit/dbf32f1b
Tree: http://git-wip-us.apache.org/repos/asf/incubator-hawq/tree/dbf32f1b
Diff: http://git-wip-us.apache.org/repos/asf/incubator-hawq/diff/dbf32f1b

Branch: refs/heads/master
Commit: dbf32f1b756f78e3fb6fb416bae8e104cd5d6792
Parents: 76260cd
Author: Ruilong Huo <r...@pivotal.io>
Authored: Wed Jun 15 23:26:57 2016 +0800
Committer: Ruilong Huo <r...@pivotal.io>
Committed: Thu Jun 16 12:16:06 2016 +0800

----------------------------------------------------------------------
 src/test/feature/lib/file_replace.cpp           | 55 ++++++++++++++++++++
 src/test/feature/lib/file_replace.h             | 29 +++++++++++
 .../feature/testlib/ans/template.ans.source     | 22 ++++++++
 src/test/feature/testlib/sql/template.csv       |  2 +
 .../feature/testlib/sql/template.sql.source     | 11 ++++
 src/test/feature/testlib/test_lib.cpp           | 23 ++++++++
 6 files changed, 142 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-hawq/blob/dbf32f1b/src/test/feature/lib/file_replace.cpp
----------------------------------------------------------------------
diff --git a/src/test/feature/lib/file_replace.cpp 
b/src/test/feature/lib/file_replace.cpp
new file mode 100644
index 0000000..f8685ac
--- /dev/null
+++ b/src/test/feature/lib/file_replace.cpp
@@ -0,0 +1,55 @@
+#include <iostream>
+#include <fstream>
+
+#include "file_replace.h"
+
+using std::string;
+using std::unordered_map;
+using std::getline;
+using std::endl;
+using std::ifstream;
+using std::ofstream;
+
+namespace hawq {
+namespace test {
+
+string FileReplace::replaceAllOccurrences(
+       string str,
+       const string& src,
+       const string& dst)
+{
+       size_t start_pos = 0;
+       while ((start_pos = str.find(src, start_pos)) != string::npos)
+       {
+               str.replace(start_pos, src.length(), dst);
+               start_pos += dst.length();
+       }
+
+       return str;
+}
+
+void FileReplace::replace(
+       const string& file_src,
+       const string& file_dst,
+       const unordered_map<string, string>& strs_src_dst)
+{
+       ifstream fin(file_src);
+       ofstream fout(file_dst);
+       string line;
+
+       while ( getline(fin, line) )
+       {
+               for(auto & mit : strs_src_dst)
+               {
+                       line = replaceAllOccurrences(line, mit.first, 
mit.second);
+               }
+
+               fout << line << endl;
+       }
+
+       fin.close();
+       fout.close();
+}
+
+} // namespace test
+} // namespace hawq

http://git-wip-us.apache.org/repos/asf/incubator-hawq/blob/dbf32f1b/src/test/feature/lib/file_replace.h
----------------------------------------------------------------------
diff --git a/src/test/feature/lib/file_replace.h 
b/src/test/feature/lib/file_replace.h
new file mode 100644
index 0000000..47d4f92
--- /dev/null
+++ b/src/test/feature/lib/file_replace.h
@@ -0,0 +1,29 @@
+#ifndef SRC_TEST_FEATURE_LIB_FILE_REPLACE_H_
+#define SRC_TEST_FEATURE_LIB_FILE_REPLACE_H_
+
+#include <string>
+#include <unordered_map>
+
+namespace hawq {
+namespace test {
+
+class FileReplace
+{
+public:
+       FileReplace() = default;
+       ~FileReplace() = default;
+       FileReplace(const FileReplace&) = delete;
+       FileReplace& operator=(const FileReplace&) = delete;
+
+       void replace(const std::string& file_src,
+                    const std::string& file_dst,
+                    const std::unordered_map<std::string, std::string>& 
strs_src_dst);
+
+private:
+       std::string replaceAllOccurrences(std::string str, const std::string& 
src, const std::string& dst);
+};
+
+} // namespace test
+} // namespace hawq
+
+#endif   // SRC_TEST_FEATURE_LIB_FILE_REPLACE_H_

http://git-wip-us.apache.org/repos/asf/incubator-hawq/blob/dbf32f1b/src/test/feature/testlib/ans/template.ans.source
----------------------------------------------------------------------
diff --git a/src/test/feature/testlib/ans/template.ans.source 
b/src/test/feature/testlib/ans/template.ans.source
new file mode 100644
index 0000000..36aa533
--- /dev/null
+++ b/src/test/feature/testlib/ans/template.ans.source
@@ -0,0 +1,22 @@
+-- start_ignore
+SET SEARCH_PATH=TestCommonLib_TestFileReplace;
+SET
+-- end_ignore
+-- start_ignore
+DROP TABLE IF EXISTS persons;
+psql:/tmp/TestCommonLib_TestFileReplace.sql:5: NOTICE:  table "persons" does 
not exist, skipping
+DROP TABLE
+-- end_ignore
+CREATE TABLE persons(id INT, name VARCHAR);
+CREATE TABLE
+COPY persons (id, name) FROM 
'@ABS_FEATURE_TEST_ROOT@/testlib/sql/template.csv' DELIMITER ',' CSV;
+COPY 2
+SELECT * FROM persons ORDER BY id;
+ id | name  
+----+-------
+  1 | Aiken
+  2 | Zack
+(2 rows)
+
+DROP TABLE IF EXISTS persons;
+DROP TABLE

http://git-wip-us.apache.org/repos/asf/incubator-hawq/blob/dbf32f1b/src/test/feature/testlib/sql/template.csv
----------------------------------------------------------------------
diff --git a/src/test/feature/testlib/sql/template.csv 
b/src/test/feature/testlib/sql/template.csv
new file mode 100644
index 0000000..e5f5820
--- /dev/null
+++ b/src/test/feature/testlib/sql/template.csv
@@ -0,0 +1,2 @@
+1,Aiken
+2,Zack

http://git-wip-us.apache.org/repos/asf/incubator-hawq/blob/dbf32f1b/src/test/feature/testlib/sql/template.sql.source
----------------------------------------------------------------------
diff --git a/src/test/feature/testlib/sql/template.sql.source 
b/src/test/feature/testlib/sql/template.sql.source
new file mode 100644
index 0000000..bb767d8
--- /dev/null
+++ b/src/test/feature/testlib/sql/template.sql.source
@@ -0,0 +1,11 @@
+-- start_ignore
+DROP TABLE IF EXISTS persons;
+-- end_ignore
+
+CREATE TABLE persons(id INT, name VARCHAR);
+
+COPY persons (id, name) FROM 
'@ABS_FEATURE_TEST_ROOT@/testlib/sql/template.csv' DELIMITER ',' CSV;
+
+SELECT * FROM persons ORDER BY id;
+
+DROP TABLE IF EXISTS persons;

http://git-wip-us.apache.org/repos/asf/incubator-hawq/blob/dbf32f1b/src/test/feature/testlib/test_lib.cpp
----------------------------------------------------------------------
diff --git a/src/test/feature/testlib/test_lib.cpp 
b/src/test/feature/testlib/test_lib.cpp
index 3b18b11..98b90d2 100644
--- a/src/test/feature/testlib/test_lib.cpp
+++ b/src/test/feature/testlib/test_lib.cpp
@@ -11,6 +11,7 @@
 #include "lib/hawq_config.h"
 #include "lib/sql_util.h"
 #include "lib/string_util.h"
+#include "lib/file_replace.h"
 
 #include "gtest/gtest.h"
 
@@ -99,3 +100,25 @@ TEST_F(TestCommonLib, TestDataGenerator) {
 
   dGen.genTableWithNull("tNull");
 }
+
+TEST_F(TestCommonLib, TestFileReplace) {
+  // prepare file names
+  hawq::test::SQLUtility util;
+  std::string d_feature_test_root(util.getTestRootPath());
+  std::string f_sql_tpl(d_feature_test_root + 
"/testlib/sql/template.sql.source");
+  std::string f_ans_tpl(d_feature_test_root + 
"/testlib/ans/template.ans.source");
+  std::string f_sql(d_feature_test_root + "/testlib/sql/template.sql");
+  std::string f_ans(d_feature_test_root + "/testlib/ans/template.ans");
+
+  // preprocess source files to get sql/ans files
+  hawq::test::FileReplace frep;
+  std::unordered_map<std::string, std::string> strs_src_dst;
+  strs_src_dst.insert(std::make_pair("@ABS_FEATURE_TEST_ROOT@", 
d_feature_test_root));
+
+  frep.replace(f_sql_tpl, f_sql, strs_src_dst);
+  frep.replace(f_ans_tpl, f_ans, strs_src_dst);
+
+  // run sql file to get ans file and then diff it with out file
+  util.execSQLFile("testlib/sql/template.sql",
+                   "testlib/ans/template.ans");
+}

Reply via email to