fix unpredictable results from GetPage method

This commit is contained in:
cuqmbr 2023-10-15 12:17:00 +03:00
parent 9c9081a44f
commit f24365a3eb
Signed by: cuqmbr
GPG Key ID: 2D72ED98B6CB200F
2 changed files with 34 additions and 1 deletions

View File

@ -17,6 +17,21 @@ public class MessagesRepository : BaseRepository<Message>, IMessagesRepository
pageSize = Math.Clamp(pageSize, 1, messageCount);
var numberOfPages = messageCount / pageSize;
if (pageNumber > numberOfPages)
{
return new List<Message>();
}
if (pageNumber < 1)
{
throw new ArgumentOutOfRangeException();
}
if (pageSize < 1)
{
throw new ArgumentOutOfRangeException();
}
return await _collection.Find(predicate)
.Skip((numberOfPages - pageNumber) * pageSize)
.Limit(pageSize)

View File

@ -310,6 +310,8 @@ public class WishlistsTests : IClassFixture<TestingFactory<Program>>
var responseString = await response.Content.ReadAsStringAsync();
var document = JsonConvert.DeserializeObject<dynamic>(responseString);
Console.WriteLine(document);
var personalWishlistsPageItems = Enumerable.ToList(document.data.personalWishlistsPage.items);
Assert.Empty(personalWishlistsPageItems);
@ -342,6 +344,9 @@ public class WishlistsTests : IClassFixture<TestingFactory<Program>>
var responseString = await response.Content.ReadAsStringAsync();
var document = JsonConvert.DeserializeObject<dynamic>(responseString);
Console.WriteLine(document);
var personalWishlistsPageItems = Enumerable.ToList(document.data.personalWishlistsPage.items);
var personalWishlistCreatedById = (string) personalWishlistsPageItems[0].createdById;
@ -446,7 +451,17 @@ public class WishlistsTests : IClassFixture<TestingFactory<Program>>
var content = new StringContent(jsonPayload, Encoding.UTF8, "application/json");
using var response = await _httpClient.PostAsync("graphql", content);
Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
var responseString = await response.Content.ReadAsStringAsync();
var document = JsonConvert.DeserializeObject<dynamic>(responseString);
Console.WriteLine(document);
var messagesPageFromPersonalWishlistItems = Enumerable.ToList(document.data.messagesPageFromPersonalWishlist.items);
Assert.Empty(messagesPageFromPersonalWishlistItems);
}
[Fact]
@ -476,6 +491,9 @@ public class WishlistsTests : IClassFixture<TestingFactory<Program>>
var responseString = await response.Content.ReadAsStringAsync();
var document = JsonConvert.DeserializeObject<dynamic>(responseString);
Console.WriteLine(document);
var messagesPageFromPersonalWishlist = Enumerable.ToList(document.data.messagesPageFromPersonalWishlist.items);
var firstMessageInPage = messagesPageFromPersonalWishlist[0];
var secondMessageInPage = messagesPageFromPersonalWishlist[1];