Improved 'helper_test_load_data_local_infile' by checking the written content

pull/3526/head
Javier Jaramago Fernández 5 years ago
parent 9529ac6ca2
commit 6bd6919d0f

@ -424,3 +424,25 @@ int exec(const std::string& cmd, std::string& result) {
}
return err;
}
std::vector<mysql_res_row> extract_mysql_rows(MYSQL_RES* my_res) {
if (my_res == nullptr) { return {}; }
std::vector<mysql_res_row> result {};
MYSQL_ROW row = nullptr;
uint32_t num_fields = mysql_num_fields(my_res);
while ((row = mysql_fetch_row(my_res))) {
mysql_res_row row_values {};
uint64_t *lengths = mysql_fetch_lengths(my_res);
for (uint32_t i = 0; i < num_fields; i++) {
std::string field_val(row[i], lengths[i]);
row_values.push_back(field_val);
}
result.push_back(row_values);
}
return result;
};

@ -66,4 +66,15 @@ int execvp(const std::string& file, const std::vector<const char*>& argv, std::s
*/
int exec(const std::string& cmd, std::string& result);
using mysql_res_row = std::vector<std::string>;
/**
* @brief Function that extracts the provided 'MYSQL_RES' into a vector of vector of
* strings.
* @param my_res The 'MYSQL_RES' for which to extract the values. In case of
* being NULL an empty vector is returned.
* @return The extracted values of all the rows present in the resultset.
*/
std::vector<mysql_res_row> extract_mysql_rows(MYSQL_RES* my_res);
#endif // #define UTILS_H

@ -15,9 +15,10 @@
#include <mysql.h>
#include <mysql/mysqld_error.h>
#include "tap.h"
#include "command_line.h"
#include "json.hpp"
#include "proxysql_utils.h"
#include "tap.h"
#include "utils.h"
/**
@ -218,6 +219,8 @@ const std::vector<std::string> prepare_table_queries {
" c1 INT NOT NULL AUTO_INCREMENT PRIMARY KEY, c2 VARCHAR(100), c3 VARCHAR(100))",
};
using mysql_res_row = std::vector<std::string>;
/**
* @brief Helper function that performs the actual check for 'test_load_data_local_infile'.
*
@ -267,11 +270,60 @@ void helper_test_load_data_local_infile(
);
}
ok(
load_data_res == EXIT_SUCCESS,
"Query '%s' should succeed. Error was: '%s'",
load_data_command.c_str(), mysql_error(proxysql)
);
if (load_data_res == EXIT_SUCCESS) {
diag(
"Supplied query '%s' succeeded, performing check on data...",
load_data_command.c_str()
);
} else {
diag(
"Supplied query '%s' failed, check not going to be performed. Error was: '%s'.",
load_data_command.c_str(), mysql_error(proxysql)
);
}
// Check that the data has actually been loaded to the database
int myerr = mysql_query(proxysql, "SELECT * FROM test.load_data_local");
if (myerr) {
diag(
"Query 'SELECT * FROM test.load_data_local' for table preparation failed"
" at line '%d', with error: '%s'", __LINE__, mysql_error(proxysql)
);
} else {
MYSQL_RES* result = mysql_store_result(proxysql);
std::vector<mysql_res_row> rows_res { extract_mysql_rows(result) };
std::vector<mysql_res_row> exp_rows {
{ "1","a string","100.20" },
{ "2","a string containing a , comma","102.20" },
{ "3","a string containing a \" quote","102.20" },
{ "4","a string containing a \", quote and comma","102.20" }
};
std::string exp_rows_str { "{\n" };
for (const auto& exp_row : exp_rows) {
std::string exp_row_str { nlohmann::json(exp_row).dump() };
exp_rows_str += " " + exp_row_str + ",\n";
}
exp_rows_str += "}\n";
diag("Expected values for rows were: \n%s", exp_rows_str.c_str());
std::string act_rows_str { "{\n" };
for (const auto& act_row : rows_res) {
std::string act_row_str { nlohmann::json(act_row).dump() };
act_rows_str += " " + act_row_str + ",\n";
}
act_rows_str += "}\n";
diag("Actual values for found rows were: \n%s", act_rows_str.c_str());
bool equal = false;
if (!rows_res.empty()) {
equal = std::equal(exp_rows.begin(), exp_rows.end(), rows_res.begin());
}
ok(equal, "The selected ROWS were equal to the expected ones");
}
} else {
if (load_data_res) {
diag(

Loading…
Cancel
Save