public async Task Get_WrongRelease()
        {
            var dataBlock = new DataBlock
            {
                Name = "Test name",
            };

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

            await using (var context = ContentDbUtils.InMemoryContentDbContext(contextId))
            {
                await context.AddAsync(dataBlock);

                await context.SaveChangesAsync();
            }

            await using (var context = ContentDbUtils.InMemoryContentDbContext(contextId))
            {
                var service = BuildDataBlockService(context);
                var result  = await service.Get(dataBlock.Id);

                Assert.True(result.IsLeft);
                Assert.IsType <ForbidResult>(result.Left);
            }
        }
Пример #2
0
        public async Task ReleaseIsNotLive()
        {
            var publication = new Publication();

            var release = new Release
            {
                Publication = publication
            };

            publication.Releases.Add(release);

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

            await using (var contentDbContext = ContentDbUtils.InMemoryContentDbContext(contextId))
            {
                await contentDbContext.AddAsync(publication);

                await contentDbContext.SaveChangesAsync();
            }

            await using (var contentDbContext = ContentDbUtils.InMemoryContentDbContext(contextId))
            {
                var handler = new ViewReleaseAuthorizationHandler(contentDbContext);

                var authContext = new AuthorizationHandlerContext(
                    new IAuthorizationRequirement[] { Activator.CreateInstance <ViewReleaseRequirement>() },
                    null,
                    release
                    );

                await handler.HandleAsync(authContext);

                Assert.False(authContext.HasSucceeded);
            }
        }
        public async Task Migrate()
        {
            await using var contentDbContext = ContentDbUtils.InMemoryContentDbContext();

            var service = SetupService(contentDbContext);

            var result = await service.Migrate();

            result.AssertRight();
        }
        public async Task GetDeletePlan_WrongRelease()
        {
            var fileId = Guid.NewGuid();

            var dataBlock = new DataBlock
            {
                Name   = "Test name",
                Charts = new List <IChart>
                {
                    new InfographicChart
                    {
                        Title  = "Test chart",
                        FileId = fileId.ToString(),
                        Height = 400,
                        Width  = 500,
                    }
                },
                ContentSection = new ContentSection
                {
                    Heading = "Test heading"
                }
            };
            var file = new File
            {
                Id       = fileId,
                Filename = "test-infographic.jpg"
            };

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

            await using (var context = ContentDbUtils.InMemoryContentDbContext(contextId))
            {
                await context.AddAsync(
                    new ReleaseContentBlock
                {
                    Release      = new Release(),
                    ContentBlock = dataBlock
                }
                    );

                await context.AddAsync(file);

                await context.SaveChangesAsync();
            }

            await using (var context = ContentDbUtils.InMemoryContentDbContext(contextId))
            {
                var service = BuildDataBlockService(context);
                var result  = await service.GetDeletePlan(Guid.NewGuid(), dataBlock.Id);

                Assert.True(result.IsLeft);
                Assert.IsType <NotFoundResult>(result.Left);
            }
        }
Пример #5
0
        public async Task GetChecklist_AllWarningsWithNoDataFiles()
        {
            var publication = new Publication();
            var release     = new Release
            {
                Publication = publication,
            };

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

            await using (var context = ContentDbUtils.InMemoryContentDbContext(contextId))
            {
                await context.AddRangeAsync(release);

                await context.SaveChangesAsync();
            }

            await using (var context = ContentDbUtils.InMemoryContentDbContext(contextId))
            {
                var fileRepository = new Mock <IFileRepository>();

                fileRepository
                .Setup(r => r.ListDataFiles(release.Id))
                .ReturnsAsync(new List <File>());

                fileRepository
                .Setup(r => r.ListReplacementDataFiles(release.Id))
                .ReturnsAsync(new List <File>());

                var metaGuidanceService = new Mock <IMetaGuidanceService>();

                metaGuidanceService
                .Setup(s => s.Validate(release.Id))
                .ReturnsAsync(ValidationActionResult(PublicMetaGuidanceRequired));

                var service = BuildReleaseChecklistService(
                    context,
                    fileRepository: fileRepository.Object,
                    metaGuidanceService: metaGuidanceService.Object
                    );
                var checklist = await service.GetChecklist(release.Id);

                Assert.True(checklist.IsRight);

                Assert.False(checklist.Right.Valid);

                Assert.Equal(4, checklist.Right.Warnings.Count);
                Assert.Equal(NoMethodology, checklist.Right.Warnings[0].Code);
                Assert.Equal(NoNextReleaseDate, checklist.Right.Warnings[1].Code);
                Assert.Equal(NoDataFiles, checklist.Right.Warnings[2].Code);
                Assert.Equal(NoPublicPreReleaseAccessList, checklist.Right.Warnings[3].Code);
            }
        }
