aboutsummaryrefslogtreecommitdiff
path: root/src/test.zig
diff options
context:
space:
mode:
authorMartin Ashby <martin@ashbysoft.com>2023-09-03 20:32:51 +0100
committerMartin Ashby <martin@ashbysoft.com>2023-09-03 20:32:51 +0100
commit2c4ac3819b8c42de1410fd524c2c9d08d937ec70 (patch)
tree570d63ee4c92d69c96c5d21bfb2be3adb35376a6 /src/test.zig
downloadsql-zig-2c4ac3819b8c42de1410fd524c2c9d08d937ec70.tar.gz
sql-zig-2c4ac3819b8c42de1410fd524c2c9d08d937ec70.tar.bz2
sql-zig-2c4ac3819b8c42de1410fd524c2c9d08d937ec70.tar.xz
sql-zig-2c4ac3819b8c42de1410fd524c2c9d08d937ec70.zip
InitialHEADmain
Diffstat (limited to 'src/test.zig')
-rw-r--r--src/test.zig102
1 files changed, 102 insertions, 0 deletions
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);
+ }
+}