-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Fix typos * Add some unit tests
- Loading branch information
Showing
12 changed files
with
208 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"cSpell.words": [ | ||
"Anar", | ||
"appsettings", | ||
"Arien", | ||
"Enphase" | ||
] | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
using Anar.Services; | ||
|
||
using Microsoft.Extensions.Logging.Testing; | ||
using Microsoft.Extensions.Options; | ||
|
||
using Moq; | ||
using Moq.Protected; | ||
|
||
using System.Net; | ||
|
||
namespace Anar.Tests.Services; | ||
|
||
public class GatewayClientTests | ||
{ | ||
private readonly FakeLogger<GatewayClient> _fakeLogger; | ||
private readonly HttpClient _httpClient; | ||
private readonly Mock<IOptions<GatewayOptions>> _mockOptions; | ||
private readonly Mock<HttpMessageHandler> _mockHandler; | ||
private readonly GatewayOptions _options; | ||
|
||
public GatewayClientTests() | ||
{ | ||
_options = new GatewayOptions | ||
{ | ||
Uri = new Uri("http://localhost/"), | ||
Token = "token", | ||
Interval = TimeSpan.FromSeconds(10), | ||
Layout = [ | ||
new Location { SerialNumber = "12345", Azimuth = 0, ArrayName = "Array 1" }, | ||
] | ||
}; | ||
|
||
_fakeLogger = new FakeLogger<GatewayClient>(); | ||
_mockOptions = new Mock<IOptions<GatewayOptions>>(); | ||
_mockHandler = new Mock<HttpMessageHandler>(); | ||
_httpClient = new HttpClient(_mockHandler.Object) { | ||
BaseAddress = _options.Uri, | ||
}; | ||
|
||
_mockOptions.Setup(o => o.Value).Returns(_options); | ||
} | ||
|
||
private GatewayClient CreateGatewayClient() | ||
{ | ||
return new GatewayClient(_fakeLogger, _mockOptions.Object, _httpClient); | ||
} | ||
|
||
[Fact] | ||
public void Interval_ShouldReturnOptionsValue() | ||
{ | ||
var client = CreateGatewayClient(); | ||
|
||
Assert.Equal(_options.Interval, client.Interval); | ||
} | ||
|
||
[Fact] | ||
public async Task GetInvertersAsync_Success_ReturnsInverters() | ||
{ | ||
_mockHandler | ||
.Protected() | ||
.Setup<Task<HttpResponseMessage>>( | ||
"SendAsync", | ||
ItExpr.IsAny<HttpRequestMessage>(), | ||
ItExpr.IsAny<CancellationToken>() | ||
) | ||
.ReturnsAsync(new HttpResponseMessage | ||
{ | ||
StatusCode = HttpStatusCode.OK, | ||
Content = new StringContent("[{\"SerialNumber\": \"12345\"}]") | ||
}) | ||
.Verifiable(); | ||
|
||
var client = CreateGatewayClient(); | ||
var result = await client.GetInvertersAsync(); | ||
|
||
Assert.Single(result); | ||
Assert.Equal("12345", result.First().SerialNumber); | ||
Assert.NotNull(result.First().Location); | ||
} | ||
|
||
[Fact] | ||
public async Task GetInvertersAsync_Success_ReturnsEmpty() | ||
{ | ||
_mockHandler | ||
.Protected() | ||
.Setup<Task<HttpResponseMessage>>( | ||
"SendAsync", | ||
ItExpr.IsAny<HttpRequestMessage>(), | ||
ItExpr.IsAny<CancellationToken>() | ||
) | ||
.ReturnsAsync(new HttpResponseMessage | ||
{ | ||
StatusCode = HttpStatusCode.NotFound | ||
}) | ||
.Verifiable(); | ||
|
||
var client = CreateGatewayClient(); | ||
var result = await client.GetInvertersAsync(); | ||
|
||
Assert.Empty(result); | ||
Assert.Equal(LogEvent.GetInvertersFailed, _fakeLogger.LatestRecord.Id); | ||
} | ||
|
||
} |
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,21 @@ | ||
using Anar.Services; | ||
|
||
namespace Anar.Tests.Services; | ||
|
||
public class LocationTest | ||
{ | ||
[Fact] | ||
public void Facing_Should_Return_A_Direction() | ||
{ | ||
Location north = new(){ Azimuth = 0 }; | ||
Assert.Equal("north", north.Facing); | ||
Location east = new() { Azimuth = 90 }; | ||
Assert.Equal("east", east.Facing); | ||
Location south = new() { Azimuth = 180 }; | ||
Assert.Equal("south", south.Facing); | ||
Location west = new() { Azimuth = 270 }; | ||
Assert.Equal("west", west.Facing); | ||
Location unknown = new() { Azimuth = 123 }; | ||
Assert.Equal("unknown", unknown.Facing); | ||
} | ||
} |
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,42 @@ | ||
using Anar.Services; | ||
|
||
namespace Anar.Tests.Services; | ||
|
||
public sealed class DomainExtensionsTests | ||
{ | ||
[Fact] | ||
public void ToPointData_WhenLocationIsNotNull_ShouldReturnPointData() | ||
{ | ||
var inverter = new Inverter | ||
{ | ||
SerialNumber = "123", | ||
LastReportDate = 123456789, | ||
LastReportWatts = 123, | ||
Location = new() | ||
{ | ||
ArrayName = "array", | ||
Azimuth = 90, | ||
SerialNumber = "123", | ||
} | ||
}; | ||
|
||
var point = inverter.ToPointData(); | ||
|
||
Assert.Equal("inverter,arrayName=array,facing=east,serialNumber=123 watts=123i 123456789", point.ToLineProtocol()); | ||
} | ||
|
||
[Fact] | ||
public void ToPointData_WhenLocationNull_ShouldReturnPointData() | ||
{ | ||
var inverter = new Inverter | ||
{ | ||
SerialNumber = "123", | ||
LastReportDate = 123456789, | ||
LastReportWatts = 123, | ||
}; | ||
|
||
var point = inverter.ToPointData(); | ||
|
||
Assert.Equal("inverter,serialNumber=123 watts=123i 123456789", point.ToLineProtocol()); | ||
} | ||
} |
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
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