Skip to content

Commit a9bad55

Browse files
committed
CSHARP-4694: Fix a few $dateToString tests on latest.
1 parent b1f4bde commit a9bad55

File tree

1 file changed

+33
-6
lines changed

1 file changed

+33
-6
lines changed

tests/MongoDB.Driver.Tests/Linq/Linq3Implementation/Jira/CSharp3136Tests.cs

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
*/
1515

1616
using System;
17+
using System.Collections.Generic;
1718
using System.Linq;
1819
using FluentAssertions;
1920
using MongoDB.Driver.Core.Misc;
@@ -71,7 +72,7 @@ public void DateTime_ToString_with_format_should_work()
7172
[Theory]
7273
[InlineData(null, null, "{ $project : { _v : { $dateToString : { date : '$D' } }, _id : 0 } }", new[] { "2021-01-02T03:04:05.123Z", "2021-01-02T03:04:05.123Z" })]
7374
[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" })]
7576
[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" })]
7677
public void DateTime_ToString_with_format_and_timezone_constants_should_work(string format, string timezone, string expectedProjectStage, string[] expectedResults)
7778
{
@@ -94,13 +95,17 @@ public void DateTime_ToString_with_format_and_timezone_constants_should_work(str
9495
expectedProjectStage);
9596

9697
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+
}
97102
results.Should().Equal(expectedResults);
98103
}
99104

100105
[Theory]
101106
[InlineData(false, false, "{ $project : { _v : { $dateToString : { date : '$D' } }, _id : 0 } }", new[] { "2021-01-02T03:04:05.123Z", "2021-01-02T03:04:05.123Z" })]
102107
[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" })]
104109
[InlineData(true, true, "{ $project : { _v : { $dateToString : { date : '$D', format : '$Format', timezone : '$Timezone' } }, _id : 0 } }", new[] { "23:04:05", "23:04:05" })]
105110
public void DateTime_ToString_with_format_and_timezone_expressions_should_work(bool withFormat, bool withTimezone, string expectedProjectStage, string[] expectedResults)
106111
{
@@ -128,6 +133,10 @@ public void DateTime_ToString_with_format_and_timezone_expressions_should_work(b
128133
expectedProjectStage);
129134

130135
var results = queryable.ToList();
136+
if (!withFormat && withTimezone)
137+
{
138+
results = RemoveTrailingZFromResults(results); // older servers incorrectly added a trailing Z in this case
139+
}
131140
results.Should().Equal(expectedResults);
132141
}
133142

@@ -158,8 +167,8 @@ public void NullableDateTime_ToString_with_no_arguments_should_work()
158167
[InlineData(null, null, "xx", "{ $project : { _v : { $dateToString : { date : '$N', onNull : 'xx' } }, _id : 0 } }", new[] { "2021-01-02T03:04:05.123Z", "xx" })]
159168
[InlineData("%H:%M:%S", null, null, "{ $project : { _v : { $dateToString : { date : '$N', format : '%H:%M:%S' } }, _id : 0 } }", new[] { "03:04:05", null })]
160169
[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" })]
163172
[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 })]
164173
[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" })]
165174
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
183192
expectedProjectStage);
184193

185194
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+
}
186199
results.Should().Equal(expectedResults);
187200
}
188201

189202
[Theory]
190203
[InlineData(false, false, false, "{ $project : { _v : { $dateToString : { date : '$N' } }, _id : 0 } }", new[] { "2021-01-02T03:04:05.123Z", null })]
191204
[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" })]
194207
[InlineData(true, false, false, "{ $project : { _v : { $dateToString : { date : '$N', format : '$Format' } }, _id : 0 } }", new[] { "03:04:05", null })]
195208
[InlineData(true, false, true, "{ $project : { _v : { $dateToString : { date : '$N', format : '$Format', onNull : '$OnNull' } }, _id : 0 } }", new[] { "03:04:05", "missing" })]
196209
[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
225238
expectedProjectStage);
226239

227240
var results = queryable.ToList();
241+
if (!withFormat && withTimezone)
242+
{
243+
results = RemoveTrailingZFromResults(results); // older servers incorrectly added a trailing Z in this case
244+
}
228245
results.Should().Equal(expectedResults);
229246
}
230247

@@ -240,6 +257,16 @@ private IMongoCollection<C> CreateCollection()
240257
return collection;
241258
}
242259

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+
243270
private class C
244271
{
245272
public int Id { get; set; }

0 commit comments

Comments
 (0)