Skip to content

Commit 781f7a7

Browse files
committed
Added test for enum serialization
Added test for enum serialization
1 parent 1e58ef7 commit 781f7a7

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

src/CarbonAware.DataSources/CarbonAware.DataSources.WattTime/test/Client/WattTimeClientTests.cs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,60 @@ public async Task GetHistoricalDataAsync_RefreshesTokenWhenNoneSet()
413413
}
414414
}
415415

416+
[Test]
417+
public async Task JsonStringEnumConverter_CorrectlyDeserializesEnumValues()
418+
{
419+
// Arrange
420+
// Create a custom response with a specific signal_type enum value
421+
var customResponse = new
422+
{
423+
meta = new
424+
{
425+
region = WattTimeTestData.Constants.Region,
426+
generated_at = WattTimeTestData.Constants.GeneratedAt,
427+
generated_at_period_seconds = 30,
428+
signal_type = "co2_aoer", // Different from default co2_moer to verify conversion
429+
units = "lbs_co2_per_mwh",
430+
model = new { date = WattTimeTestData.Constants.Date, type = "binned_regression" }
431+
},
432+
data = new[]
433+
{
434+
new {
435+
point_time = WattTimeTestData.Constants.PointTime,
436+
value = WattTimeTestData.Constants.Value,
437+
frequency = WattTimeTestData.Constants.Frequency,
438+
market = WattTimeTestData.Constants.Market,
439+
version = WattTimeTestData.Constants.Version
440+
}
441+
}
442+
};
443+
444+
var responseJson = System.Text.Json.JsonSerializer.Serialize(customResponse);
445+
446+
this.AddHandlers_Auth();
447+
this.AddHandler_RequestResponse(r =>
448+
{
449+
return r.RequestUri!.ToString().Contains("/v3/historical") && r.Method == HttpMethod.Get;
450+
}, System.Net.HttpStatusCode.OK, responseJson);
451+
452+
// Act
453+
var client = new WattTimeClient(this.HttpClientFactory, this.Options.Object, this.Log.Object, this.MemoryCache);
454+
client.SetBearerAuthenticationHeader(_DEFAULT_TOKEN_VALUE);
455+
456+
var emissionsResponse = await client.GetDataAsync(
457+
WattTimeTestData.Constants.Region,
458+
new DateTimeOffset(2022, 4, 22, 0, 0, 0, TimeSpan.Zero),
459+
new DateTimeOffset(2022, 4, 22, 0, 0, 0, TimeSpan.Zero));
460+
461+
// Assert
462+
Assert.IsNotNull(emissionsResponse);
463+
Assert.AreEqual(SignalTypes.co2_aoer, emissionsResponse.Meta.SignalType);
464+
465+
// Verify other properties were correctly deserialized
466+
Assert.AreEqual(WattTimeTestData.Constants.Region, emissionsResponse.Meta.Region);
467+
Assert.IsTrue(emissionsResponse.Data.Count() > 0);
468+
}
469+
416470
/**
417471
* Helper to add client handlers for auth checking
418472
*/

0 commit comments

Comments
 (0)