Skip to content

Commit

Permalink
Consider null value in carbonIntensity on EM (#575)
Browse files Browse the repository at this point in the history
Issue #571

Signed-off-by: Yasumasa Suenaga <[email protected]>
  • Loading branch information
YaSuenag authored Oct 15, 2024
1 parent f05dcf4 commit 0afba03
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ internal record CarbonIntensity
/// Carbon Intensity value.
/// </summary>
[JsonPropertyName("carbonIntensity")]
public int Value { get; init; }
public int? Value { get; init; }

/// <summary>
/// Indicates the datetime of the carbon intensity
Expand Down Expand Up @@ -74,7 +74,7 @@ public static explicit operator EmissionsData(CarbonIntensity historyCarbonInten
{
return new EmissionsData
{
Rating = historyCarbonIntensity.Value,
Rating = historyCarbonIntensity.Value ?? -1,
Time = historyCarbonIntensity.UpdatedAt,
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Moq.Protected;
using Moq.Contrib.HttpClient;
using System.Text.Json;
using CarbonAware.Model;

namespace CarbonAware.DataSources.ElectricityMaps.Tests;

Expand Down Expand Up @@ -299,4 +300,36 @@ private void AddHandler_RequestResponse(Predicate<HttpRequestMessage> requestPre
.ReturnsResponse(statusCode);
}
}

[Test]
public async Task GetRecentCarbonIntensityHistoryAsync_DeserializesExpectedResponseWithNull()
{
AddHandler_RequestResponse(r =>
{
return r.Method == HttpMethod.Get;
}, System.Net.HttpStatusCode.OK, TestData.GetHistoryCarbonIntensityDataJsonStringWithNull());

var client = new ElectricityMapsClient(this.HttpClientFactory, this.Options.Object, this.Log.Object);

// Act
var data = await client.GetRecentCarbonIntensityHistoryAsync(TestLatitude, TestLongitude);
var dataPoint = data?.HistoryData.First();

// Assert
Assert.That(data, Is.Not.Null);
Assert.That(data?.Zone, Is.EqualTo(TestZone));
Assert.That(data?.HistoryData.Count(), Is.GreaterThan(0));
Assert.Multiple(() =>
{
Assert.That(dataPoint?.DateTime, Is.EqualTo(new DateTimeOffset(2099, 1, 1, 0, 0, 0, TimeSpan.Zero)));
Assert.That(dataPoint?.UpdatedAt, Is.EqualTo(new DateTimeOffset(2099, 1, 1, 0, 0, 0, TimeSpan.Zero)));
Assert.That(dataPoint?.CreatedAt, Is.EqualTo(new DateTimeOffset(2099, 1, 1, 0, 0, 0, TimeSpan.Zero)));
Assert.That(dataPoint?.Value, Is.Null);
Assert.That(((EmissionsData)dataPoint).Rating, Is.EqualTo(-1));
Assert.That(dataPoint?.EmissionFactorType, Is.EqualTo("lifecycle"));
Assert.That(dataPoint?.IsEstimated, Is.False);
Assert.That(dataPoint?.EstimationMethod, Is.Null);
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,28 @@ public static string GetCurrentForecastJsonString()

return json.ToString();
}

public static string GetHistoryCarbonIntensityDataJsonStringWithNull()
{
var json = new JsonObject
{
["zone"] = TestZoneId1,
["history"] = new JsonArray
{
new JsonObject
{
["datetime"] = new DateTimeOffset(2099, 1, 1, 0, 0, 0, TimeSpan.Zero),
["updatedAt"] = new DateTimeOffset(2099, 1, 1, 0, 0, 0, TimeSpan.Zero),
["createdAt"] = new DateTimeOffset(2099, 1, 1, 0, 0, 0, TimeSpan.Zero),
["carbonIntensity"] = null,
["emissionFactorType"] = "lifecycle",
["isEstimated"] = false,
["estimatedMethod"] = null,
}
}
};

return json.ToString();
}

}

0 comments on commit 0afba03

Please sign in to comment.