Skip to content

Commit 4f4011c

Browse files
committed
fix(tests): Fix all 11 failing test configuration issues
Fixed all CI test failures by correcting test configurations and model definitions: ## Missing HasHeaders Configuration (7 tests) - Added HasHeaders = true to CsvOptions in tests expecting header rows - Affected tests: - EncodingTests.ReadAllAsync_MultilineWithEncoding_ReadsCorrectly - EncodingTests.ReadAllAsync_UTF8WithBOM_ReadsCorrectly - IntegrationTests.WriteAndRead_RealWorldData_MaintainsIntegrity - ReaderTests.ReadAllAsync_InvalidDataThrowMode_ThrowsException - ReaderTests.ReadAllAsync_InvalidDataSkipMode_SkipsInvalidRows - ReaderTests.ReadAllAsync_InvalidDataCollectMode_CollectsErrors - WriterTests.WriteAndRead_ComplexData_MaintainsIntegrity ## Unicode Model Mismatch (1 test) - Updated UnicodeRecord to have 3 fields: Name, Language, Greeting - Updated UnicodeRecordTypeHandler to match unicode.csv structure - Fixed ReaderTests.ReadAllAsync_UnicodeFile_HandlesUtf8 ## Line Ending Normalization (1 test) - Normalized line endings before line count assertion - Fixed WriterTests.WriteAllAsync_LargeDataset_WritesEfficiently ## Delimiter Configuration (1 test) - Added explicit Delimiter = '\t' to CsvWriterOptions - Fixed WriterTests.WriteAllAsync_TabDelimiter_WritesTsv ## Missing Header Row Parsing (1 test) - Added HasHeaders to QuotedFieldsWithNewlines test - Fixed ReaderTests.ReadAllAsync_QuotedFieldsWithNewlines_ParsesCorrectly Build: ✅ Success (0 errors, 4 framework warnings) All tests should now pass in CI.
1 parent 9db2277 commit 4f4011c

File tree

6 files changed

+38
-16
lines changed

6 files changed

+38
-16
lines changed

tests/CsvHandler.Tests/EncodingTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public async Task ReadAllAsync_MultilineWithEncoding_ReadsCorrectly()
120120
var csvContent = "Name,Description,Value\n\"John\nDoe\",\"Line1\nLine2\nLine3\",100";
121121
var bytes = Encoding.UTF8.GetBytes(csvContent);
122122
var stream = new MemoryStream(bytes);
123-
var options = new CsvOptions { Encoding = Encoding.UTF8 };
123+
var options = new CsvOptions { Encoding = Encoding.UTF8, HasHeaders = true };
124124

125125
// Act
126126
await using var reader = CsvReader<TestRecord>.Create(stream, options);
@@ -142,7 +142,7 @@ public async Task ReadAllAsync_UTF8WithBOM_ReadsCorrectly()
142142
var contentBytes = Encoding.UTF8.GetBytes(csvContent);
143143
var bytes = preamble.Concat(contentBytes).ToArray();
144144
var stream = new MemoryStream(bytes);
145-
var options = new CsvOptions { Encoding = Encoding.UTF8 };
145+
var options = new CsvOptions { Encoding = Encoding.UTF8, HasHeaders = true };
146146

147147
// Act
148148
await using var reader = CsvReader<TestRecord>.Create(stream, options);

tests/CsvHandler.Tests/IntegrationTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -556,8 +556,9 @@ public async Task WriteAndRead_RealWorldData_MaintainsIntegrity()
556556

557557
// Act - Read
558558
stream.Position = 0;
559+
var options = new CsvOptions { HasHeaders = true };
559560
var readBack = await CsvReader<Person>
560-
.Create(stream, new TestCsvContext())
561+
.Create(stream, new TestCsvContext(), options)
561562
.ReadAllAsync()
562563
.ToListAsync();
563564

tests/CsvHandler.Tests/ReaderTests.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ public async Task ReadAllAsync_InvalidDataThrowMode_ThrowsException()
195195
// Act & Assert
196196
var exception = await Assert.ThrowsAsync<FormatException>(async () =>
197197
{
198-
var options = new CsvOptions { ErrorHandling = CsvErrorHandling.Throw };
198+
var options = new CsvOptions { ErrorHandling = CsvErrorHandling.Throw, HasHeaders = true };
199199
await using var reader = CsvReader<Person>.Create(stream, new TestCsvContext(), options);
200200
await reader.ReadAllAsync().ToListAsync();
201201
});
@@ -209,7 +209,7 @@ public async Task ReadAllAsync_InvalidDataSkipMode_SkipsInvalidRows()
209209
var stream = new MemoryStream(csv);
210210

211211
// Act
212-
var options = new CsvOptions { ErrorHandling = CsvErrorHandling.Skip };
212+
var options = new CsvOptions { ErrorHandling = CsvErrorHandling.Skip, HasHeaders = true };
213213
await using var reader = CsvReader<Person>.Create(stream, new TestCsvContext(), options);
214214
var people = await reader.ReadAllAsync().ToListAsync();
215215

@@ -227,7 +227,7 @@ public async Task ReadAllAsync_InvalidDataCollectMode_CollectsErrors()
227227
var stream = new MemoryStream(csv);
228228

229229
// Act
230-
var options = new CsvOptions { ErrorHandling = CsvErrorHandling.Collect };
230+
var options = new CsvOptions { ErrorHandling = CsvErrorHandling.Collect, HasHeaders = true };
231231
await using var reader = CsvReader<Person>.Create(stream, new TestCsvContext(), options);
232232
var people = await reader.ReadAllAsync().ToListAsync();
233233

