public async Task Get_Report_For_Organization(bool submitted, string[] include) { using (var context = new TestHedwigContextProvider().Context) { // Set up test data var organization = OrganizationHelper.CreateOrganization(context); var reportingPeriod = ReportingPeriodHelper.CreateReportingPeriod(context); var report = ReportHelper.CreateCdcReport(context, organization: organization, reportingPeriod: reportingPeriod); var site = SiteHelper.CreateSite(context, organization: organization); var enrollment = EnrollmentHelper.CreateEnrollment(context, site: site, ageGroup: Age.Preschool); var funding = FundingHelper.CreateFunding(context, enrollment: enrollment, firstReportingPeriod: reportingPeriod); if (submitted) { Thread.Sleep(1000); report.SubmittedAt = DateTime.Now; context.SaveChanges(); Thread.Sleep(1000); } enrollment.AgeGroup = Age.SchoolAge; context.SaveChanges(); // When the repository is queried var repo = new ReportRepository(context); var result = await repo.GetReportForOrganizationAsync(report.Id, organization.Id, include) as CdcReport; // It returns the Report Assert.Equal(result.Id, report.Id); // It includes the Organization Assert.Equal(include.Contains("organizations"), result.Organization != null); // It includes the Sites Assert.Equal(include.Contains("sites"), result.Organization != null && result.Organization.Sites != null); // It includes the Enrollments (and Fundings too) Assert.Equal( include.Contains("enrollments"), result.Enrollments != null && result.Enrollments.FirstOrDefault().Fundings != null ); // When it includes enrollments if (include.Contains("enrollments")) { var enrollmentResult = result.Enrollments.FirstOrDefault(); var fundingResult = enrollmentResult.Fundings.FirstOrDefault(); // A submitted report should return the data as of when it was submitted Assert.Equal(submitted ? Age.Preschool : Age.SchoolAge, enrollmentResult.AgeGroup); } } }
public void UpdateEnrollment_AddsFundings() { Enrollment enrollment; FundingSpace fundingSpace; using (var context = new TestHedwigContextProvider().Context) { enrollment = EnrollmentHelper.CreateEnrollment(context); fundingSpace = FundingSpaceHelper.CreateFundingSpace( context, organizationId: enrollment.Site.OrganizationId, source: FundingSource.CDC, ageGroup: enrollment.AgeGroup.Value ); } Funding funding = new Funding { EnrollmentId = enrollment.Id, Source = FundingSource.CDC, FundingSpaceId = fundingSpace.Id }; Assert.Null(enrollment.Fundings); enrollment.Fundings = new List <Funding> { funding }; var mapper = new MapperConfiguration(opts => { opts.AddProfile(new EnrollmentProfile()); opts.AddProfile(new FundingProfile()); opts.AddProfile(new ChildProfile()); opts.AddProfile(new FamilyProfile()); opts.AddProfile(new SiteProfile()); }).CreateMapper(); using (var context = new TestHedwigContextProvider().Context) { var enrollmentRepo = new EnrollmentRepository(context, mapper); var enrollmentDTO = mapper.Map <Enrollment, EnrollmentDTO>(enrollment); enrollmentRepo.UpdateEnrollment(enrollment, enrollmentDTO); context.SaveChanges(); } using (var context = new TestHedwigContextProvider().Context) { var enrollmentRepo = new EnrollmentRepository(context, mapper); var retrievedEnrollment = enrollmentRepo.GetEnrollmentById(enrollment.Id); Assert.NotEmpty(retrievedEnrollment.Fundings); var retrievedFunding = retrievedEnrollment.Fundings.FirstOrDefault(); Assert.Equal(funding.Id, retrievedFunding.Id); } }
public void UpdateEnrollment_UpdatesFundings() { Enrollment enrollment; Funding funding; ReportingPeriod reportingPeriod; using (var context = new TestHedwigContextProvider().Context) { enrollment = EnrollmentHelper.CreateEnrollment(context); funding = FundingHelper.CreateFunding(context, enrollment: enrollment); reportingPeriod = ReportingPeriodHelper.CreateReportingPeriod(context); } Assert.Null(funding.FirstReportingPeriodId); funding.FirstReportingPeriodId = reportingPeriod.Id; enrollment.Fundings = new List <Funding> { funding }; enrollment.Fundings.FirstOrDefault().Enrollment = null; using (var context = new TestHedwigContextProvider().Context) { var mapper = new MapperConfiguration(opts => { opts.AddProfile(new EnrollmentProfile()); opts.AddProfile(new FundingProfile()); opts.AddProfile(new FundingSpaceProfile()); opts.AddProfile(new ChildProfile()); opts.AddProfile(new FamilyProfile()); opts.AddProfile(new SiteProfile()); }).CreateMapper(); var enrollmentRepo = new EnrollmentRepository(context, mapper); var enrollmentDTO = mapper.Map <Enrollment, EnrollmentDTO>(enrollment); enrollmentRepo.UpdateEnrollment(enrollment, enrollmentDTO); context.SaveChanges(); } using (var context = new TestHedwigContextProvider().Context) { var retrievedEnrollment = context.Enrollments .Where(e => e.Id == enrollment.Id) .Include(e => e.Fundings) .FirstOrDefault(); var retrievedFunding = retrievedEnrollment.Fundings.FirstOrDefault(); Assert.NotNull(retrievedFunding.FirstReportingPeriodId); Assert.Equal(funding.FirstReportingPeriodId, retrievedFunding.FirstReportingPeriodId); } }
public void UpdatedTemporalEntity_IsUpdated_OnSaveChanges() { using (var context = new TestHedwigContextProvider().Context) { var family = FamilyHelper.CreateFamily(context); var updatedAt = family.UpdatedAt; context.Update(family); context.SaveChanges(); Assert.NotNull(family.AuthorId); Assert.NotNull(family.UpdatedAt); Assert.NotEqual(updatedAt, family.UpdatedAt); } }
public void AddedTemporalEntity_IsUpdated_OnSaveChanges() { using (var context = new TestHedwigContextProvider().Context) { var family = new Family { OrganizationId = context.Organizations.First().Id }; context.Add(family); context.SaveChanges(); Assert.NotNull(family.AuthorId); Assert.NotNull(family.UpdatedAt); } }
public void UpdateEnrollment_RemovesFundings() { Enrollment enrollment; using (var context = new TestHedwigContextProvider().Context) { enrollment = EnrollmentHelper.CreateEnrollment(context); FundingHelper.CreateFunding(context, enrollment: enrollment); } Assert.NotEmpty(enrollment.Fundings); enrollment.Fundings = new List <Funding>(); var mapper = new MapperConfiguration(opts => { opts.AddProfile(new EnrollmentProfile()); opts.AddProfile(new FundingProfile()); opts.AddProfile(new ChildProfile()); opts.AddProfile(new FamilyProfile()); opts.AddProfile(new SiteProfile()); }).CreateMapper(); using (var context = new TestHedwigContextProvider().Context) { var enrollmentRepo = new EnrollmentRepository(context, mapper); var enrollmentDTO = mapper.Map <Enrollment, EnrollmentDTO>(enrollment); enrollmentRepo.UpdateEnrollment(enrollment, enrollmentDTO); context.SaveChanges(); } using (var context = new TestHedwigContextProvider().Context) { var enrollmentRepo = new EnrollmentRepository(context, mapper); var retrievedEnrollment = enrollmentRepo.GetEnrollmentById(enrollment.Id); Assert.Empty(retrievedEnrollment.Fundings); } }