Пример #6
0
        public async Task SyncStatisticsTaxonomy_UpdatesTheme()
        {
            var theme1 = new Theme
            {
                Title  = "Updated Theme 1",
                Slug   = "updated-theme-1",
                Topics = new List <Topic>()
            };

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

            await using (var contentDbContext = ContentDbUtils.InMemoryContentDbContext(contextId))
                await using (var statisticsDbContext = StatisticsDbUtils.InMemoryStatisticsDbContext(contextId))
                {
                    await contentDbContext.AddAsync(theme1);

                    await contentDbContext.SaveChangesAsync();

                    await statisticsDbContext.AddAsync(new Data.Model.Theme
                    {
                        Id     = theme1.Id,
                        Title  = "Old Theme 1",
                        Slug   = "old-theme-1",
                        Topics = new HashSet <Data.Model.Topic>()
                    });

                    await statisticsDbContext.SaveChangesAsync();
                }

            await using (var contentDbContext = ContentDbUtils.InMemoryContentDbContext(contextId))
                await using (var statisticsDbContext = StatisticsDbUtils.InMemoryStatisticsDbContext(contextId))
                {
                    var service = BuildTaxonomyService(contentDbContext, statisticsDbContext);

                    await service.SyncTaxonomy();
                }

            await using (var statisticsDbContext = StatisticsDbUtils.InMemoryStatisticsDbContext(contextId))
            {
                var themes = statisticsDbContext.Theme
                             .Include(t => t.Topics)
                             .ToList();

                Assert.Single(themes);

                Assert.Equal(theme1.Id, themes[0].Id);
                Assert.Equal("Updated Theme 1", themes[0].Title);
                Assert.Equal("updated-theme-1", themes[0].Slug);

                Assert.Empty(themes[0].Topics);
            }
        }
        public async Task Get_NotFound()
        {
            var contextId = Guid.NewGuid().ToString();

            await using (var context = ContentDbUtils.InMemoryContentDbContext(contextId))
            {
                var service = BuildDataBlockService(context);
                var result  = await service.Get(Guid.NewGuid());

                Assert.True(result.IsLeft);
                Assert.IsType <NotFoundResult>(result.Left);
            }
        }
Пример #8
0
        public async Task ReleaseIsLiveAndLatestVersion()
        {
            var publication = new Publication();

            var olderRelease = new Release
            {
                Published   = DateTime.Parse("2019-10-08T12:00:00"),
                Publication = publication
            };

            var newerRelease = new Release
            {
                Published       = DateTime.Parse("2019-10-10T12:00:00"),
                Publication     = publication,
                PreviousVersion = olderRelease,
            };

            publication.Releases.AddRange(
                new List <Release>
            {
                olderRelease,
                newerRelease,
            }
                );

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

            await using (var contentDbContext = ContentDbUtils.InMemoryContentDbContext(contextId))
            {
                await contentDbContext.AddAsync(publication);

                await contentDbContext.SaveChangesAsync();
            }

            await using (var contentDbContext = ContentDbUtils.InMemoryContentDbContext(contextId))
            {
                var handler = new ViewReleaseAuthorizationHandler(contentDbContext);

                var authContext = new AuthorizationHandlerContext(
                    new IAuthorizationRequirement[] { Activator.CreateInstance <ViewReleaseRequirement>() },
                    null,
                    newerRelease
                    );

                await handler.HandleAsync(authContext);

                Assert.True(authContext.HasSucceeded);
            }
        }
        public async Task Migrate()
        {
            await PolicyCheckBuilder()
            .SetupCheck(SecurityPolicies.CanRunReleaseMigrations, false)
            .AssertForbidden(
                async userService =>
            {
                await using var contentDbContext = ContentDbUtils.InMemoryContentDbContext();

                var service = SetupService(
                    contentDbContext: contentDbContext,
                    userService: userService.Object
                    );

                return(await service.Migrate());
            }
                );
        }
Пример #10
0
        public async Task ReleaseNotFound()
        {
            await using (var contentDbContext = ContentDbUtils.InMemoryContentDbContext())
            {
                var handler = new ViewReleaseAuthorizationHandler(contentDbContext);

                var authContext = new AuthorizationHandlerContext(
                    new IAuthorizationRequirement[] { Activator.CreateInstance <ViewReleaseRequirement>() },
                    null,
                    new Release
                {
                    Id = Guid.NewGuid()
                }
                    );

                await handler.HandleAsync(authContext);

                Assert.False(authContext.HasSucceeded);
            }
        }
        public void GetTree()
        {
            var contextId = Guid.NewGuid().ToString();

            using (var context = ContentDbUtils.InMemoryContentDbContext(contextId))
            {
                context.Add(Theme);
                context.Add(Topic);
                context.AddRange(Publications);
                context.AddRange(Releases);

                context.SaveChanges();
            }

            using (var context = ContentDbUtils.InMemoryContentDbContext(contextId))
            {
                var service = BuildPublicationService(context);

                var result = service.GetTree(Enumerable.Empty <Guid>());

                Assert.Single(result);
                var theme = result.First();
                Assert.Equal("Theme A", theme.Title);

                Assert.Single(theme.Topics);
                var topic = theme.Topics.First();
                Assert.Equal("Topic A", topic.Title);

                var publications = topic.Publications;
                Assert.Equal(2, publications.Count);
                Assert.Equal("publication-a", publications[0].Slug);
                Assert.Equal("first publication summary", publications[0].Summary);
                Assert.Equal("Publication A", publications[0].Title);
                // The Publication has a legacy url but it's not set because Releases exist
                Assert.Null(publications[0].LegacyPublicationUrl);
                Assert.Equal("publication-c", publications[1].Slug);
                Assert.Equal("third publication summary", publications[1].Summary);
                Assert.Equal("Publication C", publications[1].Title);
                Assert.Equal("http://legacy.url/", publications[1].LegacyPublicationUrl);
            }
        }
        public async Task GetDeletePlan_NotFound()
        {
            var release = new Release();

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

            await using (var context = ContentDbUtils.InMemoryContentDbContext(contextId))
            {
                await context.AddAsync(release);

                await context.SaveChangesAsync();
            }

            await using (var context = ContentDbUtils.InMemoryContentDbContext(contextId))
            {
                var service = BuildDataBlockService(context);
                var result  = await service.GetDeletePlan(release.Id, Guid.NewGuid());

                Assert.True(result.IsLeft);
                Assert.IsType <NotFoundResult>(result.Left);
            }
        }
