Repository navigation
Expand file tree
/
Copy pathtest_export.cpp
More file actions
221 lines (184 loc) · 6.35 KB
/
Copy pathtest_export.cpp
File metadata and controls
221 lines (184 loc) · 6.35 KB
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
219
220
221
/**
* Export tests — exercises SQL dump output against a temp SQLite database.
* No ONNX model needed; uses sqlite::Backend's DB-only constructor + raw SQL.
*/
#include "export.h"
#include "config.h"
#include "test_env.h"
#include <sqlite3.h>
#include <cassert>
#include <filesystem>
#include <print>
#include <sstream>
namespace fs = std::filesystem;
static const std::string TEMP_DB = "/tmp/ragger_test_export.db";
static void cleanup() {
fs::remove(TEMP_DB);
fs::remove(TEMP_DB + "-wal");
fs::remove(TEMP_DB + "-shm");
}
static void exec(sqlite3* db, const char* sql) {
char* err = nullptr;
int rc = sqlite3_exec(db, sql, nullptr, nullptr, &err);
if (rc != SQLITE_OK) {
std::string msg = err ? err : "unknown";
sqlite3_free(err);
throw std::runtime_error(msg);
}
}
static sqlite3* create_test_db() {
cleanup();
sqlite3* db = nullptr;
sqlite3_open(TEMP_DB.c_str(), &db);
exec(db, R"(
CREATE TABLE memories (
id INTEGER PRIMARY KEY AUTOINCREMENT,
text TEXT NOT NULL,
embedding BLOB,
metadata TEXT,
timestamp TEXT NOT NULL
)
)");
exec(db, R"(
CREATE TABLE settings (
key TEXT PRIMARY KEY,
value TEXT NOT NULL
)
)");
exec(db, "INSERT INTO memories (text, embedding, metadata, timestamp) "
"VALUES ('hello world', X'DEADBEEF', '{\"k\":\"v\"}', '2026-01-01T00:00:00Z')");
exec(db, "INSERT INTO memories (text, embedding, metadata, timestamp) "
"VALUES ('it''s a test', NULL, '{}', '2026-06-15T12:00:00Z')");
exec(db, "INSERT INTO settings (key, value) VALUES ('version', '1')");
return db;
}
// -----------------------------------------------------------------------
void test_export_all_no_embeddings() {
std::println(" test_export_all_no_embeddings...");
auto* db = create_test_db();
sqlite3_close(db);
std::ostringstream out;
ragger::ExportOptions opts;
int rows = ragger::export_sql(out, TEMP_DB, opts);
std::string sql = out.str();
assert(rows == 3);
assert(sql.find("BEGIN TRANSACTION") != std::string::npos);
assert(sql.find("COMMIT") != std::string::npos);
assert(sql.find("CREATE TABLE memories") != std::string::npos);
assert(sql.find("CREATE TABLE settings") != std::string::npos);
// embedding column should NOT appear in INSERT column lists
assert(sql.find("INSERT INTO memories (id, text, metadata, timestamp)") != std::string::npos);
assert(sql.find("DEADBEEF") == std::string::npos);
// Data values present
assert(sql.find("hello world") != std::string::npos);
assert(sql.find("it''s a test") != std::string::npos);
cleanup();
std::println(" OK");
}
void test_export_all_with_embeddings() {
std::println(" test_export_all_with_embeddings...");
auto* db = create_test_db();
sqlite3_close(db);
std::ostringstream out;
ragger::ExportOptions opts;
opts.include_embeddings = true;
int rows = ragger::export_sql(out, TEMP_DB, opts);
std::string sql = out.str();
assert(rows == 3);
assert(sql.find("INSERT INTO memories (id, text, embedding, metadata, timestamp)") != std::string::npos);
assert(sql.find("DEADBEEF") != std::string::npos);
cleanup();
std::println(" OK");
}
void test_export_single_table() {
std::println(" test_export_single_table...");
auto* db = create_test_db();
sqlite3_close(db);
std::ostringstream out;
ragger::ExportOptions opts;
opts.table = "settings";
int rows = ragger::export_sql(out, TEMP_DB, opts);
std::string sql = out.str();
assert(rows == 1);
assert(sql.find("CREATE TABLE settings") != std::string::npos);
assert(sql.find("CREATE TABLE memories") == std::string::npos);
assert(sql.find("INSERT INTO settings") != std::string::npos);
cleanup();
std::println(" OK");
}
void test_export_list_tables() {
std::println(" test_export_list_tables...");
auto* db = create_test_db();
sqlite3_close(db);
auto tables = ragger::export_list_tables(TEMP_DB);
assert(tables.size() == 2);
bool has_memories = false, has_settings = false;
for (auto& t : tables) {
if (t == "memories") has_memories = true;
if (t == "settings") has_settings = true;
}
assert(has_memories);
assert(has_settings);
cleanup();
std::println(" OK");
}
void test_export_null_handling() {
std::println(" test_export_null_handling...");
auto* db = create_test_db();
sqlite3_close(db);
std::ostringstream out;
ragger::ExportOptions opts;
opts.include_embeddings = true;
ragger::export_sql(out, TEMP_DB, opts);
std::string sql = out.str();
// Second row has NULL embedding
assert(sql.find("NULL") != std::string::npos);
cleanup();
std::println(" OK");
}
void test_export_roundtrip() {
std::println(" test_export_roundtrip...");
auto* db = create_test_db();
sqlite3_close(db);
// Export
std::ostringstream out;
ragger::ExportOptions opts;
opts.include_embeddings = true;
ragger::export_sql(out, TEMP_DB, opts);
std::string sql = out.str();
// Import into a fresh DB
std::string rt_db = "/tmp/ragger_test_export_rt.db";
fs::remove(rt_db);
sqlite3* db2 = nullptr;
sqlite3_open(rt_db.c_str(), &db2);
exec(db2, sql.c_str());
// Verify row count
sqlite3_stmt* stmt = nullptr;
sqlite3_prepare_v2(db2, "SELECT COUNT(*) FROM memories", -1, &stmt, nullptr);
sqlite3_step(stmt);
int count = sqlite3_column_int(stmt, 0);
sqlite3_finalize(stmt);
assert(count == 2);
sqlite3_prepare_v2(db2, "SELECT COUNT(*) FROM settings", -1, &stmt, nullptr);
sqlite3_step(stmt);
count = sqlite3_column_int(stmt, 0);
sqlite3_finalize(stmt);
assert(count == 1);
sqlite3_close(db2);
fs::remove(rt_db);
cleanup();
std::println(" OK");
}
// -----------------------------------------------------------------------
int main() {
TestEnv env("export"); // must be first: isolates from ~/.ragger
std::println("Running export tests:");
test_export_all_no_embeddings();
test_export_all_with_embeddings();
test_export_single_table();
test_export_list_tables();
test_export_null_handling();
test_export_roundtrip();
std::println("test_export: all passed");
return 0;
}