summaryrefslogtreecommitdiff
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
parentmove setFunctionArgument to helpers.setTypeFromValue (diff)
downloadzig-sqlite-7517a86fe8e714eacb336a9c1581747fe914dd24.tar.gz
zig-sqlite-7517a86fe8e714eacb336a9c1581747fe914dd24.tar.xz
zig-sqlite-7517a86fe8e714eacb336a9c1581747fe914dd24.zip
move test helpers
-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 6c6f6af..6af1d04 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);
@@ -3302,50 +3303,6 @@ test "sqlite: two nested savepoints with outer failure" {
3302 try testing.expectEqual(@as(usize, 0), rows.len); 3303 try testing.expectEqual(@as(usize, 0), rows.len);
3303} 3304}
3304 3305
3305fn getTestDb() !Db {
3306 var buf: [1024]u8 = undefined;
3307 var fba = std.heap.FixedBufferAllocator.init(&buf);
3308
3309 var mode = dbMode(fba.allocator());
3310
3311 return try Db.init(.{
3312 .open_flags = .{
3313 .write = true,
3314 .create = true,
3315 },
3316 .mode = mode,
3317 });
3318}
3319
3320fn tmpDbPath(allocator: mem.Allocator) ![:0]const u8 {
3321 const tmp_dir = testing.tmpDir(.{});
3322
3323 const path = try std.fs.path.join(allocator, &[_][]const u8{
3324 "zig-cache",
3325 "tmp",
3326 &tmp_dir.sub_path,
3327 "zig-sqlite.db",
3328 });
3329 defer allocator.free(path);
3330
3331 return allocator.dupeZ(u8, path);
3332}
3333
3334fn dbMode(allocator: mem.Allocator) Db.Mode {
3335 return if (build_options.in_memory) blk: {
3336 break :blk .{ .Memory = {} };
3337 } else blk: {
3338 if (build_options.dbfile) |dbfile| {
3339 return .{ .File = allocator.dupeZ(u8, dbfile) catch unreachable };
3340 }
3341
3342 const path = tmpDbPath(allocator) catch unreachable;
3343
3344 std.fs.cwd().deleteFile(path) catch {};
3345 break :blk .{ .File = path };
3346 };
3347}
3348
3349const MyData = struct { 3306const MyData = struct {
3350 data: [16]u8, 3307 data: [16]u8,
3351 3308
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}