Adding new tests
This commit is contained in:
@@ -6,6 +6,11 @@
|
||||
"Version": "v1.0",
|
||||
"Description": "This template contains basic configuration for a .Net 8 backend"
|
||||
},
|
||||
"DatabaseSettings": {
|
||||
"SqlServerConnectionString": "SQLSERVER_DB_SERVER",
|
||||
"MongoDbConnectionString": "MONGO_DB_SERVER",
|
||||
"PostgreSQLConnectionString": "POSTGRESQL_DB_SERVER"
|
||||
},
|
||||
"OpenApiSettings": {
|
||||
"TermsOfServiceUrl": "",
|
||||
"OpenApiContact": {
|
||||
|
||||
@@ -6,6 +6,11 @@
|
||||
"Version": "v1.0",
|
||||
"Description": "This template contains basic configuration for a .Net 8 backend"
|
||||
},
|
||||
"DatabaseSettings": {
|
||||
"SqlServerConnectionString": "SQLSERVER_DB_SERVER",
|
||||
"MongoDbConnectionString": "MONGO_DB_SERVER",
|
||||
"PostgreSQLConnectionString": "POSTGRESQL_DB_SERVER"
|
||||
},
|
||||
"OpenApiSettings": {
|
||||
"TermsOfServiceUrl": "https://github.com/csimonapastore/BasicDotnetTemplate/blob/main/LICENSE.md",
|
||||
"OpenApiContact": {
|
||||
|
||||
16
MainProject.Tests/JsonData/noDbConfigurationAppSettings.json
Normal file
16
MainProject.Tests/JsonData/noDbConfigurationAppSettings.json
Normal file
@@ -0,0 +1,16 @@
|
||||
{
|
||||
"AppSettings" :
|
||||
{
|
||||
"Settings": {
|
||||
"Name": "MainProject",
|
||||
"Version": "v1.0",
|
||||
"Description": "This template contains basic configuration for a .Net 8 backend"
|
||||
},
|
||||
"DatabaseSettings": {
|
||||
"SqlServerConnectionString": "",
|
||||
"MongoDbConnectionString": "",
|
||||
"PostgreSQLConnectionString": ""
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -231,6 +231,93 @@ public class ProgramUtils_Tests
|
||||
Assert.Fail($"An exception was thrown: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void AddSqlServerContext_Valid()
|
||||
{
|
||||
try
|
||||
{
|
||||
WebApplicationBuilder builder = WebApplication.CreateBuilder(Array.Empty<string>());
|
||||
AppSettings appSettings = ProgramUtils.AddConfiguration(ref builder, System.AppDomain.CurrentDomain.BaseDirectory + "/JsonData");
|
||||
ProgramUtils.AddDbContext(ref builder, appSettings);
|
||||
AppSettings _appSettings = new AppSettings();
|
||||
builder.Configuration.GetSection("AppSettings").Bind(_appSettings);
|
||||
var areEquals = appSettings.DatabaseSettings?.SqlServerConnectionString == _appSettings.DatabaseSettings?.SqlServerConnectionString;
|
||||
Assert.IsTrue(areEquals);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex.InnerException);
|
||||
Assert.Fail($"An exception was thrown: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void AddMongoDbContext_Valid()
|
||||
{
|
||||
try
|
||||
{
|
||||
WebApplicationBuilder builder = WebApplication.CreateBuilder(Array.Empty<string>());
|
||||
AppSettings appSettings = ProgramUtils.AddConfiguration(ref builder, System.AppDomain.CurrentDomain.BaseDirectory + "/JsonData");
|
||||
ProgramUtils.AddDbContext(ref builder, appSettings);
|
||||
AppSettings _appSettings = new AppSettings();
|
||||
builder.Configuration.GetSection("AppSettings").Bind(_appSettings);
|
||||
var areEquals = appSettings.DatabaseSettings?.MongoDbConnectionString == _appSettings.DatabaseSettings?.MongoDbConnectionString;
|
||||
Assert.IsTrue(areEquals);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex.InnerException);
|
||||
Assert.Fail($"An exception was thrown: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void AddPostgreSqlDbContext_Valid()
|
||||
{
|
||||
try
|
||||
{
|
||||
WebApplicationBuilder builder = WebApplication.CreateBuilder(Array.Empty<string>());
|
||||
AppSettings appSettings = ProgramUtils.AddConfiguration(ref builder, System.AppDomain.CurrentDomain.BaseDirectory + "/JsonData");
|
||||
ProgramUtils.AddDbContext(ref builder, appSettings);
|
||||
AppSettings _appSettings = new AppSettings();
|
||||
builder.Configuration.GetSection("AppSettings").Bind(_appSettings);
|
||||
var areEquals = appSettings.DatabaseSettings?.PostgreSQLConnectionString == _appSettings.DatabaseSettings?.PostgreSQLConnectionString;
|
||||
Assert.IsTrue(areEquals);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex.InnerException);
|
||||
Assert.Fail($"An exception was thrown: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void NoSqlServerContext_Empty()
|
||||
{
|
||||
try
|
||||
{
|
||||
DatabaseSettings expectedDbSettings = new DatabaseSettings()
|
||||
{
|
||||
SqlServerConnectionString = ""
|
||||
};
|
||||
|
||||
WebApplicationBuilder builder = WebApplication.CreateBuilder(Array.Empty<string>());
|
||||
AppSettings realAppSettings = ProgramUtils.AddConfiguration(ref builder, System.AppDomain.CurrentDomain.BaseDirectory + "/JsonData", "noApiConfigurationAppSettings.json");
|
||||
ProgramUtils.AddDbContext(ref builder, realAppSettings);
|
||||
|
||||
var areEquals = expectedDbSettings.SqlServerConnectionString == realAppSettings.DatabaseSettings?.SqlServerConnectionString;
|
||||
Console.WriteLine(realAppSettings.DatabaseSettings?.SqlServerConnectionString);
|
||||
Assert.IsTrue(areEquals);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex.InnerException);
|
||||
Assert.Fail($"An exception was thrown: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user