14
14
*/
15
15
16
16
using System ;
17
+ using System . Collections . Generic ;
17
18
using System . Linq ;
18
19
using FluentAssertions ;
19
20
using MongoDB . Driver . Core . Misc ;
@@ -71,7 +72,7 @@ public void DateTime_ToString_with_format_should_work()
71
72
[ Theory ]
72
73
[ InlineData ( null , null , "{ $project : { _v : { $dateToString : { date : '$D' } }, _id : 0 } }" , new [ ] { "2021-01-02T03:04:05.123Z" , "2021-01-02T03:04:05.123Z" } ) ]
73
74
[ InlineData ( "%H:%M:%S" , null , "{ $project : { _v : { $dateToString : { date : '$D', format : '%H:%M:%S' } }, _id : 0 } }" , new [ ] { "03:04:05" , "03:04:05" } ) ]
74
- [ InlineData ( null , "-04:00" , "{ $project : { _v : { $dateToString : { date : '$D', timezone : '-04:00' } }, _id : 0 } }" , new [ ] { "2021-01-01T23:04:05.123Z " , "2021-01-01T23:04:05.123Z " } ) ]
75
+ [ InlineData ( null , "-04:00" , "{ $project : { _v : { $dateToString : { date : '$D', timezone : '-04:00' } }, _id : 0 } }" , new [ ] { "2021-01-01T23:04:05.123 " , "2021-01-01T23:04:05.123 " } ) ]
75
76
[ InlineData ( "%H:%M:%S" , "-04:00" , "{ $project : { _v : { $dateToString : { date : '$D', format : '%H:%M:%S', timezone : '-04:00' } }, _id : 0 } }" , new [ ] { "23:04:05" , "23:04:05" } ) ]
76
77
public void DateTime_ToString_with_format_and_timezone_constants_should_work ( string format , string timezone , string expectedProjectStage , string [ ] expectedResults )
77
78
{
@@ -94,13 +95,17 @@ public void DateTime_ToString_with_format_and_timezone_constants_should_work(str
94
95
expectedProjectStage ) ;
95
96
96
97
var results = queryable . ToList ( ) ;
98
+ if ( format == null && timezone != null )
99
+ {
100
+ results = RemoveTrailingZFromResults ( results ) ; // older servers incorrectly added a trailing Z in this case
101
+ }
97
102
results . Should ( ) . Equal ( expectedResults ) ;
98
103
}
99
104
100
105
[ Theory ]
101
106
[ InlineData ( false , false , "{ $project : { _v : { $dateToString : { date : '$D' } }, _id : 0 } }" , new [ ] { "2021-01-02T03:04:05.123Z" , "2021-01-02T03:04:05.123Z" } ) ]
102
107
[ InlineData ( true , false , "{ $project : { _v : { $dateToString : { date : '$D', format : '$Format' } }, _id : 0 } }" , new [ ] { "03:04:05" , "03:04:05" } ) ]
103
- [ InlineData ( false , true , "{ $project : { _v : { $dateToString : { date : '$D', timezone : '$Timezone' } }, _id : 0 } }" , new [ ] { "2021-01-01T23:04:05.123Z " , "2021-01-01T23:04:05.123Z " } ) ]
108
+ [ InlineData ( false , true , "{ $project : { _v : { $dateToString : { date : '$D', timezone : '$Timezone' } }, _id : 0 } }" , new [ ] { "2021-01-01T23:04:05.123 " , "2021-01-01T23:04:05.123 " } ) ]
104
109
[ InlineData ( true , true , "{ $project : { _v : { $dateToString : { date : '$D', format : '$Format', timezone : '$Timezone' } }, _id : 0 } }" , new [ ] { "23:04:05" , "23:04:05" } ) ]
105
110
public void DateTime_ToString_with_format_and_timezone_expressions_should_work ( bool withFormat , bool withTimezone , string expectedProjectStage , string [ ] expectedResults )
106
111
{
@@ -128,6 +133,10 @@ public void DateTime_ToString_with_format_and_timezone_expressions_should_work(b
128
133
expectedProjectStage ) ;
129
134
130
135
var results = queryable . ToList ( ) ;
136
+ if ( ! withFormat && withTimezone )
137
+ {
138
+ results = RemoveTrailingZFromResults ( results ) ; // older servers incorrectly added a trailing Z in this case
139
+ }
131
140
results . Should ( ) . Equal ( expectedResults ) ;
132
141
}
133
142
@@ -158,8 +167,8 @@ public void NullableDateTime_ToString_with_no_arguments_should_work()
158
167
[ InlineData ( null , null , "xx" , "{ $project : { _v : { $dateToString : { date : '$N', onNull : 'xx' } }, _id : 0 } }" , new [ ] { "2021-01-02T03:04:05.123Z" , "xx" } ) ]
159
168
[ InlineData ( "%H:%M:%S" , null , null , "{ $project : { _v : { $dateToString : { date : '$N', format : '%H:%M:%S' } }, _id : 0 } }" , new [ ] { "03:04:05" , null } ) ]
160
169
[ InlineData ( "%H:%M:%S" , null , "xx" , "{ $project : { _v : { $dateToString : { date : '$N', format : '%H:%M:%S', onNull : 'xx' } }, _id : 0 } }" , new [ ] { "03:04:05" , "xx" } ) ]
161
- [ InlineData ( null , "-04:00" , null , "{ $project : { _v : { $dateToString : { date : '$N', timezone : '-04:00' } }, _id : 0 } }" , new [ ] { "2021-01-01T23:04:05.123Z " , null } ) ]
162
- [ InlineData ( null , "-04:00" , "xx" , "{ $project : { _v : { $dateToString : { date : '$N', timezone : '-04:00', onNull : 'xx' } }, _id : 0 } }" , new [ ] { "2021-01-01T23:04:05.123Z " , "xx" } ) ]
170
+ [ InlineData ( null , "-04:00" , null , "{ $project : { _v : { $dateToString : { date : '$N', timezone : '-04:00' } }, _id : 0 } }" , new [ ] { "2021-01-01T23:04:05.123 " , null } ) ]
171
+ [ InlineData ( null , "-04:00" , "xx" , "{ $project : { _v : { $dateToString : { date : '$N', timezone : '-04:00', onNull : 'xx' } }, _id : 0 } }" , new [ ] { "2021-01-01T23:04:05.123 " , "xx" } ) ]
163
172
[ InlineData ( "%H:%M:%S" , "-04:00" , null , "{ $project : { _v : { $dateToString : { date : '$N', format : '%H:%M:%S', timezone : '-04:00' } }, _id : 0 } }" , new [ ] { "23:04:05" , null } ) ]
164
173
[ InlineData ( "%H:%M:%S" , "-04:00" , "xx" , "{ $project : { _v : { $dateToString : { date : '$N', format : '%H:%M:%S', timezone : '-04:00', onNull : 'xx' } }, _id : 0 } }" , new [ ] { "23:04:05" , "xx" } ) ]
165
174
public void NullableDateTime_ToString_with_format_and_timezone_and_onNull_constants_should_work ( string format , string timezone , string onNull , string expectedProjectStage , string [ ] expectedResults )
@@ -183,14 +192,18 @@ public void NullableDateTime_ToString_with_format_and_timezone_and_onNull_consta
183
192
expectedProjectStage ) ;
184
193
185
194
var results = queryable . ToList ( ) ;
195
+ if ( format == null && timezone != null )
196
+ {
197
+ results = RemoveTrailingZFromResults ( results ) ; // older servers incorrectly added a trailing Z in this case
198
+ }
186
199
results . Should ( ) . Equal ( expectedResults ) ;
187
200
}
188
201
189
202
[ Theory ]
190
203
[ InlineData ( false , false , false , "{ $project : { _v : { $dateToString : { date : '$N' } }, _id : 0 } }" , new [ ] { "2021-01-02T03:04:05.123Z" , null } ) ]
191
204
[ InlineData ( false , false , true , "{ $project : { _v : { $dateToString : { date : '$N', onNull : '$OnNull' } }, _id : 0 } }" , new [ ] { "2021-01-02T03:04:05.123Z" , "missing" } ) ]
192
- [ InlineData ( false , true , false , "{ $project : { _v : { $dateToString : { date : '$N', timezone : '$Timezone' } }, _id : 0 } }" , new [ ] { "2021-01-01T23:04:05.123Z " , null } ) ]
193
- [ InlineData ( false , true , true , "{ $project : { _v : { $dateToString : { date : '$N', timezone : '$Timezone', onNull : '$OnNull' } }, _id : 0 } }" , new [ ] { "2021-01-01T23:04:05.123Z " , "missing" } ) ]
205
+ [ InlineData ( false , true , false , "{ $project : { _v : { $dateToString : { date : '$N', timezone : '$Timezone' } }, _id : 0 } }" , new [ ] { "2021-01-01T23:04:05.123 " , null } ) ]
206
+ [ InlineData ( false , true , true , "{ $project : { _v : { $dateToString : { date : '$N', timezone : '$Timezone', onNull : '$OnNull' } }, _id : 0 } }" , new [ ] { "2021-01-01T23:04:05.123 " , "missing" } ) ]
194
207
[ InlineData ( true , false , false , "{ $project : { _v : { $dateToString : { date : '$N', format : '$Format' } }, _id : 0 } }" , new [ ] { "03:04:05" , null } ) ]
195
208
[ InlineData ( true , false , true , "{ $project : { _v : { $dateToString : { date : '$N', format : '$Format', onNull : '$OnNull' } }, _id : 0 } }" , new [ ] { "03:04:05" , "missing" } ) ]
196
209
[ InlineData ( true , true , false , "{ $project : { _v : { $dateToString : { date : '$N', format : '$Format', timezone : '$Timezone' } }, _id : 0 } }" , new [ ] { "23:04:05" , null } ) ]
@@ -225,6 +238,10 @@ public void NullableDateTime_ToString_with_format_and_timezone_and_onNull_expres
225
238
expectedProjectStage ) ;
226
239
227
240
var results = queryable . ToList ( ) ;
241
+ if ( ! withFormat && withTimezone )
242
+ {
243
+ results = RemoveTrailingZFromResults ( results ) ; // older servers incorrectly added a trailing Z in this case
244
+ }
228
245
results . Should ( ) . Equal ( expectedResults ) ;
229
246
}
230
247
@@ -240,6 +257,16 @@ private IMongoCollection<C> CreateCollection()
240
257
return collection ;
241
258
}
242
259
260
+ private List < string > RemoveTrailingZFromResults ( List < string > results )
261
+ {
262
+ return results . Select ( RemoveTrailingZ ) . ToList ( ) ;
263
+
264
+ static string RemoveTrailingZ ( string value )
265
+ {
266
+ return value != null && value . EndsWith ( "Z" ) ? value . Substring ( 0 , value . Length - 1 ) : value ;
267
+ }
268
+ }
269
+
243
270
private class C
244
271
{
245
272
public int Id { get ; set ; }
0 commit comments