public async Task CreatesReportForOrganization( bool hasFundingSpace, string today, int numOfReportsGenerated ) { int organizationId; int lastReportingPeriodId; using (var context = new TestHedwigContextProvider().Context) { var organization = OrganizationHelper.CreateOrganization(context); organizationId = organization.Id; if (hasFundingSpace) { FundingSpaceHelper.CreateFundingSpace(context, organizationId); } var lastReportingPeriod = ReportingPeriodHelper.GetOrCreateReportingPeriodForPeriod( context, period: "2010-10-01", periodStart: "2010-10-01", periodEnd: "2010-10-31" ); lastReportingPeriodId = lastReportingPeriod.Id; } using (var context = new TestHedwigContextProvider().Context) { var logger = new Mock <ILogger <CdcReportGeneratorScopedService> >(); var orgRepo = new OrganizationRepository(context); var reportingPeriodRepo = new ReportingPeriodRepository(context); var reportRepo = new ReportRepository(context); var dateTime = new Mock <IDateTime>(); dateTime.Setup(dateTime => dateTime.UtcNow).Returns(DateTime.Parse(today)); var cdcReportGenerator = new CdcReportGeneratorScopedService( logger.Object, orgRepo, reportingPeriodRepo, reportRepo, dateTime.Object ); var previousReports = reportRepo.GetReportsForOrganization(organizationId); await cdcReportGenerator.TryGenerateReports(); var reports = reportRepo.GetReportsForOrganization(organizationId); Assert.Empty(previousReports); Assert.Equal(numOfReportsGenerated, reports.Count()); if (numOfReportsGenerated > 0) { Assert.True(reports.All(report => report.ReportingPeriodId == lastReportingPeriodId)); } } }
public async Task DoesNotCreateDuplicateReports() { int organizationId; using (var context = new TestHedwigContextProvider().Context) { var organization = OrganizationHelper.CreateOrganization(context); organizationId = organization.Id; var fundingSpace = FundingSpaceHelper.CreateFundingSpace(context, organizationId); organization.FundingSpaces = new List <FundingSpace> { fundingSpace }; var currentReportingPeriod = ReportingPeriodHelper.CreateReportingPeriod(context); } using (var context = new TestHedwigContextProvider().Context) { var logger = new Mock <ILogger <CdcReportGeneratorScopedService> >(); var orgRepo = new OrganizationRepository(context); var reportingPeriodRepo = new ReportingPeriodRepository(context); var reportRepo = new ReportRepository(context); var dateTime = new Mock <IDateTime>(); dateTime.Setup(dateTime => dateTime.UtcNow).Returns(DateTime.Parse("2019-10-01")); var cdcReportGenerator = new CdcReportGeneratorScopedService( logger.Object, orgRepo, reportingPeriodRepo, reportRepo, dateTime.Object ); var previousReports = reportRepo.GetReportsForOrganization(organizationId); await cdcReportGenerator.TryGenerateReports(); await cdcReportGenerator.TryGenerateReports(); await cdcReportGenerator.TryGenerateReports(); var reports = reportRepo.GetReportsForOrganization(organizationId); Assert.Empty(previousReports); Assert.Single(reports); } }