test.zig (3722B)
1 const std = @import("std"); 2 const Db = @import("main.zig"); 3 const OpenError = Db.OpenError; 4 const PrepareError = Db.PrepareError; 5 const StepError = Db.StepError; 6 const ColumnError = Db.ColumnError; 7 8 const Postgres = @import("postgres.zig"); 9 const Sqlite = @import("sqlite.zig"); 10 11 const TestDbIterator = struct { 12 allocator: std.mem.Allocator, 13 pg_url: [:0]const u8, 14 sqlite_url: [:0]const u8, 15 done_postgres: bool = false, 16 done_sqlite: bool = false, 17 fn init(allocator: std.mem.Allocator, pg_url: [:0]const u8, sqlite_url: [:0]const u8) TestDbIterator { 18 return TestDbIterator{ 19 .allocator = allocator, 20 .pg_url = pg_url, 21 .sqlite_url = sqlite_url, 22 }; 23 } 24 fn next(self: *TestDbIterator) ?(OpenError!Db) { 25 if (!self.done_postgres) { 26 self.done_postgres = true; 27 return Postgres.open(self.allocator, self.pg_url); 28 } 29 if (!self.done_sqlite) { 30 self.done_sqlite = true; 31 return Sqlite.open(self.allocator, self.sqlite_url); 32 } 33 return null; 34 } 35 }; 36 37 test "open" { 38 var it = TestDbIterator.init(std.testing.allocator, "postgresql:///testdb", "testdb.db"); 39 var maybe_db = it.next(); 40 while (maybe_db != null): (maybe_db = it.next()) { 41 var db = try maybe_db.?; 42 defer db.close(); 43 } 44 } 45 46 test "open error" { 47 var it = TestDbIterator.init(std.testing.allocator, "NOPE DOPE", "floogle/flungle"); 48 var maybe_db = it.next(); 49 while (maybe_db != null): (maybe_db = it.next()) { 50 var db = maybe_db.?; 51 try std.testing.expectEqual(@as(OpenError!Db, OpenError.Failed), db); 52 } 53 } 54 55 test "query" { 56 var it = TestDbIterator.init(std.testing.allocator, "postgresql:///testdb", "testdb.db"); 57 var maybe_db = it.next(); 58 while (maybe_db != null): (maybe_db = it.next()) { 59 var db = try maybe_db.?; 60 defer db.close(); 61 try db.exec("begin"); // deliberately don't commit! 62 try db.exec("create table foo(col1 int, col2 text)"); 63 try db.exec("insert into foo(col1, col2) values(123, 'hi')"); 64 var stmt = try db.query("select col1, col2 from foo", .{}); 65 defer stmt.close(); 66 try std.testing.expect(try stmt.step()); 67 const col1 = try stmt.column(i64, 0); 68 const col2 = try stmt.column([]const u8, 1); 69 try std.testing.expectEqual(@as(?i64, 123), col1); 70 try std.testing.expectEqualStrings("hi", col2.?); 71 } 72 } 73 74 test "query null column" { 75 var it = TestDbIterator.init(std.testing.allocator, "postgresql:///testdb", "testdb.db"); 76 var maybe_db = it.next(); 77 while (maybe_db != null): (maybe_db = it.next()) { 78 var db = try maybe_db.?; 79 defer db.close(); 80 try db.exec("begin"); // deliberately don't commit! 81 try db.exec("create table foo(col1 int, col2 text)"); 82 try db.exec("insert into foo(col1, col2) values(null, null)"); 83 var stmt = try db.query("select col1, col2 from foo", .{}); 84 defer stmt.close(); 85 try std.testing.expect(try stmt.step()); 86 const col1 = try stmt.column(i64, 0); 87 const col2 = try stmt.column([]const u8, 1); 88 try std.testing.expectEqual(@as(?i64, null), col1); 89 try std.testing.expectEqual(@as(?[]const u8,null), col2); 90 } 91 } 92 93 test "exec error" { 94 var it = TestDbIterator.init(std.testing.allocator, "postgresql:///testdb", "testdb.db"); 95 var maybe_db = it.next(); 96 while (maybe_db != null): (maybe_db = it.next()) { 97 var db = try maybe_db.?; 98 defer db.close(); 99 const res = db.exec("AIN'T VALID BRO"); 100 try std.testing.expectEqual(@as(StepError!void, StepError.Failed), res); 101 } 102 }