summaryrefslogtreecommitdiffhomepage
path: root/tests/test-compiledsql.cpp
blob: 00a822183642e9e8464fb53798d55ca58b8982a7 (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
67
#include <gtest/gtest.h>

#include <filesystem>
#include <memory>
#include <string>
#include <system_error>

#include "libreichwein/file.h"

#include "config.h"
#include "storage.h"
#include "whiteboard.h"

namespace fs = std::filesystem;

namespace {
 const std::string testDbFilename{"./whiteboard.db3"};
}

class CompiledSQLTest: public ::testing::Test
{
protected:
 CompiledSQLTest(){
 }

 ~CompiledSQLTest() override{
 }

 void SetUp() override
 {
  std::error_code ec;
  fs::remove(testDbFilename, ec);
 }

 void TearDown() override
 {
  std::error_code ec;
  fs::remove(testDbFilename, ec);
 }

};

TEST_F(CompiledSQLTest, create)
{
 SQLite::Database db{testDbFilename, SQLite::OPEN_READWRITE | SQLite::OPEN_CREATE};

 CompiledSQL stmt1{db};
 stmt1.init("CREATE TABLE documents (id VARCHAR(16) PRIMARY KEY)");
 stmt1.execute();

 CompiledSQL stmt2{db};
 stmt2.init("INSERT INTO documents (id) values (?)");
 stmt2.bind(1, "abc");
 ASSERT_TRUE(stmt2.execute());

 CompiledSQL stmt3{db};
 stmt3.init("SELECT id FROM documents WHERE id = ?");
 stmt3.bind(1, "abc");
 ASSERT_TRUE(stmt3.execute());
 EXPECT_EQ(stmt3.getColumn<std::string>(0), "abc");
 stmt3.init("SELECT id FROM documents WHERE id = ?");
 stmt3.bind(1, "def");
 ASSERT_FALSE(stmt3.execute());

 EXPECT_TRUE(fs::exists(testDbFilename));
}