Skip to content

Commit

Permalink
tests: consolidate sync and async variations of MsSql integration tes…
Browse files Browse the repository at this point in the history
…ts (#2095)

* Consolidate MsSql sync/async tests

* Remove unused test app

* Consolidate async/sync query param tests

* More consolidation
  • Loading branch information
nr-ahemsath authored Nov 27, 2023
1 parent ddee81d commit eadaf4b
Show file tree
Hide file tree
Showing 17 changed files with 209 additions and 1,291 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 New Relic, Inc. All rights reserved.
// Copyright 2020 New Relic, Inc. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

using System.Collections.Generic;
Expand All @@ -16,17 +16,9 @@
namespace MultiFunctionApplicationHelpers.NetStandardLibraries.MsSql
{
[Library]
public class MicrosoftDataSqlClientExerciser
public class MicrosoftDataSqlClientExerciser : MsSqlExerciserBase
{

private const string InsertPersonMsSql = "INSERT INTO {0} (FirstName, LastName, Email) VALUES('Testy', 'McTesterson', '[email protected]')";
private const string DeletePersonMsSql = "DELETE FROM {0} WHERE Email = '[email protected]'";
private const string CountPersonMsSql = "SELECT COUNT(*) FROM {0} WITH(nolock)";
private static readonly string CreateProcedureStatement = @"CREATE OR ALTER PROCEDURE [dbo].[{0}] {1} AS RETURN 0";
private const string CreatePersonTableMsSql = "CREATE TABLE {0} (FirstName varchar(20) NOT NULL, LastName varchar(20) NOT NULL, Email varchar(50) NOT NULL)";
private const string DropPersonTableMsSql = "IF (OBJECT_ID('{0}') IS NOT NULL) DROP TABLE {0}";
private const string DropProcedureSql = "IF (OBJECT_ID('{0}') IS NOT NULL) DROP PROCEDURE {0}";

[LibraryMethod]
[Transaction]
[MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)]
Expand All @@ -38,7 +30,7 @@ public string MsSql(string tableName)
{
connection.Open();

using (var command = new SqlCommand("SELECT * FROM NewRelic.dbo.TeamMembers WHERE FirstName = 'John'", connection))
using (var command = new SqlCommand(SelectPersonByFirstNameMsSql, connection))
{

using (var reader = command.ExecuteReader())
Expand Down Expand Up @@ -88,7 +80,7 @@ public async Task<string> MsSqlAsync(string tableName)
{
await connection.OpenAsync();

using (var command = new SqlCommand("SELECT * FROM NewRelic.dbo.TeamMembers WHERE FirstName = 'John'", connection))
using (var command = new SqlCommand(SelectPersonByLastNameMsSql, connection))
{
using (var reader = await command.ExecuteReaderAsync())
{
Expand Down Expand Up @@ -137,7 +129,7 @@ public string MsSqlWithParameterizedQuery(bool paramsWithAtSign)
{
connection.Open();

using (var command = new SqlCommand("SELECT * FROM NewRelic.dbo.TeamMembers WHERE FirstName = @FN", connection))
using (var command = new SqlCommand(SelectPersonByParameterizedFirstNameMsSql, connection))
{
command.Parameters.Add(new SqlParameter(paramsWithAtSign ? "@FN" : "FN", "O'Keefe"));
using (var reader = command.ExecuteReader())
Expand All @@ -160,17 +152,17 @@ public string MsSqlWithParameterizedQuery(bool paramsWithAtSign)
[LibraryMethod]
[Transaction]
[MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)]
public async Task<string> MsSqlAsync_WithParameterizedQuery(string tableName, bool paramsWithAtSign)
public async Task<string> MsSqlAsync_WithParameterizedQuery(bool paramsWithAtSign)
{
var teamMembers = new List<string>();

using (var connection = new SqlConnection(MsSqlConfiguration.MsSqlConnectionString))
{
await connection.OpenAsync();

using (var command = new SqlCommand("SELECT * FROM NewRelic.dbo.TeamMembers WHERE FirstName = @FN", connection))
using (var command = new SqlCommand(SelectPersonByParameterizedLastNameMsSql, connection))
{
command.Parameters.Add(new SqlParameter(paramsWithAtSign ? "@FN" : "FN", "O'Keefe"));
command.Parameters.Add(new SqlParameter(paramsWithAtSign ? "@LN" : "LN", "Lee"));
using (var reader = await command.ExecuteReaderAsync())
{
while (await reader.ReadAsync())
Expand All @@ -183,25 +175,6 @@ public async Task<string> MsSqlAsync_WithParameterizedQuery(string tableName, bo
}
}
}

var insertSql = string.Format(InsertPersonMsSql, tableName);
var countSql = string.Format(CountPersonMsSql, tableName);
var deleteSql = string.Format(DeletePersonMsSql, tableName);

using (var command = new SqlCommand(insertSql, connection))
{
var insertCount = await command.ExecuteNonQueryAsync();
}

using (var command = new SqlCommand(countSql, connection))
{
var teamMemberCount = await command.ExecuteScalarAsync();
}

using (var command = new SqlCommand(deleteSql, connection))
{
var deleteCount = await command.ExecuteNonQueryAsync();
}
}

return string.Join(",", teamMembers);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2020 New Relic, Inc. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

namespace MultiFunctionApplicationHelpers.NetStandardLibraries.MsSql
{
public class MsSqlExerciserBase
{

protected const string SelectPersonByFirstNameMsSql = "SELECT * FROM NewRelic.dbo.TeamMembers WHERE FirstName = 'John'";
protected const string SelectPersonByLastNameMsSql = "SELECT * FROM NewRelic.dbo.TeamMembers WHERE LastName = 'Doe'";
protected const string SelectPersonByParameterizedFirstNameMsSql = "SELECT * FROM NewRelic.dbo.TeamMembers WHERE FirstName = @FN";
protected const string SelectPersonByParameterizedLastNameMsSql = "SELECT * FROM NewRelic.dbo.TeamMembers WHERE LastName = @LN";
protected const string InsertPersonMsSql = "INSERT INTO {0} (FirstName, LastName, Email) VALUES('Testy', 'McTesterson', '[email protected]')";
protected const string DeletePersonMsSql = "DELETE FROM {0} WHERE Email = '[email protected]'";
protected const string CountPersonMsSql = "SELECT COUNT(*) FROM {0} WITH(nolock)";
protected static readonly string CreateProcedureStatement = @"CREATE OR ALTER PROCEDURE [dbo].[{0}] {1} AS RETURN 0";
protected const string CreatePersonTableMsSql = "CREATE TABLE {0} (FirstName varchar(20) NOT NULL, LastName varchar(20) NOT NULL, Email varchar(50) NOT NULL)";
protected const string DropPersonTableMsSql = "IF (OBJECT_ID('{0}') IS NOT NULL) DROP TABLE {0}";
protected const string DropProcedureSql = "IF (OBJECT_ID('{0}') IS NOT NULL) DROP PROCEDURE {0}";
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 New Relic, Inc. All rights reserved.
// Copyright 2020 New Relic, Inc. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

#if NETFRAMEWORK
Expand All @@ -19,16 +19,8 @@
namespace MultiFunctionApplicationHelpers.NetStandardLibraries.MsSql
{
[Library]
public class SystemDataExerciser
public class SystemDataExerciser : MsSqlExerciserBase
{
private const string InsertPersonMsSql = "INSERT INTO {0} (FirstName, LastName, Email) VALUES('Testy', 'McTesterson', '[email protected]')";
private const string DeletePersonMsSql = "DELETE FROM {0} WHERE Email = '[email protected]'";
private const string CountPersonMsSql = "SELECT COUNT(*) FROM {0} WITH(nolock)";
private static readonly string CreateProcedureStatement = @"CREATE OR ALTER PROCEDURE [dbo].[{0}] {1} AS RETURN 0";
private const string CreatePersonTableMsSql = "CREATE TABLE {0} (FirstName varchar(20) NOT NULL, LastName varchar(20) NOT NULL, Email varchar(50) NOT NULL)";
private const string DropPersonTableMsSql = "IF (OBJECT_ID('{0}') IS NOT NULL) DROP TABLE {0}";
private const string DropProcedureSql = "IF (OBJECT_ID('{0}') IS NOT NULL) DROP PROCEDURE {0}";

[LibraryMethod]
[Transaction]
[MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)]
Expand All @@ -40,7 +32,7 @@ public string MsSql(string tableName)
{
connection.Open();

using (var command = new SqlCommand("SELECT * FROM NewRelic.dbo.TeamMembers WHERE FirstName = 'John'", connection))
using (var command = new SqlCommand(SelectPersonByFirstNameMsSql, connection))
{

using (var reader = command.ExecuteReader())
Expand Down Expand Up @@ -90,7 +82,7 @@ public async Task<string> MsSqlAsync(string tableName)
{
await connection.OpenAsync();

using (var command = new SqlCommand("SELECT * FROM NewRelic.dbo.TeamMembers WHERE FirstName = 'John'", connection))
using (var command = new SqlCommand(SelectPersonByLastNameMsSql, connection))
{
using (var reader = await command.ExecuteReaderAsync())
{
Expand Down Expand Up @@ -139,7 +131,7 @@ public string MsSqlWithParameterizedQuery(bool paramsWithAtSign)
{
connection.Open();

using (var command = new SqlCommand("SELECT * FROM NewRelic.dbo.TeamMembers WHERE FirstName = @FN", connection))
using (var command = new SqlCommand(SelectPersonByParameterizedFirstNameMsSql, connection))
{
command.Parameters.Add(new SqlParameter(paramsWithAtSign ? "@FN" : "FN", "O'Keefe"));
using (var reader = command.ExecuteReader())
Expand All @@ -163,17 +155,17 @@ public string MsSqlWithParameterizedQuery(bool paramsWithAtSign)
[LibraryMethod]
[Transaction]
[MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)]
public async Task<string> MsSqlAsync_WithParameterizedQuery(string tableName, bool paramsWithAtSign)
public async Task<string> MsSqlAsync_WithParameterizedQuery(bool paramsWithAtSign)
{
var teamMembers = new List<string>();

using (var connection = new SqlConnection(MsSqlConfiguration.MsSqlConnectionString))
{
await connection.OpenAsync();

using (var command = new SqlCommand("SELECT * FROM NewRelic.dbo.TeamMembers WHERE FirstName = @FN", connection))
using (var command = new SqlCommand(SelectPersonByParameterizedLastNameMsSql, connection))
{
command.Parameters.Add(new SqlParameter(paramsWithAtSign ? "@FN" : "FN", "O'Keefe"));
command.Parameters.Add(new SqlParameter(paramsWithAtSign ? "@LN" : "LN", "Lee"));
using (var reader = await command.ExecuteReaderAsync())
{
while (await reader.ReadAsync())
Expand All @@ -186,25 +178,6 @@ public async Task<string> MsSqlAsync_WithParameterizedQuery(string tableName, bo
}
}
}

var insertSql = string.Format(InsertPersonMsSql, tableName);
var countSql = string.Format(CountPersonMsSql, tableName);
var deleteSql = string.Format(DeletePersonMsSql, tableName);

using (var command = new SqlCommand(insertSql, connection))
{
var insertCount = await command.ExecuteNonQueryAsync();
}

using (var command = new SqlCommand(countSql, connection))
{
var teamMemberCount = await command.ExecuteScalarAsync();
}

using (var command = new SqlCommand(deleteSql, connection))
{
var deleteCount = await command.ExecuteNonQueryAsync();
}
}

return string.Join(",", teamMembers);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 New Relic, Inc. All rights reserved.
// Copyright 2020 New Relic, Inc. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

#if NETCOREAPP3_1_OR_GREATER
Expand All @@ -13,22 +13,12 @@
using NewRelic.Agent.IntegrationTests.Shared.ReflectionHelpers;
using NewRelic.Api.Agent;
using System.Threading;
using System.Data.OleDb;
using System.Data.Odbc;

namespace MultiFunctionApplicationHelpers.NetStandardLibraries.MsSql
{
[Library]
public class SystemDataSqlClientExerciser
public class SystemDataSqlClientExerciser : MsSqlExerciserBase
{
private const string InsertPersonMsSql = "INSERT INTO {0} (FirstName, LastName, Email) VALUES('Testy', 'McTesterson', '[email protected]')";
private const string DeletePersonMsSql = "DELETE FROM {0} WHERE Email = '[email protected]'";
private const string CountPersonMsSql = "SELECT COUNT(*) FROM {0} WITH(nolock)";
private static readonly string CreateProcedureStatement = @"CREATE OR ALTER PROCEDURE [dbo].[{0}] {1} AS RETURN 0";
private const string CreatePersonTableMsSql = "CREATE TABLE {0} (FirstName varchar(20) NOT NULL, LastName varchar(20) NOT NULL, Email varchar(50) NOT NULL)";
private const string DropPersonTableMsSql = "IF (OBJECT_ID('{0}') IS NOT NULL) DROP TABLE {0}";
private const string DropProcedureSql = "IF (OBJECT_ID('{0}') IS NOT NULL) DROP PROCEDURE {0}";

[LibraryMethod]
[Transaction]
[MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)]
Expand All @@ -40,7 +30,7 @@ public string MsSql(string tableName)
{
connection.Open();

using (var command = new SqlCommand("SELECT * FROM NewRelic.dbo.TeamMembers WHERE FirstName = 'John'", connection))
using (var command = new SqlCommand(SelectPersonByFirstNameMsSql, connection))
{

using (var reader = command.ExecuteReader())
Expand Down Expand Up @@ -90,7 +80,7 @@ public async Task<string> MsSqlAsync(string tableName)
{
await connection.OpenAsync();

using (var command = new SqlCommand("SELECT * FROM NewRelic.dbo.TeamMembers WHERE FirstName = 'John'", connection))
using (var command = new SqlCommand(SelectPersonByLastNameMsSql, connection))
{
using (var reader = await command.ExecuteReaderAsync())
{
Expand Down Expand Up @@ -139,7 +129,7 @@ public string MsSqlWithParameterizedQuery(bool paramsWithAtSign)
{
connection.Open();

using (var command = new SqlCommand("SELECT * FROM NewRelic.dbo.TeamMembers WHERE FirstName = @FN", connection))
using (var command = new SqlCommand(SelectPersonByParameterizedFirstNameMsSql, connection))
{
command.Parameters.Add(new SqlParameter(paramsWithAtSign ? "@FN" : "FN", "O'Keefe"));
using (var reader = command.ExecuteReader())
Expand All @@ -163,17 +153,17 @@ public string MsSqlWithParameterizedQuery(bool paramsWithAtSign)
[LibraryMethod]
[Transaction]
[MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)]
public async Task<string> MsSqlAsync_WithParameterizedQuery(string tableName, bool paramsWithAtSign)
public async Task<string> MsSqlAsync_WithParameterizedQuery(bool paramsWithAtSign)
{
var teamMembers = new List<string>();

using (var connection = new SqlConnection(MsSqlConfiguration.MsSqlConnectionString))
{
await connection.OpenAsync();

using (var command = new SqlCommand("SELECT * FROM NewRelic.dbo.TeamMembers WHERE FirstName = @FN", connection))
using (var command = new SqlCommand(SelectPersonByParameterizedLastNameMsSql, connection))
{
command.Parameters.Add(new SqlParameter(paramsWithAtSign ? "@FN" : "FN", "O'Keefe"));
command.Parameters.Add(new SqlParameter(paramsWithAtSign ? "@LN" : "LN", "Lee"));
using (var reader = await command.ExecuteReaderAsync())
{
while (await reader.ReadAsync())
Expand All @@ -186,25 +176,6 @@ public async Task<string> MsSqlAsync_WithParameterizedQuery(string tableName, bo
}
}
}

var insertSql = string.Format(InsertPersonMsSql, tableName);
var countSql = string.Format(CountPersonMsSql, tableName);
var deleteSql = string.Format(DeletePersonMsSql, tableName);

using (var command = new SqlCommand(insertSql, connection))
{
var insertCount = await command.ExecuteNonQueryAsync();
}

using (var command = new SqlCommand(countSql, connection))
{
var teamMemberCount = await command.ExecuteScalarAsync();
}

using (var command = new SqlCommand(deleteSql, connection))
{
var deleteCount = await command.ExecuteNonQueryAsync();
}
}

return string.Join(",", teamMembers);
Expand Down

This file was deleted.

Loading

0 comments on commit eadaf4b

Please sign in to comment.