Пример #1
0
        public async Task ListSubjects_FiltersSubjectsWithNoImport()
        {
            var statisticsRelease = new Data.Model.Release();
            var releaseSubject    = new ReleaseSubject
            {
                Release = statisticsRelease,
                Subject = new Subject(),
            };

            var statisticsDbContextId = Guid.NewGuid().ToString();

            await using (var statisticsDbContext = InMemoryStatisticsDbContext(statisticsDbContextId))
            {
                await statisticsDbContext.AddRangeAsync(releaseSubject);

                await statisticsDbContext.SaveChangesAsync();
            }

            var contentRelease = new Release
            {
                Id = statisticsRelease.Id,
            };

            var releaseFile = new ReleaseFile
            {
                Release = contentRelease,
                Name    = "Subject 1",
                File    = new File
                {
                    Filename  = "data1.csv",
                    Type      = FileType.Data,
                    SubjectId = releaseSubject.Subject.Id
                }
            };

            var contentDbContextId = Guid.NewGuid().ToString();

            await using (var contentDbContext = InMemoryContentDbContext(contentDbContextId))
            {
                await contentDbContext.AddAsync(contentRelease);

                await contentDbContext.AddRangeAsync(releaseFile);

                await contentDbContext.SaveChangesAsync();
            }

            await using (var contentDbContext = InMemoryContentDbContext(contentDbContextId))
                await using (var statisticsDbContext = InMemoryStatisticsDbContext(statisticsDbContextId))
                {
                    var service = BuildReleaseService(
                        contentDbContext: contentDbContext,
                        statisticsDbContext: statisticsDbContext
                        );

                    var result = await service.ListSubjects(contentRelease.Id);

                    var subjects = result.AssertRight();

                    Assert.Empty(subjects);
                }
        }
Пример #2
0
        public async Task ListSubjects_FiltersImportingSubjects()
        {
            var statisticsRelease = new Data.Model.Release();

            var releaseSubject1 = new ReleaseSubject
            {
                Release = statisticsRelease,
                Subject = new Subject
                {
                    Id = Guid.NewGuid()
                }
            };

            var releaseSubject2 = new ReleaseSubject
            {
                Release = statisticsRelease,
                Subject = new Subject
                {
                    Id = Guid.NewGuid(),
                }
            };

            var statisticsDbContextId = Guid.NewGuid().ToString();

            await using (var statisticsDbContext = InMemoryStatisticsDbContext(statisticsDbContextId))
            {
                await statisticsDbContext.AddRangeAsync(releaseSubject1, releaseSubject2);

                await statisticsDbContext.SaveChangesAsync();
            }

            var contentRelease = new Release
            {
                Id = statisticsRelease.Id,
            };

            var releaseFile1 = new ReleaseFile
            {
                Name    = "Data 1",
                Release = contentRelease,
                File    = new File
                {
                    Filename  = "data1.csv",
                    Type      = FileType.Data,
                    SubjectId = releaseSubject1.Subject.Id
                }
            };

            var releaseFile2 = new ReleaseFile
            {
                Name    = "Data 2",
                Release = contentRelease,
                File    = new File
                {
                    Filename  = "data2.csv",
                    Type      = FileType.Data,
                    SubjectId = releaseSubject2.Subject.Id,
                }
            };

            var import1 = new DataImport
            {
                File   = releaseFile1.File,
                Status = DataImportStatus.STAGE_1
            };

            var import2 = new DataImport
            {
                File   = releaseFile2.File,
                Status = DataImportStatus.COMPLETE
            };

            var contentDbContextId = Guid.NewGuid().ToString();

            await using (var contentDbContext = InMemoryContentDbContext(contentDbContextId))
            {
                await contentDbContext.AddRangeAsync(releaseFile1, releaseFile2);

                await contentDbContext.AddRangeAsync(import1, import2);

                await contentDbContext.SaveChangesAsync();
            }

            await using (var contentDbContext = InMemoryContentDbContext(contentDbContextId))
                await using (var statisticsDbContext = InMemoryStatisticsDbContext(statisticsDbContextId))
                {
                    var service = BuildReleaseService(
                        contentDbContext: contentDbContext,
                        statisticsDbContext: statisticsDbContext
                        );

                    var result = await service.ListSubjects(contentRelease.Id);

                    var subjects = result.AssertRight();

                    Assert.Single(subjects);
                    Assert.Equal(releaseSubject2.Subject.Id, subjects[0].Id);
                    Assert.Equal(releaseFile2.Name, subjects[0].Name);
                }
        }
