Skip to content

Commit

Permalink
Consider null value in carbonIntensity on EM
Browse files Browse the repository at this point in the history
Issue Green-Software-Foundation#571

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

/// <summary>
/// Indicates the datetime of the carbon intensity
Expand Down Expand Up @@ -74,7 +75,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 ba68e52

Please sign in to comment.