Skip to content

Commit e4d8466

Browse files
SNOW-2681481 Split unit and integration test infrastructure (#1276)
1 parent a906865 commit e4d8466

File tree

8 files changed

+120
-74
lines changed

8 files changed

+120
-74
lines changed

Snowflake.Data.Tests/IntegrationTests/FileUploadDownloadLargeFilesIT.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55
using Snowflake.Data.Client;
66
using Snowflake.Data.Tests.Util;
77

8-
namespace Snowflake.Data.Tests
8+
namespace Snowflake.Data.Tests.IntegrationTests
99
{
10-
1110
[TestFixture]
1211
public class FileUploadDownloadLargeFilesIT : SFBaseTest
1312
{
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
using System;
2+
using System.Data;
3+
using NUnit.Framework;
4+
using Snowflake.Data.Client;
5+
6+
namespace Snowflake.Data.Tests.IntegrationTests
7+
{
8+
[SetUpFixture]
9+
public class IntegrationTestSetup
10+
{
11+
[OneTimeSetUp]
12+
public void SetupIntegrationTests()
13+
{
14+
var testConfig = TestEnvironment.TestConfig;
15+
ModifySchema(testConfig.schema, SchemaAction.Create);
16+
}
17+
18+
[OneTimeTearDown]
19+
public void CleanupIntegrationTests()
20+
{
21+
var testConfig = TestEnvironment.TestConfig;
22+
23+
if (testConfig == null)
24+
return;
25+
26+
ModifySchema(testConfig.schema, SchemaAction.Drop);
27+
}
28+
29+
private enum SchemaAction
30+
{
31+
Create,
32+
Drop
33+
}
34+
35+
private static void ModifySchema(string schemaName, SchemaAction schemaAction)
36+
{
37+
var testConfig = TestEnvironment.TestConfig;
38+
39+
using (IDbConnection conn = new SnowflakeDbConnection())
40+
{
41+
conn.ConnectionString = BuildConnectionString(testConfig);
42+
conn.Open();
43+
var dbCommand = conn.CreateCommand();
44+
45+
switch (schemaAction)
46+
{
47+
case SchemaAction.Create:
48+
dbCommand.CommandText = $"CREATE OR REPLACE SCHEMA {schemaName}";
49+
break;
50+
case SchemaAction.Drop:
51+
dbCommand.CommandText = $"DROP SCHEMA IF EXISTS {schemaName}";
52+
break;
53+
default:
54+
Assert.Fail($"Not supported action on schema: {schemaAction}");
55+
break;
56+
}
57+
58+
try
59+
{
60+
dbCommand.ExecuteNonQuery();
61+
}
62+
catch (InvalidOperationException e)
63+
{
64+
Assert.Fail($"Unable to {schemaAction.ToString().ToLower()} schema {schemaName}:\n{e.StackTrace}");
65+
}
66+
}
67+
}
68+
69+
private static string BuildConnectionString(TestConfig config)
70+
{
71+
return $"scheme={config.protocol};host={config.host};port={config.port};" +
72+
$"certRevocationCheckMode=enabled;account={config.account};role={config.role};" +
73+
$"db={config.database};warehouse={config.warehouse};" +
74+
$"user={config.user};password={config.password};";
75+
}
76+
}
77+
}
78+

Snowflake.Data.Tests/SFBaseTest.cs

Lines changed: 31 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
using System.Runtime.InteropServices;
99
using NUnit.Framework;
1010
using Snowflake.Data.Client;
11-
using Snowflake.Data.Core.Tools;
1211
using Snowflake.Data.Log;
1312
using Snowflake.Data.Tests.Util;
1413

@@ -22,9 +21,37 @@ namespace Snowflake.Data.Tests
2221
using Newtonsoft.Json.Serialization;
2322

2423
/*
25-
* This is the base class for all tests that call blocking methods in the library - it uses MockSynchronizationContext to verify that
24+
* Base infrastructure shared by all test classes - provides common test utilities
25+
*/
26+
public class BaseTestInfrastructure
27+
{
28+
protected virtual string TestName => TestContext.CurrentContext.Test.MethodName;
29+
protected string TestNameWithWorker => TestName + TestContext.CurrentContext.WorkerId?.Replace("#", "_");
30+
}
31+
32+
/*
33+
* Base class for unit tests - it uses MockSynchronizationContext to verify that
34+
* there are no async deadlocks in the library
35+
*/
36+
[TestFixture]
37+
public class UnitTestBase : BaseTestInfrastructure
38+
{
39+
[SetUp]
40+
public static void SetUpMockContext()
41+
{
42+
MockSynchronizationContext.SetupContext();
43+
}
44+
45+
[TearDown]
46+
public static void TearDownMockContext()
47+
{
48+
MockSynchronizationContext.Verify();
49+
}
50+
}
51+
52+
/*
53+
* Base class for integration tests that call blocking methods in the library - it uses MockSynchronizationContext to verify that
2654
* there are no async deadlocks in the library
27-
*
2855
*/
2956
[TestFixture]
3057
public class SFBaseTest : SFBaseTestAsync
@@ -43,8 +70,7 @@ public static void TearDownContext()
4370
}
4471

4572
/*
46-
* This is the base class for all tests that call async methods in the library - it does not use a special SynchronizationContext
47-
*
73+
* Base class for integration tests that call async methods - provides database connection infrastructure
4874
*/
4975
[TestFixture]
5076
[FixtureLifeCycle(LifeCycle.InstancePerTestCase)]
@@ -276,7 +302,6 @@ public void Setup()
276302
Assert.IsTrue(cloud == null || cloud == "AWS" || cloud == "AZURE" || cloud == "GCP", "{0} is not supported. Specify AWS, AZURE or GCP as cloud environment", cloud);
277303

278304
TestConfig = ReadTestConfig();
279-
ModifySchema(TestConfig.schema, SchemaAction.CREATE);
280305
}
281306

282307
private static TestConfig ReadTestConfig()
@@ -361,12 +386,6 @@ internal static TestConfig ReadTestConfigFile(string fileName)
361386
}
362387
}
363388

364-
[OneTimeTearDown]
365-
public void Cleanup()
366-
{
367-
ModifySchema(TestConfig.schema, SchemaAction.DROP);
368-
}
369-
370389
[OneTimeSetUp]
371390
public void SetupTestPerformance()
372391
{
@@ -390,56 +409,6 @@ public void CreateTestTimeArtifact()
390409
File.WriteAllText($"..{separator}..{separator}..{separator}{GetOs()}_{dotnetVersion}_{cloudEnv}_performance.csv", resultText);
391410
}
392411

393-
private const string ConnectionStringFmt = "scheme={0};host={1};port={2};certRevocationCheckMode=enabled;" +
394-
"account={3};role={4};db={5};warehouse={6};user={7};password={8};";
395-
396-
private static string s_connectionString => string.Format(ConnectionStringFmt,
397-
TestConfig.protocol,
398-
TestConfig.host,
399-
TestConfig.port,
400-
TestConfig.account,
401-
TestConfig.role,
402-
TestConfig.database,
403-
TestConfig.warehouse,
404-
TestConfig.user,
405-
TestConfig.password);
406-
407-
private enum SchemaAction
408-
{
409-
CREATE,
410-
DROP
411-
}
412-
413-
private static void ModifySchema(string schemaName, SchemaAction schemaAction)
414-
{
415-
using (IDbConnection conn = new SnowflakeDbConnection())
416-
{
417-
conn.ConnectionString = s_connectionString;
418-
conn.Open();
419-
var dbCommand = conn.CreateCommand();
420-
switch (schemaAction)
421-
{
422-
case SchemaAction.CREATE:
423-
dbCommand.CommandText = $"CREATE OR REPLACE SCHEMA {schemaName}";
424-
break;
425-
case SchemaAction.DROP:
426-
dbCommand.CommandText = $"DROP SCHEMA IF EXISTS {schemaName}";
427-
break;
428-
default:
429-
Assert.Fail($"Not supported action on schema: {schemaAction}");
430-
break;
431-
}
432-
try
433-
{
434-
dbCommand.ExecuteNonQuery();
435-
}
436-
catch (InvalidOperationException e)
437-
{
438-
Assert.Fail($"Unable to {schemaAction.ToString().ToLower()} schema {schemaName}:\n{e.StackTrace}");
439-
}
440-
}
441-
}
442-
443412
private static string GetOs()
444413
{
445414
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))

