|
1 | 1 | using CsvHelper;
|
2 |
| -using CsvHelper.Configuration; |
| 2 | +using MiniExcelLibs.Attributes; |
| 3 | +using MiniExcelLibs.Exceptions; |
3 | 4 | using MiniExcelLibs.Tests.Utils;
|
4 | 5 | using System;
|
5 | 6 | using System.Collections.Generic;
|
@@ -207,6 +208,13 @@ public class Test
|
207 | 208 | public string c1 { get; set; }
|
208 | 209 | public string c2 { get; set; }
|
209 | 210 | }
|
| 211 | + public class TestWithAlias |
| 212 | + { |
| 213 | + [ExcelColumnName(excelColumnName: "c1", aliases: new[] { "column1", "col1" })] |
| 214 | + public string c1 { get; set; } |
| 215 | + [ExcelColumnName(excelColumnName: "c2", aliases: new[] { "column2", "col2" })] |
| 216 | + public string c2 { get; set; } |
| 217 | + } |
210 | 218 |
|
211 | 219 | [Fact]
|
212 | 220 | public void CsvExcelTypeTest()
|
@@ -294,6 +302,63 @@ public void CsvTypeMappingTest()
|
294 | 302 | Assert.Equal("A2", rows[1].c1);
|
295 | 303 | Assert.Equal("B2", rows[1].c2);
|
296 | 304 | }
|
| 305 | + } |
| 306 | + |
| 307 | + [Fact()] |
| 308 | + public void CsvColumnNotFoundTest() |
| 309 | + { |
| 310 | + var path = Path.Combine(Path.GetTempPath(), $"{Guid.NewGuid().ToString()}.csv"); |
| 311 | + File.WriteAllLines(path, new[] { "c1,c2", "v1" }); |
| 312 | + |
| 313 | + using (var stream = File.OpenRead(path)) |
| 314 | + { |
| 315 | + var exception = Assert.Throws<ExcelColumnNotFoundException>(() => stream.Query<Test>(excelType: ExcelType.CSV).ToList()); |
| 316 | + |
| 317 | + Assert.Equal("c2", exception.ColumnName); |
| 318 | + Assert.Equal(2, exception.RowIndex); |
| 319 | + Assert.Null(exception.ColumnIndex); |
| 320 | + Assert.True(exception.RowValues is IDictionary<string, object>); |
| 321 | + Assert.Equal(1, ((IDictionary<string, object>)exception.RowValues).Count); |
| 322 | + } |
| 323 | + |
| 324 | + { |
| 325 | + var exception = Assert.Throws<ExcelColumnNotFoundException>(() => MiniExcel.Query<Test>(path, excelType: ExcelType.CSV).ToList()); |
| 326 | + |
| 327 | + Assert.Equal("c2", exception.ColumnName); |
| 328 | + Assert.Equal(2, exception.RowIndex); |
| 329 | + Assert.Null(exception.ColumnIndex); |
| 330 | + Assert.True(exception.RowValues is IDictionary<string, object>); |
| 331 | + Assert.Equal(1, ((IDictionary<string, object>)exception.RowValues).Count); |
| 332 | + } |
| 333 | + |
| 334 | + File.Delete(path); |
| 335 | + } |
| 336 | + |
| 337 | + [Fact()] |
| 338 | + public void CsvColumnNotFoundWithAliasTest() |
| 339 | + { |
| 340 | + var path = Path.Combine(Path.GetTempPath(), $"{Guid.NewGuid().ToString()}.csv"); |
| 341 | + File.WriteAllLines(path, new[] { "col1,col2", "v1" }); |
| 342 | + using (var stream = File.OpenRead(path)) |
| 343 | + { |
| 344 | + var exception = Assert.Throws<ExcelColumnNotFoundException>(() => stream.Query<TestWithAlias>(excelType: ExcelType.CSV).ToList()); |
| 345 | + |
| 346 | + Assert.Equal("c2", exception.ColumnName); |
| 347 | + Assert.Equal(2, exception.RowIndex); |
| 348 | + Assert.Null(exception.ColumnIndex); |
| 349 | + Assert.True(exception.RowValues is IDictionary<string, object>); |
| 350 | + Assert.Equal(1, ((IDictionary<string, object>)exception.RowValues).Count); |
| 351 | + } |
| 352 | + |
| 353 | + { |
| 354 | + var exception = Assert.Throws<ExcelColumnNotFoundException>(() => MiniExcel.Query<TestWithAlias>(path, excelType: ExcelType.CSV).ToList()); |
| 355 | + |
| 356 | + Assert.Equal("c2", exception.ColumnName); |
| 357 | + Assert.Equal(2, exception.RowIndex); |
| 358 | + Assert.Null(exception.ColumnIndex); |
| 359 | + Assert.True(exception.RowValues is IDictionary<string, object>); |
| 360 | + Assert.Equal(1, ((IDictionary<string, object>)exception.RowValues).Count); |
| 361 | + } |
297 | 362 |
|
298 | 363 | File.Delete(path);
|
299 | 364 | }
|
|
0 commit comments