Browse Source

Test: test_pEpSQLite - Switch from pEpLog() to using TESTLOG()

Also, use the ASSERT_EXCEPT macro from Test::Utils
LIB-11
heck 4 years ago
parent
commit
b5bd084580
  1. 218
      test/test_pEpSQLite.cc

218
test/test_pEpSQLite.cc

@ -1,6 +1,5 @@
#include "test_pEpSQLite.hh"
#include "../src/pEpSQLite.hh"
#include "../src/pEpLog.hh"
#include "framework/utils.hh"
#include <fstream>
@ -8,7 +7,6 @@
using namespace std;
using namespace pEp;
using namespace pEp::Test;
using namespace pEp::Test::Log;
using namespace pEp::Test::Utils;
namespace pEp {
@ -17,28 +15,28 @@ namespace pEp {
// filenames
string fixture_db_filename_new()
{
// pEpLog("called");
// TESTLOG("called");
string path = "new.db";
return path;
}
string fixture_db_filename_bad()
{
// pEpLog("called");
// TESTLOG("called");
string db_path_bad = "/will_not_create_dirs/bad.db";
return db_path_bad;
}
string fixture_db_filename_existing_and_verified()
{
// pEpLog("called");
// TESTLOG("called");
string path = "new.db";
return path;
}
string fixture_db_filename_corrupt()
{
// pEpLog("called");
// TESTLOG("called");
string path = "corrupt.db";
return path;
}
@ -46,26 +44,23 @@ namespace pEp {
// prepared db's
string fixture_init_db_new()
{
// pEpLog("called");
// TESTLOG("called");
string path = fixture_db_filename_new();
// cout << "fixture: \"" << path << "\" not existing" << endl;
file_ensure_not_existing(path);
return path;
}
string fixture_init_db_existing_and_verified()
{
// pEpLog("called");
// TESTLOG("called");
string path = "existing.db";
// cout << "fixture: \"" << path << "\" not existing" << endl;
file_ensure_not_existing(path);
return path;
}
string fixture_init_db_corrupt()
{
// pEpLog("called");
// cout << "creating corrupt db" << endl;
// TESTLOG("called");
string path = fixture_db_filename_corrupt();
file_ensure_not_existing(path);
ofstream db_corrupt = file_create(path);
@ -77,89 +72,89 @@ namespace pEp {
// instance
pEpSQLite fixture_instance_of_new()
{
// pEpLog("called");
// TESTLOG("called");
return test_create_instance_on_new();
}
pEpSQLite fixture_instance_of_existing_and_verified()
{
// pEpLog("called");
// TESTLOG("called");
return test_create_instance_on_existing();
}
pEpSQLite fixture_instance_of_bad()
{
// pEpLog("called");
// TESTLOG("called");
return test_create_instance_on_path_bad();
}
pEpSQLite fixture_instance_of_corrupt()
{
// pEpLog("called");
// TESTLOG("called");
return test_create_instance_on_path_corrupt();
}
// open
pEpSQLite fixture_db_open_of_new()
{
// pEpLog("called");
// TESTLOG("called");
return test_createopen_db_new();
}
pEpSQLite fixture_db_open_of_existing_and_verified()
{
// pEpLog("called");
// TESTLOG("called");
return test_db_verify_content_after_insert_on_tables_exist();
}
pEpSQLite fixture_db_open_of_bad()
{
// pEpLog("called");
// TESTLOG("called");
return test_createopen_db_bad();
}
pEpSQLite fixture_db_open_of_corrupt()
{
// pEpLog("called");
// TESTLOG("called");
return test_createopen_db_corrupt();
}
pEpSQLite fixture_db_open_after_close()
{
// pEpLog("called");
// TESTLOG("called");
return test_close_after_open();
}
// tables
pEpSQLite fixture_db_open_with_tables_of_new()
{
// pEpLog("called");
// TESTLOG("called");
return test_db_create_tables_on_open_new();
}
pEpSQLite fixture_db_open_with_tables_of_corrupt()
{
// pEpLog("called");
// TESTLOG("called");
return test_db_create_tables_on_open_corrupt();
}
// content
pEpSQLite fixture_db_open_with_tables_and_content()
{
// pEpLog("called");
// TESTLOG("called");
return test_db_insert_on_tables_exist();
}
// delete
pEpSQLite fixture_db_open_after_delete()
{
// pEpLog("called");
// TESTLOG("called");
return test_delete_file_gone_after_open_existing();
}
pEpSQLite fixture_db_open_after_close_after_delete()
{
// pEpLog("called");
// TESTLOG("called");
return test_delete_file_gone_after_close_new();
}
@ -169,7 +164,7 @@ namespace pEp {
// OK
pEpSQLite test_create_instance_on_new()
{
pEpLog("called");
TESTLOG("called");
pEpSQLite db(fixture_init_db_new());
return db;
}
@ -177,7 +172,7 @@ namespace pEp {
// OK
pEpSQLite test_create_instance_on_existing()
{
pEpLog("called");
TESTLOG("called");
pEpSQLite db(fixture_init_db_existing_and_verified());
return db;
}
@ -185,7 +180,7 @@ namespace pEp {
// OK
pEpSQLite test_create_instance_on_path_bad()
{
pEpLog("called");
TESTLOG("called");
pEpSQLite db(fixture_db_filename_bad());
return db;
}
@ -193,7 +188,7 @@ namespace pEp {
// OK
pEpSQLite test_create_instance_on_path_corrupt()
{
pEpLog("called");
TESTLOG("called");
pEpSQLite db(fixture_init_db_corrupt());
return db;
}
@ -202,7 +197,7 @@ namespace pEp {
// OK, new db
pEpSQLite test_createopen_db_new()
{
pEpLog("called");
TESTLOG("called");
pEpSQLite db = fixture_instance_of_new();
assert(!file_exists(fixture_db_filename_new()));
db.create_or_open_db();
@ -213,7 +208,7 @@ namespace pEp {
// OK, open db
pEpSQLite test_createopen_db_existing()
{
pEpLog("called");
TESTLOG("called");
pEpSQLite db = fixture_instance_of_existing_and_verified();
assert(file_exists(fixture_db_filename_existing_and_verified()));
db.create_or_open_db();
@ -223,16 +218,11 @@ namespace pEp {
// ERR, cant create
pEpSQLite test_createopen_db_bad()
{
pEpLog("called");
TESTLOG("called");
assert(!file_exists(fixture_db_filename_bad()));
pEpSQLite db = fixture_instance_of_bad();
assert(!file_exists(fixture_db_filename_bad()));
try {
db.create_or_open_db();
assert(false);
} catch (const exception& e) {
pEp::Test::Utils::print_exception(e);
}
ASSERT_EXCEPT(db.create_or_open_db());
assert(!file_exists(fixture_db_filename_bad()));
return db;
}
@ -240,7 +230,7 @@ namespace pEp {
// OK(cant detect corruption)
pEpSQLite test_createopen_db_corrupt()
{
pEpLog("called");
TESTLOG("called");
pEpSQLite db = fixture_instance_of_corrupt();
assert(file_exists(fixture_db_filename_corrupt()));
db.create_or_open_db();
@ -252,7 +242,7 @@ namespace pEp {
// OK
pEpSQLite test_close_before_open()
{
pEpLog("called");
TESTLOG("called");
pEpSQLite db = fixture_instance_of_new();
db.close_db();
return db;
@ -261,7 +251,7 @@ namespace pEp {
// OK
pEpSQLite test_close_after_open()
{
pEpLog("called");
TESTLOG("called");
pEpSQLite db = fixture_db_open_of_new();
db.close_db();
return db;
@ -270,7 +260,7 @@ namespace pEp {
// OK
pEpSQLite test_close_idempotent()
{
pEpLog("called");
TESTLOG("called");
pEpSQLite db = fixture_db_open_of_new();
db.close_db();
db.close_db();
@ -283,7 +273,7 @@ namespace pEp {
// OK
pEpSQLite test_db_create_tables_on_open_new()
{
pEpLog("called");
TESTLOG("called");
pEpSQLite db = fixture_db_open_of_new();
db.execute("CREATE TABLE test(i,i_squared);");
return db;
@ -292,42 +282,28 @@ namespace pEp {
// ERR, Tables already exist
pEpSQLite test_db_create_tables_open_existing()
{
pEpLog("called");
TESTLOG("called");
pEpSQLite db = fixture_db_open_of_existing_and_verified();
try {
db.execute("CREATE TABLE test(i,i_squared);");
assert(false);
} catch (const exception& e) {
pEp::Test::Utils::print_exception(e);
}
ASSERT_EXCEPT(db.execute("CREATE TABLE test(i,i_squared);"));
return db;
}
// ERR, db closed
pEpSQLite test_db_create_tables_on_open_bad()
{
pEpLog("called");
TESTLOG("called");
pEpSQLite db = fixture_db_open_of_bad();
try {
db.execute("CREATE TABLE test(i,i_squared);");
assert(false);
} catch (const exception& e) {
pEp::Test::Utils::print_exception(e);
}
ASSERT_EXCEPT(db.execute("CREATE TABLE test(i,i_squared);"));
return db;
}
// ERR, db corrupt
pEpSQLite test_db_create_tables_on_open_corrupt()
{
pEpLog("called");
TESTLOG("called");
pEpSQLite db = fixture_db_open_of_corrupt();
try {
db.execute("CREATE TABLE test(i,i_squared);");
assert(false);
} catch (const exception& e) {
pEp::Test::Utils::print_exception(e);
}
ASSERT_EXCEPT(db.execute("CREATE TABLE test(i,i_squared);"));
return db;
}
@ -335,7 +311,7 @@ namespace pEp {
// insert (execute())
void insert_operation(pEpSQLite& db)
{
pEpLog("called");
TESTLOG("called");
for (int i = 0; i < 9; i = (i + 2)) {
db.execute(
"INSERT INTO test(i,i_squared) VALUES ('" + to_string(i) + "', '" +
@ -346,7 +322,7 @@ namespace pEp {
// OK
pEpSQLite test_db_insert_on_tables_exist()
{
pEpLog("called");
TESTLOG("called");
pEpSQLite db = fixture_db_open_with_tables_of_new();
insert_operation(db);
return db;
@ -356,67 +332,51 @@ namespace pEp {
pEpSQLite test_db_insert_on_tables_dont_exist()
{
pEpSQLite db = fixture_db_open_of_new();
try {
insert_operation(db);
assert(false);
} catch (const exception& e) {
pEp::Test::Utils::print_exception(e);
}
ASSERT_EXCEPT(insert_operation(db));
return db;
}
// ERR, Tables missing
pEpSQLite test_db_insert_before_open()
{
pEpLog("called");
TESTLOG("called");
pEpSQLite db = fixture_instance_of_new();
try {
insert_operation(db);
assert(false);
} catch (const exception& e) {
pEp::Test::Utils::print_exception(e);
}
ASSERT_EXCEPT(insert_operation(db));
return db;
}
// ERR, Tables missing
pEpSQLite test_db_insert_after_close()
{
pEpLog("called");
TESTLOG("called");
pEpSQLite db = fixture_db_open_after_close();
try {
insert_operation(db);
assert(false);
} catch (const exception& e) {
pEp::Test::Utils::print_exception(e);
}
ASSERT_EXCEPT(insert_operation(db));
return db;
}
// verify contents (execute())
void verify_operation(pEpSQLite& db)
{
pEpLog("called");
TESTLOG("called");
for (int i = 0; i < 9; i++) {
ResultSet rs = db.execute(
"SELECT i, i_squared FROM test "
"WHERE (test.i == '" +
to_string(i) + "');");
// cout << "RESULT: " << endl << pEpSQLite::to_string(rs) << endl;
if (i % 2) {
if (!rs.empty()) {
runtime_error e{ "Exception verifying database content" };
throw(e);
}
} else {
if(rs.size() != 1) {
if (rs.size() != 1) {
runtime_error e{ "Exception verifying database content" };
throw(e);
}
for (const RSRecord& r : rs) {
const int x = stoi(r.at("i"));
const int x_squared = stoi(r.at("i_squared"));
if((x * x) != x_squared) {
if ((x * x) != x_squared) {
runtime_error e{ "Exception verifying database content" };
throw(e);
}
@ -428,7 +388,7 @@ namespace pEp {
// OK
pEpSQLite test_db_verify_content_existing_open_db()
{
pEpLog("called");
TESTLOG("called");
pEpSQLite db = fixture_db_open_of_existing_and_verified();
verify_operation(db);
return db;
@ -437,7 +397,7 @@ namespace pEp {
// OK
pEpSQLite test_db_verify_content_after_insert_on_tables_exist()
{
pEpLog("called");
TESTLOG("called");
pEpSQLite db = fixture_db_open_with_tables_and_content();
verify_operation(db);
return db;
@ -446,28 +406,19 @@ namespace pEp {
// ERR - no tables
pEpSQLite test_db_verify_content_no_tables()
{
pEpLog("called");
TESTLOG("called");
pEpSQLite db = fixture_db_open_of_new();
try {
verify_operation(db);
assert(false);
} catch (const exception& e) {
pEp::Test::Utils::print_exception(e);
}
ASSERT_EXCEPT(verify_operation(db));
return db;
}
// ERR - err no data
pEpSQLite test_db_verify_content_after_create_tables()
{
pEpLog("called");
TESTLOG("called");
pEpSQLite db = fixture_db_open_with_tables_of_new();
try {
verify_operation(db);
assert(false);
} catch (const exception& e) {
pEp::Test::Utils::print_exception(e);
}
ASSERT_EXCEPT(verify_operation(db));
return db;
}
@ -476,7 +427,7 @@ namespace pEp {
// OK
pEpSQLite test_get_path_on_instance_good()
{
pEpLog("called");
TESTLOG("called");
pEpSQLite db = fixture_instance_of_new();
assert(db.get_db_path() == fixture_db_filename_new());
return db;
@ -485,7 +436,7 @@ namespace pEp {
// OK
pEpSQLite test_get_path_on_instance_bad()
{
pEpLog("called");
TESTLOG("called");
pEpSQLite db = fixture_instance_of_bad();
assert(db.get_db_path() == fixture_db_filename_bad());
return db;
@ -495,14 +446,9 @@ namespace pEp {
// ERR, file not found
pEpSQLite test_delete_file_gone_before_open_new()
{
pEpLog("called");
TESTLOG("called");
pEpSQLite db = fixture_instance_of_new();
try {
db.delete_db();
assert(false);
} catch (const exception& e) {
pEp::Test::Utils::print_exception(e);
}
ASSERT_EXCEPT(db.delete_db());
assert(!file_exists(fixture_db_filename_new()));
return db;
}
@ -510,14 +456,9 @@ namespace pEp {
// ERR, file not found
pEpSQLite test_delete_file_gone_before_open_existing()
{
pEpLog("called");
TESTLOG("called");
pEpSQLite db = fixture_instance_of_existing_and_verified();
try {
db.delete_db();
assert(false);
} catch (const exception& e) {
pEp::Test::Utils::print_exception(e);
}
ASSERT_EXCEPT(db.delete_db());
assert(!file_exists(fixture_db_filename_existing_and_verified()));
return db;
}
@ -525,7 +466,7 @@ namespace pEp {
// OK
pEpSQLite test_delete_file_gone_after_close_new()
{
pEpLog("called");
TESTLOG("called");
pEpSQLite db = fixture_db_open_after_close();
db.delete_db();
assert(!file_exists(fixture_db_filename_new()));
@ -535,7 +476,7 @@ namespace pEp {
// OK
pEpSQLite test_delete_file_gone_after_open_existing()
{
pEpLog("called");
TESTLOG("called");
pEpSQLite db = fixture_db_open_of_existing_and_verified();
db.delete_db();
assert(!file_exists(fixture_db_filename_existing_and_verified()));
@ -545,7 +486,7 @@ namespace pEp {
// OK
pEpSQLite test_delete_file_gone_after_open_corrupt()
{
pEpLog("called");
TESTLOG("called");
pEpSQLite db = fixture_db_open_of_corrupt();
db.delete_db();
assert(!file_exists(fixture_db_filename_corrupt()));
@ -555,14 +496,9 @@ namespace pEp {
// ERR
pEpSQLite test_delete_file_gone_after_open_bad()
{
pEpLog("called");
TESTLOG("called");
pEpSQLite db = fixture_db_open_of_bad();
try {
db.delete_db();
assert(false);
} catch (const exception& e) {
pEp::Test::Utils::print_exception(e);
}
ASSERT_EXCEPT(db.delete_db());
assert(!file_exists(fixture_db_filename_bad()));
return db;
}
@ -571,7 +507,7 @@ namespace pEp {
// false
pEpSQLite test_is_open_before_open_new()
{
pEpLog("called");
TESTLOG("called");
pEpSQLite db = fixture_instance_of_new();
assert(!db.is_open());
return db;
@ -580,7 +516,7 @@ namespace pEp {
// true
pEpSQLite test_is_open_after_open_new()
{
pEpLog("called");
TESTLOG("called");
pEpSQLite db = fixture_db_open_of_new();
assert(db.is_open());
return db;
@ -589,7 +525,7 @@ namespace pEp {
// true
pEpSQLite test_is_open_after_open_existing()
{
pEpLog("called");
TESTLOG("called");
pEpSQLite db = fixture_db_open_of_existing_and_verified();
assert(db.is_open());
return db;
@ -598,7 +534,7 @@ namespace pEp {
// false
pEpSQLite test_is_open_after_open_bad()
{
pEpLog("called");
TESTLOG("called");
pEpSQLite db = fixture_db_open_of_bad();
assert(!db.is_open());
return db;
@ -607,7 +543,7 @@ namespace pEp {
// true (cant detect corruption)
pEpSQLite test_is_open_after_open_corrupt()
{
pEpLog("called");
TESTLOG("called");
pEpSQLite db = fixture_db_open_of_corrupt();
assert(db.is_open());
return db;
@ -616,7 +552,7 @@ namespace pEp {
// false
pEpSQLite test_is_open_after_close()
{
pEpLog("called");
TESTLOG("called");
pEpSQLite db = fixture_db_open_after_close();
assert(!db.is_open());
return db;
@ -625,7 +561,7 @@ namespace pEp {
// false
pEpSQLite test_is_open_after_delete_on_open()
{
pEpLog("called");
TESTLOG("called");
pEpSQLite db = fixture_db_open_after_delete();
assert(!db.is_open());
return db;
@ -634,7 +570,7 @@ namespace pEp {
// false
pEpSQLite test_is_open_after_delete_on_closed()
{
pEpLog("called");
TESTLOG("called");
pEpSQLite db = fixture_db_open_after_close_after_delete();
assert(!db.is_open());
return db;

Loading…
Cancel
Save