aboutsummaryrefslogtreecommitdiff
path: root/src/test.zig
blob: afd726b9e1dd8e2d82a2dfa4197dc950444ce039 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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);
    }
}