Skip to content

Commit 8640c8c

Browse files
committed
Lots of test updates
Lots of test updates, need to do some fixes.
1 parent aa81382 commit 8640c8c

File tree

11 files changed

+219
-176
lines changed

11 files changed

+219
-176
lines changed

src/CarbonAware.DataSources/CarbonAware.DataSources.WattTime/mock/WattTimeDataSourceMocker.cs

Lines changed: 49 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
using CarbonAware.Interfaces;
2-
using CarbonAware.DataSources.WattTime.Constants;
1+
using CarbonAware.DataSources.WattTime.Constants;
32
using CarbonAware.DataSources.WattTime.Model;
3+
using CarbonAware.Interfaces;
44
using System.Net;
55
using System.Net.Mime;
66
using System.Text.Json;
@@ -13,11 +13,11 @@ internal class WattTimeDataSourceMocker : IDataSourceMocker
1313
{
1414
protected WireMockServer _server;
1515

16-
private static readonly RegionResponse defaultBalancingAuthority = new()
16+
private static readonly RegionResponse defaultRegion = new()
1717
{
18-
Id = 12345,
19-
Region = "TEST_BA",
20-
RegionFullName = "Test Balancing Authority"
18+
Region = "TEST_REGION",
19+
RegionFullName = "Test Region Full Name",
20+
SignalType = SignalTypes.co2_moer
2121
};
2222

2323
private static readonly LoginResult defaultLoginResult = new() { Token = "myDefaultToken123" };
@@ -39,20 +39,31 @@ public void SetupDataMock(DateTimeOffset start, DateTimeOffset end, string locat
3939
{
4040
var newDataPoint = new GridEmissionDataPoint()
4141
{
42-
Region = defaultBalancingAuthority.Region,
4342
PointTime = pointTime,
4443
Value = 999.99F,
4544
Version = "1.0",
46-
SignalType = "dt",
4745
Frequency = 300,
4846
Market = "mkt",
4947
};
5048

49+
5150
data.Add(newDataPoint);
5251
pointTime = newDataPoint.PointTime + duration;
5352
}
5453

55-
SetupResponseGivenGetRequest(Paths.Data, JsonSerializer.Serialize(data));
54+
var meta = new GridEmissionsMetaData()
55+
{
56+
Region = defaultRegion.Region,
57+
SignalType = SignalTypes.co2_moer
58+
};
59+
60+
var gridEmissionsResponse = new GridEmissionsDataResponse()
61+
{
62+
Data = data,
63+
Meta = meta
64+
};
65+
66+
SetupResponseGivenGetRequest(Paths.Data, JsonSerializer.Serialize(gridEmissionsResponse));
5667
}
5768

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

6980
while (pointTime < end)
7081
{
7182
var newForecastPoint = new GridEmissionDataPoint()
7283
{
73-
Region = defaultBalancingAuthority.Region,
74-
SignalType = "dt",
7584
Frequency = 300,
7685
Market = "mkt",
7786
PointTime = start,
@@ -80,32 +89,39 @@ public void SetupForecastMock()
8089
};
8190
newForecastPoint.PointTime = pointTime;
8291
newForecastPoint.Value = currValue;
83-
ForecastData.Add(newForecastPoint);
92+
forecastData.Add(newForecastPoint);
8493
pointTime = pointTime + TimeSpan.FromMinutes(5);
8594
currValue = currValue + 5.0F;
8695
}
8796

88-
var forecast = new Forecast()
97+
var meta = new GridEmissionsMetaData()
8998
{
90-
ForecastData = ForecastData,
99+
Region = defaultRegion.Region,
100+
SignalType = SignalTypes.co2_moer,
91101
GeneratedAt = new DateTimeOffset(2022, 1, 1, 0, 0, 0, TimeSpan.Zero)
92102
};
93-
SetupResponseGivenGetRequest(Paths.Forecast, JsonSerializer.Serialize(forecast));
103+
104+
var forecastResponse = new ForecastEmissionsDataResponse()
105+
{
106+
Data = forecastData,
107+
Meta = meta
108+
};
109+
110+
111+
SetupResponseGivenGetRequest(Paths.Forecast, JsonSerializer.Serialize(forecastResponse));
94112
}
95113

