Skip to content

Commit de1ffc3

Browse files
Add tests for schema inference of leading zeros and decimals
Renamed and updated the test for leading zeros to clarify that such values are parsed as integers, not strings, and added assertions for inferred types. Introduced a new test to verify that decimal values without an integer part are correctly inferred as decimals and that their scale is accurately determined.
1 parent 941f26d commit de1ffc3

1 file changed

Lines changed: 29 additions & 5 deletions

File tree

project/dbatools.Tests/Csv/CsvSchemaInferenceTest.cs

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,7 @@ public void TestInferSchema_QuotedFields()
515515
}
516516

517517
[TestMethod]
518-
public void TestInferSchema_LeadingZeros_TreatedAsString()
518+
public void TestInferSchema_LeadingZeros_ParseAsInteger()
519519
{
520520
string csvPath = Path.Combine(_tempDir, "leadingzeros.csv");
521521
File.WriteAllText(csvPath, @"ZipCode,Phone
@@ -525,10 +525,34 @@ public void TestInferSchema_LeadingZeros_TreatedAsString()
525525

526526
var columns = CsvSchemaInference.InferSchemaFromSample(csvPath);
527527

528-
// Leading zeros should preserve as integer since parsing ignores them
529-
// but this tests that we handle them gracefully
530-
Assert.IsNotNull(columns[0].SqlDataType);
531-
Assert.IsNotNull(columns[1].SqlDataType);
528+
// Leading zeros are parsed successfully by int.TryParse (01234 -> 1234)
529+
// so the column is inferred as integer. Note: the leading zeros are NOT
530+
// preserved in the parsed value. If preserving leading zeros is required,
531+
// callers should override the inferred type to varchar.
532+
Assert.AreEqual("int", columns[0].SqlDataType);
533+
Assert.AreEqual("int", columns[1].SqlDataType); // 0123456789 fits in int (< 2.1 billion)
534+
}
535+
536+
[TestMethod]
537+
public void TestInferSchema_DecimalWithNoIntegerPart()
538+
{
539+
string csvPath = Path.Combine(_tempDir, "decimalnoint.csv");
540+
File.WriteAllText(csvPath, @"Value,Tiny,Mixed
541+
.5,.001,.999
542+
.25,.002,1.5
543+
.125,.003,10.25
544+
");
545+
546+
var columns = CsvSchemaInference.InferSchemaFromSample(csvPath);
547+
548+
// Decimals without integer part (e.g., .5 instead of 0.5) should be handled
549+
Assert.IsTrue(columns[0].SqlDataType.Contains("decimal"), $"Expected decimal, got {columns[0].SqlDataType}");
550+
Assert.IsTrue(columns[1].SqlDataType.Contains("decimal"), $"Expected decimal, got {columns[1].SqlDataType}");
551+
Assert.IsTrue(columns[2].SqlDataType.Contains("decimal"), $"Expected decimal, got {columns[2].SqlDataType}");
552+
553+
// Verify scale is tracked correctly
554+
Assert.AreEqual(3, columns[0].Scale); // .5, .25, .125 -> max 3 digits after decimal
555+
Assert.AreEqual(3, columns[1].Scale); // .001, .002, .003 -> 3 digits after decimal
532556
}
533557

534558
#endregion

0 commit comments

Comments
 (0)