Skip to content

Commit b340ce1

Browse files
committed
move tests to catch
1 parent ab60859 commit b340ce1

17 files changed

+365
-566
lines changed

CMakeLists.txt

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,22 @@ set(CATCH_INCLUDE_DIR ${CMAKE_BINARY_DIR}/catch-src/single_include/)
2222
set(TEST_SOURCE_DIR ${CMAKE_SOURCE_DIR}/tests)
2323
#file(GLOB TEST_SOURCES ${TEST_SOURCE_DIR}/*.cc)
2424
set(TEST_SOURCES
25-
${TEST_SOURCE_DIR}/readme_example.cc
2625
${TEST_SOURCE_DIR}/blob_example.cc
2726
${TEST_SOURCE_DIR}/error_log.cc
28-
${TEST_SOURCE_DIR}/error_log2.cc
27+
${TEST_SOURCE_DIR}/exceptions.cc
28+
${TEST_SOURCE_DIR}/prepared_statment.cc
29+
${TEST_SOURCE_DIR}/lvalue_functor.cc
30+
${TEST_SOURCE_DIR}/functors.cc
31+
${TEST_SOURCE_DIR}/std_optional.cc
32+
${TEST_SOURCE_DIR}/flags.cc
33+
${TEST_SOURCE_DIR}/mov_ctor.cc
34+
${TEST_SOURCE_DIR}/shared_connection.cc
35+
${TEST_SOURCE_DIR}/trycatchblocks.cc
36+
${TEST_SOURCE_DIR}/readme_example.cc
37+
${TEST_SOURCE_DIR}/functions.cc
38+
${TEST_SOURCE_DIR}/nullptr_uniqueptr.cc
39+
${TEST_SOURCE_DIR}/simple_examples.cc
40+
${TEST_SOURCE_DIR}/variant.cc
2941
${TEST_SOURCE_DIR}/exception_dont_execute.cc
3042
${TEST_SOURCE_DIR}/exception_dont_execute_nested.cc
3143
)

tests/error_log.cc

Lines changed: 49 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,61 @@
33
#include <string>
44
#include <memory>
55
#include <stdexcept>
6-
#include <catch.hpp>
76
#include <sqlite_modern_cpp.h>
87
#include <sqlite_modern_cpp/log.h>
8+
#include <catch.hpp>
99
using namespace sqlite;
1010
using namespace std;
1111

