-
Notifications
You must be signed in to change notification settings - Fork 876
Expand file tree
/
Copy pathLegacyNodaTimeTests.cs
More file actions
93 lines (78 loc) · 3.29 KB
/
LegacyNodaTimeTests.cs
File metadata and controls
93 lines (78 loc) · 3.29 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
using System;
using System.Data;
using System.Threading.Tasks;
using NodaTime;
using Npgsql.NodaTime.Internal;
using Npgsql.Tests;
using NUnit.Framework;
namespace Npgsql.PluginTests;
[NonParallelizable] // Since this test suite manipulates an AppContext switch
public class LegacyNodaTimeTests : TestBase, IDisposable
{
const string TimeZone = "Europe/Berlin";
[Test]
public async Task Timestamp_as_ZonedDateTime()
=> await AssertType(
new LocalDateTime(1998, 4, 12, 13, 26, 38, 789).InZoneLeniently(DateTimeZoneProviders.Tzdb[TimeZone]),
"1998-04-12 13:26:38.789+02",
"timestamp with time zone", dataTypeInference: DataTypeInference.Nothing,
dbType: new(DbType.DateTimeOffset, DbType.Object), valueTypeEqualsFieldType: false);
[Test]
public Task Timestamp_as_Instant()
=> AssertType(
new LocalDateTime(1998, 4, 12, 13, 26, 38, 789).InUtc().ToInstant(),
"1998-04-12 13:26:38.789",
"timestamp without time zone", dataTypeInference: DataTypeInference.Nothing,
dbType: new(DbType.DateTime, DbType.Object));
[Test]
public Task Timestamp_as_LocalDateTime()
=> AssertType(
new LocalDateTime(1998, 4, 12, 13, 26, 38, 789),
"1998-04-12 13:26:38.789",
"timestamp without time zone", dataTypeInference: DataTypeInference.Nothing,
dbType: new(DbType.DateTime, DbType.Object), valueTypeEqualsFieldType: false);
[Test]
public Task Timestamptz_as_Instant()
=> AssertType(
new LocalDateTime(1998, 4, 12, 13, 26, 38, 789).InUtc().ToInstant(),
"1998-04-12 15:26:38.789+02",
"timestamp with time zone", dataTypeInference: DataTypeInference.Nothing,
dbType: new(DbType.DateTimeOffset, DbType.Object));
[Test]
public async Task Timestamptz_ZonedDateTime_infinite_values_are_not_supported()
{
await AssertTypeUnsupportedRead<OffsetDateTime, InvalidCastException>("infinity", "timestamptz");
await AssertTypeUnsupportedWrite<OffsetDateTime, ArgumentException>(Instant.MaxValue.WithOffset(Offset.Zero), "timestamptz");
}
[Test]
public async Task Timestamptz_OffsetDateTime_infinite_values_are_not_supported()
{
await AssertTypeUnsupportedRead<OffsetDateTime, InvalidCastException>("infinity", "timestamptz");
await AssertTypeUnsupportedWrite<OffsetDateTime, ArgumentException>(Instant.MaxValue.WithOffset(Offset.Zero), "timestamptz");
}
#region Support
protected override NpgsqlDataSource DataSource { get; }
public LegacyNodaTimeTests()
{
#if DEBUG
NodaTimeUtils.LegacyTimestampBehavior = true;
Util.Statics.LegacyTimestampBehavior = true;
var builder = CreateDataSourceBuilder();
builder.UseNodaTime();
builder.ConnectionStringBuilder.Timezone = TimeZone;
DataSource = builder.Build();
#else
Assert.Ignore(
"Legacy NodaTime tests rely on the Npgsql.EnableLegacyTimestampBehavior AppContext switch and can only be run in DEBUG builds");
#endif
}
public void Dispose()
{
#if DEBUG
NodaTimeUtils.LegacyTimestampBehavior = false;
Util.Statics.LegacyTimestampBehavior = false;
DataSource.Dispose();
#endif
}
#endregion Support
}