Пример #13
0
        public async Task GetChecklist_NotFound()
        {
            var release = new Release();

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

            await using (var context = ContentDbUtils.InMemoryContentDbContext(contextId))
            {
                await context.AddAsync(release);

                await context.SaveChangesAsync();
            }

            await using (var context = ContentDbUtils.InMemoryContentDbContext(contextId))
            {
                var service   = BuildReleaseChecklistService(context);
                var checklist = await service.GetChecklist(Guid.NewGuid());

                Assert.True(checklist.IsLeft);

                Assert.IsType <NotFoundResult>(checklist.Left);
            }
        }
Пример #14
0
        public async Task GetChecklist_AllErrors()
        {
            var publication = new Publication
            {
                Methodology = new Methodology()
            };
            var originalRelease = new Release
            {
                Publication = publication,
                Version     = 0
            };
            var release = new Release
            {
                Publication     = publication,
                PreviousVersion = originalRelease,
                Version         = 1,
            };

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

            await using (var context = ContentDbUtils.InMemoryContentDbContext(contextId))
            {
                await context.AddRangeAsync(release, originalRelease);

                await context.SaveChangesAsync();
            }

            await using (var context = ContentDbUtils.InMemoryContentDbContext(contextId))
            {
                var fileRepository = new Mock <IFileRepository>();

                fileRepository
                .Setup(r => r.ListDataFiles(release.Id))
                .ReturnsAsync(new List <File>());

                fileRepository
                .Setup(r => r.ListReplacementDataFiles(release.Id))
                .ReturnsAsync(
                    new List <File>
                {
                    new File()
                }
                    );

                var metaGuidanceService = new Mock <IMetaGuidanceService>();

                metaGuidanceService
                .Setup(s => s.Validate(release.Id))
                .ReturnsAsync(ValidationActionResult(PublicMetaGuidanceRequired));

                var tableStorageService = MockTableStorageService(
                    new List <DatafileImport>
                {
                    new DatafileImport()
                }
                    );

                var service = BuildReleaseChecklistService(
                    context,
                    fileRepository: fileRepository.Object,
                    metaGuidanceService: metaGuidanceService.Object,
                    tableStorageService: tableStorageService.Object
                    );
                var checklist = await service.GetChecklist(release.Id);

                Assert.True(checklist.IsRight);

                Assert.False(checklist.Right.Valid);

                Assert.Equal(5, checklist.Right.Errors.Count);

                Assert.Equal(DataFileImportsMustBeCompleted, checklist.Right.Errors[0].Code);
                Assert.Equal(DataFileReplacementsMustBeCompleted, checklist.Right.Errors[1].Code);

                var methodologyMustBeApprovedError =
                    Assert.IsType <MethodologyMustBeApprovedError>(checklist.Right.Errors[2]);
                Assert.Equal(MethodologyMustBeApproved, methodologyMustBeApprovedError.Code);
                Assert.Equal(publication.MethodologyId, methodologyMustBeApprovedError.MethodologyId);

                Assert.Equal(PublicMetaGuidanceRequired, checklist.Right.Errors[3].Code);
                Assert.Equal(ReleaseNoteRequired, checklist.Right.Errors[4].Code);
            }
        }
Пример #15
0
        public async Task GetChecklist_FullyValid()
        {
            var publication = new Publication
            {
                Methodology = new Methodology
                {
                    Status = MethodologyStatus.Approved
                }
            };

            var originalRelease = new Release
            {
                Publication = publication,
                Version     = 0
            };
            var release = new Release
            {
                Publication          = publication,
                PreviousVersion      = originalRelease,
                Version              = 1,
                MetaGuidance         = "Test meta guidance",
                PreReleaseAccessList = "Test access list",
                NextReleaseDate      = new PartialDate
                {
                    Month = "12",
                    Year  = "2021"
                },
                Updates = new List <Update>
                {
                    new Update
                    {
                        Reason  = "Test reason 1",
                        Release = originalRelease
                    },
                    new Update
                    {
                        Reason = "Test reason 2"
                    }
                },
            };

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

            await using (var context = ContentDbUtils.InMemoryContentDbContext(contextId))
            {
                await context.AddRangeAsync(release, originalRelease);

                await context.SaveChangesAsync();
            }

            await using (var context = ContentDbUtils.InMemoryContentDbContext(contextId))
            {
                var subject = new Subject
                {
                    Id = Guid.NewGuid()
                };

                var fileRepository = new Mock <IFileRepository>();

                fileRepository
                .Setup(r => r.ListDataFiles(release.Id))
                .ReturnsAsync(
                    new List <File>
                {
                    new File
                    {
                        Id        = Guid.NewGuid(),
                        Filename  = "test-file-1.csv",
                        Type      = FileType.Data,
                        Release   = release,
                        ReleaseId = release.Id,
                        SubjectId = subject.Id,
                    },
                }
                    );

                fileRepository
                .Setup(r => r.ListReplacementDataFiles(release.Id))
                .ReturnsAsync(new List <File>());

                var metaGuidanceService = new Mock <IMetaGuidanceService>();

                metaGuidanceService
                .Setup(s => s.Validate(release.Id))
                .ReturnsAsync(Unit.Instance);

                var footnoteRepository = new Mock <IFootnoteRepository>();

                footnoteRepository
                .Setup(r => r.GetSubjectsWithNoFootnotes(release.Id))
                .ReturnsAsync(new List <Subject>());

                var dataBlockService = new Mock <IDataBlockService>();

                dataBlockService
                .Setup(s => s.List(release.Id))
                .ReturnsAsync(
                    new List <DataBlockViewModel>
                {
                    new DataBlockViewModel
                    {
                        HighlightName = "Test highlight name"
                    },
                }
                    );

                var service = BuildReleaseChecklistService(
                    context,
                    metaGuidanceService: metaGuidanceService.Object,
                    fileRepository: fileRepository.Object,
                    footnoteRepository: footnoteRepository.Object,
                    dataBlockService: dataBlockService.Object
                    );
                var checklist = await service.GetChecklist(release.Id);

                Assert.True(checklist.IsRight);

                Assert.Empty(checklist.Right.Errors);
                Assert.Empty(checklist.Right.Warnings);
                Assert.True(checklist.Right.Valid);
            }
        }
Пример #16
0
        public async Task GetChecklist_AllWarningsWithDataFiles()
        {
            var publication = new Publication();
            var release     = new Release
            {
                Publication = publication,
            };

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

            await using (var context = ContentDbUtils.InMemoryContentDbContext(contextId))
            {
                await context.AddRangeAsync(release);

                await context.SaveChangesAsync();
            }

            await using (var context = ContentDbUtils.InMemoryContentDbContext(contextId))
            {
                var subject = new Subject
                {
                    Id = Guid.NewGuid()
                };
                var otherSubject = new Subject
                {
                    Id = Guid.NewGuid(),
                };

                var fileRepository = new Mock <IFileRepository>();

                fileRepository
                .Setup(r => r.ListDataFiles(release.Id))
                .ReturnsAsync(
                    new List <File>
                {
                    new File
                    {
                        Id        = Guid.NewGuid(),
                        Filename  = "test-file-1.csv",
                        Type      = FileType.Data,
                        Release   = release,
                        ReleaseId = release.Id,
                        SubjectId = subject.Id
                    },
                    new File
                    {
                        Id        = Guid.NewGuid(),
                        Filename  = "test-file-2.csv",
                        Type      = FileType.Data,
                        Release   = release,
                        ReleaseId = release.Id,
                        SubjectId = otherSubject.Id
                    }
                }
                    );

                var metaGuidanceService = new Mock <IMetaGuidanceService>();

                metaGuidanceService
                .Setup(s => s.Validate(release.Id))
                .ReturnsAsync(ValidationActionResult(PublicMetaGuidanceRequired));

                var footnoteRepository = new Mock <IFootnoteRepository>();

                footnoteRepository
                .Setup(r => r.GetSubjectsWithNoFootnotes(release.Id))
                .ReturnsAsync(
                    new List <Subject>
                {
                    subject,
                }
                    );

                fileRepository
                .Setup(r => r.ListReplacementDataFiles(release.Id))
                .ReturnsAsync(new List <File>());

                var dataBlockService = new Mock <IDataBlockService>();

                dataBlockService
                .Setup(s => s.List(release.Id))
                .ReturnsAsync(
                    new List <DataBlockViewModel>
                {
                    new DataBlockViewModel(),
                    new DataBlockViewModel(),
                }
                    );

                var service = BuildReleaseChecklistService(
                    context,
                    fileRepository: fileRepository.Object,
                    metaGuidanceService: metaGuidanceService.Object,
                    footnoteRepository: footnoteRepository.Object,
                    dataBlockService: dataBlockService.Object
                    );
                var checklist = await service.GetChecklist(release.Id);

                Assert.True(checklist.IsRight);

                Assert.False(checklist.Right.Valid);

                Assert.Equal(5, checklist.Right.Warnings.Count);
                Assert.Equal(NoMethodology, checklist.Right.Warnings[0].Code);
                Assert.Equal(NoNextReleaseDate, checklist.Right.Warnings[1].Code);

                var noFootnotesWarning = Assert.IsType <NoFootnotesOnSubjectsWarning>(checklist.Right.Warnings[2]);
                Assert.Equal(NoFootnotesOnSubjects, noFootnotesWarning.Code);
                Assert.Equal(1, noFootnotesWarning.TotalSubjects);

                Assert.Equal(NoTableHighlights, checklist.Right.Warnings[3].Code);
                Assert.Equal(NoPublicPreReleaseAccessList, checklist.Right.Warnings[4].Code);
            }
        }
        public async Task List_FiltersUnrelated()
        {
            var release = new Release();

            var dataBlock1 = new DataBlock
            {
                Heading       = "Test heading 1",
                Name          = "Test name 1",
                HighlightName = "Test highlight name 1",
                Source        = "Test source 1",
                Order         = 5,
                Query         = new ObservationQueryContext
                {
                    Filters = new List <Guid>
                    {
                        Guid.NewGuid(),
                    },
                    Indicators = new List <Guid>
                    {
                        Guid.NewGuid(),
                    },
                },
                Table = new TableBuilderConfiguration
                {
                    TableHeaders = new TableHeaders
                    {
                        Rows = new List <TableHeader>
                        {
                            new TableHeader(Guid.NewGuid().ToString(), TableHeaderType.Indicator)
                        },
                        Columns = new List <TableHeader>
                        {
                            new TableHeader(Guid.NewGuid().ToString(), TableHeaderType.Filter)
                        }
                    }
                },
                Charts = new List <IChart>
                {
                    new LineChart
                    {
                        Title  = "Test chart 1",
                        Height = 400,
                        Width  = 500,
                    }
                },
            };

            var dataBlock2 = new DataBlock
            {
                Name = "Test name 2",
            };

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

            await using (var context = ContentDbUtils.InMemoryContentDbContext(contextId))
            {
                await context.AddRangeAsync(
                    new ReleaseContentBlock
                {
                    Release      = release,
                    ContentBlock = dataBlock1
                },
                    new ReleaseContentBlock
                {
                    Release      = new Release(),
                    ContentBlock = dataBlock2
                }
                    );

                await context.SaveChangesAsync();
            }

            await using (var context = ContentDbUtils.InMemoryContentDbContext(contextId))
            {
                var service = BuildDataBlockService(context);
                var result  = await service.List(release.Id);

                Assert.True(result.IsRight);

                Assert.Single(result.Right);

                Assert.Equal(dataBlock1.Heading, result.Right[0].Heading);
                Assert.Equal(dataBlock1.Name, result.Right[0].Name);
                Assert.Equal(dataBlock1.HighlightName, result.Right[0].HighlightName);
                Assert.Equal(dataBlock1.Source, result.Right[0].Source);
                Assert.Equal(dataBlock1.Order, result.Right[0].Order);

                Assert.Equal(dataBlock1.Query, result.Right[0].Query);
                Assert.Equal(dataBlock1.Table, result.Right[0].Table);
                Assert.Equal(dataBlock1.Charts, result.Right[0].Charts);
            }
        }
        public async Task GetDeletePlan()
        {
            var release = new Release();
            var fileId  = Guid.NewGuid();

            var dataBlock = new DataBlock
            {
                Name   = "Test name",
                Charts = new List <IChart>
                {
                    new InfographicChart
                    {
                        Title  = "Test chart",
                        FileId = fileId.ToString(),
                        Height = 400,
                        Width  = 500,
                    }
                },
                ContentSection = new ContentSection
                {
                    Heading = "Test heading"
                }
            };
            var file = new File
            {
                Id       = fileId,
                Filename = "test-infographic.jpg"
            };

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

            await using (var context = ContentDbUtils.InMemoryContentDbContext(contextId))
            {
                await context.AddAsync(
                    new ReleaseContentBlock
                {
                    Release      = release,
                    ContentBlock = dataBlock
                }
                    );

                await context.AddAsync(file);

                await context.SaveChangesAsync();
            }

            await using (var context = ContentDbUtils.InMemoryContentDbContext(contextId))
            {
                var service = BuildDataBlockService(context);
                var result  = await service.GetDeletePlan(release.Id, dataBlock.Id);

                Assert.True(result.IsRight);
                Assert.Equal(release.Id, result.Right.ReleaseId);

                var dependentBlocks = result.Right.DependentDataBlocks;

                Assert.Single(dependentBlocks);

                Assert.Equal(dataBlock.Id, dependentBlocks[0].Id);
                Assert.Equal(dataBlock.Name, dependentBlocks[0].Name);
                Assert.Equal(dataBlock.ContentSection.Heading, dependentBlocks[0].ContentSectionHeading);

                Assert.Single(dependentBlocks[0].InfographicFilesInfo);

                Assert.Equal(file.Id, dependentBlocks[0].InfographicFilesInfo[0].Id);
                Assert.Equal(file.Filename, dependentBlocks[0].InfographicFilesInfo[0].Filename);
            }
        }
        public async Task Update_RemoveOldInfographic()
        {
            var release = new Release();
            var fileId  = Guid.NewGuid();

            var dataBlock = new DataBlock
            {
                Charts = new List <IChart>
                {
                    new InfographicChart
                    {
                        Title  = "Old chart",
                        FileId = fileId.ToString(),
                        Height = 400,
                        Width  = 500,
                    }
                },
            };

            var file = new File
            {
                Id       = fileId,
                Filename = "test-infographic.jpg"
            };

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

            await using (var context = ContentDbUtils.InMemoryContentDbContext(contextId))
            {
                await context.AddAsync(
                    new ReleaseContentBlock
                {
                    Release      = release,
                    ContentBlock = dataBlock
                }
                    );

                await context.AddAsync(file);

                await context.SaveChangesAsync();
            }

            var updateRequest = new UpdateDataBlockViewModel
            {
                Charts = new List <IChart>
                {
                    new LineChart
                    {
                        Title  = "New chart",
                        Height = 600,
                        Width  = 700,
                    }
                },
            };

            await using (var context = ContentDbUtils.InMemoryContentDbContext(contextId))
            {
                var releaseFileService = new Mock <IReleaseFileService>();

                releaseFileService
                .Setup(s => s.Delete(release.Id, fileId, false))
                .ReturnsAsync(Unit.Instance);

                var service = BuildDataBlockService(context, releaseFileService: releaseFileService.Object);
                var result  = await service.Update(dataBlock.Id, updateRequest);

                Assert.True(result.IsRight);

                Assert.Equal(updateRequest.Charts, result.Right.Charts);

                MockUtils.VerifyAllMocks(releaseFileService);
            }
        }
        public void GetViewModelAsync()
        {
            var contextId = Guid.NewGuid().ToString();

            using (var context = ContentDbUtils.InMemoryContentDbContext(contextId))
            {
                context.Add(Methodology);
                context.Add(Theme);
                context.Add(Topic);
                context.AddRange(Publications);
                context.AddRange(Releases);

                context.SaveChanges();
            }

            using (var context = ContentDbUtils.InMemoryContentDbContext(contextId))
            {
                var releaseService = new Mock <IReleaseService>();

                releaseService.Setup(s => s.GetLatestRelease(PublicationA.Id, Enumerable.Empty <Guid>()))
                .ReturnsAsync(PublicationARelease1V1);

                var service = BuildPublicationService(context, releaseService: releaseService.Object);

                var result = service.GetViewModelAsync(PublicationA.Id, Enumerable.Empty <Guid>());
                Assert.True(result.IsCompleted);
                var viewModel = result.Result;

                Assert.Equal(PublicationA.Id, viewModel.Id);
                Assert.Equal("Publication A", viewModel.Title);
                Assert.Equal("publication-a", viewModel.Slug);
                Assert.Equal("first publication description", viewModel.Description);
                Assert.Equal("first publication data source", viewModel.DataSource);
                Assert.Equal("first publication summary", viewModel.Summary);
                Assert.Equal(PublicationARelease1V1.Id, viewModel.LatestReleaseId);
                Assert.Contains(PublicationARelease1V1.Id, viewModel.Releases.Select(r => r.Id));
                Assert.DoesNotContain(PublicationARelease1V0.Id, viewModel.Releases.Select(r => r.Id));
                Assert.DoesNotContain(PublicationARelease1V1Deleted.Id, viewModel.Releases.Select(r => r.Id));

                Assert.NotNull(viewModel.Topic);
                var topic = viewModel.Topic;

                Assert.NotNull(topic.Theme);
                var theme = topic.Theme;
                Assert.Equal(Theme.Title, theme.Title);

                Assert.NotNull(viewModel.Contact);
                var contact = viewModel.Contact;
                Assert.Equal("first contact name", contact.ContactName);
                Assert.Equal("first contact tel no", contact.ContactTelNo);
                Assert.Equal("*****@*****.**", contact.TeamEmail);
                Assert.Equal("first contact team name", contact.TeamName);

                Assert.NotNull(viewModel.ExternalMethodology);
                var externalMethodology = viewModel.ExternalMethodology;
                Assert.Equal("external methodology title", externalMethodology.Title);
                Assert.Equal("http://external.methodology/", externalMethodology.Url);

                Assert.NotNull(viewModel.LegacyReleases);
                var legacyReleases = viewModel.LegacyReleases;
                Assert.Equal(3, legacyReleases.Count);
                Assert.Equal("Academic Year 2010/11", legacyReleases[0].Description);
                Assert.Equal("http://link.three/", legacyReleases[0].Url);
                Assert.Equal("Academic Year 2009/10", legacyReleases[1].Description);
                Assert.Equal("http://link.two/", legacyReleases[1].Url);
                Assert.Equal("Academic Year 2008/09", legacyReleases[2].Description);
                Assert.Equal("http://link.one/", legacyReleases[2].Url);

                Assert.NotNull(viewModel.Methodology);
                var methodology = viewModel.Methodology;
                Assert.Equal(Methodology.Id, methodology.Id);
                Assert.Equal("methodology-slug", methodology.Slug);
                Assert.Equal("methodology summary", methodology.Summary);
                Assert.Equal("methodology title", methodology.Title);

                Assert.NotNull(viewModel.Releases);
                var releases = viewModel.Releases;
                Assert.Equal(3, releases.Count);
                Assert.Equal(PublicationARelease2.Id, releases[0].Id);
                Assert.Equal("publication-a-release-2018-q2", releases[0].Slug);
                Assert.Equal("Academic Year Q2 2018/19", releases[0].Title);
                Assert.Equal(PublicationARelease1V1.Id, releases[1].Id);
                Assert.Equal("publication-a-release-2018-q1", releases[1].Slug);
                Assert.Equal("Academic Year Q1 2018/19", releases[1].Title);
                Assert.Equal(PublicationARelease3.Id, releases[2].Id);
                Assert.Equal("publication-a-release-2017-q4", releases[2].Slug);
                Assert.Equal("Academic Year Q4 2017/18", releases[2].Title);
            }
        }
