Skip to content

Commit 2751df6

Browse files
committed
Init AppContext switches before any tests
1 parent 33c22e4 commit 2751df6

3 files changed

Lines changed: 76 additions & 3 deletions

File tree

Orm/Xtensive.Orm.Tests.Framework/TestConfiguration.cs

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
// Copyright (C) 2010 Xtensive LLC.
2-
// All rights reserved.
3-
// For conditions of distribution and use, see license.
1+
// Copyright (C) 2010-2025 Xtensive LLC.
2+
// This code is distributed under MIT license terms.
3+
// See the License.txt file in the project root for more information.
44
// Created by: Denis Krjuchkov
55
// Created: 2010.02.11
66

@@ -17,6 +17,9 @@ public sealed class TestConfiguration
1717
private const string StorageFileKey = "DO_STORAGE_FILE";
1818
private const string ConfigurationFileKey = "DO_CONFIG_FILE";
1919

20+
private const string InfinityAliasesKey = "DO_PG_INFINITY_ALIASES";
21+
private const string LegacyTimestapmKey = "DO_PG_LEGACY_TIMESTAMP";
22+
2023
private const string DefaultStorage = "default";
2124

2225
private static readonly object InstanceLock = new object();
@@ -53,6 +56,30 @@ public ConnectionInfo GetConnectionInfo(string name)
5356
return new ConnectionInfo(items[0], items[1]);
5457
}
5558

59+
public void InitAppContextSwitches()
60+
{
61+
if (configuration.TryGetValue(Storage + "cs", out var info)) {
62+
var items = info.Split(new[] { '[', ']' }, StringSplitOptions.RemoveEmptyEntries).Select(i => i.Trim()).ToArray();
63+
if (items.Length != 2)
64+
throw new InvalidOperationException(string.Format("Invalid connection string format: {0}", info));
65+
var provider = items[0];
66+
if (provider.Equals(WellKnown.Provider.PostgreSql, StringComparison.OrdinalIgnoreCase))
67+
InitPostgreSqlSwitches();
68+
}
69+
}
70+
71+
private void InitPostgreSqlSwitches()
72+
{
73+
var infinityAliasesValue = GetEnvironmentVariable(InfinityAliasesKey);
74+
if (bool.TryParse(infinityAliasesValue, out var bbb)) {
75+
AppContext.SetSwitch("Npgsql.DisableDateTimeInfinityConversions", !bbb);
76+
}
77+
var legacyTimestampsValue = GetEnvironmentVariable(LegacyTimestapmKey);
78+
if (bool.TryParse(legacyTimestampsValue, out var ccc)) {
79+
AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", ccc);
80+
}
81+
}
82+
5683
private string GetEnvironmentVariable(string key)
5784
{
5885
return new[] {EnvironmentVariableTarget.Process, EnvironmentVariableTarget.User, EnvironmentVariableTarget.Machine}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright (C) 2025 Xtensive LLC.
2+
// This code is distributed under MIT license terms.
3+
// See the License.txt file in the project root for more information.
4+
5+
using NUnit.Framework;
6+
7+
namespace Xtensive.Orm.Tests.Sql
8+
{
9+
[SetUpFixture]
10+
public class GlobalTestsSetup
11+
{
12+
[OneTimeSetUp]
13+
public void GlobalSetup()
14+
{
15+
TestConfiguration.Instance.InitAppContextSwitches();
16+
}
17+
18+
[OneTimeTearDown]
19+
public void GlobalTeardown()
20+
{
21+
}
22+
}
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright (C) 2025 Xtensive LLC.
2+
// This code is distributed under MIT license terms.
3+
// See the License.txt file in the project root for more information.
4+
5+
using NUnit.Framework;
6+
7+
namespace Xtensive.Orm.Tests
8+
{
9+
[SetUpFixture]
10+
public class GlobalTestsSetup
11+
{
12+
[OneTimeSetUp]
13+
public void GlobalSetup()
14+
{
15+
TestConfiguration.Instance.InitAppContextSwitches();
16+
}
17+
18+
[OneTimeTearDown]
19+
public void GlobalTeardown()
20+
{
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)