summaryrefslogtreecommitdiff
path: root/test.zig
diff options
context:
space:
mode:
authorGravatar Vincent Rischmann2022-08-15 21:53:02 +0200
committerGravatar Vincent Rischmann2022-09-11 17:12:37 +0200
commit7517a86fe8e714eacb336a9c1581747fe914dd24 (patch)
tree91d872d94306a40ac4c31592dfa855ccfce9c785 /test.zig
parentmove setFunctionArgument to helpers.setTypeFromValue (diff)
downloadzig-sqlite-7517a86fe8e714eacb336a9c1581747fe914dd24.tar.gz
zig-sqlite-7517a86fe8e714eacb336a9c1581747fe914dd24.tar.xz
zig-sqlite-7517a86fe8e714eacb336a9c1581747fe914dd24.zip
move test helpers
Diffstat (limited to 'test.zig')
-rw-r--r--test.zig50
1 files changed, 50 insertions, 0 deletions
diff --git a/test.zig b/test.zig
new file mode 100644
index 0000000..f6d9c9c
--- /dev/null
+++ b/test.zig
@@ -0,0 +1,50 @@
1const std = @import("std");
2const build_options = @import("build_options");
3const mem = std.mem;
4const testing = std.testing;
5
6const Db = @import("sqlite.zig").Db;
7
8pub fn getTestDb() !Db {
9 var buf: [1024]u8 = undefined;
10 var fba = std.heap.FixedBufferAllocator.init(&buf);
11
12 var mode = dbMode(fba.allocator());
13
14 return try Db.init(.{
15 .open_flags = .{
16 .write = true,
17 .create = true,
18 },
19 .mode = mode,
20 });
21}
22
23fn tmpDbPath(allocator: mem.Allocator) ![:0]const u8 {
24 const tmp_dir = testing.tmpDir(.{});
25
26 const path = try std.fs.path.join(allocator, &[_][]const u8{
27 "zig-cache",
28 "tmp",
29 &tmp_dir.sub_path,
30 "zig-sqlite.db",
31 });
32 defer allocator.free(path);
33
34 return allocator.dupeZ(u8, path);
35}
36
37fn dbMode(allocator: mem.Allocator) Db.Mode {
38 return if (build_options.in_memory) blk: {
39 break :blk .{ .Memory = {} };
40 } else blk: {
41 if (build_options.dbfile) |dbfile| {
42 return .{ .File = allocator.dupeZ(u8, dbfile) catch unreachable };
43 }
44
45 const path = tmpDbPath(allocator) catch unreachable;
46
47 std.fs.cwd().deleteFile(path) catch {};
48 break :blk .{ .File = path };
49 };
50}