Пример #21
0
        public async Task SyncStatisticsTaxonomy_AddsNewTheme()
        {
            var theme1 = new Theme
            {
                Title  = "Theme 1",
                Slug   = "theme-1",
                Topics = new List <Topic>
                {
                    new Topic
                    {
                        Title = "Topic 1",
                        Slug  = "topic-1",
                    },
                    new Topic
                    {
                        Title = "Topic 2",
                        Slug  = "topic-2",
                    }
                }
            };

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

            await using (var contentDbContext = ContentDbUtils.InMemoryContentDbContext(contextId))
            {
                await contentDbContext.AddAsync(theme1);

                await contentDbContext.SaveChangesAsync();
            }

            await using (var contentDbContext = ContentDbUtils.InMemoryContentDbContext(contextId))
                await using (var statisticsDbContext = StatisticsDbUtils.InMemoryStatisticsDbContext(contextId))
                {
                    var service = BuildTaxonomyService(contentDbContext, statisticsDbContext);

                    await service.SyncTaxonomy();
                }

            await using (var statisticsDbContext = StatisticsDbUtils.InMemoryStatisticsDbContext(contextId))
            {
                var themes = statisticsDbContext.Theme
                             .Include(t => t.Topics)
                             .ToList();

                Assert.Single(themes);

                Assert.Equal(theme1.Id, themes[0].Id);
                Assert.Equal("Theme 1", themes[0].Title);
                Assert.Equal("theme-1", themes[0].Slug);

                var topics = themes[0].Topics.ToList();
                Assert.Equal(2, topics.Count);

                Assert.Equal(theme1.Topics[0].Id, topics[0].Id);
                Assert.Equal("Topic 1", topics[0].Title);
                Assert.Equal("topic-1", topics[0].Slug);

                Assert.Equal(theme1.Topics[1].Id, topics[1].Id);
                Assert.Equal("Topic 2", topics[1].Title);
                Assert.Equal("topic-2", topics[1].Slug);
            }
        }
        public async Task Delete()
        {
            var release = new Release();
            var fileId  = Guid.NewGuid();

            var dataBlock = new DataBlock
            {
                Name   = "Test name",
                Charts = new List <IChart>
                {
                    new InfographicChart
                    {
                        Title  = "Test chart",
                        FileId = fileId.ToString(),
                        Height = 400,
                        Width  = 500,
                    }
                },
                ContentSection = new ContentSection
                {
                    Heading = "Test heading"
                }
            };
            var file = new File
            {
                Id       = fileId,
                Filename = "test-infographic.jpg"
            };

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

            await using (var context = ContentDbUtils.InMemoryContentDbContext(contextId))
            {
                await context.AddAsync(
                    new ReleaseContentBlock
                {
                    Release      = release,
                    ContentBlock = dataBlock
                }
                    );

                await context.AddAsync(file);

                await context.SaveChangesAsync();
            }

            await using (var context = ContentDbUtils.InMemoryContentDbContext(contextId))
            {
                var releaseFileService = new Mock <IReleaseFileService>();

                releaseFileService
                .Setup(
                    s =>
                    s.Delete(release.Id, new List <Guid> {
                    fileId
                }, false)
                    )
                .ReturnsAsync(Unit.Instance);

                var service = BuildDataBlockService(context, releaseFileService: releaseFileService.Object);
                var result  = await service.Delete(release.Id, dataBlock.Id);

                Assert.True(result.IsRight);

                MockUtils.VerifyAllMocks(releaseFileService);
            }

            await using (var context = ContentDbUtils.InMemoryContentDbContext(contextId))
            {
                Assert.Empty(context.DataBlocks.ToList());
                Assert.Empty(context.ContentBlocks.ToList());
                Assert.Empty(context.ReleaseContentBlocks.ToList());
            }
        }
        public async Task SetPublishedDate_UpdatesStatsPublication()
        {
            var publication = new Publication
            {
                Title = "Test publication",
                Slug  = "test-publication",
                Topic = new Topic
                {
                    Title = "Test topic",
                    Slug  = "test-topic"
                }
            };

            var publishDate = DateTime.Now;

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

            await using (var contentDbContext = ContentDbUtils.InMemoryContentDbContext(contextId))
                await using (var statisticsDbContext = StatisticsDbUtils.InMemoryStatisticsDbContext(contextId))
                {
                    await contentDbContext.AddAsync(publication);

                    await contentDbContext.SaveChangesAsync();

                    await statisticsDbContext.AddAsync(
                        new Data.Model.Publication
                    {
                        Id    = publication.Id,
                        Title = "Old test publication",
                        Slug  = "old-test-publication"
                    }
                        );

                    await statisticsDbContext.AddAsync(
                        new Data.Model.Topic
                    {
                        Id    = publication.Topic.Id,
                        Title = "Test topic",
                        Slug  = "test-topic"
                    }
                        );

                    await statisticsDbContext.SaveChangesAsync();
                }

            await using (var contentDbContext = ContentDbUtils.InMemoryContentDbContext(contextId))
                await using (var statisticsDbContext = StatisticsDbUtils.InMemoryStatisticsDbContext(contextId))
                {
                    var service = BuildPublicationService(contentDbContext, statisticsDbContext);

                    await service.SetPublishedDate(publication.Id, publishDate);
                }

            await using (var contentDbContext = ContentDbUtils.InMemoryContentDbContext(contextId))
                await using (var statisticsDbContext = StatisticsDbUtils.InMemoryStatisticsDbContext(contextId))
                {
                    var contentPublication = await contentDbContext.Publications.FindAsync(publication.Id);

                    var statsPublication = await statisticsDbContext.Publication.FindAsync(publication.Id);

                    Assert.Equal(publication.Id, contentPublication.Id);
                    Assert.Equal("Test publication", contentPublication.Title);
                    Assert.Equal("test-publication", contentPublication.Slug);
                    Assert.Equal(publishDate, contentPublication.Published);

                    Assert.Equal(publication.Id, statsPublication.Id);
                    Assert.Equal("Test publication", statsPublication.Title);
                    Assert.Equal("test-publication", statsPublication.Slug);
                    Assert.Equal(publication.TopicId, statsPublication.TopicId);
                }
        }
