Repository: incubator-hawq
Updated Branches:
  refs/heads/master 025eecbad -> 3c2419bcc


HAWQ-740. Fix bug in SQLUtility.


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

Branch: refs/heads/master
Commit: 3c2419bcc51a55cf1fd8445fbd41b046c2b50ab3
Parents: 025eecb
Author: ztao1987 <zhenglin.ta...@gmail.com>
Authored: Thu May 19 10:53:29 2016 +0800
Committer: ztao1987 <zhenglin.ta...@gmail.com>
Committed: Thu May 19 10:53:29 2016 +0800

----------------------------------------------------------------------
 src/test/feature/lib/sql-util.cpp | 50 +++++++++++++++++++++++-----------
 src/test/feature/lib/sql-util.h   |  8 ++++--
 2 files changed, 40 insertions(+), 18 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-hawq/blob/3c2419bc/src/test/feature/lib/sql-util.cpp
----------------------------------------------------------------------
diff --git a/src/test/feature/lib/sql-util.cpp 
b/src/test/feature/lib/sql-util.cpp
index 61b3d87..6283adb 100644
--- a/src/test/feature/lib/sql-util.cpp
+++ b/src/test/feature/lib/sql-util.cpp
@@ -1,5 +1,6 @@
 #include "sql-util.h"
 
+#include <libproc.h>
 #include <pwd.h>
 #include <sys/types.h>
 #include <unistd.h>
@@ -16,22 +17,26 @@ SQLUtility::SQLUtility()
       test_info(::testing::UnitTest::GetInstance()->current_test_info()) {
   schemaName =
       std::string(test_info->test_case_name()) + "_" + test_info->name();
-  conn->runSQLCommand("DROP SCHEMA IF EXISTS " + schemaName + " CASCADE");
-  conn->runSQLCommand("CREATE SCHEMA " + schemaName);
+  exec("DROP SCHEMA IF EXISTS " + schemaName + " CASCADE");
+  exec("CREATE SCHEMA " + schemaName);
 }
 
 SQLUtility::~SQLUtility() {
   if (!test_info->result()->Failed())
-    conn->runSQLCommand("DROP SCHEMA " + schemaName + " CASCADE");
+    exec("DROP SCHEMA " + schemaName + " CASCADE");
 }
 
-void SQLUtility::execute(const std::string &sql) {
-  EXPECT_EQ((conn->runSQLCommand("SET SEARCH_PATH=" + schemaName + ";" + sql))
-                .getLastStatus(),
-            0)
+void SQLUtility::exec(const std::string &sql) {
+  EXPECT_EQ((conn->runSQLCommand(sql)).getLastStatus(), 0)
       << conn->getLastResult();
 }
 
+void SQLUtility::execute(const std::string &sql, bool check) {
+  conn->runSQLCommand("SET SEARCH_PATH=" + schemaName + ";" + sql);
+  EXPECT_NE(conn.get(), nullptr);
+  if (check) EXPECT_EQ(conn->getLastStatus(), 0) << conn->getLastResult();
+}
+
 void SQLUtility::query(const std::string &sql, int expectNum) {
   const PSQLQueryResult &result = executeQuery(sql);
   ASSERT_FALSE(result.isError()) << result.getErrorMessage();
@@ -69,22 +74,27 @@ void SQLUtility::execSQLFile(const std::string &sqlFile,
   const std::string newSqlFile = generateSQLFile(sqlFile);
 
   // outFile is located in the same folder with ansFile
-  std::string outFile = fp.path + "/" + fp.fileBaseName + ".out";
-  conn->setOutputFile(outFile);
+  std::string outFileAbsPath = fp.path + "/" + fp.fileBaseName + ".out";
+  conn->setOutputFile(outFileAbsPath);
   EXPECT_EQ(conn->runSQLFile(newSqlFile).getLastStatus(), 0);
   conn->resetOutput();
-  if (remove(newSqlFile.c_str()))
-    ASSERT_TRUE(false) << "Error deleting file " << newSqlFile;
-  EXPECT_FALSE(conn->checkDiff(ansFile, outFile, true));
+  EXPECT_FALSE(conn->checkDiff(ansFileAbsPath, outFileAbsPath, true));
+  if (conn->checkDiff(ansFileAbsPath, outFileAbsPath, true) == false) {
+    // no diff, continue to delete the generated sql file
+    if (remove(newSqlFile.c_str()))
+      ASSERT_TRUE(false) << "Error deleting file " << newSqlFile;
+  } else {
+    EXPECT_FALSE(true);
+  }
 }
 
 std::unique_ptr<PSQL> SQLUtility::getConnection() {
   std::string user = HAWQ_USER;
-  if(user == ""){
+  if (user.empty()) {
     struct passwd *pw;
     uid_t uid = geteuid();
     pw = getpwuid(uid);
-    user.assign(pw->pw_name);;
+    user.assign(pw->pw_name);
   }
   std::unique_ptr<PSQL> psql(
       new PSQL(HAWQ_DB, HAWQ_HOST, HAWQ_PORT, user, HAWQ_PASSWORD));
@@ -125,8 +135,16 @@ const PSQLQueryResult &SQLUtility::executeQuery(const 
std::string &sql) {
 PSQL *SQLUtility::getPSQL() const { return conn.get(); }
 
 std::string SQLUtility::getTestRootPath() const {
-  FilePath fp = splitFilePath(__FILE__);
-  return splitFilePath(__FILE__).path + "/..";
+  int ret;
+  pid_t pid;
+  char pathbuf[PROC_PIDPATHINFO_MAXSIZE];
+
+  pid = getpid();
+  ret = proc_pidpath(pid, pathbuf, sizeof(pathbuf));
+  if (ret <= 0)
+    EXPECT_TRUE(false) << "PID " << pid << ": proc_pidpath () "
+                       << strerror(errno);
+  return splitFilePath(pathbuf).path;
 }
 
 FilePath SQLUtility::splitFilePath(const std::string &filePath) const {

http://git-wip-us.apache.org/repos/asf/incubator-hawq/blob/3c2419bc/src/test/feature/lib/sql-util.h
----------------------------------------------------------------------
diff --git a/src/test/feature/lib/sql-util.h b/src/test/feature/lib/sql-util.h
index 8070d3f..e9455d7 100644
--- a/src/test/feature/lib/sql-util.h
+++ b/src/test/feature/lib/sql-util.h
@@ -26,7 +26,7 @@ class SQLUtility {
   // Execute sql command
   // @param sql The given sql command
   // @return void
-  void execute(const std::string &sql);
+  void execute(const std::string &sql, bool check = true);
 
   // Execute query command and check the rowCount
   // @param sql The given query command
@@ -50,12 +50,16 @@ class SQLUtility {
   // @return PSQL raw pointer
   PSQL *getPSQL() const;
 
+  // Get test root dir abs path
+  // @return path string
+  std::string getTestRootPath() const;
+
  private:
   std::unique_ptr<PSQL> getConnection();
   const std::string generateSQLFile(const std::string &sqlFile);
   const PSQLQueryResult &executeQuery(const std::string &sql);
-  std::string getTestRootPath() const;
   FilePath splitFilePath(const std::string &filePath) const;
+  void exec(const std::string &sql);
 
  private:
   std::string schemaName;

Reply via email to