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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
|
/*
* Copyright (c) 2021, Jan de Visser <jan@de-visser.net>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <unistd.h>
#include <AK/ScopeGuard.h>
#include <LibSQL/BTree.h>
#include <LibSQL/Database.h>
#include <LibSQL/Heap.h>
#include <LibSQL/Meta.h>
#include <LibSQL/Row.h>
#include <LibSQL/Value.h>
#include <LibTest/TestCase.h>
NonnullRefPtr<SQL::SchemaDef> setup_schema(SQL::Database&);
NonnullRefPtr<SQL::TableDef> setup_table(SQL::Database&);
void insert_into_table(SQL::Database&, int);
void verify_table_contents(SQL::Database&, int);
void insert_and_verify(int);
void commit(SQL::Database&);
NonnullRefPtr<SQL::SchemaDef> setup_schema(SQL::Database& db)
{
auto schema = SQL::SchemaDef::construct("TestSchema");
auto maybe_error = db.add_schema(schema);
EXPECT(!maybe_error.is_error());
return schema;
}
NonnullRefPtr<SQL::TableDef> setup_table(SQL::Database& db)
{
auto schema = setup_schema(db);
auto table = SQL::TableDef::construct(schema, "TestTable");
table->append_column("TextColumn", SQL::SQLType::Text);
table->append_column("IntColumn", SQL::SQLType::Integer);
EXPECT_EQ(table->num_columns(), 2u);
auto maybe_error = db.add_table(table);
EXPECT(!maybe_error.is_error());
return table;
}
void insert_into_table(SQL::Database& db, int count)
{
auto table = MUST(db.get_table("TestSchema", "TestTable"));
for (int ix = 0; ix < count; ix++) {
SQL::Row row(*table);
StringBuilder builder;
builder.appendff("Test{}", ix);
row["TextColumn"] = builder.build();
row["IntColumn"] = ix;
auto maybe_error = db.insert(row);
EXPECT(!maybe_error.is_error());
}
}
void verify_table_contents(SQL::Database& db, int expected_count)
{
auto table = MUST(db.get_table("TestSchema", "TestTable"));
int sum = 0;
int count = 0;
auto rows_or_error = db.select_all(*table);
EXPECT(!rows_or_error.is_error());
for (auto& row : rows_or_error.value()) {
StringBuilder builder;
builder.appendff("Test{}", row["IntColumn"].to_int<i32>().value());
EXPECT_EQ(row["TextColumn"].to_deprecated_string(), builder.build());
count++;
sum += row["IntColumn"].to_int<i32>().value();
}
EXPECT_EQ(count, expected_count);
EXPECT_EQ(sum, (expected_count * (expected_count - 1)) / 2);
}
void commit(SQL::Database& db)
{
auto maybe_error = db.commit();
EXPECT(!maybe_error.is_error());
}
void insert_and_verify(int count)
{
ScopeGuard guard([]() { unlink("/tmp/test.db"); });
{
auto db = SQL::Database::construct("/tmp/test.db");
EXPECT(!db->open().is_error());
(void)setup_table(db);
commit(db);
}
{
auto db = SQL::Database::construct("/tmp/test.db");
EXPECT(!db->open().is_error());
insert_into_table(db, count);
commit(db);
}
{
auto db = SQL::Database::construct("/tmp/test.db");
EXPECT(!db->open().is_error());
verify_table_contents(db, count);
}
}
TEST_CASE(create_heap)
{
ScopeGuard guard([]() { unlink("/tmp/test.db"); });
auto heap = SQL::Heap::construct("/tmp/test.db");
EXPECT(!heap->open().is_error());
EXPECT_EQ(heap->version(), SQL::Heap::current_version);
}
TEST_CASE(create_from_dev_random)
{
auto heap = SQL::Heap::construct("/dev/random");
auto should_be_error = heap->open();
EXPECT(should_be_error.is_error());
}
TEST_CASE(create_from_unreadable_file)
{
auto heap = SQL::Heap::construct("/etc/shadow");
auto should_be_error = heap->open();
EXPECT(should_be_error.is_error());
}
TEST_CASE(create_in_non_existing_dir)
{
auto heap = SQL::Heap::construct("/tmp/bogus/test.db");
auto should_be_error = heap->open();
EXPECT(should_be_error.is_error());
}
TEST_CASE(create_database)
{
ScopeGuard guard([]() { unlink("/tmp/test.db"); });
auto db = SQL::Database::construct("/tmp/test.db");
auto should_not_be_error = db->open();
EXPECT(!should_not_be_error.is_error());
commit(db);
}
TEST_CASE(add_schema_to_database)
{
ScopeGuard guard([]() { unlink("/tmp/test.db"); });
auto db = SQL::Database::construct("/tmp/test.db");
EXPECT(!db->open().is_error());
(void)setup_schema(db);
commit(db);
}
TEST_CASE(get_schema_from_database)
{
ScopeGuard guard([]() { unlink("/tmp/test.db"); });
{
auto db = SQL::Database::construct("/tmp/test.db");
EXPECT(!db->open().is_error());
(void)setup_schema(db);
commit(db);
}
{
auto db = SQL::Database::construct("/tmp/test.db");
EXPECT(!db->open().is_error());
auto schema_or_error = db->get_schema("TestSchema");
EXPECT(!schema_or_error.is_error());
}
}
TEST_CASE(add_table_to_database)
{
ScopeGuard guard([]() { unlink("/tmp/test.db"); });
auto db = SQL::Database::construct("/tmp/test.db");
EXPECT(!db->open().is_error());
(void)setup_table(db);
commit(db);
}
TEST_CASE(get_table_from_database)
{
ScopeGuard guard([]() { unlink("/tmp/test.db"); });
{
auto db = SQL::Database::construct("/tmp/test.db");
EXPECT(!db->open().is_error());
(void)setup_table(db);
commit(db);
}
{
auto db = SQL::Database::construct("/tmp/test.db");
EXPECT(!db->open().is_error());
auto table = MUST(db->get_table("TestSchema", "TestTable"));
EXPECT_EQ(table->name(), "TestTable");
EXPECT_EQ(table->num_columns(), 2u);
}
}
TEST_CASE(insert_one_into_and_select_from_table)
{
insert_and_verify(1);
}
TEST_CASE(insert_two_into_table)
{
insert_and_verify(2);
}
TEST_CASE(insert_10_into_table)
{
insert_and_verify(10);
}
TEST_CASE(insert_100_into_table)
{
insert_and_verify(100);
}
|