Пример #24
0
 public static ContentDbContext InMemoryApplicationDbContext(string dbName)
 {
     return(ContentDbUtils.InMemoryContentDbContext(dbName));
 }
        public async Task Update()
        {
            var release = new Release();

            var dataBlock = new DataBlock
            {
                Heading       = "Old heading",
                Name          = "Old name",
                HighlightName = "Old highlight name",
                Source        = "Old source",
                Order         = 5,
                Query         = new ObservationQueryContext
                {
                    Filters = new List <Guid>
                    {
                        Guid.NewGuid(),
                    },
                    Indicators = new List <Guid>
                    {
                        Guid.NewGuid(),
                    },
                },
                Table = new TableBuilderConfiguration
                {
                    TableHeaders = new TableHeaders
                    {
                        Rows = new List <TableHeader>
                        {
                            new TableHeader(Guid.NewGuid().ToString(), TableHeaderType.Indicator)
                        },
                        Columns = new List <TableHeader>
                        {
                            new TableHeader(Guid.NewGuid().ToString(), TableHeaderType.Filter)
                        }
                    }
                },
                Charts = new List <IChart>
                {
                    new LineChart
                    {
                        Title  = "Old chart",
                        Height = 400,
                        Width  = 500,
                    }
                },
            };

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

            await using (var context = ContentDbUtils.InMemoryContentDbContext(contextId))
            {
                await context.AddAsync(
                    new ReleaseContentBlock
                {
                    Release      = release,
                    ContentBlock = dataBlock
                }
                    );

                await context.SaveChangesAsync();
            }

            var updateRequest = new UpdateDataBlockViewModel
            {
                Heading       = "New heading",
                Name          = "New name",
                HighlightName = "New highlight name",
                Source        = "New source",
                Query         = new ObservationQueryContext
                {
                    Filters = new List <Guid>
                    {
                        Guid.NewGuid(),
                    },
                    Indicators = new List <Guid>
                    {
                        Guid.NewGuid(),
                    },
                },
                Table = new TableBuilderConfiguration
                {
                    TableHeaders = new TableHeaders
                    {
                        Rows = new List <TableHeader>
                        {
                            new TableHeader(Guid.NewGuid().ToString(), TableHeaderType.Indicator)
                        },
                        Columns = new List <TableHeader>
                        {
                            new TableHeader(Guid.NewGuid().ToString(), TableHeaderType.Filter)
                        }
                    }
                },
                Charts = new List <IChart>
                {
                    new LineChart
                    {
                        Title  = "New chart",
                        Height = 600,
                        Width  = 700,
                    }
                },
            };

            await using (var context = ContentDbUtils.InMemoryContentDbContext(contextId))
            {
                var service = BuildDataBlockService(context);
                var result  = await service.Update(dataBlock.Id, updateRequest);

                Assert.True(result.IsRight);

                Assert.Equal(dataBlock.Id, result.Right.Id);
                Assert.Equal(updateRequest.Heading, result.Right.Heading);
                Assert.Equal(updateRequest.Name, result.Right.Name);
                Assert.Equal(updateRequest.HighlightName, result.Right.HighlightName);
                Assert.Equal(updateRequest.Source, result.Right.Source);
                Assert.Equal(dataBlock.Order, result.Right.Order);

                Assert.Equal(updateRequest.Query, result.Right.Query);
                Assert.Equal(updateRequest.Table, result.Right.Table);
                Assert.Equal(updateRequest.Charts, result.Right.Charts);
            }

            await using (var context = ContentDbUtils.InMemoryContentDbContext(contextId))
            {
                var updatedDataBlock = await context.DataBlocks.FindAsync(dataBlock.Id);

                Assert.Equal(updateRequest.Heading, updatedDataBlock.Heading);
                Assert.Equal(updateRequest.Name, updatedDataBlock.Name);
                Assert.Equal(updateRequest.HighlightName, updatedDataBlock.HighlightName);
                Assert.Equal(updateRequest.Source, updatedDataBlock.Source);

                Assert.Equal(updateRequest.Query, updatedDataBlock.Query);
                Assert.Equal(updateRequest.Table, updatedDataBlock.Table);
                Assert.Equal(updateRequest.Charts, updatedDataBlock.Charts);
            }
        }
