Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider null value in carbonIntensity on EM #575

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
.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));

Check warning on line 328 in src/CarbonAware.DataSources/CarbonAware.DataSources.ElectricityMaps/test/Client/ElectricityMapsClientTests.cs

View workflow job for this annotation

GitHub Actions / sln-build-and-test (csharp)

Possible null reference argument for parameter 'historyCarbonIntensity' in 'CarbonIntensity.explicit operator EmissionsData(CarbonIntensity historyCarbonIntensity)'.

Check warning on line 328 in src/CarbonAware.DataSources/CarbonAware.DataSources.ElectricityMaps/test/Client/ElectricityMapsClientTests.cs

View workflow job for this annotation

GitHub Actions / webapp-container-dotnet-build

Possible null reference argument for parameter 'historyCarbonIntensity' in 'CarbonIntensity.explicit operator EmissionsData(CarbonIntensity historyCarbonIntensity)'.
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();
}

}
Loading