Пример #3
0
        public async Task ListSubjects()
        {
            var statisticsRelease = new Data.Model.Release();

            var releaseSubject1 = new ReleaseSubject
            {
                Release      = statisticsRelease,
                Subject      = new Subject(),
                DataGuidance = "Guidance 1"
            };

            var releaseSubject2 = new ReleaseSubject
            {
                Release      = statisticsRelease,
                Subject      = new Subject(),
                DataGuidance = "Guidance 2"
            };

            var statisticsDbContextId = Guid.NewGuid().ToString();

            await using (var statisticsDbContext = InMemoryStatisticsDbContext(statisticsDbContextId))
            {
                await statisticsDbContext.AddRangeAsync(releaseSubject1, releaseSubject2);

                await statisticsDbContext.SaveChangesAsync();
            }

            var contentRelease = new Release
            {
                Id = statisticsRelease.Id,
            };

            var releaseFile1 = new ReleaseFile
            {
                Release = contentRelease,
                Name    = "Subject 1",
                File    = new File
                {
                    Filename  = "data1.csv",
                    Type      = FileType.Data,
                    SubjectId = releaseSubject1.Subject.Id
                },
            };

            var releaseFile2 = new ReleaseFile
            {
                Release = contentRelease,
                Name    = "Subject 2",
                File    = new File
                {
                    Filename  = "data2.csv",
                    Type      = FileType.Data,
                    SubjectId = releaseSubject2.Subject.Id,
                }
            };

            var import1 = new DataImport
            {
                File   = releaseFile1.File,
                Status = DataImportStatus.COMPLETE
            };

            var import2 = new DataImport
            {
                File   = releaseFile2.File,
                Status = DataImportStatus.COMPLETE
            };

            var contentDbContextId = Guid.NewGuid().ToString();

            await using (var contentDbContext = InMemoryContentDbContext(contentDbContextId))
            {
                await contentDbContext.AddRangeAsync(releaseFile1, releaseFile2);

                await contentDbContext.AddRangeAsync(import1, import2);

                await contentDbContext.SaveChangesAsync();
            }

            await using (var contentDbContext = InMemoryContentDbContext(contentDbContextId))
                await using (var statisticsDbContext = InMemoryStatisticsDbContext(statisticsDbContextId))
                {
                    var dataGuidanceSubjectService = new Mock <IDataGuidanceSubjectService>();
                    var timePeriodService          = new Mock <ITimePeriodService>();

                    timePeriodService
                    .Setup(s => s.GetTimePeriodLabels(releaseSubject1.SubjectId))
                    .Returns(new TimePeriodLabels("2020/21", "2021/22"));

                    timePeriodService
                    .Setup(s => s.GetTimePeriodLabels(releaseSubject2.SubjectId))
                    .Returns(new TimePeriodLabels("2030", "2031"));

                    dataGuidanceSubjectService
                    .Setup(s => s.GetGeographicLevels(releaseSubject1.SubjectId))
                    .ReturnsAsync(ListOf("Local Authority", "Local Authority District"));

                    dataGuidanceSubjectService
                    .Setup(s => s.GetGeographicLevels(releaseSubject2.SubjectId))
                    .ReturnsAsync(ListOf("National"));

                    var fileInfoGetter = new Mock <IReleaseService.IBlobInfoGetter>(MockBehavior.Strict);

                    fileInfoGetter
                    .Setup(
                        s => s.Get(
                            It.Is <ReleaseFile>(rf => rf.Id == releaseFile1.Id)
                            )
                        )
                    .ReturnsAsync(
                        new BlobInfo(
                            path: releaseFile1.Path(),
                            size: "1 Mb",
                            contentType: "text/csv",
                            contentLength: 0L
                            )
                        );

                    fileInfoGetter
                    .Setup(
                        s => s.Get(
                            It.Is <ReleaseFile>(rf => rf.Id == releaseFile2.Id)
                            )
                        )
                    .ReturnsAsync(
                        new BlobInfo(
                            path: releaseFile2.Path(),
                            size: "2 Mb",
                            contentType: "text/csv",
                            contentLength: 0L
                            )
                        );

                    var service = BuildReleaseService(
                        contentDbContext: contentDbContext,
                        statisticsDbContext: statisticsDbContext,
                        dataGuidanceSubjectService: dataGuidanceSubjectService.Object,
                        timePeriodService: timePeriodService.Object,
                        fileSizeGetter: fileInfoGetter.Object
                        );

                    var result = await service.ListSubjects(contentRelease.Id);

                    MockUtils.VerifyAllMocks(dataGuidanceSubjectService, fileInfoGetter);

                    var subjects = result.AssertRight();

                    Assert.NotNull(subjects);
                    Assert.Equal(2, subjects.Count);
                    Assert.Equal(releaseSubject1.Subject.Id, subjects[0].Id);
                    Assert.Equal(releaseFile1.Name, subjects[0].Name);
                    Assert.Equal(releaseFile1.File.Id, subjects[0].File.Id);
                    Assert.Equal(releaseFile1.File.Filename, subjects[0].File.FileName);
                    Assert.Equal("1 Mb", subjects[0].File.Size);
                    Assert.Equal("csv", subjects[0].File.Extension);

                    Assert.Equal(releaseSubject1.DataGuidance, subjects[0].Content);

                    Assert.Equal("2020/21", subjects[0].TimePeriods.From);
                    Assert.Equal("2021/22", subjects[0].TimePeriods.To);

                    Assert.Equal(2, subjects[0].GeographicLevels.Count);
                    Assert.Equal("Local Authority", subjects[0].GeographicLevels[0]);
                    Assert.Equal("Local Authority District", subjects[0].GeographicLevels[1]);

                    Assert.Equal(releaseSubject2.Subject.Id, subjects[1].Id);
                    Assert.Equal(releaseFile2.Name, subjects[1].Name);
                    Assert.Equal(releaseFile2.File.Id, subjects[1].File.Id);
                    Assert.Equal(releaseFile2.File.Filename, subjects[1].File.FileName);
                    Assert.Equal("2 Mb", subjects[1].File.Size);
                    Assert.Equal(releaseSubject2.DataGuidance, subjects[1].Content);
                    Assert.Equal("csv", subjects[1].File.Extension);

                    Assert.Equal("2030", subjects[1].TimePeriods.From);
                    Assert.Equal("2031", subjects[1].TimePeriods.To);

                    Assert.Single(subjects[1].GeographicLevels);
                    Assert.Equal("National", subjects[1].GeographicLevels[0]);
                }
        }