96114
public void SetupBatchForecastMock()
97115
{
98116
var start = new DateTimeOffset(2021, 9, 1, 8, 30, 0, TimeSpan.Zero);
99117
var end = start + TimeSpan.FromDays(1.0);
100118
var pointTime = start;
101-
var ForecastData = new List<GridEmissionDataPoint>();
119+
var forecastData = new List<GridEmissionDataPoint>();
102120
var currValue = 200.0F;
103121
while (pointTime < end)
104122
{
105123
var newForecastPoint = new GridEmissionDataPoint()
106124
{
107-
Region = defaultBalancingAuthority.Region,
108-
SignalType = "dt",
109125
Frequency = 300,
110126
Market = "mkt",
111127
PointTime = start,
@@ -114,19 +130,26 @@ public void SetupBatchForecastMock()
114130
};
115131
newForecastPoint.PointTime = pointTime;
116132
newForecastPoint.Value = currValue;
117-
ForecastData.Add(newForecastPoint);
133+
forecastData.Add(newForecastPoint);
118134
pointTime = pointTime + TimeSpan.FromMinutes(5);
119135
currValue = currValue + 5.0F;
120136
}
121137

122-
var forecastData = new List<Forecast> {
123-
new Forecast()
138+
var meta = new GridEmissionsMetaData()
139+
{
140+
Region = defaultRegion.Region,
141+
SignalType = SignalTypes.co2_moer,
142+
GeneratedAt = new DateTimeOffset(2022, 1, 1, 0, 0, 0, TimeSpan.Zero)
143+
};
144+
145+
var forecastBatchData = new List<ForecastEmissionsDataResponse> {
146+
new ForecastEmissionsDataResponse()
124147
{
125-
ForecastData = ForecastData,
126-
GeneratedAt = new DateTimeOffset(2022, 1, 1, 0, 0, 0, TimeSpan.Zero)
148+
Data = forecastData,
149+
Meta = meta
127150
}
128151
};
129-
SetupResponseGivenGetRequest(Paths.Forecast, JsonSerializer.Serialize(forecastData));
152+
SetupResponseGivenGetRequest(Paths.Forecast, JsonSerializer.Serialize(forecastBatchData));
130153
}
131154

132155
public void Initialize()
@@ -157,7 +180,7 @@ private void SetupResponseGivenGetRequest(string path, string body)
157180
);
158181
}
159182
private void SetupBaMock(RegionResponse? content = null) =>
160-
SetupResponseGivenGetRequest(Paths.BalancingAuthorityFromLocation, JsonSerializer.Serialize(content ?? defaultBalancingAuthority));
183+
SetupResponseGivenGetRequest(Paths.BalancingAuthorityFromLocation, JsonSerializer.Serialize(content ?? defaultRegion));
161184

162185
private void SetupLoginMock(LoginResult? content = null) =>
163186
SetupResponseGivenGetRequest(Paths.Login, JsonSerializer.Serialize(content ?? defaultLoginResult));

src/CarbonAware.DataSources/CarbonAware.DataSources.WattTime/src/Client/IWattTimeClient.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@ internal interface IWattTimeClient
3636
/// <param name="balancingAuthorityAbbreviation">Balancing authority abbreviation</param>
3737
/// <returns>An <see cref="Task{Forecast}"/> which contains forecasted emissions data points.</returns>
3838
/// <exception cref="WattTimeClientException">Can be thrown when errors occur connecting to WattTime client. See the WattTimeClientException class for documentation of expected status codes.</exception>
39-
Task<Forecast> GetCurrentForecastAsync(string balancingAuthorityAbbreviation);
39+
Task<ForecastEmissionsDataResponse> GetCurrentForecastAsync(string balancingAuthorityAbbreviation);
4040

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

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

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

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

7676
/// <summary>
7777
/// Async method to get the balancing authority abbreviation for a given location.

src/CarbonAware.DataSources/CarbonAware.DataSources.WattTime/src/Client/WattTimeClient.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public Task<GridEmissionsDataResponse> GetDataAsync(RegionResponse region, DateT
8585
}
8686

8787
/// <inheritdoc/>
88-
public async Task<Forecast> GetCurrentForecastAsync(string region)
88+
public async Task<ForecastEmissionsDataResponse> GetCurrentForecastAsync(string region)
8989
{
9090

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

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

111111
return forecast;
112112
}
113113

114114
/// <inheritdoc/>
115-
public Task<Forecast> GetCurrentForecastAsync(RegionResponse balancingAuthority)
115+
public Task<ForecastEmissionsDataResponse> GetCurrentForecastAsync(RegionResponse balancingAuthority)
116116
{
117117
return this.GetCurrentForecastAsync(balancingAuthority.Region);
118118
}
119119

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

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

143143
/// <inheritdoc/>
144-
public Task<Forecast?> GetForecastOnDateAsync(RegionResponse balancingAuthority, DateTimeOffset requestedAt)
144+
public Task<ForecastEmissionsDataResponse?> GetForecastOnDateAsync(RegionResponse balancingAuthority, DateTimeOffset requestedAt)
145145
{
146146
return this.GetForecastOnDateAsync(balancingAuthority.Region, requestedAt);
147147
}
148148

