Skip to content

Commit

Permalink
Merge pull request #16 from PhilPJL/develop
Browse files Browse the repository at this point in the history
Merge develop into master
  • Loading branch information
PhilPJL authored Oct 21, 2023
2 parents c18adb9 + 4dc4cdd commit 0d698b4
Show file tree
Hide file tree
Showing 26 changed files with 1,712 additions and 1,341 deletions.
2 changes: 1 addition & 1 deletion .idea/.idea.ImpSoft.OctopusEnergy/.idea/indexLayout.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 0 additions & 7 deletions .idea/.idea.ImpSoft.OctopusEnergy/riderModule.iml

This file was deleted.

195 changes: 103 additions & 92 deletions ImpSoft.OctopusEnergy.Api.Tests/ConsumptionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,125 +4,136 @@
using System.Linq;
using System.Threading.Tasks;

namespace ImpSoft.OctopusEnergy.Api.Tests
// ReSharper disable PossibleMultipleEnumeration

namespace ImpSoft.OctopusEnergy.Api.Tests;

[TestClass]
public class ConsumptionTests
{
[TestClass]
public class ConsumptionTests
[TestMethod]
public async Task GetElectricityConsumptionSucceedsAsync()
{
[TestMethod]
public async Task GetElectricityConsumptionSucceedsAsync()
{
var from = DateTimeOffset.UtcNow.AddDays(-2);
var to = DateTimeOffset.UtcNow.AddDays(-1);
var from = DateTimeOffset.UtcNow.AddDays(-2);
var to = DateTimeOffset.UtcNow.AddDays(-1);

var mpan = "A123Z";
var serialNumber = "J0123456K";
var interval = Interval.Hour;
const string mpan = "A123Z";
const string serialNumber = "J0123456K";
const Interval interval = Interval.Hour;

var consumption = new PagedResults<Consumption>
var consumption = new PagedResults<Consumption>
{
Results = new List<Consumption>
{
Results = new List<Consumption> {
new Consumption {
Start = from,
End = from.AddMinutes(30),
Quantity = 0.5m
},
new Consumption {
Start = from.AddMinutes(30),
End = from.AddMinutes(60),
Quantity = 0.75m
},
new()
{
Start = from,
End = from.AddMinutes(30),
Quantity = 0.5m
},
Count = 1,
Next = null,
Previous = null
};
new()
{
Start = from.AddMinutes(30),
End = from.AddMinutes(60),
Quantity = 0.75m
}
},
Count = 1,
Next = string.Empty,
Previous = string.Empty
};

var uri = OctopusEnergyClient.ComposeGetElectricityConsumptionUri(mpan, serialNumber, from, to, interval);
var uri = OctopusEnergyClient.ComposeGetElectricityConsumptionUri(OctopusEnergyClient.DefaultBaseAddress, mpan,
serialNumber, from, to, interval);

var client = TestHelper.CreateClient(uri, consumption);
var client = TestHelper.CreateClient(uri, consumption);

var consumption1 = await client.GetElectricityConsumptionAsync("key", mpan, serialNumber, from, to, Interval.Hour);
var consumption1 = await client.GetElectricityConsumptionAsync(mpan, serialNumber, from, to, Interval.Hour);

Assert.AreEqual(consumption.Results.Count(), consumption1.Count());
Assert.AreEqual(consumption.Results.Count(), consumption1.Count());

var firstExpected = consumption.Results.First();
var firstActual = consumption1.First();
var firstExpected = consumption.Results.First();
var firstActual = consumption1.First();

Assert.AreEqual(firstExpected.Start, firstActual.Start);
Assert.AreEqual(firstExpected.End, firstActual.End);
Assert.AreEqual(firstExpected.Quantity, firstActual.Quantity);
}
Assert.AreEqual(firstExpected.Start, firstActual.Start);
Assert.AreEqual(firstExpected.End, firstActual.End);
Assert.AreEqual(firstExpected.Quantity, firstActual.Quantity);
}

[TestMethod]
public void GetElectricityConsumptionWithNoMpanCodeThrows()
{
var from = DateTimeOffset.UtcNow.AddDays(-2);
var to = DateTimeOffset.UtcNow.AddDays(-1);
[TestMethod]
public void GetElectricityConsumptionWithNoMpanCodeThrows()
{
var from = DateTimeOffset.UtcNow.AddDays(-2);
var to = DateTimeOffset.UtcNow.AddDays(-1);

var mpan = "";
var serialNumber = "J0123456K";
var interval = Interval.Hour;
const string mpan = "";
const string serialNumber = "J0123456K";
const Interval interval = Interval.Hour;

Assert.ThrowsException<ArgumentException>(() => OctopusEnergyClient.ComposeGetElectricityConsumptionUri(mpan, serialNumber, from, to, interval));
}
Assert.ThrowsException<ArgumentException>(() => OctopusEnergyClient.ComposeGetElectricityConsumptionUri(
OctopusEnergyClient.DefaultBaseAddress, mpan, serialNumber, from, to, interval));
}

[TestMethod]
public void GetElectricityConsumptionWithNoSerialThrows()
{
var from = DateTimeOffset.UtcNow.AddDays(-2);
var to = DateTimeOffset.UtcNow.AddDays(-1);
[TestMethod]
public void GetElectricityConsumptionWithNoSerialThrows()
{
var from = DateTimeOffset.UtcNow.AddDays(-2);
var to = DateTimeOffset.UtcNow.AddDays(-1);

var mpan = "A123Z";
var serialNumber = "";
var interval = Interval.Hour;
const string mpan = "A123Z";
const string serialNumber = "";
const Interval interval = Interval.Hour;

Assert.ThrowsException<ArgumentException>(() => OctopusEnergyClient.ComposeGetElectricityConsumptionUri(mpan, serialNumber, from, to, interval));
}
Assert.ThrowsException<ArgumentException>(() => OctopusEnergyClient.ComposeGetElectricityConsumptionUri(
OctopusEnergyClient.DefaultBaseAddress, mpan, serialNumber, from, to, interval));
}

[TestMethod]
public async Task GetGasConsumptionSucceedsAsync()
{
var from = DateTimeOffset.UtcNow.AddDays(-2);
var to = DateTimeOffset.UtcNow.AddDays(-1);
[TestMethod]
public async Task GetGasConsumptionSucceedsAsync()
{
var from = DateTimeOffset.UtcNow.AddDays(-2);
var to = DateTimeOffset.UtcNow.AddDays(-1);

var mprn = "A123Z";
var serialNumber = "J0123456K";
var interval = Interval.Hour;
const string mprn = "A123Z";
const string serialNumber = "J0123456K";
const Interval interval = Interval.Hour;

var consumption = new PagedResults<Consumption>
var consumption = new PagedResults<Consumption>
{
Results = new List<Consumption>
{
Results = new List<Consumption> {
new Consumption {
Start = from,
End = from.AddMinutes(30),
Quantity = 0.5m
},
new Consumption {
Start = from.AddMinutes(30),
End = from.AddMinutes(60),
Quantity = 0.75m
},
new()
{
Start = from,
End = from.AddMinutes(30),
Quantity = 0.5m
},
Count = 1,
Next = null,
Previous = null
};
new()
{
Start = from.AddMinutes(30),
End = from.AddMinutes(60),
Quantity = 0.75m
}
},
Count = 1,
Next = string.Empty,
Previous = string.Empty
};

var uri = OctopusEnergyClient.ComposeGetGasConsumptionUri(mprn, serialNumber, from, to, interval);
var uri = OctopusEnergyClient.ComposeGetGasConsumptionUri(OctopusEnergyClient.DefaultBaseAddress, mprn,
serialNumber, from, to, interval);

var client = TestHelper.CreateClient(uri, consumption);
var client = TestHelper.CreateClient(uri, consumption);

var consumption1 = await client.GetGasConsumptionAsync("key", mprn, serialNumber, from, to, Interval.Hour);
var consumption1 = await client.GetGasConsumptionAsync(mprn, serialNumber, from, to, Interval.Hour);

Assert.AreEqual(consumption.Results.Count(), consumption1.Count());
Assert.AreEqual(consumption.Results.Count(), consumption1.Count());

var firstExpected = consumption.Results.First();
var firstActual = consumption1.First();
var firstExpected = consumption.Results.First();
var firstActual = consumption1.First();

Assert.AreEqual(firstExpected.Start, firstActual.Start);
Assert.AreEqual(firstExpected.End, firstActual.End);
Assert.AreEqual(firstExpected.Quantity, firstActual.Quantity);
}
Assert.AreEqual(firstExpected.Start, firstActual.Start);
Assert.AreEqual(firstExpected.End, firstActual.End);
Assert.AreEqual(firstExpected.Quantity, firstActual.Quantity);
}
}
}
86 changes: 45 additions & 41 deletions ImpSoft.OctopusEnergy.Api.Tests/FakeHttpMessageHandler.cs
Original file line number Diff line number Diff line change
@@ -1,61 +1,65 @@
using System;
using System.Collections.Generic;
using CommunityToolkit.Diagnostics;
using System;
using System.Diagnostics;
using System.Net.Http;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;

