using FoodsharingSiegen.Contracts.Entity; using FoodsharingSiegen.Contracts.Enums; using FoodsharingSiegen.Contracts.Model; using FoodsharingSiegen.Server.Auth; using FoodsharingSiegen.Server.Data; using FoodsharingSiegen.Server.Data.Service; using FoodsharingSiegen.Server.Service; using Microsoft.AspNetCore.Components.Authorization; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Options; using Microsoft.JSInterop; using Moq; using Xunit; namespace FoodsharingSiegen.Tests; public class ProspectServiceTests { private static FsContext CreateInMemoryContext(string dbName) { var options = new DbContextOptionsBuilder() .UseInMemoryDatabase(databaseName: dbName) .Options; return new FsContext(options); } private static AuthService CreateAuthService(FsContext context) { var mockJsRuntime = new Mock(); var localStorageService = new LocalStorageService(mockJsRuntime.Object); var authStateProvider = new Mock(); var mailService = new Mock(); var appSettings = new Mock>(); appSettings.Setup(x => x.Value).Returns(new AppSettings()); return new AuthService(context, localStorageService, authStateProvider.Object, mailService.Object, appSettings.Object); } [Fact] public async Task GetProspectsAsync_RespectsSkipAndTake() { var dbName = Guid.NewGuid().ToString(); using var context = CreateInMemoryContext(dbName); var authService = CreateAuthService(context); var auditService = new AuditService(context, authService); var prospectService = new ProspectService(context, authService, auditService); for (var i = 0; i < 5; i++) { context.Prospects!.Add(new Prospect { Id = Guid.NewGuid(), Name = $"Prospect {i}", Modified = DateTime.UtcNow.AddDays(-i), Interactions = new List() }); } await context.SaveChangesAsync(); var result = await prospectService.GetProspectsAsync(new GetProspectsParameter { Skip = 1, Take = 2, IncludeDeleted = true }); Assert.True(result.Success); Assert.Equal(2, result.Data.Count); Assert.Equal("Prospect 1", result.Data[0].Name); Assert.Equal("Prospect 2", result.Data[1].Name); } }