[core] support moving Database object during a Transaction

This commit is contained in:
Konstantin Käfer
2018-06-07 15:05:32 +02:00
parent a50493ea51
commit 4477dd4119
5 changed files with 72 additions and 21 deletions

View File

@@ -1,8 +1,10 @@
#include <mbgl/test/util.hpp>
#include <mbgl/test/fixture_log_observer.hpp>
#include <gtest/gtest.h>
#include <sqlite3.hpp>
using namespace mbgl;
TEST(SQLite, Statement) {
using namespace mbgl;
@@ -29,9 +31,36 @@ TEST(SQLite, Statement) {
}
TEST(SQLite, TEST_REQUIRES_WRITE(TryOpen)) {
FixtureLog log;
// Should return a CANTOPEN exception when the database doesn't exist,
// make sure all the backends behave the same way.
auto result = mapbox::sqlite::Database::tryOpen("test/fixtures/offline_database/foobar123.db", mapbox::sqlite::ReadOnly);
ASSERT_TRUE(result.is<mapbox::sqlite::Exception>());
ASSERT_EQ(result.get<mapbox::sqlite::Exception>().code, mapbox::sqlite::ResultCode::CantOpen);
#ifndef __QT__
// Only non-Qt platforms are setting a logger on the SQLite object.
EXPECT_EQ(1u, log.count({ EventSeverity::Info, Event::Database, static_cast<int64_t>(mapbox::sqlite::ResultCode::CantOpen), "cannot open file" }, true));
EXPECT_EQ(1u, log.count({ EventSeverity::Info, Event::Database, static_cast<int64_t>(mapbox::sqlite::ResultCode::CantOpen), "No such file or directory" }, true));
#endif
EXPECT_EQ(0u, log.uncheckedCount());
}
TEST(SQLite, CloseDatabaseWithPendingTransaction) {
auto db = std::make_unique<mapbox::sqlite::Database>(mapbox::sqlite::Database::open(
":memory:", mapbox::sqlite::ReadWrite | mapbox::sqlite::Create));
mapbox::sqlite::Transaction transaction(*db);
transaction.commit();
}
TEST(SQLite, CloseMovedDatabaseWithPendingTransaction) {
// Verifies that we can correctly commit a transaction even if we move the Database object to
// another address.
auto db1 = mapbox::sqlite::Database::open(":memory:",
mapbox::sqlite::ReadWrite | mapbox::sqlite::Create);
std::unique_ptr<mapbox::sqlite::Database> db2;
mapbox::sqlite::Transaction transaction(db1);
db2 = std::make_unique<mapbox::sqlite::Database>(std::move(db1));
transaction.commit();
}