Commit 246f6579 authored by Vadim Zeitlin's avatar Vadim Zeitlin

Convert the tests to use CATCH testing framework.

The tests have been converted with minimal changes (but this could be a bit
difficult to see without the "-w" diff option as there have been many changes
in indentation), basically we just use CATCH CHECK() (and REQUIRE(), in a few
places where it is appropriate) macro instead of assert() and the test driver
now just uses Catch::Session::run() instead of enumerating the tests to run
explicitly.

This has a lot of advantages such as:

- Provides information about the failed test and the values of variables in it.
- Allows to continue running the tests even if one of them fails.
- Allows to run just some of the tests with flexible selection mechanism.
parent b2060fbc
......@@ -38,7 +38,7 @@ else()
if (CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++98")
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++98")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++98 -Wno-variadic-macros")
endif()
endif()
......
......@@ -11,6 +11,7 @@
include_directories(
${SOCI_SOURCE_DIR}/include/private
${SOCI_SOURCE_DIR}/tests
${CMAKE_CURRENT_SOURCE_DIR})
file(GLOB SOCI_TESTS_COMMON common-tests.h)
......
This diff is collapsed.
This diff is collapsed.
......@@ -7,9 +7,14 @@
#include "soci/soci.h"
#include "soci/empty/soci-empty.h"
// Normally the tests would include common-tests.h here, but we can't run any
// of the tests registered there, so instead include CATCH header directly.
#define CATCH_CONFIG_RUNNER
#include <catch.hpp>
#include <iostream>
#include <string>
#include <cassert>
#include <cstdlib>
#include <ctime>
......@@ -52,77 +57,72 @@ namespace soci
};
}
void test1()
TEST_CASE("Dummy test", "[empty]")
{
{
session sql(backEnd, connectString);
session sql(backEnd, connectString);
sql << "Do what I want.";
sql << "Do what I want " << 123 << " times.";
sql << "Do what I want.";
sql << "Do what I want " << 123 << " times.";
std::string query = "some query";
sql << query;
std::string query = "some query";
sql << query;
int i = 7;
sql << "insert", use(i);
sql << "select", into(i);
int i = 7;
sql << "insert", use(i);
sql << "select", into(i);
#if defined (__LP64__) || ( __WORDSIZE == 64 )
long int li = 9;
sql << "insert", use(li);
sql << "select", into(li);
long int li = 9;
sql << "insert", use(li);
sql << "select", into(li);
#endif
long long ll = 11;
sql << "insert", use(ll);
sql << "select", into(ll);
long long ll = 11;
sql << "insert", use(ll);
sql << "select", into(ll);
indicator ind = i_ok;
sql << "insert", use(i, ind);
sql << "select", into(i, ind);
indicator ind = i_ok;
sql << "insert", use(i, ind);
sql << "select", into(i, ind);
std::vector<int> numbers(100);
sql << "insert", use(numbers);
sql << "select", into(numbers);
std::vector<int> numbers(100);
sql << "insert", use(numbers);
sql << "select", into(numbers);
std::vector<indicator> inds(100);
sql << "insert", use(numbers, inds);
sql << "select", into(numbers, inds);
{
statement st = (sql.prepare << "select", into(i));
st.execute();
st.fetch();
}
{
statement st = (sql.prepare << "select", into(i, ind));
}
{
statement st = (sql.prepare << "select", into(numbers));
}
{
statement st = (sql.prepare << "select", into(numbers, inds));
}
{
statement st = (sql.prepare << "insert", use(i));
}
{
statement st = (sql.prepare << "insert", use(i, ind));
}
{
statement st = (sql.prepare << "insert", use(numbers));
}
{
statement st = (sql.prepare << "insert", use(numbers, inds));
}
{
Person p;
sql << "select person", into(p);
}
std::vector<indicator> inds(100);
sql << "insert", use(numbers, inds);
sql << "select", into(numbers, inds);
{
statement st = (sql.prepare << "select", into(i));
st.execute();
st.fetch();
}
{
statement st = (sql.prepare << "select", into(i, ind));
}
{
statement st = (sql.prepare << "select", into(numbers));
}
{
statement st = (sql.prepare << "select", into(numbers, inds));
}
{
statement st = (sql.prepare << "insert", use(i));
}
{
statement st = (sql.prepare << "insert", use(i, ind));
}
{
statement st = (sql.prepare << "insert", use(numbers));
}
{
statement st = (sql.prepare << "insert", use(numbers, inds));
}
{
Person p;
sql << "select person", into(p);
}
std::cout << "test 1 passed" << std::endl;
}
......@@ -138,33 +138,25 @@ int main(int argc, char** argv)
_CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
#endif //_MSC_VER
if (argc == 2)
if (argc >= 2)
{
connectString = argv[1];
// Replace the connect string with the process name to ensure that
// CATCH uses the correct name in its messages.
argv[1] = argv[0];
argc--;
argv++;
}
else
{
std::cout << "usage: " << argv[0]
<< " connectstring\n"
<< " connectstring [test-arguments...]\n"
<< "example: " << argv[0]
<< " \'connect_string_for_empty_backend\'\n";
std::exit(1);
}
try
{
test1();
// test2();
// ...
std::cout << "\nOK, all tests passed.\n\n";
return EXIT_SUCCESS;
}
catch (std::exception const & e)
{
std::cout << e.what() << '\n';
}
return EXIT_FAILURE;
return Catch::Session().run(argc, argv);
}
This diff is collapsed.
This diff is collapsed.
......@@ -10,7 +10,6 @@
#include "common-tests.h"
#include <iostream>
#include <string>
#include <cassert>
#include <ctime>
#include <cmath>
......@@ -122,35 +121,23 @@ int main(int argc, char** argv)
_CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
#endif //_MSC_VER
if (argc == 2)
if (argc >= 2 && argv[1][0] != '-')
{
connectString = argv[1];
// Replace the connect string with the process name to ensure that
// CATCH uses the correct name in its messages.
argv[1] = argv[0];
argc--;
argv++;
}
else
{
connectString = "FILEDSN=./test-access.dsn";
}
try
{
std::cout << "\nSOCI ODBC with MS Access Tests:\n\n";
test_context tc(backEnd, connectString);
common_tests tests(tc);
tests.run();
test_context tc(backEnd, connectString);
std::cout << "\nOK, all tests passed.\n\n";
return EXIT_SUCCESS;
}
catch (soci::odbc_soci_error const & e)
{
std::cout << "ODBC Error Code: " << e.odbc_error_code() << std::endl
<< "Native Error Code: " << e.native_error_code() << std::endl
<< "SOCI Message: " << e.what() << std::endl
<< "ODBC Message: " << e.odbc_error_message() << std::endl;
}
catch (std::exception const & e)
{
std::cout << e.what() << '\n';
}
return EXIT_FAILURE;
return Catch::Session().run(argc, argv);
}
......@@ -10,7 +10,6 @@
#include "common-tests.h"
#include <iostream>
#include <string>
#include <cassert>
#include <ctime>
#include <cmath>
......@@ -105,7 +104,7 @@ struct table_creator_bigint : table_creator_base
}
};
void test_odbc_db2_long_long()
TEST_CASE("ODBC/DB2 long long", "[odbc][db2][longlong]")
{
const int num_recs = 100;
session sql(backEnd, connectString);
......@@ -129,14 +128,12 @@ void test_odbc_db2_long_long()
for (int i = 0; i < num_recs; i++)
{
st.fetch();
assert(n2 == 1000000000LL + i);
CHECK(n2 == 1000000000LL + i);
}
}
std::cout << "test odbc_db2_long_long passed" << std::endl;
}
void test_odbc_db2_unsigned_long_long()
TEST_CASE("ODBC/DB2 unsigned long long", "[odbc][db2][unsigned][longlong]")
{
const int num_recs = 100;
session sql(backEnd, connectString);
......@@ -160,14 +157,12 @@ void test_odbc_db2_unsigned_long_long()
for (int i = 0; i < num_recs; i++)
{
st.fetch();
assert(n2 == 1000000000LL + i);
CHECK(n2 == 1000000000LL + i);
}
}
std::cout << "test odbc_db2_unsigned_long_long passed" << std::endl;
}
void test_odbc_db2_long_long_vector()
TEST_CASE("ODBC/DB2 vector long long", "[odbc][db2][vector][longlong]")
{
const std::size_t num_recs = 100;
session sql(backEnd, connectString);
......@@ -199,18 +194,16 @@ void test_odbc_db2_long_long_vector()
const std::size_t vsize = v.size();
for (std::size_t i = 0; i < vsize; i++)
{
assert(v[i] == 1000000000LL +
CHECK(v[i] == 1000000000LL +
static_cast<long long>(recs));
recs++;
}
}
assert(recs == num_recs);
CHECK(recs == num_recs);
}
std::cout << "test odbc_db2_long_long_vector passed" << std::endl;
}
void test_odbc_db2_unsigned_long_long_vector()
TEST_CASE("ODBC/DB2 vector unsigned long long", "[odbc][db2][vector][unsigned][longlong]")
{
const std::size_t num_recs = 100;
session sql(backEnd, connectString);
......@@ -242,12 +235,12 @@ void test_odbc_db2_unsigned_long_long_vector()
const std::size_t vsize = v.size();
for (std::size_t i = 0; i < vsize; i++)
{
assert(v[i] == 1000000000LL +
CHECK(v[i] == 1000000000LL +
static_cast<unsigned long long>(recs));
recs++;
}
}
assert(recs == num_recs);
CHECK(recs == num_recs);
}
std::cout << "test odbc_db2_unsigned_long_long_vector passed" << std::endl;
......@@ -264,9 +257,16 @@ int main(int argc, char** argv)
_CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
#endif //_MSC_VER
if (argc == 2)
if (argc >= 2)
{
connectString = argv[1];
// Replace the connect string with the process name to ensure that
// CATCH uses the correct name in its messages.
argv[1] = argv[0];
argc--;
argv++;
}
else
{
......@@ -275,33 +275,8 @@ int main(int argc, char** argv)
std::endl << std::endl;
return EXIT_FAILURE;
}
try
{
std::cout << "\nSOCI ODBC with DB2 Tests:\n\n";
test_context tc(backEnd, connectString);
common_tests tests(tc);
tests.run();
test_context tc(backEnd, connectString);
std::cout << "\nSOCI DB2 Specific Tests:\n\n";
test_odbc_db2_long_long();
test_odbc_db2_unsigned_long_long();
test_odbc_db2_long_long_vector();
test_odbc_db2_unsigned_long_long_vector();
std::cout << "\nOK, all tests passed.\n\n";
return EXIT_SUCCESS;
}
catch (soci::odbc_soci_error const & e)
{
std::cout << "ODBC Error Code: " << e.odbc_error_code() << std::endl
<< "Native Error Code: " << e.native_error_code() << std::endl
<< "SOCI Message: " << e.what() << std::endl
<< "ODBC Message: " << e.odbc_error_message() << std::endl;
}
catch (std::exception const & e)
{
std::cout << e.what() << '\n';
}
return EXIT_FAILURE;
return Catch::Session().run(argc, argv);
}
......@@ -10,7 +10,6 @@
#include "common-tests.h"
#include <iostream>
#include <string>
#include <cassert>
#include <ctime>
#include <cmath>
......@@ -120,39 +119,23 @@ int main(int argc, char** argv)
_CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
#endif //_MSC_VER
if (argc == 2)
if (argc >= 2 && argv[1][0] != '-')
{
connectString = argv[1];
// Replace the connect string with the process name to ensure that
// CATCH uses the correct name in its messages.
argv[1] = argv[0];
argc--;
argv++;
}
else
{
connectString = "FILEDSN=./test-mssql.dsn";
}
try
{
std::cout << "\nSOCI ODBC with MS SQL Server Tests:\n\n";
test_context tc(backEnd, connectString);
common_tests tests(tc);
tests.run();
test_context tc(backEnd, connectString);
std::cout << "\nOK, all tests passed.\n\n";
return EXIT_SUCCESS;
}
catch (soci::odbc_soci_error const & e)
{
std::cout << "ODBC Error Code: " << e.odbc_error_code() << std::endl
<< "Native Error Code: " << e.native_error_code() << std::endl
<< "SOCI Message: " << e.what() << std::endl
<< "ODBC Message: " << e.odbc_error_message() << std::endl;
}
catch (soci::soci_error const & e)
{
std::cout << "SOCIERROR: " << e.what() << '\n';
}
catch (std::exception const & e)
{
std::cout << e.what() << '\n';
}
return EXIT_FAILURE;
return Catch::Session().run(argc, argv);
}
......@@ -10,7 +10,6 @@
#include "common-tests.h"
#include <iostream>
#include <string>
#include <cassert>
#include <ctime>
#include <cmath>
......@@ -111,35 +110,23 @@ int main(int argc, char** argv)
_CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
#endif //_MSC_VER
if (argc == 2)
if (argc >= 2 && argv[1][0] != '-')
{
connectString = argv[1];
// Replace the connect string with the process name to ensure that
// CATCH uses the correct name in its messages.
argv[1] = argv[0];
argc--;
argv++;
}
else
{
connectString = "FILEDSN=./test-mysql.dsn";
}
try
{
std::cout << "\nSOCI ODBC with MySQL Tests:\n\n";
test_context tc(backEnd, connectString);
common_tests tests(tc);
tests.run();
test_context tc(backEnd, connectString);
std::cout << "\nOK, all tests passed.\n\n";
return EXIT_SUCCESS;
}
catch (soci::odbc_soci_error const & e)
{
std::cout << "ODBC Error Code: " << e.odbc_error_code() << std::endl
<< "Native Error Code: " << e.native_error_code() << std::endl
<< "SOCI Message: " << e.what() << std::endl
<< "ODBC Message: " << e.odbc_error_message() << std::endl;
}
catch (std::exception const & e)
{
std::cout << e.what() << '\n';
}
return EXIT_FAILURE;
return Catch::Session().run(argc, argv);
}
......@@ -10,7 +10,6 @@
#include "common-tests.h"
#include <iostream>
#include <string>
#include <cassert>
#include <cstdio>
#include <ctime>
#include <cmath>
......@@ -228,35 +227,23 @@ int main(int argc, char** argv)
_CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
#endif //_MSC_VER
if (argc == 2)
if (argc >= 2 && argv[1][0] != '-')
{
connectString = argv[1];
// Replace the connect string with the process name to ensure that
// CATCH uses the correct name in its messages.
argv[1] = argv[0];
argc--;
argv++;
}
else
{
connectString = "FILEDSN=./test-postgresql.dsn";
}
try
{
std::cout << "\nSOCI ODBC with PostgreSQL Tests:\n\n";
test_context tc(backEnd, connectString);
common_tests tests(tc);
tests.run();
std::cout << "\nOK, all tests passed.\n\n";
return EXIT_SUCCESS;
}
catch (soci::odbc_soci_error const & e)
{
std::cout << "ODBC Error Code: " << e.odbc_error_code() << std::endl
<< "Native Error Code: " << e.native_error_code() << std::endl
<< "SOCI Message: " << e.what() << std::endl
<< "ODBC Message: " << e.odbc_error_message() << std::endl;
}
catch (std::exception const & e)
{
std::cout << e.what() << '\n';
}
return EXIT_FAILURE;
test_context tc(backEnd, connectString);
return Catch::Session().run(argc, argv);
}
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment