public async Task GetLastMonthAuditsReturnsOnlyLatestAuditAtAnyGivenDay() { // Arrange await using var context = JosekiTestsDb.CreateUniqueContext(); var wrapper = new InfraScoreDbWrapper(context); // create three audits at each day at 00:00, 08:00, 16:00 var expectedComponent = this.GenerateComponent(); var today = DateTime.UtcNow.Date; foreach (var auditDate in Enumerable.Range(0, 31).Select(i => today.AddDays(-i))) { var entities = Enumerable .Range(0, 3) .Select(i => new AuditEntity { Date = auditDate.AddHours(i * 8), ComponentId = expectedComponent.ComponentId, InfrastructureComponent = expectedComponent }); await context.Audit.AddRangeAsync(entities); } await context.SaveChangesAsync(); // Act var audits = await wrapper.GetLastMonthAudits(expectedComponent.ComponentId); // Assert // returned audit should be the latest one at each day audits.Should().HaveCount(31); audits.All(i => i.Date.Hour == 16).Should().BeTrue(); }
public async Task GetLastMonthAuditsReturnsOnlyRequestedComponentAudits() { // Arrange await using var context = JosekiTestsDb.CreateUniqueContext(); var wrapper = new InfraScoreDbWrapper(context); var auditDate = DateTime.UtcNow.Date.AddDays(-1); var expectedComponent = this.GenerateComponent(); var anotherComponent = this.GenerateComponent(); await context.Audit.AddRangeAsync(new[] { new AuditEntity { Date = auditDate, ComponentId = expectedComponent.ComponentId, InfrastructureComponent = expectedComponent }, new AuditEntity { Date = DateTime.UtcNow.AddDays(-2), ComponentId = anotherComponent.ComponentId, InfrastructureComponent = anotherComponent }, }); await context.SaveChangesAsync(); // Act & Assert var audits = await wrapper.GetLastMonthAudits(expectedComponent.ComponentId); audits.Should().ContainSingle(a => a.ComponentId == expectedComponent.ComponentId && a.Date == auditDate); }
public async Task GetLastMonthAuditsReturnsOnlyLast31DaysAudits() { // Arrange await using var context = JosekiTestsDb.CreateUniqueContext(); var wrapper = new InfraScoreDbWrapper(context); var expectedComponent = this.GenerateComponent(); var today = DateTime.UtcNow.Date; var entities = Enumerable .Range(0, 35) .Select(i => new AuditEntity { Date = today.AddDays(-i), ComponentId = expectedComponent.ComponentId, InfrastructureComponent = expectedComponent }); await context.Audit.AddRangeAsync(entities); await context.SaveChangesAsync(); // Act var audits = await wrapper.GetLastMonthAudits(expectedComponent.ComponentId); // Assert var oneMonthAgo = today.AddDays(-30); audits.Should().HaveCount(31); audits.All(i => i.Date >= oneMonthAgo).Should().BeTrue(); }