Skip to content
Permalink
Browse files
HAWQ-740. Fix bug in SQLUtility.
  • Loading branch information
ztao1987 committed May 19, 2016
1 parent 025eecb commit bb9d2f1786c56acb78dee837afa8c85cc6e6f6db
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 19 deletions.
@@ -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));
@@ -109,7 +119,7 @@ const std::string SQLUtility::generateSQLFile(const std::string &sqlFile) {
<< "-- end_ignore" << std::endl;
std::string line;
while (getline(in, line)) {
out << line;
out << line << std::endl;
}
in.close();
out.close();
@@ -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 {
@@ -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;

0 comments on commit bb9d2f1

Please sign in to comment.