File: transaction.cpp

package info (click to toggle)
openmw 0.50.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 37,076 kB
  • sloc: cpp: 380,958; xml: 2,192; sh: 1,449; python: 911; makefile: 26; javascript: 5
file content (67 lines) | stat: -rw-r--r-- 1,754 bytes parent folder | download
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
67
#include <components/sqlite3/db.hpp>
#include <components/sqlite3/request.hpp>
#include <components/sqlite3/statement.hpp>
#include <components/sqlite3/transaction.hpp>

#include <gmock/gmock.h>
#include <gtest/gtest.h>

#include <limits>
#include <tuple>
#include <vector>

namespace
{
    using namespace testing;
    using namespace Sqlite3;

    struct InsertId
    {
        static std::string_view text() noexcept { return "INSERT INTO test (id) VALUES (42)"; }
        static void bind(sqlite3&, sqlite3_stmt&) {}
    };

    struct GetIds
    {
        static std::string_view text() noexcept { return "SELECT id FROM test"; }
        static void bind(sqlite3&, sqlite3_stmt&) {}
    };

    struct Sqlite3TransactionTest : Test
    {
        const Db mDb = makeDb(":memory:", "CREATE TABLE test ( id INTEGER )");

        void insertId() const
        {
            Statement insertId(*mDb, InsertId{});
            EXPECT_EQ(execute(*mDb, insertId), 1);
        }

        std::vector<std::tuple<int>> getIds() const
        {
            Statement getIds(*mDb, GetIds{});
            std::vector<std::tuple<int>> result;
            request(*mDb, getIds, std::back_inserter(result), std::numeric_limits<std::size_t>::max());
            return result;
        }
    };

    TEST_F(Sqlite3TransactionTest, shouldRollbackOnDestruction)
    {
        {
            const Transaction transaction(*mDb);
            insertId();
        }
        EXPECT_THAT(getIds(), IsEmpty());
    }

    TEST_F(Sqlite3TransactionTest, commitShouldCommitTransaction)
    {
        {
            Transaction transaction(*mDb);
            insertId();
            transaction.commit();
        }
        EXPECT_THAT(getIds(), ElementsAre(std::tuple(42)));
    }
}