From 7517a86fe8e714eacb336a9c1581747fe914dd24 Mon Sep 17 00:00:00 2001 From: Vincent Rischmann Date: Mon, 15 Aug 2022 21:53:02 +0200 Subject: move test helpers --- sqlite.zig | 45 +-------------------------------------------- test.zig | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 44 deletions(-) create mode 100644 test.zig diff --git a/sqlite.zig b/sqlite.zig index 6c6f6af..6af1d04 100644 --- a/sqlite.zig +++ b/sqlite.zig @@ -19,6 +19,7 @@ pub const DetailedError = errors.DetailedError; const getLastDetailedErrorFromDb = errors.getLastDetailedErrorFromDb; const getDetailedErrorFromResultCode = errors.getDetailedErrorFromResultCode; +const getTestDb = @import("test.zig").getTestDb; const helpers = @import("helpers.zig"); const logger = std.log.scoped(.sqlite); @@ -3302,50 +3303,6 @@ test "sqlite: two nested savepoints with outer failure" { try testing.expectEqual(@as(usize, 0), rows.len); } -fn getTestDb() !Db { - var buf: [1024]u8 = undefined; - var fba = std.heap.FixedBufferAllocator.init(&buf); - - var mode = dbMode(fba.allocator()); - - return try Db.init(.{ - .open_flags = .{ - .write = true, - .create = true, - }, - .mode = mode, - }); -} - -fn tmpDbPath(allocator: mem.Allocator) ![:0]const u8 { - const tmp_dir = testing.tmpDir(.{}); - - const path = try std.fs.path.join(allocator, &[_][]const u8{ - "zig-cache", - "tmp", - &tmp_dir.sub_path, - "zig-sqlite.db", - }); - defer allocator.free(path); - - return allocator.dupeZ(u8, path); -} - -fn dbMode(allocator: mem.Allocator) Db.Mode { - return if (build_options.in_memory) blk: { - break :blk .{ .Memory = {} }; - } else blk: { - if (build_options.dbfile) |dbfile| { - return .{ .File = allocator.dupeZ(u8, dbfile) catch unreachable }; - } - - const path = tmpDbPath(allocator) catch unreachable; - - std.fs.cwd().deleteFile(path) catch {}; - break :blk .{ .File = path }; - }; -} - const MyData = struct { data: [16]u8, diff --git a/test.zig b/test.zig new file mode 100644 index 0000000..f6d9c9c --- /dev/null +++ b/test.zig @@ -0,0 +1,50 @@ +const std = @import("std"); +const build_options = @import("build_options"); +const mem = std.mem; +const testing = std.testing; + +const Db = @import("sqlite.zig").Db; + +pub fn getTestDb() !Db { + var buf: [1024]u8 = undefined; + var fba = std.heap.FixedBufferAllocator.init(&buf); + + var mode = dbMode(fba.allocator()); + + return try Db.init(.{ + .open_flags = .{ + .write = true, + .create = true, + }, + .mode = mode, + }); +} + +fn tmpDbPath(allocator: mem.Allocator) ![:0]const u8 { + const tmp_dir = testing.tmpDir(.{}); + + const path = try std.fs.path.join(allocator, &[_][]const u8{ + "zig-cache", + "tmp", + &tmp_dir.sub_path, + "zig-sqlite.db", + }); + defer allocator.free(path); + + return allocator.dupeZ(u8, path); +} + +fn dbMode(allocator: mem.Allocator) Db.Mode { + return if (build_options.in_memory) blk: { + break :blk .{ .Memory = {} }; + } else blk: { + if (build_options.dbfile) |dbfile| { + return .{ .File = allocator.dupeZ(u8, dbfile) catch unreachable }; + } + + const path = tmpDbPath(allocator) catch unreachable; + + std.fs.cwd().deleteFile(path) catch {}; + break :blk .{ .File = path }; + }; +} -- cgit v1.2.3