Last active
December 6, 2022 01:16
-
-
Save warriordog/b71edf0b53200b4fa5856f2066258a7b to your computer and use it in GitHub Desktop.
This was entirely generated by ChatGPT with just a few minutes of conversation. The only manual change is corrected indentation.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.Extensions.Configuration; | |
using Microsoft.Extensions.DependencyInjection; | |
using Microsoft.Extensions.Hosting; | |
using Microsoft.Extensions.Options; | |
using SQLServerConnectionTest; | |
using System; | |
using System.Data.SqlClient; | |
using Xunit; | |
namespace MyProject | |
{ | |
/// <summary> | |
/// The entry point of the application. | |
/// </summary> | |
public class Program | |
{ | |
/// <summary> | |
/// The main entry point of the application. | |
/// </summary> | |
/// <param name="args">Command line arguments.</param> | |
public static void Main(string[] args) | |
{ | |
CreateHostBuilder(args).Build().Run(); | |
} | |
/// <summary> | |
/// Configures and creates a host builder for the application. | |
/// </summary> | |
/// <param name="args">Command line arguments.</param> | |
/// <returns>The configured host builder.</returns> | |
public static IHostBuilder CreateHostBuilder(string[] args) => | |
Host.CreateDefaultBuilder(args) | |
.ConfigureAppConfiguration((hostingContext, config) => | |
{ | |
// Add the appsettings.json file to the configuration | |
config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true); | |
}) | |
.ConfigureServices((hostContext, services) => | |
{ | |
// Bind the "SQLServerConnection" section of appsettings.json to the SQLServerConnectionOptions class | |
services.Configure<SQLServerConnectionOptions>(hostContext.Configuration.GetSection("SQLServerConnection")); | |
// Register the database connection service with the DI container | |
services.AddScoped<IDatabaseConnectionService, DatabaseConnectionService>(); | |
}); | |
} | |
/// <summary> | |
/// Options for the SQLServer connection. | |
/// </summary> | |
public class SQLServerConnectionOptions | |
{ | |
/// <summary> | |
/// The connection string for the SQLServer database. | |
/// </summary> | |
public string ConnectionString { get; set; } | |
} | |
/// <summary> | |
/// A service for testing connections to a SQLServer database. | |
/// </summary> | |
public class DatabaseConnectionService : IDatabaseConnectionService | |
{ | |
private readonly string _connectionString; | |
/// <summary> | |
/// Constructs a new instance of the <see cref="DatabaseConnectionService"/> class. | |
/// </summary> | |
/// <param name="options">The options for the SQLServer connection.</param> | |
public DatabaseConnectionService(IOptions<SQLServerConnectionOptions> options) | |
{ | |
_connectionString = options.Value.ConnectionString; | |
} | |
/// <summary> | |
/// Tests the connection to the SQLServer database. | |
/// </summary> | |
/// <returns>True if the connection was successful, false otherwise.</returns> | |
public bool TestSQLServerConnection() | |
{ | |
try | |
{ | |
// Establish a connection to the database using the provided connection string | |
using (SqlConnection conn = new SqlConnection(_connectionString)) | |
{ | |
conn.Open(); | |
return true; | |
} | |
} | |
catch (SqlException) | |
{ | |
return false; | |
} | |
} | |
} | |
/// <summary> | |
/// Unit tests for the <see cref="DatabaseConnectionService"/> class. | |
/// </summary> | |
public class DatabaseConnectionServiceTests | |
{ | |
/// <summary> | |
/// Tests the <see cref="DatabaseConnectionService.TestSQLServerConnection"/> method with a valid connection string. | |
/// </summary> | |
[Fact] | |
public void TestSQLServerConnection_WithValidConnectionString_ReturnsTrue() | |
{ | |
// Arrange | |
var service = new DatabaseConnectionService(new OptionsWrapper<SQLServerConnectionOptions>(new SQLServerConnectionOptions | |
{ | |
ConnectionString = "Server=localhost;Database=myDatabase;User ID=myUsername;Password=myPassword;" | |
})); | |
// Act | |
var result = service.TestSQLServerConnection(); | |
// Assert | |
Assert.True(result); | |
} | |
/// <summary> | |
/// Tests the <see cref="DatabaseConnectionService.TestSQLServerConnection"/> method with an invalid connection string. | |
/// </summary> | |
[Fact] | |
public void TestSQLServerConnection_WithInvalidConnectionString_ReturnsFalse() | |
{ | |
// Arrange | |
var service = new DatabaseConnectionService(new OptionsWrapper<SQLServerConnectionOptions>(new SQLServerConnectionOptions | |
{ | |
ConnectionString = "InvalidConnectionString" | |
})); | |
// Act | |
var result = service.TestSQLServerConnection(); | |
// Assert | |
Assert.False(result); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment