refactor root.zig tests into higher level api
This commit is contained in:
parent
2febea12d7
commit
474aadd498
1 changed files with 139 additions and 159 deletions
298
src/root.zig
298
src/root.zig
|
@ -3,13 +3,32 @@ const notmuch = @import("notmuch.zig");
|
||||||
|
|
||||||
// Thread representation for JSON serialization
|
// Thread representation for JSON serialization
|
||||||
pub const Thread = struct {
|
pub const Thread = struct {
|
||||||
|
allocator: std.mem.Allocator,
|
||||||
thread: *notmuch.Db.Thread,
|
thread: *notmuch.Db.Thread,
|
||||||
|
|
||||||
pub fn init(t: *notmuch.Db.Thread) Thread {
|
pub fn init(allocator: std.mem.Allocator, t: *notmuch.Db.Thread) Thread {
|
||||||
return .{ .thread = t };
|
return .{ .allocator = allocator, .thread = t };
|
||||||
|
}
|
||||||
|
pub fn deinit(self: Thread) void {
|
||||||
|
self.allocator.destroy(self.thread);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn jsonStringify(self: Thread, jws: anytype) !void {
|
pub fn jsonStringify(self: Thread, jws: anytype) !void {
|
||||||
|
// Format we're looking for on api/thread/<threadid>
|
||||||
|
//[
|
||||||
|
// {
|
||||||
|
// "from": "The Washington Post <email@washingtonpost.com>",
|
||||||
|
// "to": "elerch@lerch.org",
|
||||||
|
// "cc": null,
|
||||||
|
// "bcc": null,
|
||||||
|
// "date": "Sun, 21 Jul 2024 19:23:38 +0000",
|
||||||
|
// "subject": "Biden steps aside",
|
||||||
|
// "content": "...content...",
|
||||||
|
// "content_type": "text/html",
|
||||||
|
// "attachments": [],
|
||||||
|
// "message_id": "01010190d6bfe4e1-185e2720-e415-4086-8865-9604cde886c2-000000@us-west-2.amazonses.com"
|
||||||
|
// }
|
||||||
|
//]
|
||||||
try jws.beginArray();
|
try jws.beginArray();
|
||||||
var mi = self.thread.getMessages() catch return error.OutOfMemory;
|
var mi = self.thread.getMessages() catch return error.OutOfMemory;
|
||||||
while (mi.next()) |m| {
|
while (mi.next()) |m| {
|
||||||
|
@ -43,110 +62,32 @@ pub const Thread = struct {
|
||||||
|
|
||||||
// Threads collection for JSON serialization
|
// Threads collection for JSON serialization
|
||||||
pub const Threads = struct {
|
pub const Threads = struct {
|
||||||
|
allocator: std.mem.Allocator,
|
||||||
iterator: *notmuch.Db.ThreadIterator,
|
iterator: *notmuch.Db.ThreadIterator,
|
||||||
|
|
||||||
pub fn init(it: *notmuch.Db.ThreadIterator) Threads {
|
pub fn init(allocator: std.mem.Allocator, it: *notmuch.Db.ThreadIterator) Threads {
|
||||||
return .{
|
return .{
|
||||||
|
.allocator = allocator,
|
||||||
.iterator = it,
|
.iterator = it,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn deinit(self: *Threads) void {
|
||||||
|
self.iterator.deinit();
|
||||||
|
self.allocator.destroy(self.iterator);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn next(self: *Threads) !?Thread {
|
||||||
|
const nxt = self.iterator.next();
|
||||||
|
if (nxt) |_| {
|
||||||
|
const tptr = try self.allocator.create(notmuch.Db.Thread);
|
||||||
|
tptr.* = nxt.?;
|
||||||
|
return Thread.init(self.allocator, tptr);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
pub fn jsonStringify(self: Threads, jws: anytype) !void {
|
pub fn jsonStringify(self: Threads, jws: anytype) !void {
|
||||||
//[
|
|
||||||
// {
|
|
||||||
// "from": "The Washington Post <email@washingtonpost.com>",
|
|
||||||
// "to": "elerch@lerch.org",
|
|
||||||
// "cc": null,
|
|
||||||
// "bcc": null,
|
|
||||||
// "date": "Sun, 21 Jul 2024 19:23:38 +0000",
|
|
||||||
// "subject": "Biden steps aside",
|
|
||||||
// "content": "...content...",
|
|
||||||
// "content_type": "text/html",
|
|
||||||
// "attachments": [],
|
|
||||||
// "message_id": "01010190d6bfe4e1-185e2720-e415-4086-8865-9604cde886c2-000000@us-west-2.amazonses.com"
|
|
||||||
// }
|
|
||||||
//]
|
|
||||||
try jws.beginArray();
|
|
||||||
while (self.iterator.next()) |t| {
|
|
||||||
defer t.deinit();
|
|
||||||
try jws.beginObject();
|
|
||||||
try jws.objectField("authors");
|
|
||||||
try jws.write(t.getAuthors());
|
|
||||||
try jws.objectField("matched_messages");
|
|
||||||
try jws.write(t.getMatchedMessages());
|
|
||||||
try jws.objectField("newest_date");
|
|
||||||
try jws.write(t.getNewestDate());
|
|
||||||
try jws.objectField("oldest_date");
|
|
||||||
try jws.write(t.getOldestDate());
|
|
||||||
try jws.objectField("subject");
|
|
||||||
try jws.write(t.getSubject());
|
|
||||||
try jws.objectField("tags");
|
|
||||||
var tags = t.getTags() catch return error.OutOfMemory;
|
|
||||||
try tags.jsonStringify(jws);
|
|
||||||
try jws.objectField("thread_id");
|
|
||||||
try jws.write(t.getThreadId());
|
|
||||||
try jws.objectField("total_messages");
|
|
||||||
try jws.write(t.getTotalMessages());
|
|
||||||
try jws.endObject();
|
|
||||||
}
|
|
||||||
try jws.endArray();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// Helper function to open a notmuch database from the current directory
|
|
||||||
pub const NotmuchDb = struct {
|
|
||||||
db: notmuch.Db,
|
|
||||||
path: [:0]u8,
|
|
||||||
allocator: std.mem.Allocator,
|
|
||||||
|
|
||||||
pub fn close(self: *NotmuchDb) void {
|
|
||||||
self.db.close();
|
|
||||||
self.db.deinit();
|
|
||||||
self.allocator.free(self.path);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn searchThreads(self: *NotmuchDb, query: []const u8) !Threads {
|
|
||||||
var query_buf: [1024:0]u8 = undefined;
|
|
||||||
const query_z = try std.fmt.bufPrintZ(&query_buf, "{s}", .{query});
|
|
||||||
var thread_iter = try self.db.searchThreads(query_z);
|
|
||||||
return Threads.init(&thread_iter);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn getThread(self: *NotmuchDb, thread_id: []const u8) !Thread {
|
|
||||||
var query_buf: [1024:0]u8 = undefined;
|
|
||||||
const query_z = try std.fmt.bufPrintZ(&query_buf, "thread:{s}", .{thread_id});
|
|
||||||
var thread_iter = try self.db.searchThreads(query_z);
|
|
||||||
defer thread_iter.deinit();
|
|
||||||
|
|
||||||
var thread = thread_iter.next();
|
|
||||||
if (thread) |_| return Thread.init(&thread.?);
|
|
||||||
return error.ThreadNotFound;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
pub fn openNotmuchDb(allocator: std.mem.Allocator, relative_path: []const u8) !NotmuchDb {
|
|
||||||
var cwd_buf: [std.fs.max_path_bytes]u8 = undefined;
|
|
||||||
const cwd = try std.fs.cwd().realpath(".", cwd_buf[0..]);
|
|
||||||
const db_path = try std.fs.path.joinZ(allocator, &[_][]const u8{ cwd, relative_path });
|
|
||||||
|
|
||||||
const db = try notmuch.Db.open(db_path, null);
|
|
||||||
return .{
|
|
||||||
.db = db,
|
|
||||||
.path = db_path,
|
|
||||||
.allocator = allocator,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
test "ensure all references are observed" {
|
|
||||||
std.testing.refAllDeclsRecursive(@This());
|
|
||||||
}
|
|
||||||
|
|
||||||
test "open database with public api" {
|
|
||||||
const allocator = std.testing.allocator;
|
|
||||||
var db = try openNotmuchDb(allocator, "mail");
|
|
||||||
defer db.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
// This is the json we're looking to match on api/query/<term>
|
// This is the json we're looking to match on api/query/<term>
|
||||||
// [
|
// [
|
||||||
// {
|
// {
|
||||||
|
@ -188,23 +129,91 @@ test "open database with public api" {
|
||||||
// "total_messages": 1
|
// "total_messages": 1
|
||||||
// }
|
// }
|
||||||
// ]
|
// ]
|
||||||
//
|
try jws.beginArray();
|
||||||
// And on api/thread/<threadid>
|
while (self.iterator.next()) |t| {
|
||||||
//
|
defer t.deinit();
|
||||||
// [
|
try jws.beginObject();
|
||||||
// {
|
try jws.objectField("authors");
|
||||||
// "from": "The Washington Post <email@washingtonpost.com>",
|
try jws.write(t.getAuthors());
|
||||||
// "to": "elerch@lerch.org",
|
try jws.objectField("matched_messages");
|
||||||
// "cc": null,
|
try jws.write(t.getMatchedMessages());
|
||||||
// "bcc": null,
|
try jws.objectField("newest_date");
|
||||||
// "date": "Sun, 21 Jul 2024 19:23:38 +0000",
|
try jws.write(t.getNewestDate());
|
||||||
// "subject": "Biden steps aside",
|
try jws.objectField("oldest_date");
|
||||||
// "content": "...content...",
|
try jws.write(t.getOldestDate());
|
||||||
// "content_type": "text/html",
|
try jws.objectField("subject");
|
||||||
// "attachments": [],
|
try jws.write(t.getSubject());
|
||||||
// "message_id": "01010190d6bfe4e1-185e2720-e415-4086-8865-9604cde886c2-000000@us-west-2.amazonses.com"
|
try jws.objectField("tags");
|
||||||
// }
|
var tags = t.getTags() catch return error.OutOfMemory;
|
||||||
// ]
|
try tags.jsonStringify(jws);
|
||||||
|
try jws.objectField("thread_id");
|
||||||
|
try jws.write(t.getThreadId());
|
||||||
|
try jws.objectField("total_messages");
|
||||||
|
try jws.write(t.getTotalMessages());
|
||||||
|
try jws.endObject();
|
||||||
|
}
|
||||||
|
try jws.endArray();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Helper function to open a notmuch database from the current directory
|
||||||
|
pub const NotmuchDb = struct {
|
||||||
|
db: notmuch.Db,
|
||||||
|
path: [:0]u8,
|
||||||
|
allocator: std.mem.Allocator,
|
||||||
|
|
||||||
|
pub fn close(self: *NotmuchDb) void {
|
||||||
|
self.db.close();
|
||||||
|
self.db.deinit();
|
||||||
|
self.allocator.free(self.path);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn search(self: *NotmuchDb, query: []const u8) !Threads {
|
||||||
|
var query_buf: [1024:0]u8 = undefined;
|
||||||
|
const query_z = try std.fmt.bufPrintZ(&query_buf, "{s}", .{query});
|
||||||
|
const ti = try self.allocator.create(notmuch.Db.ThreadIterator);
|
||||||
|
ti.* = try self.db.searchThreads(query_z);
|
||||||
|
return Threads.init(self.allocator, ti);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn getThread(self: *NotmuchDb, thread_id: []const u8) !Thread {
|
||||||
|
var query_buf: [1024:0]u8 = undefined;
|
||||||
|
const query_z = try std.fmt.bufPrintZ(&query_buf, "thread:{s}", .{thread_id});
|
||||||
|
var thread_iter = try self.db.searchThreads(query_z);
|
||||||
|
defer thread_iter.deinit();
|
||||||
|
|
||||||
|
const thread = thread_iter.next();
|
||||||
|
if (thread) |t| {
|
||||||
|
const tptr = try self.allocator.create(notmuch.Db.Thread);
|
||||||
|
tptr.* = t;
|
||||||
|
return Thread.init(self.allocator, tptr);
|
||||||
|
}
|
||||||
|
return error.ThreadNotFound;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
pub fn openNotmuchDb(allocator: std.mem.Allocator, relative_path: []const u8) !NotmuchDb {
|
||||||
|
var cwd_buf: [std.fs.max_path_bytes]u8 = undefined;
|
||||||
|
const cwd = try std.fs.cwd().realpath(".", cwd_buf[0..]);
|
||||||
|
const db_path = try std.fs.path.joinZ(allocator, &[_][]const u8{ cwd, relative_path });
|
||||||
|
|
||||||
|
const db = try notmuch.Db.open(db_path, null);
|
||||||
|
return .{
|
||||||
|
.db = db,
|
||||||
|
.path = db_path,
|
||||||
|
.allocator = allocator,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
test "ensure all references are observed" {
|
||||||
|
std.testing.refAllDeclsRecursive(@This());
|
||||||
|
}
|
||||||
|
|
||||||
|
test "open database with public api" {
|
||||||
|
const allocator = std.testing.allocator;
|
||||||
|
var db = try openNotmuchDb(allocator, "mail");
|
||||||
|
defer db.close();
|
||||||
|
}
|
||||||
|
|
||||||
test "can stringify general queries" {
|
test "can stringify general queries" {
|
||||||
const allocator = std.testing.allocator;
|
const allocator = std.testing.allocator;
|
||||||
|
@ -213,25 +222,12 @@ test "can stringify general queries" {
|
||||||
// std.fs.cwd(),
|
// std.fs.cwd(),
|
||||||
// "mail",
|
// "mail",
|
||||||
// );
|
// );
|
||||||
|
var db = try openNotmuchDb(allocator, "mail");
|
||||||
// Current directory under test is root of project
|
|
||||||
var cwd_buf: [std.fs.max_path_bytes]u8 = undefined;
|
|
||||||
const cwd = try std.fs.cwd().realpath(".", cwd_buf[0..]);
|
|
||||||
var path_buf: [std.fs.max_path_bytes:0]u8 = undefined;
|
|
||||||
var fba = std.heap.FixedBufferAllocator.init(path_buf[0..]);
|
|
||||||
const db_path = try std.fs.path.joinZ(fba.allocator(), &[_][]const u8{ cwd, "mail" });
|
|
||||||
{
|
|
||||||
var status: notmuch.Status = undefined;
|
|
||||||
var db = try notmuch.Db.open(db_path, &status);
|
|
||||||
defer db.deinit();
|
|
||||||
defer db.close();
|
defer db.close();
|
||||||
defer status.deinit();
|
var threads = try db.search("Tablets");
|
||||||
var al = std.ArrayList(u8).init(allocator);
|
defer threads.deinit();
|
||||||
defer al.deinit();
|
const actual = try std.json.stringifyAlloc(allocator, threads, .{ .whitespace = .indent_2 });
|
||||||
var ti = try db.searchThreads("Tablets");
|
defer allocator.free(actual);
|
||||||
defer ti.deinit();
|
|
||||||
try std.json.stringify(Threads.init(&ti), .{ .whitespace = .indent_2 }, al.writer());
|
|
||||||
const actual = al.items;
|
|
||||||
try std.testing.expectEqualStrings(
|
try std.testing.expectEqualStrings(
|
||||||
\\[
|
\\[
|
||||||
\\ {
|
\\ {
|
||||||
|
@ -249,36 +245,21 @@ test "can stringify general queries" {
|
||||||
\\]
|
\\]
|
||||||
, actual);
|
, actual);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
test "can stringify specific threads" {
|
test "can stringify specific threads" {
|
||||||
if (true) return error.SkipZigTest;
|
|
||||||
const allocator = std.testing.allocator;
|
const allocator = std.testing.allocator;
|
||||||
// const db_path = try std.fs.path.join(
|
|
||||||
// allocator,
|
|
||||||
// std.fs.cwd(),
|
|
||||||
// "mail",
|
|
||||||
// );
|
|
||||||
|
|
||||||
// Current directory under test is root of project
|
var db = try openNotmuchDb(allocator, "mail");
|
||||||
var cwd_buf: [std.fs.max_path_bytes]u8 = undefined;
|
|
||||||
const cwd = try std.fs.cwd().realpath(".", cwd_buf[0..]);
|
|
||||||
var path_buf: [std.fs.max_path_bytes:0]u8 = undefined;
|
|
||||||
var fba = std.heap.FixedBufferAllocator.init(path_buf[0..]);
|
|
||||||
const db_path = try std.fs.path.joinZ(fba.allocator(), &[_][]const u8{ cwd, "mail" });
|
|
||||||
{
|
|
||||||
var status: notmuch.Status = undefined;
|
|
||||||
var db = try notmuch.Db.open(db_path, &status);
|
|
||||||
defer db.deinit();
|
|
||||||
defer db.close();
|
defer db.close();
|
||||||
defer status.deinit();
|
var threads = try db.search("Tablets");
|
||||||
var al = std.ArrayList(u8).init(allocator);
|
defer threads.deinit();
|
||||||
defer al.deinit();
|
|
||||||
var ti = try db.searchThreads("Tablets");
|
var maybe_first_thread = try threads.next();
|
||||||
defer ti.deinit();
|
defer if (maybe_first_thread) |*t| t.deinit();
|
||||||
var t = ti.next().?;
|
try std.testing.expect(maybe_first_thread != null);
|
||||||
try std.json.stringify(Thread.init(&t), .{ .whitespace = .indent_2 }, al.writer());
|
const first_thread = maybe_first_thread.?;
|
||||||
const actual = al.items;
|
const actual = try std.json.stringifyAlloc(allocator, first_thread, .{ .whitespace = .indent_2 });
|
||||||
|
defer allocator.free(actual);
|
||||||
try std.testing.expectEqualStrings(
|
try std.testing.expectEqualStrings(
|
||||||
\\[
|
\\[
|
||||||
\\ {
|
\\ {
|
||||||
|
@ -296,4 +277,3 @@ test "can stringify specific threads" {
|
||||||
\\]
|
\\]
|
||||||
, actual);
|
, actual);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue