⌘+k ctrl+k
1.1.3 (stable)
Search Shortcut cmd + k | ctrl + k
Catch C/C++ Tests

虽然我们更喜欢使用sqllogic测试来测试大多数功能,但对于某些测试,仅使用SQL是不够的。这通常发生在你想测试C++ API时。当纯SQL确实不是一个选项时,可能需要使用Catch进行C++测试。

Catch测试也位于测试目录中。以下是一个测试系统存储的catch测试示例:

#include "catch.hpp"
#include "test_helpers.hpp"

TEST_CASE("Test simple storage", "[storage]") {
    auto config = GetTestConfig();
    unique_ptr<QueryResult> result;
    auto storage_database = TestCreatePath("storage_test");

    // make sure the database does not exist
    DeleteDatabase(storage_database);
    {
        // create a database and insert values
        DuckDB db(storage_database, config.get());
        Connection con(db);
        REQUIRE_NO_FAIL(con.Query("CREATE TABLE test (a INTEGER, b INTEGER);"));
        REQUIRE_NO_FAIL(con.Query("INSERT INTO test VALUES (11, 22), (13, 22), (12, 21), (NULL, NULL)"));
        REQUIRE_NO_FAIL(con.Query("CREATE TABLE test2 (a INTEGER);"));
        REQUIRE_NO_FAIL(con.Query("INSERT INTO test2 VALUES (13), (12), (11)"));
    }
    // reload the database from disk a few times
    for (idx_t i = 0; i < 2; i++) {
        DuckDB db(storage_database, config.get());
        Connection con(db);
        result = con.Query("SELECT * FROM test ORDER BY a");
        REQUIRE(CHECK_COLUMN(result, 0, {Value(), 11, 12, 13}));
        REQUIRE(CHECK_COLUMN(result, 1, {Value(), 22, 21, 22}));
        result = con.Query("SELECT * FROM test2 ORDER BY a");
        REQUIRE(CHECK_COLUMN(result, 0, {11, 12, 13}));
    }
    DeleteDatabase(storage_database);
}

测试使用TEST_CASE包装器来创建每个测试。数据库是使用C++ API创建和查询的。结果使用REQUIRE_FAIL/REQUIRE_NO_FAIL(对应于语句成功和语句错误)或REQUIRE(CHECK_COLUMN(...))(对应于带有结果检查的查询)进行检查。以这种方式创建的每个测试都需要添加到相应的CMakeLists.txt中。