12-
TEST_CASE("error_log works with multiple handlers", "[log]") {
13-
bool error_detected = false;
14-
error_log(
15-
[&](errors::constraint) {
16-
cerr << "Wrong error detected!" << endl;
17-
},
18-
[&](errors::constraint_primarykey e) {
19-
cerr << e.get_code() << '/' << e.get_extended_code() << ": " << e.what() << endl;
20-
error_detected = true;
21-
}
22-
);
12+
struct TrackErrors {
13+
TrackErrors()
14+
: constraint_called{false}, primarykey_called{false}
15+
{
16+
error_log(
17+
[this](errors::constraint) {
18+
constraint_called = true;
19+
},
20+
[this](errors::constraint_primarykey e) {
21+
primarykey_called = true;
22+
}
23+
// We are not registering the unique key constraint:
24+
// For a unique key error the first handler (errors::constraint) will be called instead.
25+
);
26+
}
27+
28+
bool constraint_called;
29+
bool primarykey_called;
30+
/* bool unique_called; */
31+
};
32+
33+
// Run before main, before any other sqlite function.
34+
static TrackErrors track;
35+
36+
37+
TEST_CASE("error_log works", "[log]") {
2338
database db(":memory:");
24-
db << "CREATE TABLE person (id integer primary key not null, name TEXT);";
39+
db << "CREATE TABLE person (id integer primary key not null, name TEXT unique);";
40+
41+
SECTION("An extended error code gets called when registered") {
42+
try {
43+
db << "INSERT INTO person (id,name) VALUES (?,?)" << 1 << "jack";
44+
// triger primarykey constraint of 'id'
45+
db << "INSERT INTO person (id,name) VALUES (?,?)" << 1 << "bob";
46+
} catch (errors::constraint& e) { }
47+
REQUIRE(track.primarykey_called == true);
48+
REQUIRE(track.constraint_called == false);
49+
track.primarykey_called = false;
50+
}
2551

26-
try {
27-
db << "INSERT INTO person (id,name) VALUES (?,?)" << 1 << "jack";
28-
// inserting again to produce error
29-
db << "INSERT INTO person (id,name) VALUES (?,?)" << 1 << "jack";
30-
} catch (errors::constraint& e) { }
52+
SECTION("Parent gets called when the exact error code is not registered") {
53+
try {
54+
db << "INSERT INTO person (id,name) VALUES (?,?)" << 1 << "jack";
55+
// trigger unique constraint of 'name'
56+
db << "INSERT INTO person (id,name) VALUES (?,?)" << 2 << "jack";
57+
} catch (errors::constraint& e) { }
3158

32-
REQUIRE(error_detected);
59+
REQUIRE(track.primarykey_called == false);
60+
REQUIRE(track.constraint_called == true);
61+
track.constraint_called = false;
62+
}
3363
}

tests/error_log2.cc

Lines changed: 0 additions & 28 deletions
This file was deleted.

tests/exceptions.cc

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,48 @@
44
#include <memory>
55
#include <stdexcept>
66
#include <sqlite_modern_cpp.h>
7+
#include <catch.hpp>
78
using namespace sqlite;
89
using namespace std;
910

1011

11-
int main() {
12-
database db(":memory:");
13-
db << "CREATE TABLE person (id integer primary key not null, name TEXT);";
14-
bool expception_thrown = false;
12+
TEST_CASE("exceptions are thrown", "[exceptions]") {
13+
database db(":memory:");
14+
db << "CREATE TABLE person (id integer primary key not null, name TEXT);";
15+
bool expception_thrown = false;
16+
std::string get_sql_result;
1517

16-
try {
17-
db << "INSERT INTO person (id,name) VALUES (?,?)" << 1 << "jack";
18-
// inserting again to produce error
19-
db << "INSERT INTO person (id,name) VALUES (?,?)" << 1 << "jack";
20-
} catch (errors::constraint& e) {
21-
cerr << e.get_code() << ": " << e.what() << " during "
22-
<< quoted(e.get_sql()) << endl;
23-
expception_thrown = true;
2418
#if SQLITE_VERSION_NUMBER >= 3014000
25-
if(e.get_sql() != "INSERT INTO person (id,name) VALUES (1,'jack')") {
19+
std::string expedted_sql = "INSERT INTO person (id,name) VALUES (1,'jack')";
2620
#else
27-
if(e.get_sql() != "INSERT INTO person (id,name) VALUES (?,?)") {
21+
std::string expedted_sql = "INSERT INTO person (id,name) VALUES (?,?)";
2822
#endif
29-
cerr << "Wrong statement failed\n";
30-
exit(EXIT_FAILURE);
23+
24+
SECTION("Parent exception works") {
25+
try {
26+
db << "INSERT INTO person (id,name) VALUES (?,?)" << 1 << "jack";
27+
// inserting again to produce error
28+
db << "INSERT INTO person (id,name) VALUES (?,?)" << 1 << "jack";
29+
} catch (errors::constraint& e) {
30+
expception_thrown = true;
31+
get_sql_result = e.get_sql();
32+
}
33+
34+
REQUIRE(expception_thrown == true);
35+
REQUIRE(get_sql_result == expedted_sql);
3136
}
32-
}
3337

34-
if(!expception_thrown) {
35-
exit(EXIT_FAILURE);
36-
}
37-
38-
exit(EXIT_SUCCESS);
38+
SECTION("Extended exception works") {
39+
try {
40+
db << "INSERT INTO person (id,name) VALUES (?,?)" << 1 << "jack";
41+
// inserting again to produce error
42+
db << "INSERT INTO person (id,name) VALUES (?,?)" << 1 << "jack";
43+
} catch (errors::constraint_primarykey& e) {
44+
expception_thrown = true;
45+
get_sql_result = e.get_sql();
46+
}
47+
48+
REQUIRE(expception_thrown == true);
49+
REQUIRE(get_sql_result == expedted_sql);
50+
}
3951
}

tests/extended_exceptions.cc

Lines changed: 0 additions & 39 deletions
This file was deleted.

0 commit comments

Comments
 (0)