Skip to content

Commit

Permalink
Lots of test updates
Browse files Browse the repository at this point in the history
Lots of test updates, need to do some fixes.
  • Loading branch information
vaughanknight committed Jun 17, 2024
1 parent aa81382 commit 8640c8c
Show file tree
Hide file tree
Showing 11 changed files with 219 additions and 176 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using CarbonAware.Interfaces;
using CarbonAware.DataSources.WattTime.Constants;
using CarbonAware.DataSources.WattTime.Constants;
using CarbonAware.DataSources.WattTime.Model;
using CarbonAware.Interfaces;
using System.Net;
using System.Net.Mime;
using System.Text.Json;
Expand All @@ -13,11 +13,11 @@ internal class WattTimeDataSourceMocker : IDataSourceMocker
{
protected WireMockServer _server;

private static readonly RegionResponse defaultBalancingAuthority = new()
private static readonly RegionResponse defaultRegion = new()
{
Id = 12345,
Region = "TEST_BA",
RegionFullName = "Test Balancing Authority"
Region = "TEST_REGION",
RegionFullName = "Test Region Full Name",
SignalType = SignalTypes.co2_moer
};

private static readonly LoginResult defaultLoginResult = new() { Token = "myDefaultToken123" };
Expand All @@ -39,20 +39,31 @@ public void SetupDataMock(DateTimeOffset start, DateTimeOffset end, string locat
{
var newDataPoint = new GridEmissionDataPoint()
{
Region = defaultBalancingAuthority.Region,
PointTime = pointTime,
Value = 999.99F,
Version = "1.0",
SignalType = "dt",
Frequency = 300,
Market = "mkt",
};


data.Add(newDataPoint);
pointTime = newDataPoint.PointTime + duration;
}

SetupResponseGivenGetRequest(Paths.Data, JsonSerializer.Serialize(data));
var meta = new GridEmissionsMetaData()
{
Region = defaultRegion.Region,
SignalType = SignalTypes.co2_moer
};

var gridEmissionsResponse = new GridEmissionsDataResponse()
{
Data = data,
Meta = meta
};

SetupResponseGivenGetRequest(Paths.Data, JsonSerializer.Serialize(gridEmissionsResponse));
}

public void SetupForecastMock()
Expand All @@ -63,15 +74,13 @@ public void SetupForecastMock()
var start = new DateTimeOffset(((curr.Ticks + d.Ticks - 1) / d.Ticks) * d.Ticks, TimeSpan.Zero);
var end = start + TimeSpan.FromDays(1.0);
var pointTime = start;
var ForecastData = new List<GridEmissionDataPoint>();
var forecastData = new List<GridEmissionDataPoint>();
var currValue = 200.0F;

while (pointTime < end)
{
var newForecastPoint = new GridEmissionDataPoint()
{
Region = defaultBalancingAuthority.Region,
SignalType = "dt",
Frequency = 300,
Market = "mkt",
PointTime = start,
Expand All @@ -80,32 +89,39 @@ public void SetupForecastMock()
};
newForecastPoint.PointTime = pointTime;
newForecastPoint.Value = currValue;
ForecastData.Add(newForecastPoint);
forecastData.Add(newForecastPoint);
pointTime = pointTime + TimeSpan.FromMinutes(5);
currValue = currValue + 5.0F;
}

var forecast = new Forecast()
var meta = new GridEmissionsMetaData()
{
ForecastData = ForecastData,
Region = defaultRegion.Region,
SignalType = SignalTypes.co2_moer,
GeneratedAt = new DateTimeOffset(2022, 1, 1, 0, 0, 0, TimeSpan.Zero)
};
SetupResponseGivenGetRequest(Paths.Forecast, JsonSerializer.Serialize(forecast));

var forecastResponse = new ForecastEmissionsDataResponse()
{
Data = forecastData,
Meta = meta
};


SetupResponseGivenGetRequest(Paths.Forecast, JsonSerializer.Serialize(forecastResponse));
}

