1 | #include "catch.hpp" |
2 | #include "test_helpers.hpp" |
3 | |
4 | using namespace duckdb; |
5 | using namespace std; |
6 | |
7 | TEST_CASE("Test boolean casts" , "[cast]" ) { |
8 | unique_ptr<QueryResult> result; |
9 | DuckDB db(nullptr); |
10 | Connection con(db); |
11 | con.EnableQueryVerification(); |
12 | |
13 | // boolean -> string casts |
14 | result = con.Query("SELECT CAST(1=1 AS VARCHAR)" ); |
15 | REQUIRE(CHECK_COLUMN(result, 0, {"true" })); |
16 | result = con.Query("SELECT CAST(1=0 AS VARCHAR)" ); |
17 | REQUIRE(CHECK_COLUMN(result, 0, {"false" })); |
18 | // string -> boolean casts |
19 | result = con.Query("SELECT CAST('true' AS BOOLEAN)" ); |
20 | REQUIRE(CHECK_COLUMN(result, 0, {true})); |
21 | result = con.Query("SELECT CAST('t' AS BOOLEAN)" ); |
22 | REQUIRE(CHECK_COLUMN(result, 0, {true})); |
23 | result = con.Query("SELECT CAST('TRUE' AS BOOLEAN)" ); |
24 | REQUIRE(CHECK_COLUMN(result, 0, {true})); |
25 | result = con.Query("SELECT CAST('false' AS BOOLEAN)" ); |
26 | REQUIRE(CHECK_COLUMN(result, 0, {false})); |
27 | result = con.Query("SELECT CAST('f' AS BOOLEAN)" ); |
28 | REQUIRE(CHECK_COLUMN(result, 0, {false})); |
29 | result = con.Query("SELECT CAST('FALSE' AS BOOLEAN)" ); |
30 | REQUIRE(CHECK_COLUMN(result, 0, {false})); |
31 | REQUIRE_FAIL(con.Query("SELECT CAST('12345' AS BOOLEAN)" )); |
32 | |
33 | // varchar -> integer -> boolean |
34 | result = con.Query("SELECT CAST(CAST('12345' AS INTEGER) AS BOOLEAN)" ); |
35 | REQUIRE(CHECK_COLUMN(result, 0, {true})); |
36 | result = con.Query("SELECT CAST(CAST('0' AS INTEGER) AS BOOLEAN)" ); |
37 | REQUIRE(CHECK_COLUMN(result, 0, {false})); |
38 | |
39 | // varchar -> numeric -> boolean casts |
40 | vector<string> types = {"tinyint" , "smallint" , "integer" , "bigint" , "decimal" }; |
41 | for (auto &type : types) { |
42 | result = con.Query("SELECT CAST(CAST('1' AS " + type + ") AS BOOLEAN)" ); |
43 | REQUIRE(CHECK_COLUMN(result, 0, {true})); |
44 | result = con.Query("SELECT CAST(CAST('0' AS " + type + ") AS BOOLEAN)" ); |
45 | REQUIRE(CHECK_COLUMN(result, 0, {false})); |
46 | } |
47 | } |
48 | |