Date: Friday, February 3, 2006 @ 14:32:16
Author: zsolt
Path: /cvsroot/carob/libmysequoia/test
Modified: TestMySQLAPI.cpp (1.29 -> 1.30) TestMySQLAPI.hpp (1.10 -> 1.11)
implemented mysql_field_test(), mysql_options_test(), mysql_multi_query_test(),
mysql_escape_string_test() test cases and extended existing ones for better
code coverage.
------------------+
TestMySQLAPI.cpp | 168 +++++++++++++++++++++++++++++++++++++++++++++++++++--
TestMySQLAPI.hpp | 8 ++
2 files changed, 172 insertions(+), 4 deletions(-)
Index: libmysequoia/test/TestMySQLAPI.cpp
diff -u libmysequoia/test/TestMySQLAPI.cpp:1.29
libmysequoia/test/TestMySQLAPI.cpp:1.30
--- libmysequoia/test/TestMySQLAPI.cpp:1.29 Wed Feb 1 13:45:52 2006
+++ libmysequoia/test/TestMySQLAPI.cpp Fri Feb 3 14:32:16 2006
@@ -158,6 +158,9 @@
// select a nonexistent DB
CPPUNIT_ASSERT(mysql_select_db(mysql, BAD_DB) != 0);
+ CPPUNIT_ASSERT(mysql_errno(mysql) != 0);
+ CPPUNIT_ASSERT(mysql_error(mysql) != 0);
+ CPPUNIT_ASSERT(mysql_sqlstate(mysql) != 0);
}
void TestMySQLAPI::mysql_change_user_test(void)
@@ -221,6 +224,8 @@
// send the query - return 0 on success
CPPUNIT_ASSERT(mysql_real_query(mysql, query, strlen(query)) == 0);
+
+ CPPUNIT_ASSERT(mysql_affected_rows(mysql) == 3);
}
void TestMySQLAPI::mysql_real_query_3_test(void)
@@ -234,6 +239,61 @@
CPPUNIT_ASSERT(mysql_real_query(mysql, query, strlen(query)) == 0);
}
+void TestMySQLAPI::mysql_field_test(void)
+{
+ char *query = "select a,b,c,d,e,f,g,h from t1 where a <= 1";
+ enum enum_field_types fields_type[] = {MYSQL_TYPE_LONG, MYSQL_TYPE_LONG,
MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_DATE, MYSQL_TYPE_TIME,
+ MYSQL_TYPE_TIMESTAMP, MYSQL_TYPE_FLOAT, MYSQL_TYPE_DOUBLE};
+ MYSQL_RES *res = 0;
+ int fieldno = 0;
+ MYSQL_FIELD *fld;
+ MYSQL_FIELD_OFFSET first_field;
+
+ // connect to the database
+ CPPUNIT_ASSERT(mysql_real_connect(mysql, HOST, USER1, PASSWD1, DB1, 0, 0, 0)
!= 0);
+
+ // send the query - return 0 on success
+ CPPUNIT_ASSERT(mysql_real_query(mysql, query, strlen(query)) == 0);
+
+ // test the result field count
+ CPPUNIT_ASSERT(mysql_field_count(mysql) == 8);
+
+ CPPUNIT_ASSERT((res = mysql_store_result(mysql)) != NULL);
+
+ CPPUNIT_ASSERT(mysql_field_count(mysql) == mysql_num_fields(res));
+
+ first_field = mysql_field_tell(res);
+
+ // fetch each field
+ while ((fld = mysql_fetch_field(res)))
+ {
+ CPPUNIT_ASSERT(fieldno < 8);
+ CPPUNIT_ASSERT(*fld->name == 'a'+fieldno);
+ CPPUNIT_ASSERT(strcmp(fld->table, "t1") == 0);
+ CPPUNIT_ASSERT(fld->name_length == 1);
+ CPPUNIT_ASSERT(fld->table_length == 2);
+ CPPUNIT_ASSERT(fld->type == fields_type[fieldno]);
+ ++fieldno;
+ }
+
+ mysql_field_seek(res, first_field);
+ CPPUNIT_ASSERT(first_field == mysql_field_tell(res));
+
+ // fetch the first field again to ensure is really the first
+ CPPUNIT_ASSERT(fld = mysql_fetch_field(res));
+ CPPUNIT_ASSERT(*fld->name == 'a');
+ CPPUNIT_ASSERT(fld == mysql_fetch_fields(res));
+
+ // fetch the third field directly
+ CPPUNIT_ASSERT(fld = mysql_fetch_field_direct(res,2));
+ CPPUNIT_ASSERT(*fld->name == 'c');
+
+ CPPUNIT_ASSERT(mysql_errno(mysql) == 0);
+
+ // free the result set
+ mysql_free_result(res);
+}
+
void TestMySQLAPI::mysql_store_free_result_test(void)
{
char *query = "select * from t1";
@@ -275,9 +335,12 @@
void TestMySQLAPI::mysql_fetch_row_store_test(void)
{
char *query = "select * from t1";
+ unsigned long *lengths;
+ unsigned field_count;
MYSQL_RES *res = 0;
MYSQL_ROW row = 0;
- int rows_fetched = 0;
+ MYSQL_ROW_OFFSET first_row;
+ unsigned rows_fetched = 0;
// connect to the database
CPPUNIT_ASSERT(mysql_real_connect(mysql, HOST, USER1, PASSWD1, DB1, 0, 0, 0)
!= 0);
@@ -287,15 +350,40 @@
// fetch the result set
CPPUNIT_ASSERT((res = mysql_store_result(mysql)) != NULL);
+
+ CPPUNIT_ASSERT(field_count = mysql_num_fields(res));
+ first_row = mysql_row_tell(res);
+
while ((row = mysql_fetch_row(res)) != 0)
{
+ CPPUNIT_ASSERT(lengths = mysql_fetch_lengths(res));
+ for (unsigned i=0; i < field_count; i++)
+ if (row[i])
+ CPPUNIT_ASSERT(strlen(row[i]) == lengths[i]);
+ else
+ CPPUNIT_ASSERT(lengths[i] == 0);
+
rows_fetched++;
}
// check how many rows we fetched
CPPUNIT_ASSERT(rows_fetched == 3);
+ CPPUNIT_ASSERT(mysql_num_rows(res) == rows_fetched);
+ CPPUNIT_ASSERT(mysql_eof(res));
+ // try to seek to the first row bookmark, and ensure it is the right row
+ mysql_row_seek(res, first_row);
+ CPPUNIT_ASSERT(first_row == mysql_row_tell(res));
+ CPPUNIT_ASSERT(row = mysql_fetch_row(res));
+ CPPUNIT_ASSERT(strcmp(*row, "1") == 0);
+ CPPUNIT_ASSERT(!mysql_eof(res));
+
+ // try to seek to the third row directly, and ensure it is the right row
+ mysql_data_seek(res,2);
+ CPPUNIT_ASSERT(row = mysql_fetch_row(res));
+ CPPUNIT_ASSERT(strcmp(*row, "3") == 0);
+
// free the result set
mysql_free_result(res);
}
@@ -328,6 +416,77 @@
mysql_free_result(res);
}
+void TestMySQLAPI::mysql_options_test(void)
+{
+ MYSQL_RES *res;
+ MYSQL_ROW row;
+ unsigned int timeout = 10;
+
+ CPPUNIT_ASSERT(mysql_options(mysql, MYSQL_INIT_COMMAND, "delete from t1") ==
0);
+ CPPUNIT_ASSERT(mysql_options(mysql, MYSQL_SET_CHARSET_NAME, "latin2") == 0);
+ CPPUNIT_ASSERT(strcmp(mysql_character_set_name(mysql), "latin2") == 0);
+ CPPUNIT_ASSERT(mysql_options(mysql, MYSQL_SET_CHARSET_NAME, "latin1") == 0);
+ CPPUNIT_ASSERT(mysql_options(mysql, MYSQL_READ_DEFAULT_FILE, "~/.my.cnf") ==
0);
+ CPPUNIT_ASSERT(mysql_options(mysql, MYSQL_READ_DEFAULT_GROUP, "client") ==
0);
+ CPPUNIT_ASSERT(mysql_options(mysql, MYSQL_OPT_READ_TIMEOUT, (char
*)&timeout) == 0);
+
+ //we are trying to insert the rows again in the table, because the delete
from t1 will erase at connect
+ mysql_real_query_2_test();
+
+ //ensure that only 3 rows are remaining in the table
+ CPPUNIT_ASSERT(mysql_query(mysql, "select count(*) from t1") == 0);
+ CPPUNIT_ASSERT(res = mysql_store_result(mysql));
+ CPPUNIT_ASSERT(row = mysql_fetch_row(res));
+ CPPUNIT_ASSERT(*row && (strcmp(*row, "3") == 0));
+ mysql_free_result(res);
+}
+
+void TestMySQLAPI::mysql_multi_query_test(void)
+{
+ MYSQL_RES *res;
+
+ // connect to the database and execute the queries
+ CPPUNIT_ASSERT(mysql_real_connect(mysql, HOST, USER1, PASSWD1, DB1, 0, 0, 0)
!= 0);
+ CPPUNIT_ASSERT(mysql_query(mysql, "select * from t1 limit 1; select * from
t1 limit 2; select * from t1 limit 3") == 0);
+
+ // fetch the first result set
+ CPPUNIT_ASSERT(res = mysql_store_result(mysql));
+ CPPUNIT_ASSERT(mysql_num_rows(res) == 1);
+ mysql_free_result(res);
+
+ // ensure there are more results
+ CPPUNIT_ASSERT(mysql_more_results(mysql));
+ CPPUNIT_ASSERT(mysql_next_result(mysql) == 0);
+
+ // fetch the second result set
+ CPPUNIT_ASSERT(res = mysql_store_result(mysql));
+ CPPUNIT_ASSERT(mysql_num_rows(res) == 2);
+ mysql_free_result(res);
+
+ // ensure there are more results
+ CPPUNIT_ASSERT(mysql_more_results(mysql));
+ CPPUNIT_ASSERT(mysql_next_result(mysql) == 0);
+
+ // fetch the last result set
+ CPPUNIT_ASSERT(res = mysql_store_result(mysql));
+ CPPUNIT_ASSERT(mysql_num_rows(res) == 3);
+ mysql_free_result(res);
+
+ // ensure there are no more results
+ CPPUNIT_ASSERT(!mysql_more_results(mysql));
+ CPPUNIT_ASSERT(mysql_next_result(mysql) == -1);
+}
+
+void TestMySQLAPI::mysql_escape_string_test(void)
+{
+ char *from = "select '\\test' from t1";
+ char *to = "select \\'\\\\test\\' from t1";
+ char result[100];
+
+ CPPUNIT_ASSERT(mysql_escape_string(result, from, strlen(from)) ==
strlen(to));
+ CPPUNIT_ASSERT(strcmp(to, result) == 0);
+}
+
void TestMySQLAPI::mysql_stmt_init_close_test(void)
{
MYSQL_STMT *stmt;
@@ -425,7 +584,7 @@
MYSQL_BIND inbind[1];
MYSQL_BIND outbind[8];
int int_data;
- char *query = "select a,b,c,d,e,f,g,h from t1 where a <= ?";
+ char *query = "select a,b,c,d,e,f,g,h from t1 where b <= ?";
CPPUNIT_ASSERT(mysql_real_connect(mysql, HOST, USER1, PASSWD1, DB1, 0, 0, 0)
!= 0);
CPPUNIT_ASSERT((stmt = mysql_stmt_init(mysql)) != 0);
@@ -493,7 +652,8 @@
CPPUNIT_ASSERT(datetime.second == time.second);
CPPUNIT_ASSERT(fabsf(float_data - count * 1.1) < 0.000001);
CPPUNIT_ASSERT(fabs(double_data - count * 1.1) < 0.000000000000001);
- CPPUNIT_ASSERT(count++ == (unsigned int)int_res);
+ CPPUNIT_ASSERT(int_res - 3 == int_res2);
+ CPPUNIT_ASSERT(count++ == (unsigned int)int_res2);
}
CPPUNIT_ASSERT(mysql_stmt_num_rows(stmt) == 3);
@@ -508,7 +668,7 @@
MYSQL_BIND inbind[1];
MYSQL_BIND outbind[1];
int int_data;
- char *query = "select a from t1 where a <= ?";
+ char *query = "select b from t1 where b <= ?";
CPPUNIT_ASSERT(mysql_real_connect(mysql, HOST, USER1, PASSWD1, DB1, 0, 0, 0)
!= 0);
CPPUNIT_ASSERT((stmt = mysql_stmt_init(mysql)) != 0);
Index: libmysequoia/test/TestMySQLAPI.hpp
diff -u libmysequoia/test/TestMySQLAPI.hpp:1.10
libmysequoia/test/TestMySQLAPI.hpp:1.11
--- libmysequoia/test/TestMySQLAPI.hpp:1.10 Tue Jan 17 09:38:38 2006
+++ libmysequoia/test/TestMySQLAPI.hpp Fri Feb 3 14:32:16 2006
@@ -44,10 +44,14 @@
CPPUNIT_TEST (mysql_real_query_1_test);
CPPUNIT_TEST (mysql_real_query_2_test);
CPPUNIT_TEST (mysql_real_query_3_test);
+ CPPUNIT_TEST (mysql_field_test);
CPPUNIT_TEST (mysql_store_free_result_test);
CPPUNIT_TEST (mysql_use_free_result_test);
CPPUNIT_TEST (mysql_fetch_row_store_test);
CPPUNIT_TEST (mysql_fetch_row_use_test);
+ CPPUNIT_TEST (mysql_options_test);
+ CPPUNIT_TEST (mysql_multi_query_test);
+ CPPUNIT_TEST (mysql_escape_string_test);
CPPUNIT_TEST (mysql_stmt_init_close_test);
CPPUNIT_TEST (mysql_stmt_attr_get_set_test);
CPPUNIT_TEST (mysql_stmt_prepare_bind_exec_test);
@@ -75,10 +79,14 @@
void mysql_real_query_1_test(void);
void mysql_real_query_2_test(void);
void mysql_real_query_3_test(void);
+ void mysql_field_test(void);
void mysql_store_free_result_test(void);
void mysql_use_free_result_test(void);
void mysql_fetch_row_store_test(void);
void mysql_fetch_row_use_test(void);
+ void mysql_options_test(void);
+ void mysql_multi_query_test(void);
+ void mysql_escape_string_test(void);
void mysql_stmt_init_close_test(void);
void mysql_stmt_attr_get_set_test(void);
void mysql_stmt_prepare_bind_exec_test(void);
_______________________________________________
Carob-commits mailing list
[email protected]
https://forge.continuent.org/mailman/listinfo/carob-commits