public void SetupBatchForecastMock()
{
var start = new DateTimeOffset(2021, 9, 1, 8, 30, 0, TimeSpan.Zero);
var end = start + TimeSpan.FromDays(1.0);
var pointTime = start;
var ForecastData = new List<GridEmissionDataPoint>();
var forecastData = new List<GridEmissionDataPoint>();
var currValue = 200.0F;
while (pointTime < end)
{
var newForecastPoint = new GridEmissionDataPoint()
{
Region = defaultBalancingAuthority.Region,
SignalType = "dt",
Frequency = 300,
Market = "mkt",
PointTime = start,
Expand All @@ -114,19 +130,26 @@ public void SetupBatchForecastMock()
};
newForecastPoint.PointTime = pointTime;
newForecastPoint.Value = currValue;
ForecastData.Add(newForecastPoint);
forecastData.Add(newForecastPoint);
pointTime = pointTime + TimeSpan.FromMinutes(5);
currValue = currValue + 5.0F;
}

var forecastData = new List<Forecast> {
new Forecast()
var meta = new GridEmissionsMetaData()
{
Region = defaultRegion.Region,
SignalType = SignalTypes.co2_moer,
GeneratedAt = new DateTimeOffset(2022, 1, 1, 0, 0, 0, TimeSpan.Zero)
};

var forecastBatchData = new List<ForecastEmissionsDataResponse> {
new ForecastEmissionsDataResponse()
{
ForecastData = ForecastData,
GeneratedAt = new DateTimeOffset(2022, 1, 1, 0, 0, 0, TimeSpan.Zero)
Data = forecastData,
Meta = meta
}
};
SetupResponseGivenGetRequest(Paths.Forecast, JsonSerializer.Serialize(forecastData));
SetupResponseGivenGetRequest(Paths.Forecast, JsonSerializer.Serialize(forecastBatchData));
}

public void Initialize()
Expand Down Expand Up @@ -157,7 +180,7 @@ private void SetupResponseGivenGetRequest(string path, string body)
);
}
private void SetupBaMock(RegionResponse? content = null) =>
SetupResponseGivenGetRequest(Paths.BalancingAuthorityFromLocation, JsonSerializer.Serialize(content ?? defaultBalancingAuthority));
SetupResponseGivenGetRequest(Paths.BalancingAuthorityFromLocation, JsonSerializer.Serialize(content ?? defaultRegion));

private void SetupLoginMock(LoginResult? content = null) =>
SetupResponseGivenGetRequest(Paths.Login, JsonSerializer.Serialize(content ?? defaultLoginResult));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ internal interface IWattTimeClient
/// <param name="balancingAuthorityAbbreviation">Balancing authority abbreviation</param>
/// <returns>An <see cref="Task{Forecast}"/> which contains forecasted emissions data points.</returns>
/// <exception cref="WattTimeClientException">Can be thrown when errors occur connecting to WattTime client. See the WattTimeClientException class for documentation of expected status codes.</exception>
Task<Forecast> GetCurrentForecastAsync(string balancingAuthorityAbbreviation);
Task<ForecastEmissionsDataResponse> GetCurrentForecastAsync(string balancingAuthorityAbbreviation);

/// <summary>
/// Async method to get the most recent 24 hour forecasted emission data for a given balancing authority.
/// </summary>
/// <param name="balancingAuthority">Balancing authority</param>
/// <returns>An <see cref="Task{Forecast}"/> which contains forecasted emissions data points.</returns>
/// <exception cref="WattTimeClientException">Can be thrown when errors occur connecting to WattTime client. See the WattTimeClientException class for documentation of expected status codes.</exception>
Task<Forecast> GetCurrentForecastAsync(RegionResponse balancingAuthority);
Task<ForecastEmissionsDataResponse> GetCurrentForecastAsync(RegionResponse balancingAuthority);

/// <summary>
/// Async method to get generated forecast at requested time and balancing authority.
Expand All @@ -53,7 +53,7 @@ internal interface IWattTimeClient
/// <param name="requestedAt">The historical time used to fetch the most recent forecast generated as of that time.</param>
/// <returns>An <see cref="Task{Forecast}"/> which contains forecasted emissions data points or null if no Forecast generated at the requested time.</returns>
/// <exception cref="WattTimeClientException">Can be thrown when errors occur connecting to WattTime client. See the WattTimeClientException class for documentation of expected status codes.</exception>
Task<Forecast?> GetForecastOnDateAsync(string balancingAuthorityAbbreviation, DateTimeOffset requestedAt);
Task<ForecastEmissionsDataResponse?> GetForecastOnDateAsync(string balancingAuthorityAbbreviation, DateTimeOffset requestedAt);

