-
-
Notifications
You must be signed in to change notification settings - Fork 63
/
FbTimeZonesSupportTests.cs
222 lines (205 loc) · 6.38 KB
/
FbTimeZonesSupportTests.cs
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
* The contents of this file are subject to the Initial
* Developer's Public License Version 1.0 (the "License");
* you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
* https://github.com/FirebirdSQL/NETProvider/raw/master/license.txt.
*
* Software distributed under the License is distributed on
* an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
* express or implied. See the License for the specific
* language governing rights and limitations under the License.
*
* All Rights Reserved.
*/
//$Authors = Jiri Cincura ([email protected])
using System;
using System.Threading.Tasks;
using FirebirdSql.Data.TestsBase;
using FirebirdSql.Data.Types;
using NUnit.Framework;
namespace FirebirdSql.Data.FirebirdClient.Tests;
[TestFixtureSource(typeof(FbServerTypeTestFixtureSource), nameof(FbServerTypeTestFixtureSource.Default))]
[TestFixtureSource(typeof(FbServerTypeTestFixtureSource), nameof(FbServerTypeTestFixtureSource.Embedded))]
public class FbTimeZonesSupportTests : FbTestsBase
{
public FbTimeZonesSupportTests(FbServerType serverType, bool compression, FbWireCrypt wireCrypt)
: base(serverType, compression, wireCrypt, false)
{ }
[SetUp]
public override async Task SetUp()
{
await base.SetUp();
if (!EnsureServerVersionAtLeast(new Version(4, 0, 0, 0)))
return;
}
[TestCase(true)]
[TestCase(false)]
public async Task ReadsZonedDateTimeCorrectly(bool isExtended)
{
if (isExtended)
{
await SetExtended();
}
await using (var cmd = Connection.CreateCommand())
{
cmd.CommandText = "select cast('2020-08-27 10:00 Europe/Prague' as timestamp with time zone) from rdb$database";
var result = (FbZonedDateTime)await cmd.ExecuteScalarAsync();
Assert.AreEqual(new DateTime(2020, 08, 27, 08, 00, 00, DateTimeKind.Utc), result.DateTime);
Assert.AreEqual("Europe/Prague", result.TimeZone);
Assert.AreEqual(isExtended ? TimeSpan.FromMinutes(120) : (TimeSpan?)null, result.Offset);
}
}
[TestCase(true)]
[TestCase(false)]
public async Task ReadsZonedDateTimeNullCorrectly(bool isExtended)
{
if (isExtended)
{
await SetExtended();
}
await using (var cmd = Connection.CreateCommand())
{
cmd.CommandText = "select cast(null as timestamp with time zone) from rdb$database";
var result = (DBNull)await cmd.ExecuteScalarAsync();
Assert.AreEqual(DBNull.Value, result);
}
}
[TestCase(true)]
[TestCase(false)]
public async Task PassesZonedDateTimeCorrectly(bool isExtended)
{
if (isExtended)
{
await SetExtended();
}
var value = new FbZonedDateTime(new DateTime(2020, 08, 27, 08, 00, 00, DateTimeKind.Utc), "Europe/Prague");
await using (var cmd = Connection.CreateCommand())
{
cmd.CommandText = "select cast(@value as timestamp with time zone) from rdb$database";
cmd.Parameters.AddWithValue("value", value);
var result = (FbZonedDateTime)await cmd.ExecuteScalarAsync();
Assert.AreEqual(value, result);
}
}
[TestCase(true)]
[TestCase(false)]
public async Task PassesZonedDateTimeNullCorrectly(bool isExtended)
{
if (isExtended)
{
await SetExtended();
}
await using (var cmd = Connection.CreateCommand())
{
cmd.CommandText = "select cast(@value as timestamp with time zone) from rdb$database";
cmd.Parameters.AddWithValue("value", DBNull.Value);
var result = (DBNull)await cmd.ExecuteScalarAsync();
Assert.AreEqual(DBNull.Value, result);
}
}
[TestCase(true)]
[TestCase(false)]
public async Task ReadsZonedTimeCorrectly(bool isExtended)
{
if (isExtended)
{
await SetExtended();
}
await using (var cmd = Connection.CreateCommand())
{
cmd.CommandText = "select cast('15:00 Europe/Prague' as time with time zone) from rdb$database";
var result = (FbZonedTime)await cmd.ExecuteScalarAsync();
Assert.AreEqual(new TimeSpan(14, 00, 00), result.Time);
Assert.AreEqual("Europe/Prague", result.TimeZone);
Assert.AreEqual(isExtended ? TimeSpan.FromMinutes(60) : (TimeSpan?)null, result.Offset);
}
}
[TestCase(true)]
[TestCase(false)]
public async Task ReadsZonedTimeNullCorrectly(bool isExtended)
{
if (isExtended)
{
await SetExtended();
}
await using (var cmd = Connection.CreateCommand())
{
cmd.CommandText = "select cast(null as time with time zone) from rdb$database";
var result = (DBNull)await cmd.ExecuteScalarAsync();
Assert.AreEqual(DBNull.Value, result);
}
}
[TestCase(true)]
[TestCase(false)]
public async Task PassesZonedTimeCorrectly(bool isExtended)
{
if (isExtended)
{
await SetExtended();
}
var value = new FbZonedTime(new TimeSpan(14, 00, 00), "Europe/Prague");
await using (var cmd = Connection.CreateCommand())
{
cmd.CommandText = "select cast(@value as time with time zone) from rdb$database";
cmd.Parameters.AddWithValue("value", value);
var result = (FbZonedTime)await cmd.ExecuteScalarAsync();
Assert.AreEqual(value, result);
}
}
[TestCase(true)]
[TestCase(false)]
public async Task PassesZonedTimeNullCorrectly(bool isExtended)
{
if (isExtended)
{
await SetExtended();
}
await using (var cmd = Connection.CreateCommand())
{
cmd.CommandText = "select cast(@value as time with time zone) from rdb$database";
cmd.Parameters.AddWithValue("value", DBNull.Value);
var result = (DBNull)await cmd.ExecuteScalarAsync();
Assert.AreEqual(DBNull.Value, result);
}
}
[TestCase(true)]
[TestCase(false)]
public async Task SelectEmptyResultSet(bool isExtended)
{
if (isExtended)
{
await SetExtended();
}
await using (var cmd = Connection.CreateCommand())
{
cmd.CommandText = "select cast(null as time with time zone) from rdb$database where 0=1";
await using (var reader = await cmd.ExecuteReaderAsync())
{
Assert.DoesNotThrowAsync(reader.ReadAsync);
}
}
}
[Test]
public async Task SimpleSelectSchemaTableTest()
{
await using (var cmd = Connection.CreateCommand())
{
cmd.CommandText = "select current_timestamp, current_time from rdb$database";
await using (var reader = await cmd.ExecuteReaderAsync())
{
var schema = await reader.GetSchemaTableAsync();
Assert.AreEqual(typeof(FbZonedDateTime), schema.Rows[0].ItemArray[5]);
Assert.AreEqual(typeof(FbZonedTime), schema.Rows[1].ItemArray[5]);
}
}
}
async Task SetExtended()
{
await using (var cmd = Connection.CreateCommand())
{
cmd.CommandText = "set bind of time zone to extended";
await cmd.ExecuteNonQueryAsync();
}
}
}