149149
/// <inheritdoc/>
150-
public async Task<RegionResponse> GetBalancingAuthorityAsync(string latitude, string longitude)
150+
public async Task<RegionResponse> GetRegionAsync(string latitude, string longitude)
151151
{
152152
_log.LogInformation("Requesting balancing authority for lattitude {lattitude} and longitude {longitude}", latitude, longitude);
153153
return await GetBalancingAuthorityFromCacheAsync(latitude, longitude);
@@ -156,7 +156,7 @@ public async Task<RegionResponse> GetBalancingAuthorityAsync(string latitude, st
156156
/// <inheritdoc/>
157157
public async Task<string?> GetBalancingAuthorityAbbreviationAsync(string latitude, string longitude)
158158
{
159-
return (await this.GetBalancingAuthorityAsync(latitude, longitude))?.Region;
159+
return (await this.GetRegionAsync(latitude, longitude))?.Region;
160160
}
161161

162162
/// <inheritdoc/>

src/CarbonAware.DataSources/CarbonAware.DataSources.WattTime/src/Model/Forecast.cs

Lines changed: 0 additions & 22 deletions
This file was deleted.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System.Text.Json.Serialization;
2+
3+
namespace CarbonAware.DataSources.WattTime.Model;
4+
5+
[Serializable]
6+
internal record ForecastEmissionsDataResponse
7+
{
8+
[JsonPropertyName("data")]
9+
public List<GridEmissionDataPoint> Data { get; set; } = new List<GridEmissionDataPoint>();
10+
11+
12+
[JsonPropertyName("meta")]
13+
public GridEmissionsMetaData Meta { get; set; }
14+
}
15+
16+

src/CarbonAware.DataSources/CarbonAware.DataSources.WattTime/src/Model/GridEmissionsDataResponse.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,6 @@ internal record GridEmissionsDataResponse
1111

1212
[JsonPropertyName("meta")]
1313
public GridEmissionsMetaData Meta { get; set; }
14-
}
14+
}
15+
16+

src/CarbonAware.DataSources/CarbonAware.DataSources.WattTime/src/Model/GridEmissionsMetaData.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,5 @@ internal record GridEmissionsMetaData
3030
public int? GeneratedAtPeriodSeconds { get; set; }
3131

3232
[JsonPropertyName("generated_at")]
33-
public DateTimeOffset? GeneratedAt { get; set; }
33+
public DateTimeOffset GeneratedAt { get; set; } = DateTimeOffset.MinValue;
3434
}

src/CarbonAware.DataSources/CarbonAware.DataSources.WattTime/src/WattTimeDataSource.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,19 +103,19 @@ public async Task<EmissionsForecast> GetCarbonIntensityForecastAsync(Location lo
103103
return ForecastToEmissionsForecast(forecast, location, requestedAt);
104104
}
105105

106-
private EmissionsForecast ForecastToEmissionsForecast(Forecast forecast, Location location, DateTimeOffset requestedAt)
106+
private EmissionsForecast ForecastToEmissionsForecast(ForecastEmissionsDataResponse forecast, Location location, DateTimeOffset requestedAt)
107107
{
108-
var duration = GetDurationFromGridEmissionDataPoints(forecast.ForecastData);
109-
var forecastData = forecast.ForecastData.Select(e => new EmissionsData()
108+
var duration = GetDurationFromGridEmissionDataPoints(forecast.Data);
109+
var forecastData = forecast.Data.Select(e => new EmissionsData()
110110
{
111-
Location = "", //e.Region, // TODO: VAUGHAN
111+
Location = forecast.Meta.Region,
112112
Rating = ConvertMoerToGramsPerKilowattHour(e.Value),
113113
Time = e.PointTime,
114114
Duration = duration
115115
});
116116
var emissionsForecast = new EmissionsForecast()
117117
{
118-
GeneratedAt = forecast.GeneratedAt,
118+
GeneratedAt = forecast.Meta.GeneratedAt,
119119
Location = location,
120120
ForecastData = forecastData
121121
};
@@ -177,7 +177,7 @@ private async Task<RegionResponse> GetBalancingAuthority(Location location)
177177
try
178178
{
179179
var geolocation = await this.LocationSource.ToGeopositionLocationAsync(location);
180-
balancingAuthority = await WattTimeClient.GetBalancingAuthorityAsync(geolocation.LatitudeAsCultureInvariantString(), geolocation.LongitudeAsCultureInvariantString());
180+
balancingAuthority = await WattTimeClient.GetRegionAsync(geolocation.LatitudeAsCultureInvariantString(), geolocation.LongitudeAsCultureInvariantString());
181181
}
182182
catch (Exception ex) when (ex is LocationConversionException || ex is WattTimeClientHttpException)
183183
{

0 commit comments

Comments
 (0)