more on database (tests)

This commit is contained in:
Martin Brodbeck 2018-07-10 15:46:55 +02:00
parent 53666163b4
commit eeae4fe609
4 changed files with 27 additions and 23 deletions

View file

@ -1,7 +1,7 @@
#include "database.h" #include "database.h"
#include <stdexcept>
#include <iostream> #include <iostream>
#include <stdexcept>
Database::Database(const std::string& dbname) : db_(nullptr) Database::Database(const std::string& dbname) : db_(nullptr)
{ {
@ -24,16 +24,14 @@ void Database::exec(const std::string& sql)
void Database::init() void Database::init()
{ {
std::string sqlCreateSellers{ std::string sqlCreateSellers{"CREATE TABLE IF NOT EXISTS sellers ("
"CREATE TABLE IF NOT EXISTS sellers (" "id TEXT PRIMARY KEY NOT NULL, "
"id TEXT PRIMARY KEY NOT NULL, " "seller_no INTEGER, "
"seller_no INTEGER, " "first_name TEXT, "
"first_name TEXT, " "last_name TEXT, "
"last_name TEXT, " "offered_articles INTEGER, "
"offered_articles INTEGER, " "UNIQUE (seller_no)"
"UNIQUE (seller_no)" ");"};
");"
};
std::string sqlCreateArticles{ std::string sqlCreateArticles{
"CREATE TABLE IF NOT EXISTS articles (" "CREATE TABLE IF NOT EXISTS articles ("
@ -46,10 +44,7 @@ void Database::init()
"UNIQUE (source_no, article_no), " "UNIQUE (source_no, article_no), "
"FOREIGN KEY (seller_id) REFERENCES sellers(id) ON DELETE CASCADE, " "FOREIGN KEY (seller_id) REFERENCES sellers(id) ON DELETE CASCADE, "
"CHECK (article_no BETWEEN 0 AND 99999)" "CHECK (article_no BETWEEN 0 AND 99999)"
");" ");"};
};
std::cout << sqlCreateArticles << "\n";
beginTransaction(); beginTransaction();
exec(sqlCreateSellers); exec(sqlCreateSellers);

View file

@ -2,8 +2,8 @@ find_package(Boost COMPONENTS unit_test_framework REQUIRED)
add_executable(test_seller test_seller.cpp) add_executable(test_seller test_seller.cpp)
target_link_libraries(test_seller core Boost::unit_test_framework) target_link_libraries(test_seller core Boost::unit_test_framework)
add_test(Seller ${CMAKE_BINARY_DIR}/bin/test_seller) add_test(NAME Seller COMMAND ${CMAKE_BINARY_DIR}/bin/test_seller)
add_executable(test_database test_database.cpp) add_executable(test_database test_database.cpp)
target_link_libraries(test_database core Boost::unit_test_framework stdc++fs) target_link_libraries(test_database core Boost::unit_test_framework stdc++fs)
add_test(Database ${CMAKE_BINARY_DIR}/bin/test_database) add_test(NAME Database COMMAND ${CMAKE_BINARY_DIR}/bin/test_database)

View file

@ -6,11 +6,20 @@
#include <boost/test/included/unit_test.hpp> #include <boost/test/included/unit_test.hpp>
//using namespace boost::unit_test; BOOST_AUTO_TEST_CASE(create_database)
{
std::filesystem::path testPath = std::filesystem::temp_directory_path() / "kima2test.db";
if (std::filesystem::exists(testPath)) {
std::filesystem::remove(testPath);
}
BOOST_AUTO_TEST_CASE( create_database ) { std::string filename = testPath.generic_string();
std::filesystem::path testPath = std::filesystem::temp_directory_path() / "test.db";
Database db(testPath.c_str()); Database db(filename);
db.init(); db.init();
std::filesystem::remove(testPath);
BOOST_TEST(std::filesystem::exists(testPath) == true);
if (std::filesystem::exists(testPath)) {
std::filesystem::remove(testPath);
}
} }

View file

@ -4,7 +4,7 @@
#include <boost/test/included/unit_test.hpp> #include <boost/test/included/unit_test.hpp>
using namespace boost::unit_test; //using namespace boost::unit_test;
BOOST_AUTO_TEST_CASE( create_uuid_nil ) { BOOST_AUTO_TEST_CASE( create_uuid_nil ) {
Seller seller{}; Seller seller{};