Repository navigation
Expand file tree
/
Copy pathprovider_sqlite_tests.cpp
More file actions
84 lines (81 loc) · 3.6 KB
/
Copy pathprovider_sqlite_tests.cpp
File metadata and controls
84 lines (81 loc) · 3.6 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
#include <teaql/provider_sqlite/dialect.hpp>
#include <teaql/runtime/context.hpp>
#include <iostream>
#include <utility>
using namespace teaql;
static void check(bool ok, const char* message) {
if (!ok) {
throw std::runtime_error(message);
}
}
static void dialect_contract() {
GlobalContext global;
auto r = global.memory_resource();
auto& dialect = global.make_resource<SqliteDialect>();
const SqlDialect& base = dialect;
auto& e = global.make<EntityDescriptor>("Entity");
e.table("order");
constexpr std::pair<DataType, std::string_view> types[] = {
{DataType::Bool, "BOOLEAN"},
{DataType::I64, "INTEGER"},
{DataType::U64, "INTEGER"},
{DataType::F64, "REAL"},
{DataType::Decimal,
"DECIMAL_TEXT COLLATE TEAQL_DECIMAL CHECK (field IS NULL OR teaql_decimal_valid(field))"},
{DataType::Text, "VARCHAR(255)"},
{DataType::LargeText, "TEXT"},
{DataType::Json, "JSON"},
{DataType::Date, "DATE"},
{DataType::Timestamp, "TIMESTAMP"},
{DataType::Binary, "BLOB"}};
for (auto [type, expected] : types) {
PropertyDescriptor p(r, "field", type);
check(base.schema_type_sql(r, p.data_type, p) == expected,
"SQLite storage type differs from generic fallback");
}
auto& p = e.property("payload", DataType::Json).not_null();
check(base.compile_create_table(r, e) ==
"CREATE TABLE IF NOT EXISTS \"order\" (payload JSON NOT NULL)",
"create table dispatches SQLite type mapping");
check(base.compile_add_column(r, e, p) ==
"ALTER TABLE \"order\" ADD COLUMN payload JSON NOT NULL",
"SQLite add-column preserves absence of generic DEFAULT");
PropertyDescriptor decimal(r, "amount", DataType::Decimal);
decimal.with_numeric_precision(12).with_numeric_scale(3);
check(base.column_definition_sql(r, decimal) ==
"amount DECIMAL_TEXT(12,3) COLLATE TEAQL_DECIMAL CHECK (amount IS NULL OR "
"teaql_decimal_valid(amount))",
"decimal precision retains exact SQLite text storage and validation");
decimal.with_numeric_scale(13);
bool rejected = false;
try {
(void)base.compile_add_column(r, e, decimal);
} catch (const SqlCompileError& error) {
rejected = error.code == SqlCompileError::Code::InvalidSchemaShape;
}
check(rejected, "provider add-column retains generic shape validation");
check(base.kind() == DatabaseKind::Sqlite && base.prefers_small_parent_relation_probes() &&
!base.large_in_uses_array_param() && base.schema_setup_sqls().empty(),
"SQLite source capability flags are preserved");
check(base.placeholder(r, 1) == "?" && base.placeholder(r, 1000) == "?" &&
base.quote_ident(r, "select") == "\"select\"",
"SQLite parameter and identifier overrides are dispatched");
auto& q = global.make<SelectQuery>("Entity");
q.project_expr("n", Expr::count_all(r));
check(base.compile_select(r, e, q).sql == "SELECT COUNT(*) AS n FROM \"order\"",
"provider inherits full generic query compiler");
}
int main() {
try {
auto* previous = std::pmr::set_default_resource(std::pmr::null_memory_resource());
dialect_contract();
std::pmr::set_default_resource(previous);
std::cout << "SQLite provider dialect contracts passed\n";
} catch (const SqlCompileError& e) {
std::cerr << "unexpected SQL compile error " << unsigned(e.code) << '\n';
return 1;
} catch (const std::exception& e) {
std::cerr << e.what() << '\n';
return 1;
}
}