-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #298 from microsoft/240/unit-tests-review
Updates and Improvements to Unit Tests
- Loading branch information
Showing
16 changed files
with
567 additions
and
19 deletions.
There are no files selected for viewing
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
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
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
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
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
104 changes: 104 additions & 0 deletions
104
src/CarbonAware.WebApi/test/unitTests/Configuration/ServiceCollectionExtensionsTests.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
using CarbonAware.WebApi.Configuration; | ||
using NUnit.Framework; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Configuration; | ||
using NUnit.Framework.Internal; | ||
|
||
namespace CarbonAware.WepApi.UnitTests; | ||
|
||
[TestFixture] | ||
public class ServiceCollectionExtensionsTests | ||
{ | ||
[Test] | ||
public void AddMonitoringAndTelemetry_AddsServices_WithConnectionString() | ||
{ | ||
// Arrange | ||
var services = new ServiceCollection(); | ||
|
||
var inMemorySettings = new Dictionary<string, string> | ||
{ | ||
{ "CarbonAwareVars:TelemetryProvider", "ApplicationInsights" }, | ||
{ "ApplicationInsights_Connection_String", "AppInsightsConnectionString" } | ||
}; | ||
var configuration = new ConfigurationBuilder() | ||
.AddInMemoryCollection(inMemorySettings) | ||
.Build(); | ||
|
||
// Act & Assert | ||
Assert.DoesNotThrow(() => services.AddMonitoringAndTelemetry(configuration)); | ||
} | ||
|
||
[Test] | ||
public void AddMonitoringAndTelemetry_AddsServices_WithInstrumentationKey() | ||
{ | ||
// Arrange | ||
var services = new ServiceCollection(); | ||
|
||
var inMemorySettings = new Dictionary<string, string> | ||
{ | ||
{ "CarbonAwareVars:TelemetryProvider", "ApplicationInsights" }, | ||
{ "AppInsights_InstrumentationKey", "AppInsightsInstrumentationKey" } | ||
}; | ||
var configuration = new ConfigurationBuilder() | ||
.AddInMemoryCollection(inMemorySettings) | ||
.Build(); | ||
|
||
// Act & Assert | ||
Assert.DoesNotThrow(() => services.AddMonitoringAndTelemetry(configuration)); | ||
} | ||
|
||
[Test] | ||
public void AddMonitoringAndTelemetry_DoesNotAddServices_WithoutConfiguration() | ||
{ | ||
// Arrange | ||
var services = new ServiceCollection(); | ||
|
||
var inMemorySettings = new Dictionary<string, string> | ||
{ | ||
{ "CarbonAwareVars:TelemetryProvider", "ApplicationInsights" } | ||
}; | ||
var configuration = new ConfigurationBuilder() | ||
.AddInMemoryCollection(inMemorySettings) | ||
.Build(); | ||
|
||
// Act & Assert | ||
Assert.DoesNotThrow(() => services.AddMonitoringAndTelemetry(configuration)); | ||
Assert.That(services.Count, Is.EqualTo(0)); | ||
} | ||
|
||
[Test] | ||
public void AddMonitoringAndTelemetry_DoesNotAddServices_WithoutTelemetryProvider() | ||
{ | ||
// Arrange | ||
var services = new ServiceCollection(); | ||
|
||
var inMemorySettings = new Dictionary<string, string>{}; | ||
var configuration = new ConfigurationBuilder() | ||
.AddInMemoryCollection(inMemorySettings) | ||
.Build(); | ||
|
||
// Act & Assert | ||
Assert.DoesNotThrow(() => services.AddMonitoringAndTelemetry(configuration)); | ||
Assert.That(services.Count, Is.EqualTo(0)); | ||
} | ||
|
||
[Test] | ||
public void CreateConsoleLogger_ReturnsILogger() | ||
{ | ||
// Arrange | ||
var inMemorySettings = new Dictionary<string, string> | ||
{ | ||
{ "Logging:LogLevel:Default", "Information" }, | ||
{ "Logging:LogLevel:Microsoft.AspNetCore", "Warning" } | ||
}; | ||
var configuration = new ConfigurationBuilder() | ||
.AddInMemoryCollection(inMemorySettings) | ||
.Build(); | ||
|
||
// Act | ||
Microsoft.Extensions.Logging.ILogger logger = ServiceCollectionExtensions.CreateConsoleLogger(configuration); | ||
|
||
// Assert | ||
Assert.That(logger, Is.Not.Null); | ||
} | ||
} |
Oops, something went wrong.