-
Notifications
You must be signed in to change notification settings - Fork 166
Expand file tree
/
Copy pathexceptions.cc
More file actions
51 lines (44 loc) · 1.6 KB
/
exceptions.cc
File metadata and controls
51 lines (44 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <iomanip>
#include <string>
#include <memory>
#include <stdexcept>
#include <sqlite_modern_cpp.h>
#include <catch2/catch.hpp>
using namespace sqlite;
using namespace std;
TEST_CASE("exceptions are thrown", "[exceptions]") {
database db(":memory:");
db << "CREATE TABLE person (id integer primary key not null, name TEXT);";
bool expception_thrown = false;
std::string get_sql_result;
#if SQLITE_VERSION_NUMBER >= 3014000
std::string expedted_sql = "INSERT INTO person (id,name) VALUES (1,'jack')";
#else
std::string expedted_sql = "INSERT INTO person (id,name) VALUES (?,?)";
#endif
SECTION("Parent exception works") {
try {
db << "INSERT INTO person (id,name) VALUES (?,?)" << 1 << "jack";
// inserting again to produce error
db << "INSERT INTO person (id,name) VALUES (?,?)" << 1 << "jack";
} catch (errors::constraint& e) {
expception_thrown = true;
get_sql_result = e.get_sql();
}
REQUIRE(expception_thrown == true);
REQUIRE(get_sql_result == expedted_sql);
}
SECTION("Extended exception works") {
try {
db << "INSERT INTO person (id,name) VALUES (?,?)" << 1 << "jack";
// inserting again to produce error
db << "INSERT INTO person (id,name) VALUES (?,?)" << 1 << "jack";
} catch (errors::constraint_primarykey& e) {
expception_thrown = true;
get_sql_result = e.get_sql();
}
REQUIRE(expception_thrown == true);
REQUIRE(get_sql_result == expedted_sql);
}
}