Repository: duckdb/duckdb
File: test/api/capi/test_capi_appender.cpp (C++)

Code before:
		result = tester.Query("SELECT * FROM test");
		REQUIRE_NO_FAIL(*result);
		REQUIRE(result->Fetch<int32_t>(0, 0) == 42);
		REQUIRE(result->Fetch<int32_t>(1, 0) == 5);
	}

	SECTION("NON DEFAULT VALUE") {
		tester.Query("CREATE OR REPLACE TABLE test (a INTEGER, b INTEGER DEFAULT 5)");
		{
			CAPIAppender appender(tester, nullptr, "test");
			auto status = duckdb_appender_begin_row(appender);
			REQUIRE(status == DuckDBSuccess);

			// Append default to column without a default
			status = duckdb_append_default(appender);
			REQUIRE(status == DuckDBSuccess);

			status = duckdb_append_int32(appender, 42);
			REQUIRE(status == DuckDBSuccess);
			status = duckdb_appender_end_row(appender);
			REQUIRE(status == DuckDBSuccess);
		}
		result = tester.Query("SELECT * FROM test");
		REQUIRE_NO_FAIL(*result);
		REQUIRE(result->IsNull(0, 0));
		REQUIRE(result->Fetch<int32_t>(0, 0) == 0);
		REQUIRE(result->Fetch<int32_t>(1, 0) == 42);
	}
}

TEST_CASE("Test append timestamp in C API", "[capi]") {
	CAPITester tester;
	duckdb::unique_ptr<CAPIResult> result;
	duckdb_state status;

	// open the database in in-memory mode
	REQUIRE(tester.OpenDatabase(nullptr));

	tester.Query("CREATE TABLE test (t timestamp)");
	duckdb_appender appender;

	status = duckdb_appender_create(tester.connection, nullptr, "test", &appender);
	REQUIRE(status == DuckDBSuccess);
	REQUIRE(duckdb_appender_error(appender) == nullptr);

	// successful append
	status = duckdb_appender_begin_row(appender);
	REQUIRE(status == DuckDBSuccess);

	// status = duckdb_append_timestamp(appender, duckdb_timestamp{1649519797544000});
	status = duckdb_append_varchar(appender, "2022-04-09 15:56:37.544");

Review comment (refactor):
Let's remove this line. A `NULL` value should not have a numeric value.

Code after:
		REQUIRE(result->Fetch<int32_t>(1, 0) == 5);
	}

	SECTION("NON DEFAULT VALUE") {
		tester.Query("CREATE OR REPLACE TABLE test (a INTEGER, b INTEGER DEFAULT 5)");
		{
			CAPIAppender appender(tester, nullptr, "test");
			auto status = duckdb_appender_begin_row(appender);
			REQUIRE(status == DuckDBSuccess);

			// Append default to column without a default
			status = duckdb_append_default(appender);
			REQUIRE(status == DuckDBSuccess);

			status = duckdb_append_int32(appender, 42);
			REQUIRE(status == DuckDBSuccess);
			status = duckdb_appender_end_row(appender);
			REQUIRE(status == DuckDBSuccess);
		}
		result = tester.Query("SELECT * FROM test");
		REQUIRE_NO_FAIL(*result);
		REQUIRE(result->IsNull(0, 0));
		REQUIRE(result->Fetch<int32_t>(1, 0) == 42);
	}
}

TEST_CASE("Test append timestamp in C API", "[capi]") {
	CAPITester tester;
	duckdb::unique_ptr<CAPIResult> result;
	duckdb_state status;

	// open the database in in-memory mode
	REQUIRE(tester.OpenDatabase(nullptr));

	tester.Query("CREATE TABLE test (t timestamp)");
	duckdb_appender appender;

	status = duckdb_appender_create(tester.connection, nullptr, "test", &appender);
	REQUIRE(status == DuckDBSuccess);
	REQUIRE(duckdb_appender_error(appender) == nullptr);

	// successful append
	status = duckdb_appender_begin_row(appender);
	REQUIRE(status == DuckDBSuccess);

	// status = duckdb_append_timestamp(appender, duckdb_timestamp{1649519797544000});
	status = duckdb_append_varchar(appender, "2022-04-09 15:56:37.544");
	REQUIRE(status == DuckDBSuccess);

	status = duckdb_appender_end_row(appender);
	REQUIRE(status == DuckDBSuccess);