<commit_before>/**
 * @file    Database_test.cpp
 * @ingroup tests
 * @brief   Test of a SQLiteCpp Database.
 *
 * Copyright (c) 2012-2015 <NAME> (<EMAIL>)
 *
 * Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt
 * or copy at http://opensource.org/licenses/MIT)
 */

#include <SQLiteCpp/Database.h>

#include <gtest/gtest.h>

#include <cstdio>

#ifdef SQLITECPP_ENABLE_ASSERT_HANDLER
namespace SQLite
{
/// definition of the assertion handler enabled when SQLITECPP_ENABLE_ASSERT_HANDLER is defined in the project (CMakeList.txt)
void assertion_failed(const char* apFile, const long apLine, const char* apFunc, const char* apExpr, const char* apMsg)
{
    // TODO test that this assertion callback get called
    std::cout << "assertion_failed(" << apFile << ", " << apLine << ", " << apFunc << ", " << apExpr << ", " << apMsg << ")\n";
}
}
#endif


TEST(Database, ctorExecCreateDropExist) {
    remove("test.db3");
    {
        // Try to open an unexisting database
        EXPECT_THROW(SQLite::Database not_found("test.db3"), SQLite::Exception);

        // Create a new database
        SQLite::Database db("test.db3", SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
        EXPECT_STREQ("test.db3", db.getFilename().c_str());
        EXPECT_FALSE(db.tableExists("test"));
        EXPECT_FALSE(db.tableExists(std::string("test")));
        EXPECT_EQ(0, db.getLastInsertRowid());

        EXPECT_EQ(0, db.exec("CREATE TABLE test (id INTEGER PRIMARY KEY, value TEXT)"));
        EXPECT_TRUE(db.tableExists("test"));
        EXPECT_TRUE(db.tableExists(std::string("test")));
        EXPECT_EQ(0, db.getLastInsertRowid());
        
        EXPECT_EQ(0, db.exec("DROP TABLE IF EXISTS test"));
        EXPECT_FALSE(db.tableExists("test"));
        EXPECT_FALSE(db.tableExists(std::string("test")));
        EXPECT_EQ(0, db.getLastInsertRowid());
    } // Close DB test.db3
    remove("test.db3");
}

TEST(Database, inMemory) {
    {
        // Create a new database
        SQLite::Database db(":memory:", SQLITE_OPEN_READWRITE);
        EXPECT_FALSE(db.tableExists("test"));
        db.exec("CREATE TABLE test (id INTEGER PRIMARY KEY, value TEXT)");
        EXPECT_TRUE(db.tableExists("test"));
        // Create a new database: not shared with the above db
        SQLite::Database db2(":memory:");
        EXPECT_FALSE(db2.tableExists("test"));
    } // Close an destroy DBs
    {
        // Create a new database: no more "test" table
        SQLite::Database db(":memory:");
        EXPECT_FALSE(db.tableExists("test"));
    } // Close an destroy DB
}

TEST(Database, busyTimeout) {
    // Create a new database
    SQLite::Database db(":memory:");
    // Busy timeout default to 0ms: any contention between threads or process leads to SQLITE_BUSY error
    EXPECT_EQ(0, db.execAndGet("PRAGMA busy_timeout").getInt());

    // Set a non null busy timeout: any contention between threads will leads to as much retry as possible during the time
    db.setBusyTimeout(5000);
    EXPECT_EQ(5000, db.execAndGet("PRAGMA busy_timeout").getInt());

    // Reset timeout to null
    db.setBusyTimeout(0);
    EXPECT_EQ(0, db.execAndGet("PRAGMA busy_timeout").getInt());
}

TEST(Database, exec) {
    // Create a new database
    SQLite::Database db(":memory:", SQLITE_OPEN_READWRITE);<commit_msg>Deactivate unit test for setBusyTimeout() not supported before SQLite 3.7.15<commit_after>/**
 * @file    Database_test.cpp
 * @ingroup tests
 * @brief   Test of a SQLiteCpp Database.
 *
 * Copyright (c) 2012-2015 <NAME> (<EMAIL>)
 *
 * Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt
 * or copy at http://opensource.org/licenses/MIT)
 */

#include <SQLiteCpp/Database.h>

#include <gtest/gtest.h>

#include <cstdio>

#ifdef SQLITECPP_ENABLE_ASSERT_HANDLER
namespace SQLite
{
/// definition of the assertion handler enabled when SQLITECPP_ENABLE_ASSERT_HANDLER is defined in the project (CMakeList.txt)
void assertion_failed(const char* apFile, const long apLine, const char* apFunc, const char* apExpr, const char* apMsg)
{
    // TODO test that this assertion callback get called
    std::cout << "assertion_failed(" << apFile << ", " << apLine << ", " << apFunc << ", " << apExpr << ", " << apMsg << ")\n";
}
}
#endif


TEST(Database, ctorExecCreateDropExist) {
    remove("test.db3");
    {
        // Try to open an unexisting database
        EXPECT_THROW(SQLite::Database not_found("test.db3"), SQLite::Exception);

        // Create a new database
        SQLite::Database db("test.db3", SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
        EXPECT_STREQ("test.db3", db.getFilename().c_str());
        EXPECT_FALSE(db.tableExists("test"));
        EXPECT_FALSE(db.tableExists(std::string("test")));
        EXPECT_EQ(0, db.getLastInsertRowid());

        EXPECT_EQ(0, db.exec("CREATE TABLE test (id INTEGER PRIMARY KEY, value TEXT)"));
        EXPECT_TRUE(db.tableExists("test"));
        EXPECT_TRUE(db.tableExists(std::string("test")));
        EXPECT_EQ(0, db.getLastInsertRowid());
        
        EXPECT_EQ(0, db.exec("DROP TABLE IF EXISTS test"));
        EXPECT_FALSE(db.tableExists("test"));
        EXPECT_FALSE(db.tableExists(std::string("test")));
        EXPECT_EQ(0, db.getLastInsertRowid());
    } // Close DB test.db3
    remove("test.db3");
}

TEST(Database, inMemory) {
    {
        // Create a new database
        SQLite::Database db(":memory:", SQLITE_OPEN_READWRITE);
        EXPECT_FALSE(db.tableExists("test"));
        db.exec("CREATE TABLE test (id INTEGER PRIMARY KEY, value TEXT)");
        EXPECT_TRUE(db.tableExists("test"));
        // Create a new database: not shared with the above db
        SQLite::Database db2(":memory:");
        EXPECT_FALSE(db2.tableExists("test"));
    } // Close an destroy DBs
    {
        // Create a new database: no more "test" table
        SQLite::Database db(":memory:");
        EXPECT_FALSE(db.tableExists("test"));
    } // Close an destroy DB
}

#if SQLITE_VERSION_NUMBER >= 3007015 // first version with PRAGMA busy_timeout
TEST(Database, busyTimeout) {
    // Create a new database
    SQLite::Database db(":memory:");
    // Busy timeout default to 0ms: any contention between threads or process leads to SQLITE_BUSY error
    EXPECT_EQ(0, db.execAndGet("PRAGMA busy_timeout").getInt());

    // Set a non null busy timeout: any contention between threads will leads to as much retry as possible during the time
    db.setBusyTimeout(5000);
    EXPECT_EQ(5000, db.execAndGet("PRAGMA busy_timeout").getInt());

    // Reset timeout to null
    db.setBusyTimeout(0);
    EXPECT_EQ(0, db.execAndGet("PRAGMA busy_timeout").getInt());
}
#endif // SQLITE_VERSION_NUMBER >= 3007015

TEST(Database, exec) {
    // Create a new database
    SQLite::Database db(":memory:", SQLITE_OPEN_READWRITE);
{'fraction_non_alphanumeric': 0.1378398236590742, 'fraction_numerical': 0.012343864805290228, 'mean_word_length': 3.5102717031146455, 'pattern_counts': {'":': 10, '<': 57, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5736147', 'n_tokens_mistral': 2147, 'n_tokens_neox': 2126, 'n_words': 539}