Пример #4
0
        public async Task ListSubjects_FiltersPendingReplacementSubjects()
        {
            var statisticsRelease = new Data.Model.Release();

            var releaseSubject1 = new ReleaseSubject
            {
                Release = statisticsRelease,
                Subject = new Subject(),
            };

            var releaseSubject2 = new ReleaseSubject
            {
                Release = statisticsRelease,
                Subject = new Subject(),
            };

            var releaseSubject2Replacement = new ReleaseSubject
            {
                Release = statisticsRelease,
                Subject = new Subject(),
            };
            var statisticsDbContextId = Guid.NewGuid().ToString();

            await using (var statisticsDbContext = InMemoryStatisticsDbContext(statisticsDbContextId))
            {
                await statisticsDbContext.AddRangeAsync(releaseSubject1, releaseSubject2, releaseSubject2Replacement);

                await statisticsDbContext.SaveChangesAsync();
            }

            var contentRelease = new Release
            {
                Id = statisticsRelease.Id,
            };

            var releaseFile1 = new ReleaseFile
            {
                Release = contentRelease,
                Name    = "Subject 1",
                File    = new File
                {
                    Filename  = "data1.csv",
                    Type      = FileType.Data,
                    SubjectId = releaseSubject1.Subject.Id
                }
            };

            var file2 = new File
            {
                Filename  = "data2.csv",
                Type      = FileType.Data,
                SubjectId = releaseSubject2.Subject.Id,
            };

            var file2Replacement = new File
            {
                Filename  = "data2_replacement.csv",
                Type      = FileType.Data,
                SubjectId = releaseSubject2Replacement.Subject.Id,
                Replacing = file2
            };

            file2.ReplacedBy = file2Replacement;

            var releaseFile2 = new ReleaseFile
            {
                Release = contentRelease,
                Name    = "Subject 2",
                File    = file2
            };

            var releaseFile2Replacement = new ReleaseFile
            {
                Release = contentRelease,
                Name    = "Subject 2 Replacement",
                File    = file2Replacement
            };

            var import1 = new DataImport
            {
                File   = releaseFile1.File,
                Status = DataImportStatus.COMPLETE
            };

            var import2 = new DataImport
            {
                File   = releaseFile2.File,
                Status = DataImportStatus.COMPLETE
            };

            var contentDbContextId = Guid.NewGuid().ToString();

            await using (var contentDbContext = InMemoryContentDbContext(contentDbContextId))
            {
                await contentDbContext.AddRangeAsync(releaseFile1, releaseFile2, releaseFile2Replacement);

                await contentDbContext.AddRangeAsync(import1, import2);

                await contentDbContext.SaveChangesAsync();
            }

            await using (var contentDbContext = InMemoryContentDbContext(contentDbContextId))
                await using (var statisticsDbContext = InMemoryStatisticsDbContext(statisticsDbContextId))
                {
                    var service = BuildReleaseService(
                        contentDbContext: contentDbContext,
                        statisticsDbContext: statisticsDbContext
                        );

                    var result = await service.ListSubjects(contentRelease.Id);

                    var subjects = result.AssertRight();

                    Assert.NotNull(subjects);
                    Assert.Equal(2, subjects.Count);
                    Assert.Equal(releaseSubject1.Subject.Id, subjects[0].Id);
                    Assert.Equal(releaseFile1.Name, subjects[0].Name);
                    Assert.Equal(releaseSubject2.Subject.Id, subjects[1].Id);
                    Assert.Equal(releaseFile2.Name, subjects[1].Name);
                }
        }
