mirror of
https://github.com/cuberite/SQLiteCpp.git
synced 2025-08-05 10:16:01 -04:00
Add some more Unit Tests
This commit is contained in:
parent
b10bf6faa5
commit
18620457b1
@ -93,6 +93,7 @@ source_group(inc FILES ${SQLITECPP_INC})
|
||||
|
||||
# list of test files of the library
|
||||
set(SQLITECPP_TESTS
|
||||
tests/Column_test.cpp
|
||||
tests/Database_test.cpp
|
||||
tests/Statement_test.cpp
|
||||
)
|
||||
|
@ -40,7 +40,7 @@ namespace SQLite
|
||||
#else
|
||||
|
||||
// if no assert handler provided by user code, use standard assert()
|
||||
// (note: in debug mode, assert() does nothing)
|
||||
// (note: in release mode assert() does nothing)
|
||||
#define SQLITECPP_ASSERT(expression, message) assert(expression && message)
|
||||
|
||||
#endif
|
||||
|
@ -185,7 +185,7 @@ public:
|
||||
return getBlob();
|
||||
}
|
||||
|
||||
#ifdef __GNUC__
|
||||
#ifdef __GNUC__
|
||||
// NOTE : the following is required by GCC and Clang to cast a Column result in a std::string
|
||||
// (error: conversion from ‘SQLite::Column’ to non-scalar type ‘std::string {aka std::basic_string<char>}’)
|
||||
// but is not working under Microsoft Visual Studio 2010 and 2012
|
||||
|
@ -31,14 +31,14 @@ Column::~Column() noexcept // nothrow
|
||||
}
|
||||
|
||||
// Return the named assigned to this result column (potentially aliased)
|
||||
const char * Column::getName() const noexcept // nothrow
|
||||
const char* Column::getName() const noexcept // nothrow
|
||||
{
|
||||
return sqlite3_column_name(mStmtPtr, mIndex);
|
||||
}
|
||||
|
||||
#ifdef SQLITE_ENABLE_COLUMN_METADATA
|
||||
// Return the name of the table column that is the origin of this result column
|
||||
const char * Column::getOriginName() const noexcept // nothrow
|
||||
const char* Column::getOriginName() const noexcept // nothrow
|
||||
{
|
||||
return sqlite3_column_origin_name(mStmtPtr, mIndex);
|
||||
}
|
||||
|
58
tests/Column_test.cpp
Normal file
58
tests/Column_test.cpp
Normal file
@ -0,0 +1,58 @@
|
||||
/**
|
||||
* @file Column_test.cpp
|
||||
* @ingroup tests
|
||||
* @brief Test of a SQLiteCpp Column.
|
||||
*
|
||||
* Copyright (c) 2012-2015 Sebastien Rombauts (sebastien.rombauts@gmail.com)
|
||||
*
|
||||
* Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt
|
||||
* or copy at http://opensource.org/licenses/MIT)
|
||||
*/
|
||||
|
||||
#include <SQLiteCpp/Database.h>
|
||||
#include <SQLiteCpp/Statement.h>
|
||||
#include <SQLiteCpp/Column.h>
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <cstdio>
|
||||
|
||||
|
||||
TEST(Column, basics) {
|
||||
remove("test.db3");
|
||||
{
|
||||
// Create a new database
|
||||
SQLite::Database db("test.db3", SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
|
||||
EXPECT_EQ(SQLITE_OK, db.getErrorCode());
|
||||
EXPECT_EQ(SQLITE_OK, db.getExtendedErrorCode());
|
||||
|
||||
// Create a new table
|
||||
EXPECT_EQ(0, db.exec("CREATE TABLE test (id INTEGER PRIMARY KEY, msg TEXT, int INTEGER, double REAL)"));
|
||||
EXPECT_TRUE(db.tableExists("test"));
|
||||
EXPECT_TRUE(db.tableExists(std::string("test")));
|
||||
EXPECT_EQ(0, db.getLastInsertRowid());
|
||||
|
||||
// Create a first row
|
||||
EXPECT_EQ(1, db.exec("INSERT INTO test VALUES (NULL, \"first\", 123, 0.123)"));
|
||||
EXPECT_EQ(1, db.getLastInsertRowid());
|
||||
EXPECT_EQ(1, db.getTotalChanges());
|
||||
|
||||
// Compile a SQL query
|
||||
SQLite::Statement query(db, "SELECT * FROM test");
|
||||
EXPECT_STREQ("SELECT * FROM test", query.getQuery().c_str());
|
||||
EXPECT_EQ(4, query.getColumnCount ());
|
||||
query.executeStep();
|
||||
EXPECT_TRUE (query.isOk());
|
||||
EXPECT_FALSE(query.isDone());
|
||||
|
||||
// TODO
|
||||
const std::string msg = query.getColumn("msg");
|
||||
const int integer = query.getColumn("int");
|
||||
const double real = query.getColumn("double");
|
||||
EXPECT_EQ("first", msg);
|
||||
EXPECT_EQ(123, integer);
|
||||
EXPECT_EQ(0.123, real);
|
||||
|
||||
} // Close DB test.db3
|
||||
remove("test.db3");
|
||||
}
|
@ -115,6 +115,9 @@ TEST(Database, exec) {
|
||||
EXPECT_EQ(9, db.getTotalChanges());
|
||||
#endif
|
||||
|
||||
// Add a row with too many values (more than rows in the table)
|
||||
EXPECT_THROW(db.exec("INSERT INTO test VALUES (NULL, \"first\", 123, 0.123)"), SQLite::Exception);
|
||||
|
||||
} // Close DB test.db3
|
||||
remove("test.db3");
|
||||
}
|
||||
|
@ -53,7 +53,7 @@ TEST(Statement, invalid) {
|
||||
EXPECT_FALSE(query.isOk());
|
||||
EXPECT_FALSE(query.isDone());
|
||||
|
||||
query.exec();
|
||||
query.executeStep();
|
||||
EXPECT_FALSE(query.isOk());
|
||||
EXPECT_TRUE( query.isDone());
|
||||
query.reset();
|
||||
@ -70,10 +70,21 @@ TEST(Statement, invalid) {
|
||||
EXPECT_EQ(SQLITE_RANGE, db.getErrorCode());
|
||||
EXPECT_EQ(SQLITE_RANGE, db.getExtendedErrorCode());
|
||||
|
||||
query.exec();
|
||||
query.exec(); // exec() instead of executeStep() as there is no result
|
||||
EXPECT_THROW(query.isColumnNull(0), SQLite::Exception);
|
||||
EXPECT_THROW(query.getColumn(0), SQLite::Exception);
|
||||
|
||||
// Add a first row
|
||||
EXPECT_EQ(1, db.exec("INSERT INTO test VALUES (NULL, \"first\")"));
|
||||
EXPECT_EQ(1, db.getLastInsertRowid());
|
||||
EXPECT_EQ(1, db.getTotalChanges());
|
||||
|
||||
query.reset();
|
||||
EXPECT_FALSE(query.isOk());
|
||||
EXPECT_FALSE(query.isDone());
|
||||
|
||||
EXPECT_THROW(query.exec(), SQLite::Exception); // exec() shall throw as it does not expect a result
|
||||
|
||||
} // Close DB test.db3
|
||||
remove("test.db3");
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user