Skip to content

Commit

Permalink
even more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
paleolimbot committed Feb 26, 2024
1 parent c299303 commit d70034a
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 3 deletions.
6 changes: 3 additions & 3 deletions c/driver/common/driver_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class Error {
AdbcErrorDetail CDetail(int index) const {
const auto& detail = details_[index];
return {detail.first.c_str(), reinterpret_cast<const uint8_t*>(detail.second.data()),
detail.second.size()};
detail.second.size() + 1};
}

static void CRelease(AdbcError* error) {
Expand Down Expand Up @@ -312,15 +312,15 @@ class ObjectBase {
std::stringstream msg_builder;
msg_builder << "Option not found for key '" << key << "'";
Error cpperror(msg_builder.str());
cpperror.AddDetail("adbc.r.option_key", key);
cpperror.AddDetail("adbc.driver_base.option_key", key);
cpperror.ToAdbc(error, driver());
}

void InitErrorWrongType(const char* key, AdbcError* error) const {
std::stringstream msg_builder;
msg_builder << "Wrong type requested for option key '" << key << "'";
Error cpperror(msg_builder.str());
cpperror.AddDetail("adbc.r.option_key", key);
cpperror.AddDetail("adbc.driver_base.option_key", key);
cpperror.ToAdbc(error, driver());
}
};
Expand Down
57 changes: 57 additions & 0 deletions c/driver/common/driver_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ static inline void clean_up(AdbcStatement* ptr) {
ptr->private_driver->StatementRelease(ptr, nullptr);
}

static inline void clean_up(AdbcError* ptr) {
if (ptr->release != nullptr) {
ptr->release(ptr);
}
}

template <typename T>
class Handle {
public:
Expand All @@ -60,6 +66,7 @@ AdbcStatusCode VoidDriverInitFunc(int version, void* raw_driver, AdbcError* erro
}

TEST(TestDriverBase, TestVoidDriverOptions) {
// Test the get/set option implementation in the base driver
struct AdbcDriver driver;
memset(&driver, 0, sizeof(driver));
ASSERT_EQ(VoidDriverInitFunc(ADBC_VERSION_1_1_0, &driver, nullptr), ADBC_STATUS_OK);
Expand Down Expand Up @@ -147,6 +154,56 @@ TEST(TestDriverBase, TestVoidDriverOptions) {
ADBC_STATUS_NOT_FOUND);
}

TEST(TestDriverBase, TestVoidDriverError) {
// Test the extended error detail implementation in the base driver
struct AdbcDriver driver;
memset(&driver, 0, sizeof(driver));
ASSERT_EQ(VoidDriverInitFunc(ADBC_VERSION_1_1_0, &driver, nullptr), ADBC_STATUS_OK);
Handle<AdbcDriver> driver_handle(&driver);

struct AdbcDatabase database;
memset(&database, 0, sizeof(database));
ASSERT_EQ(driver.DatabaseNew(&database, nullptr), ADBC_STATUS_OK);
database.private_driver = &driver;
Handle<AdbcDatabase> database_handle(&database);
ASSERT_EQ(driver.DatabaseInit(&database, nullptr), ADBC_STATUS_OK);

struct AdbcError error;
memset(&error, 0, sizeof(error));
Handle<AdbcError> error_handle(&error);
size_t opt_size = 0;

// With zero-initialized error, should populate message but not details
ASSERT_EQ(driver.DatabaseGetOption(&database, "key_does_not_exist", nullptr, &opt_size,
&error),
ADBC_STATUS_NOT_FOUND);
EXPECT_EQ(error.vendor_code, 0);
EXPECT_STREQ(error.message, "Option not found for key 'key_does_not_exist'");
EXPECT_EQ(error.private_data, nullptr);
EXPECT_EQ(error.private_driver, nullptr);

// Release callback implementation should reset callback
error.release(&error);
ASSERT_EQ(error.release, nullptr);

// With the vendor code pre-set, should populate a version with details
memset(&error, 0, sizeof(error));
error.vendor_code = ADBC_ERROR_VENDOR_CODE_PRIVATE_DATA;

ASSERT_EQ(driver.DatabaseGetOption(&database, "key_does_not_exist", nullptr, &opt_size,
&error),
ADBC_STATUS_NOT_FOUND);
EXPECT_NE(error.private_data, nullptr);
EXPECT_EQ(error.private_driver, &driver);

ASSERT_EQ(error.private_driver->ErrorGetDetailCount(&error), 1);

struct AdbcErrorDetail detail = error.private_driver->ErrorGetDetail(&error, 0);
ASSERT_STREQ(detail.key, "adbc.driver_base.option_key");
ASSERT_EQ(detail.value_length, strlen("key_does_not_exist") + 1);
ASSERT_STREQ(reinterpret_cast<const char*>(detail.value), "key_does_not_exist");
}

TEST(TestDriverBase, TestVoidDriverMethods) {
struct AdbcDriver driver;
memset(&driver, 0, sizeof(driver));
Expand Down

0 comments on commit d70034a

Please sign in to comment.