Skip to content

Commit

Permalink
fixing database tests to read connection strings from config file (le…
Browse files Browse the repository at this point in the history
…rocha#36)

* fixing database tests to read connection strings from config file

* changing error message when connection string is not found
  • Loading branch information
lerocha authored Jan 27, 2024
1 parent 9208a2d commit cc33fac
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 134 deletions.
22 changes: 0 additions & 22 deletions ChinookDatabase.Test/App.config

This file was deleted.

4 changes: 4 additions & 0 deletions ChinookDatabase.Test/ChinookDatabase.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<PackageReference Include="FluentMigrator.Runner.SqlServerCe" Version="3.3.2" />
<PackageReference Include="IBM.Data.DB2.Core" Version="3.1.0.600" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="MySql.Data" Version="8.3.0" />
<PackageReference Include="Npgsql" Version="8.0.1" />
Expand All @@ -28,6 +29,9 @@
</ItemGroup>

<ItemGroup>
<None Update="appsettings.test.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="DatabaseTests\DatabaseFixture.tt">
<Generator>TextTemplatingFileGenerator</Generator>
<LastGenOutput>DatabaseFixture.cs</LastGenOutput>
Expand Down
20 changes: 6 additions & 14 deletions ChinookDatabase.Test/DatabaseTests/ChinookDb2Fixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
* 1. Run the generated SQL script to create the database to be tested.
* 2. Verify that app.config has the proper connection string (user/password).
********************************************************************************/
using System.Configuration;
using System.Data;
using Xunit;
using IBM.Data.DB2.Core;
using Microsoft.Extensions.Configuration;

namespace ChinookDatabase.Test.DatabaseTests
{
Expand All @@ -31,22 +31,14 @@ public partial class ChinookDb2Fixture : IDisposable
protected DB2Connection GetConnection(string connectionName)
{
// Creates an ADO.NET connection to the database, if not created yet.
if (!Connections.ContainsKey(connectionName))
if (Connections.ContainsKey(connectionName))
{
var section = (ConnectionStringsSection)ConfigurationManager.GetSection("connectionStrings");

foreach (var entry in section.ConnectionStrings.Cast<ConnectionStringSettings>()
.Where(entry => entry.Name == connectionName))
{
Connections[connectionName] = new DB2Connection(entry.ConnectionString);
break;
}

// If we failed to create a connection, then throw an exception.
if (!Connections.ContainsKey(connectionName))
throw new ApplicationException("There is no connection string defined in app.config file.");
return Connections[connectionName];
}

var config = new ConfigurationBuilder().AddJsonFile("appsettings.test.json").Build();
var connectionString = config.GetConnectionString(connectionName) ?? throw new ApplicationException("Cannot find connection string in appsettings.test.json");
Connections[connectionName] = new DB2Connection(connectionString);
return Connections[connectionName];
}

Expand Down
20 changes: 6 additions & 14 deletions ChinookDatabase.Test/DatabaseTests/ChinookMySqlFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
* 1. Run the generated SQL script to create the database to be tested.
* 2. Verify that app.config has the proper connection string (user/password).
********************************************************************************/
using System.Configuration;
using System.Data;
using Xunit;
using MySql.Data.MySqlClient;
using Microsoft.Extensions.Configuration;

namespace ChinookDatabase.Test.DatabaseTests
{
Expand All @@ -31,22 +31,14 @@ public partial class ChinookMySqlFixture : IDisposable
protected MySqlConnection GetConnection(string connectionName)
{
// Creates an ADO.NET connection to the database, if not created yet.
if (!Connections.ContainsKey(connectionName))
if (Connections.ContainsKey(connectionName))
{
var section = (ConnectionStringsSection)ConfigurationManager.GetSection("connectionStrings");

foreach (var entry in section.ConnectionStrings.Cast<ConnectionStringSettings>()
.Where(entry => entry.Name == connectionName))
{
Connections[connectionName] = new MySqlConnection(entry.ConnectionString);
break;
}

// If we failed to create a connection, then throw an exception.
if (!Connections.ContainsKey(connectionName))
throw new ApplicationException("There is no connection string defined in app.config file.");
return Connections[connectionName];
}

var config = new ConfigurationBuilder().AddJsonFile("appsettings.test.json").Build();
var connectionString = config.GetConnectionString(connectionName) ?? throw new ApplicationException("Cannot find connection string in appsettings.test.json");
Connections[connectionName] = new MySqlConnection(connectionString);
return Connections[connectionName];
}

Expand Down
20 changes: 6 additions & 14 deletions ChinookDatabase.Test/DatabaseTests/ChinookOracleFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
* 1. Run the generated SQL script to create the database to be tested.
* 2. Verify that app.config has the proper connection string (user/password).
********************************************************************************/
using System.Configuration;
using System.Data;
using Xunit;
using Oracle.ManagedDataAccess.Client;
using Microsoft.Extensions.Configuration;

namespace ChinookDatabase.Test.DatabaseTests
{
Expand All @@ -31,22 +31,14 @@ public partial class ChinookOracleFixture : IDisposable
protected OracleConnection GetConnection(string connectionName)
{
// Creates an ADO.NET connection to the database, if not created yet.
if (!Connections.ContainsKey(connectionName))
if (Connections.ContainsKey(connectionName))
{
var section = (ConnectionStringsSection)ConfigurationManager.GetSection("connectionStrings");

foreach (var entry in section.ConnectionStrings.Cast<ConnectionStringSettings>()
.Where(entry => entry.Name == connectionName))
{
Connections[connectionName] = new OracleConnection(entry.ConnectionString);
break;
}

// If we failed to create a connection, then throw an exception.
if (!Connections.ContainsKey(connectionName))
throw new ApplicationException("There is no connection string defined in app.config file.");
return Connections[connectionName];
}

var config = new ConfigurationBuilder().AddJsonFile("appsettings.test.json").Build();
var connectionString = config.GetConnectionString(connectionName) ?? throw new ApplicationException("Cannot find connection string in appsettings.test.json");
Connections[connectionName] = new OracleConnection(connectionString);
return Connections[connectionName];
}

Expand Down
20 changes: 6 additions & 14 deletions ChinookDatabase.Test/DatabaseTests/ChinookPostgreSqlFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
* 1. Run the generated SQL script to create the database to be tested.
* 2. Verify that app.config has the proper connection string (user/password).
********************************************************************************/
using System.Configuration;
using System.Data;
using Xunit;
using Npgsql;
using Microsoft.Extensions.Configuration;

namespace ChinookDatabase.Test.DatabaseTests
{
Expand All @@ -31,22 +31,14 @@ public partial class ChinookPostgreSqlFixture : IDisposable
protected NpgsqlConnection GetConnection(string connectionName)
{
// Creates an ADO.NET connection to the database, if not created yet.
if (!Connections.ContainsKey(connectionName))
if (Connections.ContainsKey(connectionName))
{
var section = (ConnectionStringsSection)ConfigurationManager.GetSection("connectionStrings");

foreach (var entry in section.ConnectionStrings.Cast<ConnectionStringSettings>()
.Where(entry => entry.Name == connectionName))
{
Connections[connectionName] = new NpgsqlConnection(entry.ConnectionString);
break;
}

// If we failed to create a connection, then throw an exception.
if (!Connections.ContainsKey(connectionName))
throw new ApplicationException("There is no connection string defined in app.config file.");
return Connections[connectionName];
}

var config = new ConfigurationBuilder().AddJsonFile("appsettings.test.json").Build();
var connectionString = config.GetConnectionString(connectionName) ?? throw new ApplicationException("Cannot find connection string in appsettings.test.json");
Connections[connectionName] = new NpgsqlConnection(connectionString);
return Connections[connectionName];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
* 1. Run the generated SQL script to create the database to be tested.
* 2. Verify that app.config has the proper connection string (user/password).
********************************************************************************/
using System.Configuration;
using System.Data;
using Xunit;
using System.Data.SqlServerCe;
using Microsoft.Extensions.Configuration;

namespace ChinookDatabase.Test.DatabaseTests
{
Expand All @@ -31,22 +31,14 @@ public partial class ChinookSqlServerCompactFixture : IDisposable
protected SqlCeConnection GetConnection(string connectionName)
{
// Creates an ADO.NET connection to the database, if not created yet.
if (!Connections.ContainsKey(connectionName))
if (Connections.ContainsKey(connectionName))
{
var section = (ConnectionStringsSection)ConfigurationManager.GetSection("connectionStrings");

foreach (var entry in section.ConnectionStrings.Cast<ConnectionStringSettings>()
.Where(entry => entry.Name == connectionName))
{
Connections[connectionName] = new SqlCeConnection(entry.ConnectionString);
break;
}

// If we failed to create a connection, then throw an exception.
if (!Connections.ContainsKey(connectionName))
throw new ApplicationException("There is no connection string defined in app.config file.");
return Connections[connectionName];
}

var config = new ConfigurationBuilder().AddJsonFile("appsettings.test.json").Build();
var connectionString = config.GetConnectionString(connectionName) ?? throw new ApplicationException("Cannot find connection string in appsettings.test.json");
Connections[connectionName] = new SqlCeConnection(connectionString);
return Connections[connectionName];
}

Expand Down
20 changes: 6 additions & 14 deletions ChinookDatabase.Test/DatabaseTests/ChinookSqlServerFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
* 1. Run the generated SQL script to create the database to be tested.
* 2. Verify that app.config has the proper connection string (user/password).
********************************************************************************/
using System.Configuration;
using System.Data;
using Xunit;
using System.Data.SqlClient;
using Microsoft.Extensions.Configuration;

namespace ChinookDatabase.Test.DatabaseTests
{
Expand All @@ -31,22 +31,14 @@ public partial class ChinookSqlServerFixture : IDisposable
protected SqlConnection GetConnection(string connectionName)
{
// Creates an ADO.NET connection to the database, if not created yet.
if (!Connections.ContainsKey(connectionName))
if (Connections.ContainsKey(connectionName))
{
var section = (ConnectionStringsSection)ConfigurationManager.GetSection("connectionStrings");

foreach (var entry in section.ConnectionStrings.Cast<ConnectionStringSettings>()
.Where(entry => entry.Name == connectionName))
{
Connections[connectionName] = new SqlConnection(entry.ConnectionString);
break;
}

// If we failed to create a connection, then throw an exception.
if (!Connections.ContainsKey(connectionName))
throw new ApplicationException("There is no connection string defined in app.config file.");
return Connections[connectionName];
}

var config = new ConfigurationBuilder().AddJsonFile("appsettings.test.json").Build();
var connectionString = config.GetConnectionString(connectionName) ?? throw new ApplicationException("Cannot find connection string in appsettings.test.json");
Connections[connectionName] = new SqlConnection(connectionString);
return Connections[connectionName];
}

Expand Down
20 changes: 6 additions & 14 deletions ChinookDatabase.Test/DatabaseTests/ChinookSqliteFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
* 1. Run the generated SQL script to create the database to be tested.
* 2. Verify that app.config has the proper connection string (user/password).
********************************************************************************/
using System.Configuration;
using System.Data;
using Xunit;
using System.Data.SQLite;
using Microsoft.Extensions.Configuration;

namespace ChinookDatabase.Test.DatabaseTests
{
Expand All @@ -31,22 +31,14 @@ public partial class ChinookSqliteFixture : IDisposable
protected SQLiteConnection GetConnection(string connectionName)
{
// Creates an ADO.NET connection to the database, if not created yet.
if (!Connections.ContainsKey(connectionName))
if (Connections.ContainsKey(connectionName))
{
var section = (ConnectionStringsSection)ConfigurationManager.GetSection("connectionStrings");

foreach (var entry in section.ConnectionStrings.Cast<ConnectionStringSettings>()
.Where(entry => entry.Name == connectionName))
{
Connections[connectionName] = new SQLiteConnection(entry.ConnectionString);
break;
}

// If we failed to create a connection, then throw an exception.
if (!Connections.ContainsKey(connectionName))
throw new ApplicationException("There is no connection string defined in app.config file.");
return Connections[connectionName];
}

var config = new ConfigurationBuilder().AddJsonFile("appsettings.test.json").Build();
var connectionString = config.GetConnectionString(connectionName) ?? throw new ApplicationException("Cannot find connection string in appsettings.test.json");
Connections[connectionName] = new SQLiteConnection(connectionString);
return Connections[connectionName];
}

Expand Down
Loading

0 comments on commit cc33fac

Please sign in to comment.