Пример #5
0
        public async Task GetSubjects_FiltersImportingSubjects()
        {
            var releaseId = Guid.NewGuid();

            var contentRelease = new Release
            {
                Id = releaseId
            };

            var statisticsRelease = new Data.Model.Release
            {
                Id = releaseId
            };

            var releaseSubject1 = new ReleaseSubject
            {
                Release = statisticsRelease,
                Subject = new Subject
                {
                    Id   = Guid.NewGuid(),
                    Name = "Subject 1"
                }
            };

            var releaseSubject2 = new ReleaseSubject
            {
                Release = statisticsRelease,
                Subject = new Subject
                {
                    Id   = Guid.NewGuid(),
                    Name = "Subject 2"
                }
            };

            var releaseFile1 = new ReleaseFile
            {
                Release = contentRelease,
                File    = new File
                {
                    Release   = contentRelease,
                    Filename  = "data1.csv",
                    Type      = FileType.Data,
                    SubjectId = releaseSubject1.Subject.Id
                }
            };

            var releaseFile2 = new ReleaseFile
            {
                Release = contentRelease,
                File    = new File
                {
                    Release   = contentRelease,
                    Filename  = "data2.csv",
                    Type      = FileType.Data,
                    SubjectId = releaseSubject2.Subject.Id,
                }
            };

            var contentDbContextId    = Guid.NewGuid().ToString();
            var statisticsDbContextId = Guid.NewGuid().ToString();

            await using (var contentDbContext = InMemoryApplicationDbContext(contentDbContextId))
            {
                await contentDbContext.AddAsync(contentRelease);

                await contentDbContext.AddRangeAsync(releaseFile1, releaseFile2);

                await contentDbContext.SaveChangesAsync();
            }

            await using (var statisticsDbContext = InMemoryStatisticsDbContext(statisticsDbContextId))
            {
                await statisticsDbContext.AddRangeAsync(releaseSubject1, releaseSubject2);

                await statisticsDbContext.SaveChangesAsync();
            }

            await using (var contentDbContext = InMemoryApplicationDbContext(contentDbContextId))
                await using (var statisticsDbContext = InMemoryStatisticsDbContext(statisticsDbContextId))
                {
                    var importStatusService = new Mock <IImportStatusService>();

                    importStatusService
                    .Setup(s =>
                           s.GetImportStatus(releaseSubject1.ReleaseId, releaseFile1.File.Filename))
                    .ReturnsAsync(
                        new ImportStatus()
                    {
                        Status = IStatus.STAGE_1
                    }
                        );

                    importStatusService
                    .Setup(s =>
                           s.GetImportStatus(releaseSubject2.ReleaseId, releaseFile2.File.Filename))
                    .ReturnsAsync(
                        new ImportStatus()
                    {
                        Status = IStatus.COMPLETE
                    }
                        );

                    var replacementService = BuildReleaseMetaService(
                        contentDbContext: contentDbContext,
                        statisticsDbContext: statisticsDbContext,
                        importStatusService: importStatusService.Object
                        );

                    var result = await replacementService.GetSubjects(contentRelease.Id);

                    Assert.True(result.IsRight);

                    Assert.Equal(contentRelease.Id, result.Right.ReleaseId);

                    var subjects = result.Right.Subjects;

                    Assert.Single(subjects);
                    Assert.Equal(releaseSubject2.Subject.Id, subjects[0].Id);
                    Assert.Equal(releaseSubject2.Subject.Name, subjects[0].Label);
                }
        }
        public async Task Update_WithSubjects_AmendedRelease()
        {
            var contentReleaseVersion1 = new Release
            {
                Id = Guid.NewGuid(),
                PreviousVersionId = null,
                MetaGuidance      = "Version 1 Release Meta Guidance"
            };

            var contentReleaseVersion2 = new Release
            {
                Id = Guid.NewGuid(),
                PreviousVersionId = contentReleaseVersion1.Id,
                MetaGuidance      = "Version 2 Release Meta Guidance"
            };

            var statsReleaseVersion1 = new Data.Model.Release
            {
                Id = contentReleaseVersion1.Id,
                PreviousVersionId = contentReleaseVersion1.PreviousVersionId
            };

            var statsReleaseVersion2 = new Data.Model.Release
            {
                Id = contentReleaseVersion2.Id,
                PreviousVersionId = contentReleaseVersion2.PreviousVersionId
            };

            var subject1 = new Subject
            {
                Id       = Guid.NewGuid(),
                Filename = "file1.csv",
                Name     = "Subject 1"
            };

            var subject2 = new Subject
            {
                Id       = Guid.NewGuid(),
                Filename = "file2.csv",
                Name     = "Subject 2"
            };

            // Version 1 has one Subject, version 2 adds another Subject

            var releaseVersion1File1 = new ReleaseFile
            {
                Release = contentReleaseVersion1,
                File    = new File
                {
                    Filename  = "file1.csv",
                    Release   = contentReleaseVersion1,
                    Type      = FileType.Data,
                    SubjectId = subject1.Id
                }
            };

            var releaseVersion2File1 = new ReleaseFile
            {
                Release = contentReleaseVersion2,
                File    = new File
                {
                    Filename  = "file1.csv",
                    Release   = contentReleaseVersion2,
                    Type      = FileType.Data,
                    SubjectId = subject1.Id
                }
            };

            var releaseVersion2File2 = new ReleaseFile
            {
                Release = contentReleaseVersion2,
                File    = new File
                {
                    Filename  = "file2.csv",
                    Release   = contentReleaseVersion2,
                    Type      = FileType.Data,
                    SubjectId = subject2.Id
                }
            };

            var releaseVersion1Subject1 = new ReleaseSubject
            {
                Release      = statsReleaseVersion1,
                Subject      = subject1,
                MetaGuidance = "Version 1 Subject 1 Meta Guidance"
            };

            var releaseVersion2Subject1 = new ReleaseSubject
            {
                Release      = statsReleaseVersion2,
                Subject      = subject1,
                MetaGuidance = "Version 2 Subject 1 Meta Guidance"
            };

            var releaseVersion2Subject2 = new ReleaseSubject
            {
                Release      = statsReleaseVersion2,
                Subject      = subject2,
                MetaGuidance = "Version 2 Subject 2 Meta Guidance"
            };

            var contentDbContextId    = Guid.NewGuid().ToString();
            var statisticsDbContextId = Guid.NewGuid().ToString();

            await using (var contentDbContext = InMemoryApplicationDbContext(contentDbContextId))
            {
                await contentDbContext.AddRangeAsync(contentReleaseVersion1, contentReleaseVersion2);

                await contentDbContext.AddRangeAsync(releaseVersion1File1, releaseVersion2File1,
                                                     releaseVersion2File2);

                await contentDbContext.SaveChangesAsync();
            }

            await using (var statisticsDbContext = InMemoryStatisticsDbContext(statisticsDbContextId))
            {
                await statisticsDbContext.AddRangeAsync(statsReleaseVersion1, statsReleaseVersion2);

                await statisticsDbContext.AddRangeAsync(subject1, subject2);

                await statisticsDbContext.AddRangeAsync(releaseVersion1Subject1, releaseVersion2Subject1,
                                                        releaseVersion2Subject2);

                await statisticsDbContext.SaveChangesAsync();
            }

            var metaGuidanceSubjectService = new Mock <IMetaGuidanceSubjectService>(MockBehavior.Strict);

            metaGuidanceSubjectService.Setup(mock =>
                                             mock.GetSubjects(statsReleaseVersion2.Id, new List <Guid>
            {
                subject1.Id, subject2.Id
            })).ReturnsAsync(SubjectMetaGuidance);

            await using (var contentDbContext = InMemoryApplicationDbContext(contentDbContextId))
                await using (var statisticsDbContext = InMemoryStatisticsDbContext(statisticsDbContextId))
                {
                    var service = SetupMetaGuidanceService(contentDbContext: contentDbContext,
                                                           metaGuidanceSubjectService: metaGuidanceSubjectService.Object,
                                                           statisticsDbContext: statisticsDbContext);

                    // Update Release and Subject 1 on version 2
                    var result = await service.Update(
                        contentReleaseVersion2.Id,
                        new MetaGuidanceUpdateViewModel
                    {
                        Content  = "Version 2 Release Meta Guidance Updated",
                        Subjects = new List <MetaGuidanceUpdateSubjectViewModel>
                        {
                            new MetaGuidanceUpdateSubjectViewModel
                            {
                                Id      = subject1.Id,
                                Content = "Version 2 Subject 1 Meta Guidance Updated"
                            }
                        }
                    }
                        );

                    Assert.True(result.IsRight);

                    metaGuidanceSubjectService.Verify(mock =>
                                                      mock.GetSubjects(statsReleaseVersion2.Id, new List <Guid>
                    {
                        subject1.Id, subject2.Id
                    }), Times.Once);

                    Assert.Equal(contentReleaseVersion2.Id, result.Right.Id);
                    Assert.Equal("Version 2 Release Meta Guidance Updated", result.Right.Content);
                    Assert.Equal(SubjectMetaGuidance, result.Right.Subjects);
                }

            await using (var contentDbContext = InMemoryApplicationDbContext(contentDbContextId))
                await using (var statisticsDbContext = InMemoryStatisticsDbContext(statisticsDbContextId))
                {
                    Assert.Equal("Version 1 Release Meta Guidance",
                                 (await contentDbContext.Releases
                                  .FindAsync(contentReleaseVersion1.Id)).MetaGuidance);

                    Assert.Equal("Version 2 Release Meta Guidance Updated",
                                 (await contentDbContext.Releases
                                  .FindAsync(contentReleaseVersion2.Id)).MetaGuidance);

                    // Assert the same Subject on version 1 hasn't been affected
                    Assert.Equal("Version 1 Subject 1 Meta Guidance",
                                 (await statisticsDbContext.ReleaseSubject
                                  .Where(rs => rs.ReleaseId == statsReleaseVersion1.Id && rs.SubjectId == subject1.Id)
                                  .FirstAsync()).MetaGuidance);

                    // Assert only one Subject on version 2 has been updated
                    Assert.Equal("Version 2 Subject 1 Meta Guidance Updated",
                                 (await statisticsDbContext.ReleaseSubject
                                  .Where(rs => rs.ReleaseId == statsReleaseVersion2.Id && rs.SubjectId == subject1.Id)
                                  .FirstAsync()).MetaGuidance);

                    Assert.Equal("Version 2 Subject 2 Meta Guidance",
                                 (await statisticsDbContext.ReleaseSubject
                                  .Where(rs => rs.ReleaseId == statsReleaseVersion2.Id && rs.SubjectId == subject2.Id)
                                  .FirstAsync()).MetaGuidance);
                }
        }