Пример #26
0
        public async Task SyncStatisticsTaxonomy_DoesNotRemoveTopicPublication()
        {
            var theme1 = new Theme
            {
                Title  = "Theme 1",
                Slug   = "theme-1",
                Topics = new List <Topic>
                {
                    new Topic
                    {
                        Title = "Updated Topic 1",
                        Slug  = "updated-topic-1",
                    }
                }
            };

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

            await using (var contentDbContext = ContentDbUtils.InMemoryContentDbContext(contextId))
                await using (var statisticsDbContext = StatisticsDbUtils.InMemoryStatisticsDbContext(contextId))
                {
                    await contentDbContext.AddAsync(theme1);

                    await contentDbContext.SaveChangesAsync();

                    await statisticsDbContext.AddAsync(
                        new Data.Model.Theme
                    {
                        Id     = theme1.Id,
                        Title  = "Theme 1",
                        Slug   = "theme-1",
                        Topics = new HashSet <Data.Model.Topic>
                        {
                            new Data.Model.Topic
                            {
                                Id           = theme1.Topics[0].Id,
                                Title        = "Old Topic 1",
                                Slug         = "old-topic-1",
                                Publications = new List <Data.Model.Publication>
                                {
                                    new Data.Model.Publication
                                    {
                                        Title = "Publication 1"
                                    }
                                }
                            }
                        }
                    }
                        );

                    await statisticsDbContext.SaveChangesAsync();
                }

            await using (var contentDbContext = ContentDbUtils.InMemoryContentDbContext(contextId))
                await using (var statisticsDbContext = StatisticsDbUtils.InMemoryStatisticsDbContext(contextId))
                {
                    var service = BuildTaxonomyService(contentDbContext, statisticsDbContext);

                    await service.SyncTaxonomy();
                }

            await using (var statisticsDbContext = StatisticsDbUtils.InMemoryStatisticsDbContext(contextId))
            {
                var themes = statisticsDbContext.Theme
                             .Include(theme => theme.Topics)
                             .ThenInclude(topic => topic.Publications)
                             .ToList();

                Assert.Single(themes);

                var topics = themes[0].Topics.ToList();
                Assert.Single(topics);

                Assert.Equal(theme1.Topics[0].Id, topics[0].Id);
                Assert.Equal("Updated Topic 1", topics[0].Title);
                Assert.Equal("updated-topic-1", topics[0].Slug);

                Assert.Single(topics[0].Publications);
                Assert.Equal("Publication 1", topics[0].Publications.First().Title);
            }
        }
Пример #27
0
 public static ContentDbContext InMemoryApplicationDbContext()
 {
     return(ContentDbUtils.InMemoryContentDbContext());
 }