-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpsql-tests.zig
More file actions
109 lines (95 loc) · 2.86 KB
/
psql-tests.zig
File metadata and controls
109 lines (95 loc) · 2.86 KB
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
103
104
105
106
107
108
109
/// Testing file
const psqlMethods = @import("psql.zig");
const psql = @import("psql.zig").psql;
const std = @import("std");
const connInfo = psql.connectionParams {
.user = "",
.password = "",
.host = "",
.port = 5432,
.database = "test",
};
// Struct model to create the creation of a table based on this struct
const UserTestAuto = struct {
id: i32,
name: []const u8,
age: i32,
};
test "Parameters" {
std.debug.print("Parameters\n", .{});
const id:i32 = 1;
const name:[]const u8 = "John";
const age:i32 = 30;
var params = try psql.QParams(.{id, name, age});
std.debug.print("{s}\n", .{params.paramString});
defer params.deinit();
}
test "Unknown type paramters" {
const id = 1;
const name = "John";
const money = 100.2;
var params = try psql.QParams(.{id, name, money});
std.debug.print("{s}\n", .{params.paramString});
defer params.deinit();
}
test "Sql Injection" {
const name = "'; DROP TABLE users; --";
var params = try psql.QParams(.{name});
std.debug.print("{s}\n", .{params.paramString});
defer params.deinit();
}
test "Try Connection" {
std.debug.print("Try Connection\n", .{});
var conn = try psql.init(connInfo);
try std.testing.expect(conn.connectionStatus == psql.connectionType.OK);
conn.close();
}
test "Try Query" {
std.debug.print("Try Query\n", .{});
var conn = try psql.init(connInfo);
const query = "SELECT * FROM users";
_ = try conn.execQuery(query);
conn.close();
}
test "Try select" {
var conn = try psql.init(connInfo);
const table = "users";
var result = try conn.select(table);
for (result.rows.items) |row| {
std.debug.print("{s}\n", .{row});
}
for (result.columns.items) |column| {
std.debug.print("{s}\n", .{column});
}
defer result.deinit();
// Assuming I want to get the first row and the first column
std.debug.print("{s}\n", .{result.rows.items[0][0]});
conn.close();
}
test "Try insert" {
var conn = try psql.init(connInfo);
const table = "tests";
// This will return an error for duplicate key value
const id:i32 = 2;
const name = "John Doe";
var params = try psql.QParams(.{id,name});
defer params.deinit();
try std.testing.expectError(psql.Errors.PrimaryKeyDuplicate, conn.insert(table, params.paramString));
conn.close();
}
test "Try inner join" {
var conn = try psql.init(connInfo);
const mainTable = "userstest";
const joinTable = "posts";
const joinValue = "id";
const columns = "userstest.id, userstest.name, posts.post";
var res = try conn.selectJoin(mainTable, joinTable, joinValue, columns);
defer res.deinit();
conn.printQueryResult(res);
conn.close();
}
test "Try creation of table" {
var conn = try psql.init(connInfo);
try conn.createTableFor(UserTestAuto);
conn.close();
}