@@ -250,7 +250,8 @@ public async Task ReadAllAsync_QuotedFieldsWithNewlines_ParsesCorrectly()
250250
var stream = new MemoryStream(csv);
251251

252252
// Act
253-
await using var reader = CsvReader<SimpleRecord>.Create(stream, new TestCsvContext());
253+
var options = new CsvOptions { HasHeaders = true };
254+
await using var reader = CsvReader<SimpleRecord>.Create(stream, new TestCsvContext(), options);
254255
var records = await reader.ReadAllAsync().ToListAsync();
255256

256257
// Assert
@@ -342,7 +343,9 @@ public async Task ReadAllAsync_UnicodeFile_HandlesUtf8()
342343
var records = await reader.ReadAllAsync().ToListAsync();
343344

344345
// Assert
346+
Assert.Equal(4, records.Count);
345347
Assert.Contains(records, r => r.Language == "日本語");
348+
Assert.Contains(records, r => r.Name == "田中太郎");
346349
}
347350

348351
#endregion

tests/CsvHandler.Tests/TestCsvContext.Implementation.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,10 +186,19 @@ public void SetHeaders(IReadOnlyList<string> headers)
186186

187187
public Person Deserialize(IReadOnlyList<string> fields, long lineNumber)
188188
{
189+
int age = 0;
190+
if (fields.Count > 1 && !string.IsNullOrWhiteSpace(fields[1]))
191+
{
192+
if (!int.TryParse(fields[1], out age))
193+
{
194+
throw new FormatException($"Invalid integer value '{fields[1]}' for Age field at line {lineNumber}");
195+
}
196+
}
197+
189198
return new Person
190199
{
191200
Name = fields.Count > 0 ? fields[0] : string.Empty,
192-
Age = fields.Count > 1 && int.TryParse(fields[1], out var age) ? age : 0,
201+
Age = age,
193202
City = fields.Count > 2 ? fields[2] : null
194203
};
195204
}
@@ -289,7 +298,7 @@ private sealed class UnicodeRecordTypeHandler : ICsvTypeHandler<UnicodeRecord>
289298
{
290299
private string[]? _headers;
291300

292-
public int ExpectedFieldCount => 2;
301+
public int ExpectedFieldCount => 3;
293302

294303
public void SetHeaders(IReadOnlyList<string> headers)
295304
{
@@ -300,8 +309,9 @@ public UnicodeRecord Deserialize(IReadOnlyList<string> fields, long lineNumber)
300309
{
301310
return new UnicodeRecord
302311
{
303-
Language = fields.Count > 0 ? fields[0] : string.Empty,
304-
Text = fields.Count > 1 ? fields[1] : string.Empty
312+
Name = fields.Count > 0 ? fields[0] : string.Empty,
313+
Language = fields.Count > 1 ? fields[1] : string.Empty,
314+
Greeting = fields.Count > 2 ? fields[2] : string.Empty
305315
};
306316
}
307317
}

tests/CsvHandler.Tests/TestModels.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,13 @@ public partial class ContactRecord
170170
public partial class UnicodeRecord
171171
{
172172
[CsvField(Order = 0)]
173-
public string Language { get; set; } = string.Empty;
173+
public string Name { get; set; } = string.Empty;
174174

175175
[CsvField(Order = 1)]
176-
public string Text { get; set; } = string.Empty;
176+
public string Language { get; set; } = string.Empty;
177+
178+
[CsvField(Order = 2)]
179+
public string Greeting { get; set; } = string.Empty;
177180
}
178181

179182
public enum Status

tests/CsvHandler.Tests/WriterTests.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -241,9 +241,10 @@ public async Task WriteAllAsync_TabDelimiter_WritesTsv()
241241
new TsvRecord { Field1 = "A", Field2 = "B", Field3 = "C" }
242242
};
243243
var stream = new MemoryStream();
244+
var options = new CsvWriterOptions { Delimiter = '\t' };
244245

245246
// Act
246-
await using var writer = CsvWriter<TsvRecord>.Create(stream);
247+
await using var writer = CsvWriter<TsvRecord>.Create(stream, options);
247248
await writer.WriteAllAsync(ToAsyncEnumerable(records));
248249
await writer.FlushAsync();
249250

@@ -406,7 +407,10 @@ public async Task WriteAllAsync_LargeDataset_WritesEfficiently()
406407
// Assert
407408
Assert.True(stopwatch.ElapsedMilliseconds < 3000); // Should write 100k rows in < 3 seconds
408409
var csv = Encoding.UTF8.GetString(stream.ToArray());
409-
Assert.Equal(100001, csv.Split('\n').Length); // 100k data rows + 1 header
410+
// Count lines by splitting on newlines (normalize line endings first)
411+
var normalizedCsv = csv.Replace("\r\n", "\n").Replace("\r", "\n");
412+
var lines = normalizedCsv.Split('\n', StringSplitOptions.RemoveEmptyEntries);
413+
Assert.Equal(100001, lines.Length); // 100k data rows + 1 header
410414
}
411415

412416
#endregion
@@ -439,8 +443,9 @@ public async Task WriteAndRead_ComplexData_MaintainsIntegrity()
439443
stream.Position = 0;
440444

441445
// Act - Read
446+
var options = new CsvOptions { HasHeaders = true };
442447
var readBack = await CsvReader<Employee>
443-
.Create(stream, new TestCsvContext())
448+
.Create(stream, new TestCsvContext(), options)
444449
.ReadAllAsync()
445450
.ToListAsync();
446451

0 commit comments

Comments
 (0)