//! Store tests against an in-memory SQLite database. const std = @import("std"); const testing = std.testing; const Store = @import("db.zig").Store; fn titles(todos: anytype) ![]const u8 { var out: std.Io.Writer.Allocating = .init(testing.allocator); errdefer out.deinit(); for (todos) |t| try out.writer.print("{s}{s};", .{ if (t.completed) "x " else "", t.title }); return out.toOwnedSlice(); } fn expectList(store: *Store, list: i64, want: []const u8) !void { var arena: std.heap.ArenaAllocator = .init(testing.allocator); defer arena.deinit(); const got = try titles(try store.todos(arena.allocator(), list)); defer testing.allocator.free(got); try testing.expectEqualStrings(want, got); } test "lists are separate and every action is scoped to its list" { var store = try Store.open(testing.io, ":memory:"); defer store.close(); const a = try store.createList("hash-a"); const b = try store.createList("hash-b"); try testing.expectEqual(a, (try store.findList("hash-a")).?); try testing.expectEqual(null, try store.findList("hash-c")); try store.add(a, "one"); try store.add(a, "two"); try store.add(b, "theirs"); try expectList(&store, a, "one;two;"); var arena: std.heap.ArenaAllocator = .init(testing.allocator); defer arena.deinit(); const theirs = (try store.todos(arena.allocator(), b))[0].id; try testing.expect(!try store.toggle(a, theirs)); try testing.expect(!try store.rename(a, theirs, "mine now")); try testing.expect(!try store.destroy(a, theirs)); try expectList(&store, b, "theirs;"); const two = (try store.todos(arena.allocator(), a))[1].id; try testing.expect(try store.toggle(a, two)); try expectList(&store, a, "one;x two;"); try testing.expect(try store.rename(a, two, "2 ✓")); try expectList(&store, a, "one;x 2 ✓;"); } test "toggle all, then back, then clear completed" { var store = try Store.open(testing.io, ":memory:"); defer store.close(); const a = try store.createList("hash-a"); try store.add(a, "one"); try store.add(a, "two"); try store.toggleAll(a); try expectList(&store, a, "x one;x two;"); try store.toggleAll(a); try expectList(&store, a, "one;two;"); var arena: std.heap.ArenaAllocator = .init(testing.allocator); defer arena.deinit(); _ = try store.toggle(a, (try store.todos(arena.allocator(), a))[0].id); try store.toggleAll(a); try expectList(&store, a, "x one;x two;"); try store.clearCompleted(a); try expectList(&store, a, ""); }