summaryrefslogtreecommitdiff
path: root/sqlite.zig
diff options
context:
space:
mode:
authorGravatar Meghan Denny2021-08-23 16:04:01 -0700
committerGravatar Meghan Denny2021-08-23 16:04:01 -0700
commit6a2ab02c93a401bc53ecb00f9c7ddcf4c7912f55 (patch)
tree540001c65bf837a353076ef2fa0969dbffbeadba /sqlite.zig
parenttests- use select * when pulling into a struct (diff)
downloadzig-sqlite-6a2ab02c93a401bc53ecb00f9c7ddcf4c7912f55.tar.gz
zig-sqlite-6a2ab02c93a401bc53ecb00f9c7ddcf4c7912f55.tar.xz
zig-sqlite-6a2ab02c93a401bc53ecb00f9c7ddcf4c7912f55.zip
tests- add enum field cases
Diffstat (limited to 'sqlite.zig')
-rw-r--r--sqlite.zig43
1 files changed, 31 insertions, 12 deletions
diff --git a/sqlite.zig b/sqlite.zig
index fc15ae6..dde8ace 100644
--- a/sqlite.zig
+++ b/sqlite.zig
@@ -1260,12 +1260,30 @@ const TestUser = struct {
1260 id: usize, 1260 id: usize,
1261 age: usize, 1261 age: usize,
1262 weight: f32, 1262 weight: f32,
1263 favorite_color: Color,
1264
1265 pub const Color = enum {
1266 red,
1267 majenta,
1268 violet,
1269 indigo,
1270 blue,
1271 cyan,
1272 green,
1273 lime,
1274 yellow,
1275 //
1276 orange,
1277 //
1278
1279 pub const BaseType = []const u8;
1280 };
1263}; 1281};
1264 1282
1265const test_users = &[_]TestUser{ 1283const test_users = &[_]TestUser{
1266 .{ .name = "Vincent", .id = 20, .age = 33, .weight = 85.4 }, 1284 .{ .name = "Vincent", .id = 20, .age = 33, .weight = 85.4, .favorite_color = .violet },
1267 .{ .name = "Julien", .id = 40, .age = 35, .weight = 100.3 }, 1285 .{ .name = "Julien", .id = 40, .age = 35, .weight = 100.3, .favorite_color = .green },
1268 .{ .name = "José", .id = 60, .age = 40, .weight = 240.2 }, 1286 .{ .name = "José", .id = 60, .age = 40, .weight = 240.2, .favorite_color = .indigo },
1269}; 1287};
1270 1288
1271fn createTestTables(db: *Db) !void { 1289fn createTestTables(db: *Db) !void {
@@ -1274,10 +1292,11 @@ fn createTestTables(db: *Db) !void {
1274 "DROP TABLE IF EXISTS article", 1292 "DROP TABLE IF EXISTS article",
1275 "DROP TABLE IF EXISTS test_blob", 1293 "DROP TABLE IF EXISTS test_blob",
1276 \\CREATE TABLE user( 1294 \\CREATE TABLE user(
1277 \\ id integer PRIMARY KEY,
1278 \\ name text, 1295 \\ name text,
1296 \\ id integer PRIMARY KEY,
1279 \\ age integer, 1297 \\ age integer,
1280 \\ weight real 1298 \\ weight real,
1299 \\ favorite_color text
1281 \\) 1300 \\)
1282 , 1301 ,
1283 \\CREATE TABLE article( 1302 \\CREATE TABLE article(
@@ -1299,7 +1318,7 @@ fn addTestData(db: *Db) !void {
1299 try createTestTables(db); 1318 try createTestTables(db);
1300 1319
1301 for (test_users) |user| { 1320 for (test_users) |user| {
1302 try db.exec("INSERT INTO user(name, id, age, weight) VALUES(?{[]const u8}, ?{usize}, ?{usize}, ?{f32})", .{}, user); 1321 try db.exec("INSERT INTO user(name, id, age, weight, favorite_color) VALUES(?{[]const u8}, ?{usize}, ?{usize}, ?{f32}, ?{[]const u8})", .{}, user);
1303 1322
1304 const rows_inserted = db.rowsAffected(); 1323 const rows_inserted = db.rowsAffected();
1305 try testing.expectEqual(@as(usize, 1), rows_inserted); 1324 try testing.expectEqual(@as(usize, 1), rows_inserted);
@@ -1771,13 +1790,13 @@ test "sqlite: statement reset" {
1771 1790
1772 // Add data 1791 // Add data
1773 1792
1774 var stmt = try db.prepare("INSERT INTO user(name, id, age, weight) VALUES(?{[]const u8}, ?{usize}, ?{usize}, ?{f32})"); 1793 var stmt = try db.prepare("INSERT INTO user(name, id, age, weight, favorite_color) VALUES(?{[]const u8}, ?{usize}, ?{usize}, ?{f32}, ?{[]const u8})");
1775 defer stmt.deinit(); 1794 defer stmt.deinit();
1776 1795
1777 const users = &[_]TestUser{ 1796 const users = &[_]TestUser{
1778 .{ .id = 200, .name = "Vincent", .age = 33, .weight = 10.0 }, 1797 .{ .id = 200, .name = "Vincent", .age = 33, .weight = 10.0, .favorite_color = .violet },
1779 .{ .id = 400, .name = "Julien", .age = 35, .weight = 12.0 }, 1798 .{ .id = 400, .name = "Julien", .age = 35, .weight = 12.0, .favorite_color = .green },
1780 .{ .id = 600, .name = "José", .age = 40, .weight = 14.0 }, 1799 .{ .id = 600, .name = "José", .age = 40, .weight = 14.0, .favorite_color = .indigo },
1781 }; 1800 };
1782 1801
1783 for (users) |user| { 1802 for (users) |user| {
@@ -1801,14 +1820,14 @@ test "sqlite: statement iterator" {
1801 try db.exec("DELETE FROM user", .{}, .{}); 1820 try db.exec("DELETE FROM user", .{}, .{});
1802 1821
1803 // Add data 1822 // Add data
1804 var stmt = try db.prepare("INSERT INTO user(name, id, age, weight) VALUES(?{[]const u8}, ?{usize}, ?{usize}, ?{f32})"); 1823 var stmt = try db.prepare("INSERT INTO user(name, id, age, weight, favorite_color) VALUES(?{[]const u8}, ?{usize}, ?{usize}, ?{f32}, ?{[]const u8})");
1805 defer stmt.deinit(); 1824 defer stmt.deinit();
1806 1825
1807 var expected_rows = std.ArrayList(TestUser).init(allocator); 1826 var expected_rows = std.ArrayList(TestUser).init(allocator);
1808 var i: usize = 0; 1827 var i: usize = 0;
1809 while (i < 20) : (i += 1) { 1828 while (i < 20) : (i += 1) {
1810 const name = try std.fmt.allocPrint(allocator, "Vincent {d}", .{i}); 1829 const name = try std.fmt.allocPrint(allocator, "Vincent {d}", .{i});
1811 const user = TestUser{ .id = i, .name = name, .age = i + 200, .weight = @intToFloat(f32, i + 200) }; 1830 const user = TestUser{ .id = i, .name = name, .age = i + 200, .weight = @intToFloat(f32, i + 200), .favorite_color = .indigo };
1812 1831
1813 try expected_rows.append(user); 1832 try expected_rows.append(user);
1814 1833