summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Vincent Rischmann2022-08-15 21:53:02 +0200
committerGravatar Vincent Rischmann2022-09-18 20:11:36 +0200
commit91d1afe0f6aae9e0d20f6380b85c4ff840cef120 (patch)
tree9aded3b0b8066c5d7d2b6fdf07b521730e60b936
parentmove setFunctionArgument to helpers.setTypeFromValue (diff)
downloadzig-sqlite-91d1afe0f6aae9e0d20f6380b85c4ff840cef120.tar.gz
zig-sqlite-91d1afe0f6aae9e0d20f6380b85c4ff840cef120.tar.xz
zig-sqlite-91d1afe0f6aae9e0d20f6380b85c4ff840cef120.zip
move test helpers
Diffstat (limited to '')
-rw-r--r--sqlite.zig45
-rw-r--r--test.zig50
2 files changed, 51 insertions, 44 deletions
diff --git a/sqlite.zig b/sqlite.zig
index b7fe80f..09e13ba 100644
--- a/sqlite.zig
+++ b/sqlite.zig
@@ -19,6 +19,7 @@ pub const DetailedError = errors.DetailedError;
19const getLastDetailedErrorFromDb = errors.getLastDetailedErrorFromDb; 19const getLastDetailedErrorFromDb = errors.getLastDetailedErrorFromDb;
20const getDetailedErrorFromResultCode = errors.getDetailedErrorFromResultCode; 20const getDetailedErrorFromResultCode = errors.getDetailedErrorFromResultCode;
21 21
22const getTestDb = @import("test.zig").getTestDb;
22const helpers = @import("helpers.zig"); 23const helpers = @import("helpers.zig");
23 24
24const logger = std.log.scoped(.sqlite); 25const logger = std.log.scoped(.sqlite);
@@ -3301,50 +3302,6 @@ test "sqlite: two nested savepoints with outer failure" {
3301 try testing.expectEqual(@as(usize, 0), rows.len); 3302 try testing.expectEqual(@as(usize, 0), rows.len);
3302} 3303}
3303 3304
3304fn getTestDb() !Db {
3305 var buf: [1024]u8 = undefined;
3306 var fba = std.heap.FixedBufferAllocator.init(&buf);
3307
3308 var mode = dbMode(fba.allocator());
3309
3310 return try Db.init(.{
3311 .open_flags = .{
3312 .write = true,
3313 .create = true,
3314 },
3315 .mode = mode,
3316 });
3317}
3318
3319fn tmpDbPath(allocator: mem.Allocator) ![:0]const u8 {
3320 const tmp_dir = testing.tmpDir(.{});
3321
3322 const path = try std.fs.path.join(allocator, &[_][]const u8{
3323 "zig-cache",
3324 "tmp",
3325 &tmp_dir.sub_path,
3326 "zig-sqlite.db",
3327 });
3328 defer allocator.free(path);
3329
3330 return allocator.dupeZ(u8, path);
3331}
3332
3333fn dbMode(allocator: mem.Allocator) Db.Mode {
3334 return if (build_options.in_memory) blk: {
3335 break :blk .{ .Memory = {} };
3336 } else blk: {
3337 if (build_options.dbfile) |dbfile| {
3338 return .{ .File = allocator.dupeZ(u8, dbfile) catch unreachable };
3339 }
3340
3341 const path = tmpDbPath(allocator) catch unreachable;
3342
3343 std.fs.cwd().deleteFile(path) catch {};
3344 break :blk .{ .File = path };
3345 };
3346}
3347
3348const MyData = struct { 3305const MyData = struct {
3349 data: [16]u8, 3306 data: [16]u8,
3350 3307
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}