/// <summary>
/// Async method to get generated forecast at requested time and balancing authority.
Expand All @@ -62,7 +62,7 @@ internal interface IWattTimeClient
/// <param name="requestedAt">The historical time used to fetch the most recent forecast generated as of that time.</param>
/// <returns>An <see cref="Task{Forecast}"/> which contains forecasted emissions data points or null if no Forecast generated at the requested time.</returns>
/// <exception cref="WattTimeClientException">Can be thrown when errors occur connecting to WattTime client. See the WattTimeClientException class for documentation of expected status codes.</exception>
Task<Forecast?> GetForecastOnDateAsync(RegionResponse balancingAuthority, DateTimeOffset requestedAt);
Task<ForecastEmissionsDataResponse?> GetForecastOnDateAsync(RegionResponse balancingAuthority, DateTimeOffset requestedAt);

/// <summary>
/// Async method to get the balancing authority for a given location.
Expand All @@ -71,7 +71,7 @@ internal interface IWattTimeClient
/// <param name="longitude">Longitude of the location</param>
/// <returns>An <see cref="Task{BalancingAuthority}"/> which contains the balancing authority details.</returns>
/// <exception cref="WattTimeClientException">Can be thrown when errors occur connecting to WattTime client. See the WattTimeClientException class for documentation of expected status codes.</exception>
Task<RegionResponse> GetBalancingAuthorityAsync(string latitude, string longitude);
Task<RegionResponse> GetRegionAsync(string latitude, string longitude);

/// <summary>
/// Async method to get the balancing authority abbreviation for a given location.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public Task<GridEmissionsDataResponse> GetDataAsync(RegionResponse region, DateT
}

/// <inheritdoc/>
public async Task<Forecast> GetCurrentForecastAsync(string region)
public async Task<ForecastEmissionsDataResponse> GetCurrentForecastAsync(string region)
{

_log.LogInformation("Requesting current forecast from balancing authority {balancingAuthority}", region);
Expand All @@ -106,19 +106,19 @@ public async Task<Forecast> GetCurrentForecastAsync(string region)
var sr = new StreamReader(result);
var s = sr.ReadToEnd();

var forecast = await JsonSerializer.DeserializeAsync<Forecast?>(result, _options) ?? throw new WattTimeClientException($"Error getting forecast for {region}");
var forecast = await JsonSerializer.DeserializeAsync<ForecastEmissionsDataResponse?>(result, _options) ?? throw new WattTimeClientException($"Error getting forecast for {region}");

return forecast;
}

/// <inheritdoc/>
public Task<Forecast> GetCurrentForecastAsync(RegionResponse balancingAuthority)
public Task<ForecastEmissionsDataResponse> GetCurrentForecastAsync(RegionResponse balancingAuthority)
{
return this.GetCurrentForecastAsync(balancingAuthority.Region);
}

/// <inheritdoc/>
public async Task<Forecast?> GetForecastOnDateAsync(string balancingAuthorityAbbreviation, DateTimeOffset requestedAt)
public async Task<ForecastEmissionsDataResponse?> GetForecastOnDateAsync(string balancingAuthorityAbbreviation, DateTimeOffset requestedAt)
{
_log.LogInformation($"Requesting forecast from balancingAuthority {balancingAuthorityAbbreviation} generated at {requestedAt}.");

Expand All @@ -135,19 +135,19 @@ public Task<Forecast> GetCurrentForecastAsync(RegionResponse balancingAuthority)
};
using (var result = await this.MakeRequestGetStreamAsync(Paths.Forecast, parameters, tags))
{
var forecasts = await JsonSerializer.DeserializeAsync<List<Forecast>>(result, _options) ?? throw new WattTimeClientException($"Error getting forecasts for {balancingAuthorityAbbreviation}");
var forecasts = await JsonSerializer.DeserializeAsync<List<ForecastEmissionsDataResponse>>(result, _options) ?? throw new WattTimeClientException($"Error getting forecasts for {balancingAuthorityAbbreviation}");
return forecasts.FirstOrDefault();
}
}

