From 478e60691fefbdfa24065bc371ea3c55fbfffb2f Mon Sep 17 00:00:00 2001 From: romankr Date: Tue, 23 Jan 2024 01:18:16 +0400 Subject: [PATCH] Remove unnecessary grouping --- .../Functions/PredictionsHttpFunctionTests.cs | 43 +------------------ .../Functions/PredictionsHttpFunction.cs | 7 +-- 2 files changed, 3 insertions(+), 47 deletions(-) diff --git a/OddsCollector.Functions.Tests/Tests/Functions/PredictionsHttpFunctionTests.cs b/OddsCollector.Functions.Tests/Tests/Functions/PredictionsHttpFunctionTests.cs index dc44830..18f7531 100644 --- a/OddsCollector.Functions.Tests/Tests/Functions/PredictionsHttpFunctionTests.cs +++ b/OddsCollector.Functions.Tests/Tests/Functions/PredictionsHttpFunctionTests.cs @@ -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); @@ -57,39 +51,6 @@ public void Run_WithValidArguments_ReturnsValidResponse() var deserialized = JsonSerializer.Deserialize(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>(); - - var function = new PredictionsHttpFunction(loggerStub); - - var contextStub = Substitute.For(); - - var headersMock = Substitute.For(); - - var responseStream = new MemoryStream(); - - var responseMock = Substitute.For(contextStub); - responseMock.Headers.Returns(headersMock); - responseMock.Body.Returns(responseStream); - - var requestStub = Substitute.For(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); } } diff --git a/OddsCollector.Functions/Functions/PredictionsHttpFunction.cs b/OddsCollector.Functions/Functions/PredictionsHttpFunction.cs index 3c846ab..8554f6c 100644 --- a/OddsCollector.Functions/Functions/PredictionsHttpFunction.cs +++ b/OddsCollector.Functions/Functions/PredictionsHttpFunction.cs @@ -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); }