Snowflake.Data.Tests/UnitTests/SFAzureClientTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ namespace Snowflake.Data.Tests.UnitTests
1818
using Azure.Storage.Blobs.Models;
1919

2020
[TestFixture, NonParallelizable]
21-
class SFAzureClientTest : SFBaseTest
21+
class SFAzureClientTest : UnitTestBase
2222
{
2323
// Mock data for file metadata
2424
const string EndPoint = "blob.core.windows.net";
@@ -56,7 +56,7 @@ class SFAzureClientTest : SFBaseTest
5656
SFFileMetadata _fileMetadata;
5757

5858
[SetUp]
59-
public new void BeforeTest()
59+
public void BeforeTest()
6060
{
6161
t_downloadFileName = TestNameWithWorker + "_mockFileName.txt";
6262

Snowflake.Data.Tests/UnitTests/SFFileTransferAgentTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace Snowflake.Data.Tests.UnitTests
1515
using System;
1616

1717
[TestFixture]
18-
class SFFileTransferAgentTest : SFBaseTest
18+
class SFFileTransferAgentTest : UnitTestBase
1919
{
2020
// Mock data for file metadata
2121
[ThreadStatic] private static string t_locationStage;

Snowflake.Data.Tests/UnitTests/SFGCSClientTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
namespace Snowflake.Data.Tests.UnitTests
1717
{
1818
[TestFixture, NonParallelizable]
19-
class SFGCSClientTest : SFBaseTest
19+
class SFGCSClientTest : UnitTestBase
2020
{
2121
// Mock data for file metadata
2222
const string LocationStage = "mock-customer-stage";
@@ -50,7 +50,7 @@ class SFGCSClientTest : SFBaseTest
5050
SFFileMetadata _fileMetadata;
5151

5252
[SetUp]
53-
public new void BeforeTest()
53+
public void BeforeTest()
5454
{
5555
t_downloadFileName = TestNameWithWorker + "_mockFileName.txt";
5656

Snowflake.Data.Tests/UnitTests/SFRemoteStorageClientTest.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace Snowflake.Data.Tests.UnitTests
1515
using Moq;
1616

1717
[TestFixture]
18-
class SFRemoteStorageClientTest : SFBaseTest
18+
class SFRemoteStorageClientTest : UnitTestBase
1919
{
2020
// Mock data for file metadata
2121
const string EndPoint = "mockEndPoint.com";
@@ -67,7 +67,7 @@ class SFRemoteStorageClientTest : SFBaseTest
6767
const bool IsAsync = true;
6868

6969
[SetUp]
70-
public new void BeforeTest()
70+
public void BeforeTest()
7171
{
7272
t_realSourceFilePath = TestNameWithWorker + "_realSrcFilePath.txt";
7373
t_downloadFileName = TestNameWithWorker + "_mockFileName.txt";
@@ -117,7 +117,7 @@ class SFRemoteStorageClientTest : SFBaseTest
117117
}
118118

119119
[TearDown]
120-
public new void AfterTest()
120+
public void AfterTest()
121121
{
122122
// Delete temporary files from upload
123123
if (File.Exists(_fileMetadata.realSrcFilePath))

Snowflake.Data.Tests/UnitTests/SFS3ClientTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
namespace Snowflake.Data.Tests.UnitTests
1717
{
1818
[TestFixture, NonParallelizable]
19-
class SFS3ClientTest : SFBaseTest
19+
class SFS3ClientTest : UnitTestBase
2020
{
2121
// Mock data for file metadata
2222
const string Endpoint = "[www.mockEndPoint.com]";
@@ -72,7 +72,7 @@ class SFS3ClientTest : SFBaseTest
7272
AmazonS3Config _clientConfig;
7373

7474
[SetUp]
75-
public new void BeforeTest()
75+
public void BeforeTest()
7676
{
7777
t_downloadFileName = TestNameWithWorker + "_mockFileName.txt";
7878

0 commit comments

Comments
 (0)