/// <inheritdoc/>
public Task<Forecast?> GetForecastOnDateAsync(RegionResponse balancingAuthority, DateTimeOffset requestedAt)
public Task<ForecastEmissionsDataResponse?> GetForecastOnDateAsync(RegionResponse balancingAuthority, DateTimeOffset requestedAt)
{
return this.GetForecastOnDateAsync(balancingAuthority.Region, requestedAt);
}

/// <inheritdoc/>
public async Task<RegionResponse> GetBalancingAuthorityAsync(string latitude, string longitude)
public async Task<RegionResponse> GetRegionAsync(string latitude, string longitude)
{
_log.LogInformation("Requesting balancing authority for lattitude {lattitude} and longitude {longitude}", latitude, longitude);
return await GetBalancingAuthorityFromCacheAsync(latitude, longitude);
Expand All @@ -156,7 +156,7 @@ public async Task<RegionResponse> GetBalancingAuthorityAsync(string latitude, st
/// <inheritdoc/>
public async Task<string?> GetBalancingAuthorityAbbreviationAsync(string latitude, string longitude)
{
return (await this.GetBalancingAuthorityAsync(latitude, longitude))?.Region;
return (await this.GetRegionAsync(latitude, longitude))?.Region;
}

/// <inheritdoc/>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Text.Json.Serialization;

namespace CarbonAware.DataSources.WattTime.Model;

[Serializable]
internal record ForecastEmissionsDataResponse
{
[JsonPropertyName("data")]
public List<GridEmissionDataPoint> Data { get; set; } = new List<GridEmissionDataPoint>();


[JsonPropertyName("meta")]
public GridEmissionsMetaData Meta { get; set; }
}


Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ internal record GridEmissionsDataResponse

[JsonPropertyName("meta")]
public GridEmissionsMetaData Meta { get; set; }
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ internal record GridEmissionsMetaData
public int? GeneratedAtPeriodSeconds { get; set; }

[JsonPropertyName("generated_at")]
public DateTimeOffset? GeneratedAt { get; set; }
public DateTimeOffset GeneratedAt { get; set; } = DateTimeOffset.MinValue;
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,19 +103,19 @@ public async Task<EmissionsForecast> GetCarbonIntensityForecastAsync(Location lo
return ForecastToEmissionsForecast(forecast, location, requestedAt);
}

private EmissionsForecast ForecastToEmissionsForecast(Forecast forecast, Location location, DateTimeOffset requestedAt)
private EmissionsForecast ForecastToEmissionsForecast(ForecastEmissionsDataResponse forecast, Location location, DateTimeOffset requestedAt)
{
var duration = GetDurationFromGridEmissionDataPoints(forecast.ForecastData);
var forecastData = forecast.ForecastData.Select(e => new EmissionsData()
var duration = GetDurationFromGridEmissionDataPoints(forecast.Data);
var forecastData = forecast.Data.Select(e => new EmissionsData()
{
Location = "", //e.Region, // TODO: VAUGHAN
Location = forecast.Meta.Region,
Rating = ConvertMoerToGramsPerKilowattHour(e.Value),
Time = e.PointTime,
Duration = duration
});
var emissionsForecast = new EmissionsForecast()
{
GeneratedAt = forecast.GeneratedAt,
GeneratedAt = forecast.Meta.GeneratedAt,
Location = location,
ForecastData = forecastData
};
Expand Down Expand Up @@ -177,7 +177,7 @@ private async Task<RegionResponse> GetBalancingAuthority(Location location)
try
{
var geolocation = await this.LocationSource.ToGeopositionLocationAsync(location);
balancingAuthority = await WattTimeClient.GetBalancingAuthorityAsync(geolocation.LatitudeAsCultureInvariantString(), geolocation.LongitudeAsCultureInvariantString());
balancingAuthority = await WattTimeClient.GetRegionAsync(geolocation.LatitudeAsCultureInvariantString(), geolocation.LongitudeAsCultureInvariantString());
}
catch (Exception ex) when (ex is LocationConversionException || ex is WattTimeClientHttpException)
{
Expand Down
Loading

0 comments on commit 8640c8c

Please sign in to comment.