From 2c4ac3819b8c42de1410fd524c2c9d08d937ec70 Mon Sep 17 00:00:00 2001 From: Martin Ashby Date: Sun, 3 Sep 2023 20:32:51 +0100 Subject: Initial --- src/test.zig | 102 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 src/test.zig (limited to 'src/test.zig') diff --git a/src/test.zig b/src/test.zig new file mode 100644 index 0000000..afd726b --- /dev/null +++ b/src/test.zig @@ -0,0 +1,102 @@ +const std = @import("std"); +const Db = @import("main.zig"); +const OpenError = Db.OpenError; +const PrepareError = Db.PrepareError; +const StepError = Db.StepError; +const ColumnError = Db.ColumnError; + +const Postgres = @import("postgres.zig"); +const Sqlite = @import("sqlite.zig"); + +const TestDbIterator = struct { + allocator: std.mem.Allocator, + pg_url: [:0]const u8, + sqlite_url: [:0]const u8, + done_postgres: bool = false, + done_sqlite: bool = false, + fn init(allocator: std.mem.Allocator, pg_url: [:0]const u8, sqlite_url: [:0]const u8) TestDbIterator { + return TestDbIterator{ + .allocator = allocator, + .pg_url = pg_url, + .sqlite_url = sqlite_url, + }; + } + fn next(self: *TestDbIterator) ?(OpenError!Db) { + if (!self.done_postgres) { + self.done_postgres = true; + return Postgres.open(self.allocator, self.pg_url); + } + if (!self.done_sqlite) { + self.done_sqlite = true; + return Sqlite.open(self.allocator, self.sqlite_url); + } + return null; + } +}; + +test "open" { + var it = TestDbIterator.init(std.testing.allocator, "postgresql:///testdb", "testdb.db"); + var maybe_db = it.next(); + while (maybe_db != null): (maybe_db = it.next()) { + var db = try maybe_db.?; + defer db.close(); + } +} + +test "open error" { + var it = TestDbIterator.init(std.testing.allocator, "NOPE DOPE", "floogle/flungle"); + var maybe_db = it.next(); + while (maybe_db != null): (maybe_db = it.next()) { + var db = maybe_db.?; + try std.testing.expectEqual(@as(OpenError!Db, OpenError.Failed), db); + } +} + +test "query" { + var it = TestDbIterator.init(std.testing.allocator, "postgresql:///testdb", "testdb.db"); + var maybe_db = it.next(); + while (maybe_db != null): (maybe_db = it.next()) { + var db = try maybe_db.?; + defer db.close(); + try db.exec("begin"); // deliberately don't commit! + try db.exec("create table foo(col1 int, col2 text)"); + try db.exec("insert into foo(col1, col2) values(123, 'hi')"); + var stmt = try db.query("select col1, col2 from foo", .{}); + defer stmt.close(); + try std.testing.expect(try stmt.step()); + const col1 = try stmt.column(i64, 0); + const col2 = try stmt.column([]const u8, 1); + try std.testing.expectEqual(@as(?i64, 123), col1); + try std.testing.expectEqualStrings("hi", col2.?); + } +} + +test "query null column" { + var it = TestDbIterator.init(std.testing.allocator, "postgresql:///testdb", "testdb.db"); + var maybe_db = it.next(); + while (maybe_db != null): (maybe_db = it.next()) { + var db = try maybe_db.?; + defer db.close(); + try db.exec("begin"); // deliberately don't commit! + try db.exec("create table foo(col1 int, col2 text)"); + try db.exec("insert into foo(col1, col2) values(null, null)"); + var stmt = try db.query("select col1, col2 from foo", .{}); + defer stmt.close(); + try std.testing.expect(try stmt.step()); + const col1 = try stmt.column(i64, 0); + const col2 = try stmt.column([]const u8, 1); + try std.testing.expectEqual(@as(?i64, null), col1); + try std.testing.expectEqual(@as(?[]const u8,null), col2); + } +} + +test "exec error" { + var it = TestDbIterator.init(std.testing.allocator, "postgresql:///testdb", "testdb.db"); + var maybe_db = it.next(); + while (maybe_db != null): (maybe_db = it.next()) { + var db = try maybe_db.?; + defer db.close(); + const res = db.exec("AIN'T VALID BRO"); + try std.testing.expectEqual(@as(StepError!void, StepError.Failed), res); + } +} -- cgit v1.2.3-ZIG