Skip to content

Commit

Permalink
Remove unnecessary grouping
Browse files Browse the repository at this point in the history
  • Loading branch information
romankr committed Jan 22, 2024
1 parent 274f856 commit 478e606
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,7 @@ public void Run_WithValidArguments_ReturnsValidResponse()

var predictions = new[]
{
new EventPredictionBuilder().SetSampleData().SetCommenceTime(timestamp.AddDays(-2)).Instance,
new EventPredictionBuilder().SetSampleData()
.SetCommenceTime(timestamp.AddDays(2))
.SetTimestamp(timestamp.AddDays(-2)).Instance,
new EventPredictionBuilder().SetSampleData()
.SetCommenceTime(timestamp.AddDays(2))
.SetTimestamp(timestamp.AddDays(2)).Instance
new EventPredictionBuilder().SetSampleData().SetTimestamp(timestamp).Instance,
};

var response = function.Run(requestStub, predictions);
Expand All @@ -57,39 +51,6 @@ public void Run_WithValidArguments_ReturnsValidResponse()

var deserialized = JsonSerializer.Deserialize<EventPrediction[]>(text);
deserialized.Should().NotBeNull().And.HaveCount(1);
deserialized![0].Timestamp.Should().Be(timestamp.AddDays(2));
}

[Test]
public void Run_WithInvalidArguments_ReturnsValidResponse()
{
var loggerStub = Substitute.For<ILogger<PredictionsHttpFunction>>();

var function = new PredictionsHttpFunction(loggerStub);

var contextStub = Substitute.For<FunctionContext>();

var headersMock = Substitute.For<HttpHeadersCollection>();

var responseStream = new MemoryStream();

var responseMock = Substitute.For<HttpResponseData>(contextStub);
responseMock.Headers.Returns(headersMock);
responseMock.Body.Returns(responseStream);

var requestStub = Substitute.For<HttpRequestData>(contextStub);

requestStub.CreateResponse().Returns(responseMock);

var response = function.Run(requestStub, null!);

response.Should().NotBeNull();
responseMock.StatusCode.Should().Be(HttpStatusCode.InternalServerError);

using var reader = new StreamReader(responseMock.Body);
responseMock.Body.Seek(0, SeekOrigin.Begin);
var text = reader.ReadToEnd();

text.Should().Be("Failed to return predictions");
deserialized![0].Timestamp.Should().Be(timestamp);
}
}
7 changes: 1 addition & 6 deletions OddsCollector.Functions/Functions/PredictionsHttpFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,7 @@ public HttpResponseData Run(
{
try
{
// cosmosdb sql doesn't support grouping
// so doing it manually
var grouped = predictions.GroupBy(p => p.Id)
.Select(group => group.OrderByDescending(p => p.Timestamp).First()).ToList();

var serialized = JsonSerializer.Serialize(grouped, SerializerOptions);
var serialized = JsonSerializer.Serialize(predictions, SerializerOptions);

return CreateResponse(HttpStatusCode.OK, request, serialized);
}
Expand Down

0 comments on commit 478e606

Please sign in to comment.