namespace ImpSoft.OctopusEnergy.Api.Tests
namespace ImpSoft.OctopusEnergy.Api.Tests;

internal class FakeHttpMessageHandler<TResponse> : HttpClientHandler where TResponse : class, new()
{
class FakeHttpMessageHandler<TResponse> : HttpClientHandler where TResponse : class
public FakeHttpMessageHandler(Uri expectedUri, TResponse response)
{
public FakeHttpMessageHandler(Uri expectedUri, TResponse response)
{
Preconditions.IsNotNull(response, nameof(response));
AutomaticDecompression = System.Net.DecompressionMethods.All;
ResponseObject = response;
ExpectedRequestUri = expectedUri;
}
Guard.IsNotNull(response);
Guard.IsNotNull(expectedUri);

public FakeHttpMessageHandler(Uri expectedUri, string response)
{
Preconditions.IsNotNullOrWhiteSpace(response, nameof(response));
AutomaticDecompression = System.Net.DecompressionMethods.All;
ResponseString = response;
ExpectedRequestUri = expectedUri;
}
#if NET48_OR_GREATER
AutomaticDecompression = System.Net.DecompressionMethods.None;
#else
AutomaticDecompression = System.Net.DecompressionMethods.All;
#endif
ResponseObject = response;
ExpectedRequestUri = expectedUri;

public FakeHttpMessageHandler(IList<(Uri, TResponse response)> pages)
{
// TODO: enable faking/testing paged responses
}
ResponseString = string.Empty;
}

private TResponse ResponseObject { get; }
private string ResponseString { get; }
public Uri ExpectedRequestUri { get; }
// ReSharper disable once MemberCanBePrivate.Global
public Uri ExpectedRequestUri { get; }
private TResponse? ResponseObject { get; }
private string ResponseString { get; }

protected async override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
await Task.Yield();
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,
CancellationToken cancellationToken)
{
await Task.Yield();

if (request.RequestUri.AbsoluteUri != ExpectedRequestUri.AbsoluteUri)
if (request.RequestUri?.AbsoluteUri != ExpectedRequestUri.AbsoluteUri)
{
return new HttpResponseMessage
{
return new HttpResponseMessage
{
StatusCode = System.Net.HttpStatusCode.NotFound
};
}
StatusCode = System.Net.HttpStatusCode.NotFound
};
}

Debug.WriteLine(ExpectedRequestUri.AbsoluteUri);
Debug.WriteLine(ExpectedRequestUri.AbsoluteUri);

var httpResponse = ResponseObject != null
? new HttpResponseMessage { Content = new StringContent(JsonSerializer.Serialize(ResponseObject)) }
: new HttpResponseMessage { Content = new StringContent(ResponseString) };
var httpResponse = ResponseObject != null
? new HttpResponseMessage { Content = new StringContent(JsonSerializer.Serialize(ResponseObject)) }
: new HttpResponseMessage { Content = new StringContent(ResponseString) };

if (httpResponse.Content.Headers.ContentType != null)
{
httpResponse.Content.Headers.ContentType.MediaType = "application/json";

return httpResponse;
}
else
{
throw new InvalidOperationException(
$"{nameof(httpResponse.Content.Headers.ContentType)} is unexpectedly null.");
}

return httpResponse;
}
}
}
Loading

0 comments on commit 0d698b4

Please sign in to comment.