zrm/tests/query.zig
Madeorsk f16ad46f8d
Relations and SQL query build improvements.
+ Relations definition.
+ Add test submodels in testing database.
* Generalize raw SQL query preparation and build.
* Simplify and improve SQL query build algorithms.
2024-11-22 15:40:10 +01:00

56 lines
2.2 KiB
Zig

const std = @import("std");
const zrm = @import("zrm");
test "zrm.conditions.value" {
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena.deinit();
const condition = try zrm.conditions.value(usize, arena.allocator(), "test", "=", 5);
try std.testing.expectEqualStrings("test = ?", condition.sql);
try std.testing.expectEqual(1, condition.params.len);
try std.testing.expectEqual(5, condition.params[0].integer);
}
test "zrm.conditions.in" {
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena.deinit();
const condition = try zrm.conditions.in(usize, arena.allocator(), "intest", &[_]usize{2, 3, 8});
try std.testing.expectEqualStrings("intest IN (?,?,?)", condition.sql);
try std.testing.expectEqual(3, condition.params.len);
try std.testing.expectEqual(2, condition.params[0].integer);
try std.testing.expectEqual(3, condition.params[1].integer);
try std.testing.expectEqual(8, condition.params[2].integer);
}
test "zrm.conditions.column" {
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena.deinit();
const condition = try zrm.conditions.column(arena.allocator(), "firstcol", "<>", "secondcol");
try std.testing.expectEqualStrings("firstcol <> secondcol", condition.sql);
try std.testing.expectEqual(0, condition.params.len);
}
test "zrm.conditions combined" {
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena.deinit();
const condition = try zrm.conditions.@"and"(arena.allocator(), &[_]zrm.RawQuery{
try zrm.conditions.value(usize, arena.allocator(), "test", "=", 5),
try zrm.conditions.@"or"(arena.allocator(), &[_]zrm.RawQuery{
try zrm.conditions.in(usize, arena.allocator(), "intest", &[_]usize{2, 3, 8}),
try zrm.conditions.column(arena.allocator(), "firstcol", "<>", "secondcol"),
}),
});
try std.testing.expectEqualStrings("(test = ? AND (intest IN (?,?,?) OR firstcol <> secondcol))", condition.sql);
try std.testing.expectEqual(4, condition.params.len);
try std.testing.expectEqual(5, condition.params[0].integer);
try std.testing.expectEqual(2, condition.params[1].integer);
try std.testing.expectEqual(3, condition.params[2].integer);
try std.testing.expectEqual(8, condition.params[3].integer);
}