@@ -44,11 +44,88 @@ public async Task WriteAsync_WithFlatObjects_WritesToValidFile()
44
44
Assert . IsTrue ( outputData . Any ( o => o . Id == 2 && o . Name == "Two" ) ) ;
45
45
Assert . IsTrue ( outputData . Any ( o => o . Id == 3 && o . Name == "Three" ) ) ;
46
46
}
47
+
48
+ [ TestMethod ]
49
+ public async Task WriteAsync_WithSourceDates_PreservesDateFormats ( )
50
+ {
51
+ var sink = new JsonDataSinkExtension ( ) ;
52
+
53
+ var now = DateTime . UtcNow ;
54
+ var randomTime = DateTime . UtcNow . AddMinutes ( Random . Shared . NextDouble ( ) * 10000 ) ;
55
+ var data = new List < DictionaryDataItem >
56
+ {
57
+ new ( new Dictionary < string , object ? >
58
+ {
59
+ { "Id" , 1 } ,
60
+ { "Created" , now } ,
61
+ } ) ,
62
+ new ( new Dictionary < string , object ? >
63
+ {
64
+ { "Id" , 2 } ,
65
+ { "Created" , DateTime . UnixEpoch } ,
66
+ } ) ,
67
+ new ( new Dictionary < string , object ? >
68
+ {
69
+ { "Id" , 3 } ,
70
+ { "Created" , randomTime } ,
71
+ } ) ,
72
+ } ;
73
+ string outputFile = $ "{ now : yy-MM-dd} _DateOutput.json";
74
+ var config = TestHelpers . CreateConfig ( new Dictionary < string , string >
75
+ {
76
+ { "FilePath" , outputFile }
77
+ } ) ;
78
+
79
+ await sink . WriteAsync ( data . ToAsyncEnumerable ( ) , config , new JsonDataSourceExtension ( ) , NullLogger . Instance ) ;
80
+
81
+ string json = await File . ReadAllTextAsync ( outputFile ) ;
82
+ var outputData = JsonConvert . DeserializeObject < List < TestDataObject > > ( json ) ;
83
+
84
+ Assert . IsTrue ( outputData . Any ( o => o . Id == 1 && o . Created == now ) ) ;
85
+ Assert . IsTrue ( outputData . Any ( o => o . Id == 2 && o . Created == DateTime . UnixEpoch ) ) ;
86
+ Assert . IsTrue ( outputData . Any ( o => o . Id == 3 && o . Created == randomTime ) ) ;
87
+ }
88
+
89
+
90
+ [ TestMethod ]
91
+ public async Task WriteAsync_WithDateArray_PreservesDateFormats ( )
92
+ {
93
+ var sink = new JsonDataSinkExtension ( ) ;
94
+
95
+ var now = DateTime . UtcNow ;
96
+ var randomTime = DateTime . UtcNow . AddMinutes ( Random . Shared . NextDouble ( ) * 10000 ) ;
97
+ var data = new List < DictionaryDataItem >
98
+ {
99
+ new ( new Dictionary < string , object ? >
100
+ {
101
+ { "Id" , 1 } ,
102
+ { "Dates" , new [ ] { now , randomTime , DateTime . UnixEpoch } } ,
103
+ } )
104
+ } ;
105
+
106
+ string outputFile = $ "{ now : yy-MM-dd} _DateArrayOutput.json";
107
+ var config = TestHelpers . CreateConfig ( new Dictionary < string , string >
108
+ {
109
+ { "FilePath" , outputFile }
110
+ } ) ;
111
+
112
+ await sink . WriteAsync ( data . ToAsyncEnumerable ( ) , config , new JsonDataSourceExtension ( ) , NullLogger . Instance ) ;
113
+
114
+ string json = await File . ReadAllTextAsync ( outputFile ) ;
115
+ var outputData = JsonConvert . DeserializeObject < List < TestDataObject > > ( json ) ;
116
+
117
+ Assert . AreEqual ( now , outputData . Single ( ) . Dates . ElementAt ( 0 ) ) ;
118
+ Assert . AreEqual ( randomTime , outputData . Single ( ) . Dates . ElementAt ( 1 ) ) ;
119
+ Assert . AreEqual ( DateTime . UnixEpoch , outputData . Single ( ) . Dates . ElementAt ( 2 ) ) ;
120
+ }
121
+
47
122
}
48
123
49
124
public class TestDataObject
50
125
{
51
126
public int Id { get ; set ; }
52
127
public string ? Name { get ; set ; }
128
+ public DateTime ? Created { get ; set ; }
129
+ public List < DateTime > ? Dates { get ; set ; }
53
130
}
54
131
}
0 commit comments