summaryrefslogtreecommitdiff
path: root/test/storage/sqlite.test.cpp
blob: 22958c8bed29b3e25c4dca6e3a80f21f9b04c5ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <mbgl/test/util.hpp>
#include <mbgl/test/fixture_log_observer.hpp>

#include <sqlite3.hpp>

using namespace mbgl;

TEST(SQLite, Statement) {
    using namespace mbgl;

    mapbox::sqlite::Database db = mapbox::sqlite::Database::open(":memory:", mapbox::sqlite::ReadWriteCreate);
    db.exec("CREATE TABLE test (id INTEGER);");

    mapbox::sqlite::Statement stmt1{ db, "INSERT INTO test (id) VALUES (?1);" };
    mapbox::sqlite::Query query1{ stmt1 };
    ASSERT_EQ(query1.lastInsertRowId(), 0);
    ASSERT_EQ(query1.changes(), 0u);
    query1.bind(1, 10);
    query1.run();
    ASSERT_EQ(query1.lastInsertRowId(), 1);
    ASSERT_EQ(query1.changes(), 1u);

    mapbox::sqlite::Statement stmt2{ db, "INSERT INTO test (id) VALUES (?1);" };
    mapbox::sqlite::Query query2{ stmt2 };
    ASSERT_EQ(query2.lastInsertRowId(), 0);
    ASSERT_EQ(query2.changes(), 0u);
    query2.bind(1, 20);
    query2.run();
    ASSERT_EQ(query2.lastInsertRowId(), 2);
    ASSERT_EQ(query2.changes(), 1u);
}

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::ReadWriteCreate));
    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::ReadWriteCreate);
    std::unique_ptr<mapbox::sqlite::Database> db2;
    mapbox::sqlite::Transaction transaction(db1);
    db2 = std::make_unique<mapbox::sqlite::Database>(std::move(db1));
    transaction.commit();
}