Skip to content

Commit

Permalink
Merge pull request #389 from YaSuenag/pr/emfree-timerange
Browse files Browse the repository at this point in the history
EMFree data source should regard specified time range
  • Loading branch information
Willmish authored Sep 20, 2023
2 parents cfd76fe + 1695a63 commit ff355f3
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public async Task Emissions_StartAndEndOptions_ReturnsExpectedData()
_dataSourceMocker.SetupDataMock(start, end, location);

// Act
var exitCode = await InvokeCliAsync($"emissions -l {location} -s 2022-09-01T02:01:00Z -e 2022-09-01T02:04:00Z");
var exitCode = await InvokeCliAsync($"emissions -l {location} -s 2022-09-01T00:00:00Z -e 2022-09-01T00:04:00Z");

// Assert
Assert.AreEqual(0, exitCode);
Expand Down Expand Up @@ -117,7 +117,7 @@ public async Task Emissions_BestOption_ReturnsExpectedData()
_dataSourceMocker.SetupDataMock(start, end, location);

// Act
var exitCode = await InvokeCliAsync($"emissions -l {location} -s 2022-09-01T02:01:00Z -e 2022-09-01T02:04:00Z -b");
var exitCode = await InvokeCliAsync($"emissions -l {location} -s 2022-09-01T00:00:00Z -e 2022-09-01T00:04:00Z -b");

// Assert
Assert.AreEqual(0, exitCode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ public async Task EmissionsForecasts_StartAndEndOptions_ReturnsExpectedData()
var location = "eastus";
var start = DateTimeOffset.UtcNow.AddMinutes(10);
var end = start.AddHours(5);
var dataStartAt = start.ToString("yyyy-MM-ddTHH:mm:ss");
var dataEndAt = end.ToString("yyyy-MM-ddTHH:mm:ss");
var dataStartAt = start.ToString("yyyy-MM-ddTHH:mm:ssZ");
var dataEndAt = end.ToString("yyyy-MM-ddTHH:mm:ssZ");

_dataSourceMocker.SetupForecastMock();
// Act
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,29 @@ public async Task<IEnumerable<EmissionsData>> GetCarbonIntensityAsync(Location l
}

List<EmissionsData> EmissionsDataList = new List<EmissionsData>();
var emissionDateTime = gridEmissionData.Data.Datetime;

var emissionData = new EmissionsData()
// periodStartTime should be less than current date time because this method should not handle forecast data.
var shouldReturn = periodStartTime <= DateTimeOffset.UtcNow;
if (shouldReturn && emissionDateTime != null && periodStartTime < periodEndTime)
{
Location = location.Name ?? "",
Time = gridEmissionData.Data.Datetime ?? new DateTimeOffset(),
Rating = gridEmissionData.Data.CarbonIntensity ?? 0.0,
Duration = new TimeSpan(0, 0, 0)
};
EmissionsDataList.Add(emissionData);
// periodEndTime would be set periodStartTime in EmissionHandler if it is not specified.
// So we can assume we should return the most recent data if they equal.
// If not, we should return the data after checking it is within specified time range.
shouldReturn = periodStartTime <= emissionDateTime && emissionDateTime < periodEndTime;
}

if (shouldReturn)
{
var emissionData = new EmissionsData()
{
Location = location.Name ?? "",
Time = emissionDateTime ?? new DateTimeOffset(),
Rating = gridEmissionData.Data.CarbonIntensity ?? 0.0,
Duration = new TimeSpan(0, 0, 0)
};
EmissionsDataList.Add(emissionData);
}

return EmissionsDataList;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ public ElectricityMapsFreeDataSourceTests()
_dataSource = new ElectricityMapsFreeDataSource(_logger.Object, _ElectricityMapsFreeClient.Object, _locationSource.Object);
}

[Test]
public async Task GetCarbonIntensity_ReturnsResultsWhenRecordsFound()
[TestCase(false, TestName = "GetCarbonIntensity_ReturnsResultsWhenRecordsFound without emission date time")]
[TestCase(true, TestName = "GetCarbonIntensity_ReturnsResultsWhenRecordsFound with emission date time")]
public async Task GetCarbonIntensity_ReturnsResultsWhenRecordsFound(bool withEmissionDateTime)
{
var startDate = DateTimeOffset.UtcNow.AddHours(-10);
var endDate = startDate.AddHours(1);
Expand All @@ -49,6 +50,7 @@ public async Task GetCarbonIntensity_ReturnsResultsWhenRecordsFound()
{
Data = new Data()
{
Datetime = withEmissionDateTime ? startDate.AddMinutes(30) : null,
CarbonIntensity = expectedCarbonIntensity,
}
};
Expand All @@ -71,8 +73,9 @@ public async Task GetCarbonIntensity_ReturnsResultsWhenRecordsFound()
this._locationSource.Verify(l => l.ToGeopositionLocationAsync(_defaultLocation));
}

[Test]
public async Task GetCarbonIntensity_UseRegionNameWhenCoordinatesNotAvailable()
[TestCase(false, TestName = "GetCarbonIntensity_UseRegionNameWhenCoordinatesNotAvailable without emission date time")]
[TestCase(true, TestName = "GetCarbonIntensity_UseRegionNameWhenCoordinatesNotAvailable with emission date time")]
public async Task GetCarbonIntensity_UseRegionNameWhenCoordinatesNotAvailable(bool withEmissionDateTime)
{
var startDate = new DateTimeOffset(2022, 4, 18, 12, 32, 42, TimeSpan.FromHours(-6));
var endDate = startDate.AddMinutes(1);
Expand All @@ -84,6 +87,7 @@ public async Task GetCarbonIntensity_UseRegionNameWhenCoordinatesNotAvailable()
{
Data = new Data()
{
Datetime = withEmissionDateTime ? startDate.AddSeconds(30) : null,
CarbonIntensity = expectedCarbonIntensity,
}
};
Expand Down

0 comments on commit ff355f3

Please sign in to comment.