Repository navigation
Expand file tree
/
Copy pathPowerShellDataTableConverterTests.cs
More file actions
113 lines (95 loc) · 4.09 KB
/
Copy pathPowerShellDataTableConverterTests.cs
File metadata and controls
113 lines (95 loc) · 4.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using System.Data;
using DBAClientX.PowerShell;
namespace DbaClientX.Tests;
public class PowerShellDataTableConverterTests
{
[Fact]
public void ToDataTable_InputBatch_RejectsReusingCaseDistinctSchemaColumn()
{
using var schema = new DataTable();
schema.Columns.Add("Name", typeof(string));
schema.Columns.Add("name", typeof(string));
using var source = new DataTable();
source.Columns.Add("Name", typeof(string));
source.Columns.Add("Extra", typeof(string));
source.Rows.Add("kept", "must not disappear");
Assert.Throws<ArgumentException>(() => PowerShellDataTableConverter.ToDataTable(
new object?[] { source.Rows[0] }, "Records", schema));
}
[Theory]
[InlineData(false)]
[InlineData(true)]
public void ToDataTable_MixedDateTimeModes_RejectsIncompatibleSchema(bool rowViews)
{
using var a = new DataTable();
using var b = new DataTable();
a.Columns.Add("Value", typeof(DateTime)).DateTimeMode = DataSetDateTime.Utc;
b.Columns.Add("Value", typeof(DateTime)).DateTimeMode = DataSetDateTime.Local;
a.Rows.Add(new DateTime(2026, 9, 27, 12, 0, 0));
b.Rows.Add(new DateTime(2026, 9, 27, 13, 0, 0));
var inputs = rowViews ? new object?[] { a.DefaultView[0], b.DefaultView[0] } : new object?[] { a.Rows[0], b.Rows[0] };
Assert.Throws<ArgumentException>(() => PowerShellDataTableConverter.ToDataTable(inputs));
}
[Fact]
public void ToDataTable_ExpandsSingleEnumerableInput()
{
var rows = new[]
{
new ConverterRow { Id = 1, Name = "Alpha" },
new ConverterRow { Id = 2, Name = "Beta" }
};
var table = PowerShellDataTableConverter.ToDataTable(new object?[] { rows }, "Records");
Assert.Equal(typeof(int), table.Columns["Id"]!.DataType);
Assert.Equal(typeof(string), table.Columns["Name"]!.DataType);
Assert.Equal(2, table.Rows.Count);
Assert.Equal("Beta", table.Rows[1]["Name"]);
}
[Fact]
public void ToDataTable_DiscoversColumnsAcrossDictionaryRows()
{
var table = PowerShellDataTableConverter.ToDataTable(new object?[]
{
new Dictionary<string, object?>(StringComparer.Ordinal) { ["Name"] = "Alpha" },
new Dictionary<string, object?>(StringComparer.OrdinalIgnoreCase)
{
["name"] = "Beta",
["Age"] = 42
}
}, "Records");
Assert.Equal(new[] { "Name", "Age" }, table.Columns.Cast<DataColumn>().Select(column => column.ColumnName));
Assert.Equal("Beta", table.Rows[1]["Name"]);
Assert.Equal(42, table.Rows[1]["Age"]);
}
[Fact]
public void ToDataTable_ReturnsDirectDataTableInput()
{
var source = new DataTable("Source");
source.Columns.Add("Name", typeof(string));
source.Rows.Add("Alpha");
var table = PowerShellDataTableConverter.ToDataTable(new object?[] { source }, "Records");
Assert.Same(source, table);
Assert.Equal("Alpha", table.Rows[0]["Name"]);
}
[Fact]
public void ToDataTable_PreservesDataRecordFieldTypes()
{
using var source = new DataTable();
source.Columns.Add("Id", typeof(int));
source.Columns.Add("Score", typeof(decimal));
source.Columns.Add("CreatedUtc", typeof(DateTime));
source.Rows.Add(1, 12.5m, new DateTime(2026, 6, 27, 12, 0, 0, DateTimeKind.Utc));
using var reader = source.CreateDataReader();
Assert.True(reader.Read());
var table = PowerShellDataTableConverter.ToDataTable(new object?[] { reader, reader }, "Records");
Assert.Equal(typeof(int), table.Columns["Id"]!.DataType);
Assert.Equal(typeof(decimal), table.Columns["Score"]!.DataType);
Assert.Equal(typeof(DateTime), table.Columns["CreatedUtc"]!.DataType);
Assert.Equal(1, table.Rows[0]["Id"]);
Assert.Equal(12.5m, table.Rows[0]["Score"]);
}
private sealed class ConverterRow
{
public int Id { get; set; }
public string Name { get; set; } = string.Empty;
}
}