|
| 1 | +const std = @import("std"); |
| 2 | +const csv = @import("zig_csv"); |
| 3 | +const expect = std.testing.expect; |
| 4 | +const allocator = std.testing.allocator; |
| 5 | +const StructuredTable = csv.StructuredTable; |
| 6 | + |
| 7 | +test "StructuredTable: Parse CSV into struct and access rows" { |
| 8 | + const DogTable = struct { |
| 9 | + name: []const u8, |
| 10 | + age: u8, |
| 11 | + alive: bool, |
| 12 | + foo: f32, |
| 13 | + }; |
| 14 | + |
| 15 | + var table = StructuredTable(DogTable).init(allocator, csv.Settings.default()); |
| 16 | + defer table.deinit(); |
| 17 | + try table.parse( |
| 18 | + \\name,age,alive,foo |
| 19 | + \\Fido,4,Yes,0.3 |
| 20 | + \\Rex,7,0,0.11 |
| 21 | + ); |
| 22 | + |
| 23 | + try expect(table.getRowCount() == 2); |
| 24 | + |
| 25 | + const row_1 = try table.getRow(0); |
| 26 | + const row_1_value = row_1.ok.value; |
| 27 | + const row_2 = try table.getRow(1); |
| 28 | + const row_2_value = row_2.ok.value; |
| 29 | + try expect(table.getRow(2) == csv.TableError.RowNotFound); |
| 30 | + |
| 31 | + try expect(std.mem.eql(u8, row_1_value.name, "Fido")); |
| 32 | + try expect(std.mem.eql(u8, row_2_value.name, "Rex")); |
| 33 | + try expect(row_1_value.age == 4); |
| 34 | + try expect(row_2_value.age == 7); |
| 35 | + try expect(row_1_value.alive); |
| 36 | + try expect(!row_2_value.alive); |
| 37 | + try expect(row_1_value.foo == 0.3); |
| 38 | + try expect(row_2_value.foo == 0.11); |
| 39 | +} |
| 40 | + |
| 41 | +test "StructuredTable: Edit struct row and export to CSV" { |
| 42 | + const DogTable = struct { |
| 43 | + name: []const u8, |
| 44 | + age: u8, |
| 45 | + alive: bool, |
| 46 | + foo: f32, |
| 47 | + }; |
| 48 | + |
| 49 | + var table = StructuredTable(DogTable).init(allocator, csv.Settings.default()); |
| 50 | + defer table.deinit(); |
| 51 | + try table.parse( |
| 52 | + \\name,age,alive,foo |
| 53 | + \\Fido,4,true,0.3 |
| 54 | + \\Rex,7,false,0.11 |
| 55 | + ); |
| 56 | + |
| 57 | + try expect(table.getRowCount() == 2); |
| 58 | + |
| 59 | + const row = try table.getRow(0); |
| 60 | + var value = row.ok.value; |
| 61 | + try expect(std.mem.eql(u8, value.name, "Fido")); |
| 62 | + |
| 63 | + value.name = "Berta"; |
| 64 | + _ = try table.editRow(0, value); |
| 65 | + |
| 66 | + const exported_csv = try table.exportCSV(allocator); |
| 67 | + defer allocator.free(exported_csv); |
| 68 | + const expected_csv = |
| 69 | + \\name,age,alive,foo |
| 70 | + \\Berta,4,true,0.3 |
| 71 | + \\Rex,7,false,0.11 |
| 72 | + ; |
| 73 | + try expect(std.mem.eql(u8, exported_csv, expected_csv)); |
| 74 | +} |
| 75 | + |
| 76 | +test "StructuredTable: Delete struct row" { |
| 77 | + const DogTable = struct { |
| 78 | + name: []const u8, |
| 79 | + age: u8, |
| 80 | + alive: bool, |
| 81 | + foo: f32, |
| 82 | + }; |
| 83 | + |
| 84 | + var table = StructuredTable(DogTable).init(allocator, csv.Settings.default()); |
| 85 | + defer table.deinit(); |
| 86 | + try table.parse( |
| 87 | + \\name,age,alive,foo |
| 88 | + \\Fido,4,true,0.3 |
| 89 | + \\Rex,7,false,0.11 |
| 90 | + ); |
| 91 | + |
| 92 | + try expect(table.getRowCount() == 2); |
| 93 | + |
| 94 | + try table.deleteRow(0); |
| 95 | + try expect(table.getRowCount() == 1); |
| 96 | + |
| 97 | + const exported_csv = try table.exportCSV(allocator); |
| 98 | + defer allocator.free(exported_csv); |
| 99 | + const expected_csv = |
| 100 | + \\name,age,alive,foo |
| 101 | + \\Rex,7,false,0.11 |
| 102 | + ; |
| 103 | + try expect(std.mem.eql(u8, exported_csv, expected_csv)); |
| 104 | +} |
| 105 | + |
| 106 | +test "StructuredTable: Create empty struct table and insert rows" { |
| 107 | + const DogTable = struct { |
| 108 | + name: []const u8, |
| 109 | + age: u8, |
| 110 | + alive: bool, |
| 111 | + foo: f32, |
| 112 | + }; |
| 113 | + |
| 114 | + var table = StructuredTable(DogTable).init(allocator, csv.Settings.default()); |
| 115 | + defer table.deinit(); |
| 116 | + |
| 117 | + const new_row_1 = DogTable{ |
| 118 | + .name = "Buddy", |
| 119 | + .age = 3, |
| 120 | + .alive = true, |
| 121 | + .foo = 0.5, |
| 122 | + }; |
| 123 | + _ = try table.insertRow(null, new_row_1); |
| 124 | + |
| 125 | + const new_row_2 = DogTable{ |
| 126 | + .name = "Max", |
| 127 | + .age = 5, |
| 128 | + .alive = false, |
| 129 | + .foo = 0.2, |
| 130 | + }; |
| 131 | + _ = try table.insertRow(null, new_row_2); |
| 132 | + |
| 133 | + try expect(table.getRowCount() == 2); |
| 134 | + |
| 135 | + const exported_csv = try table.exportCSV(allocator); |
| 136 | + defer allocator.free(exported_csv); |
| 137 | + const expected_csv = |
| 138 | + \\name,age,alive,foo |
| 139 | + \\Buddy,3,true,0.5 |
| 140 | + \\Max,5,false,0.2 |
| 141 | + ; |
| 142 | + try expect(std.mem.eql(u8, exported_csv, expected_csv)); |
| 143 | +} |
| 144 | + |
| 145 | +test "StructuredTable: Delete row" { |
| 146 | + const DogTable = struct { |
| 147 | + name: []const u8, |
| 148 | + age: u8, |
| 149 | + alive: bool, |
| 150 | + foo: f32, |
| 151 | + }; |
| 152 | + |
| 153 | + var table = StructuredTable(DogTable).init(allocator, csv.Settings.default()); |
| 154 | + defer table.deinit(); |
| 155 | + try table.parse( |
| 156 | + \\name,age,alive,foo |
| 157 | + \\Fido,4,true,0.3 |
| 158 | + \\Rex,7,false,0.11 |
| 159 | + ); |
| 160 | + |
| 161 | + try expect(table.getRowCount() == 2); |
| 162 | + |
| 163 | + try table.deleteRow(0); |
| 164 | + try expect(table.getRowCount() == 1); |
| 165 | + |
| 166 | + const exported_csv = try table.exportCSV(allocator); |
| 167 | + defer allocator.free(exported_csv); |
| 168 | + const expected_csv = |
| 169 | + \\name,age,alive,foo |
| 170 | + \\Rex,7,false,0.11 |
| 171 | + ; |
| 172 | + try expect(std.mem.eql(u8, exported_csv, expected_csv)); |
| 173 | +} |
| 174 | + |
| 175 | +test "StructureTable: Insert row at specific index" { |
| 176 | + const DogTable = struct { |
| 177 | + name: []const u8, |
| 178 | + age: u8, |
| 179 | + alive: bool, |
| 180 | + foo: f32, |
| 181 | + }; |
| 182 | + |
| 183 | + var table = StructuredTable(DogTable).init(allocator, csv.Settings.default()); |
| 184 | + defer table.deinit(); |
| 185 | + try table.parse( |
| 186 | + \\name,age,alive,foo |
| 187 | + \\Fido,4,true,0.3 |
| 188 | + \\Rex,7,false,0.11 |
| 189 | + ); |
| 190 | + |
| 191 | + const new_row = DogTable{ |
| 192 | + .name = "Buddy", |
| 193 | + .age = 3, |
| 194 | + .alive = true, |
| 195 | + .foo = 0.5, |
| 196 | + }; |
| 197 | + _ = try table.insertRow(1, new_row); |
| 198 | + |
| 199 | + try expect(table.getRowCount() == 3); |
| 200 | + |
| 201 | + const exported_csv = try table.exportCSV(allocator); |
| 202 | + defer allocator.free(exported_csv); |
| 203 | + const expected_csv = |
| 204 | + \\name,age,alive,foo |
| 205 | + \\Fido,4,true,0.3 |
| 206 | + \\Buddy,3,true,0.5 |
| 207 | + \\Rex,7,false,0.11 |
| 208 | + ; |
| 209 | + try expect(std.mem.eql(u8, exported_csv, expected_csv)); |
| 210 | +} |
| 211 | + |
| 212 | +test "StructuredTable: Handle parsing error due to invalid csv type" { |
| 213 | + const DogTable = struct { |
| 214 | + name: []const u8, |
| 215 | + age: u8, |
| 216 | + alive: bool, |
| 217 | + foo: f32, |
| 218 | + }; |
| 219 | + |
| 220 | + var table = StructuredTable(DogTable).init(allocator, csv.Settings.default()); |
| 221 | + defer table.deinit(); |
| 222 | + try table.parse( |
| 223 | + \\name,age,alive,foo |
| 224 | + \\Fido,invalid_age,true,0.3 |
| 225 | + ); |
| 226 | + const result = try table.getRow(0); |
| 227 | + const err = result.@"error"; |
| 228 | + try expect(err.kind == csv.StructureError.UnexpectedType); |
| 229 | + try expect(std.mem.eql(u8, err.csv_value.?, "invalid_age")); |
| 230 | + try expect(std.mem.eql(u8, err.field_name.?, "age")); |
| 231 | + try expect(std.mem.eql(u8, err.field_type.?, "u8")); |
| 232 | +} |
0 commit comments