Skip to content

Commit

Permalink
refactor: use global variables for database tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ni-jessica committed Dec 4, 2023
1 parent eb3ceb2 commit 4ddcf1c
Showing 1 changed file with 25 additions and 39 deletions.
64 changes: 25 additions & 39 deletions backend/test/database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@
#include <fstream>
#include "database.hpp"

std::string path = "test.db";
std::string failpath = "random.db";
std::ofstream file(path);

TEST_CASE("Test Database class")
{
std::string path = "test.db";
std::ofstream file(path);
REQUIRE(file.is_open());

SECTION("Execute SELECT query and then open a new database with the same file, database should be the same")
SECTION("Create, insert, and select queries")
{
// open a database
// open database
database::Database db = database::Database(path);
REQUIRE_NOTHROW(db.execute("CREATE TABLE names (name TEXT);"));

Expand All @@ -30,34 +32,13 @@ TEST_CASE("Test Database class")
std::vector<std::string> result = db.execute("SELECT * FROM names;", callback);
CHECK(result == names);

// reopen the same database, build is false so the contents should be the same
database::Database reopen_db = database::Database(path);

// check if the names table is still there
std::function<bool(sqlite3_stmt *)> callback_names_table = [](sqlite3_stmt *stmt)
{
int count = atoi(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)));
return count > 0;
};
std::vector<bool> result_names_table = reopen_db.execute("SELECT COUNT(*) FROM sqlite_schema WHERE name = 'names';", callback_names_table);
CHECK(result_names_table.front() == true);

// check if the specific names are still there
std::function<std::string(sqlite3_stmt *)> callback_each_name = [](sqlite3_stmt *stmt)
{
return std::string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)));
};
std::vector<std::string> result_each_name = reopen_db.execute("SELECT * FROM names;", callback_each_name);
CHECK(result_each_name == names);

db.close();
reopen_db.close();
}

SECTION("Reopen the database"){
// reopen the same database, build is false so the contents should be the same
SECTION("Reopening a database has persistent storage")
{
// reopen database
database::Database reopen_db = database::Database(path);

std::vector<std::string> names = {"Cedric", "Stella", "Jessica"};

// check if the names table is still there
Expand All @@ -69,7 +50,7 @@ TEST_CASE("Test Database class")
std::vector<bool> result_names_table = reopen_db.execute("SELECT COUNT(*) FROM sqlite_schema WHERE name = 'names';", callback_names_table);
CHECK(result_names_table.front() == true);

// check if the specific names are still there
// check if the previously inserted names are still there
std::function<std::string(sqlite3_stmt *)> callback_each_name = [](sqlite3_stmt *stmt)
{
return std::string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)));
Expand All @@ -80,37 +61,42 @@ TEST_CASE("Test Database class")
reopen_db.close();
}

SECTION("Rebuild the database of names, database should be empty")
SECTION("Rebuild existing database")
{
// setting build to true for an existing database will rebuild the database
database::Database db = database::Database(path, true);

// callback function to capture the counts of executing the query
std::function<bool(sqlite3_stmt *)> callback = [](sqlite3_stmt *stmt)
{
int count = atoi(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)));

// count == 0 since there is nothing in the database
// there is nothing in the database
return (count == 0);
};

// query the sqlite_schema table to check if the database is empty
// query the sqlite_schema table to check that the database is empty
std::vector<bool> result = db.execute("SELECT COUNT(*) FROM sqlite_schema;", callback);
CHECK(result.front() == true);

db.close();
}

SECTION("Allow build")
SECTION("Build flags")
{
// !build and file does not exist: runtime error
REQUIRE_THROWS_AS(database::Database("random.db").close(), std::runtime_error);
// !build and file exists: reuse the same file
REQUIRE_THROWS_AS(database::Database(failpath).close(), std::runtime_error);
// !build and file exists: reopen
REQUIRE_NOTHROW(database::Database(path).close());
// build and file exists: replace the file
REQUIRE_NOTHROW(database::Database(path, true).close());
// build and file does not exist: create a new file
// build and file exists: rebuild
REQUIRE_NOTHROW(database::Database(path, true).close());
// build and file does not exist: create
REQUIRE_NOTHROW(database::Database(failpath, true).close());
}

REQUIRE(std::remove(path.c_str()) == 0);
SECTION("Teardown")
{
REQUIRE(std::remove(path.c_str()) == 0);
REQUIRE(std::remove(failpath.c_str()) == 0);
}
}

0 comments on commit 4ddcf1c

Please sign in to comment.