1 | #include "catch.hpp" |
2 | #include "duckdb/common/file_system.hpp" |
3 | #include "test_helpers.hpp" |
4 | |
5 | using namespace duckdb; |
6 | using namespace std; |
7 | |
8 | TEST_CASE("Test serialization of CHECK constraint" , "[storage]" ) { |
9 | unique_ptr<QueryResult> result; |
10 | auto storage_database = TestCreatePath("storage_test" ); |
11 | auto config = GetTestConfig(); |
12 | |
13 | // make sure the database does not exist |
14 | DeleteDatabase(storage_database); |
15 | { |
16 | // create a database and insert values |
17 | DuckDB db(storage_database, config.get()); |
18 | Connection con(db); |
19 | REQUIRE_NO_FAIL(con.Query("CREATE TABLE test(a INTEGER CHECK (a<10), b INTEGER CHECK(CASE " |
20 | "WHEN b < 10 THEN a < b ELSE a + b < 100 END));" )); |
21 | } |
22 | // reload the database from disk |
23 | { |
24 | DuckDB db(storage_database, config.get()); |
25 | Connection con(db); |
26 | // matching tuple |
27 | REQUIRE_NO_FAIL(con.Query("INSERT INTO test VALUES (3, 7);" )); |
28 | // check constraint on a violated (a < 10) |
29 | REQUIRE_FAIL(con.Query("INSERT INTO test VALUES (12, 13);" )); |
30 | // check constraint on b violated (b < 10) => (a < b) |
31 | REQUIRE_FAIL(con.Query("INSERT INTO test VALUES (5, 3);" )); |
32 | // check constraint on b not violated !(b < 10) => (a + b < 100) |
33 | REQUIRE_NO_FAIL(con.Query("INSERT INTO test VALUES (9, 90);" )); |
34 | // check constraint on b violated !(b < 10) => (a + b < 100) |
35 | REQUIRE_FAIL(con.Query("INSERT INTO test VALUES (9, 99);" )); |
36 | } |
37 | DeleteDatabase(storage_database); |
38 | } |
39 | |
40 | TEST_CASE("Test serialization of NOT NULL constraint" , "[storage]" ) { |
41 | unique_ptr<QueryResult> result; |
42 | auto storage_database = TestCreatePath("storage_test" ); |
43 | auto config = GetTestConfig(); |
44 | |
45 | // make sure the database does not exist |
46 | DeleteDatabase(storage_database); |
47 | { |
48 | // create a database and insert values |
49 | DuckDB db(storage_database, config.get()); |
50 | Connection con(db); |
51 | REQUIRE_NO_FAIL(con.Query("CREATE TABLE test(a INTEGER NOT NULL);" )); |
52 | } |
53 | // reload the database from disk |
54 | { |
55 | DuckDB db(storage_database, config.get()); |
56 | Connection con(db); |
57 | REQUIRE_FAIL(con.Query("INSERT INTO test VALUES (NULL)" )); |
58 | } |
59 | { |
60 | DuckDB db(storage_database, config.get()); |
61 | Connection con(db); |
62 | REQUIRE_FAIL(con.Query("INSERT INTO test VALUES (NULL)" )); |
63 | } |
64 | DeleteDatabase(storage_database); |
65 | } |
66 | |