|
3 | 3 | #include <string> |
4 | 4 | #include <memory> |
5 | 5 | #include <stdexcept> |
6 | | -#include <catch.hpp> |
7 | 6 | #include <sqlite_modern_cpp.h> |
8 | 7 | #include <sqlite_modern_cpp/log.h> |
| 8 | +#include <catch.hpp> |
9 | 9 | using namespace sqlite; |
10 | 10 | using namespace std; |
11 | 11 |
|
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]") { |
23 | 38 | 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 | + } |
25 | 51 |
|
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) { } |
31 | 58 |
|
32 | | - REQUIRE(error_detected); |
| 59 | + REQUIRE(track.primarykey_called == false); |
| 60 | + REQUIRE(track.constraint_called == true); |
| 61 | + track.constraint_called = false; |
| 62 | + } |
33 | 63 | } |
0 commit comments