public async Task FindPublicationForSubject()
        {
            var releaseSubject = new ReleaseSubject
            {
                Release = new Release
                {
                    Publication = new Publication
                    {
                        Title = "Test publication"
                    },
                },
                Subject = new Subject
                {
                    Name = "Test subject"
                }
            };

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

            await using (var context = StatisticsDbUtils.InMemoryStatisticsDbContext(contextId))
            {
                await context.AddAsync(releaseSubject);

                await context.SaveChangesAsync();
            }

            await using (var context = StatisticsDbUtils.InMemoryStatisticsDbContext(contextId))
            {
                var service = BuildSubjectService(context);
                var result  = await service.FindPublicationForSubject(releaseSubject.SubjectId);

                Assert.Equal(releaseSubject.Release.PublicationId, result.Id);
                Assert.Equal("Test publication", result.Title);
            }
        }
        public async Task IsSubjectForLatestPublishedRelease()
        {
            var releaseSubject = new ReleaseSubject
            {
                Release = new Release
                {
                    Publication = new Publication(),
                    Published   = DateTime.UtcNow
                },
                Subject = new Subject()
            };

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

            await using (var context = StatisticsDbUtils.InMemoryStatisticsDbContext(contextId))
            {
                await context.AddAsync(releaseSubject);

                await context.SaveChangesAsync();
            }

            await using (var context = StatisticsDbUtils.InMemoryStatisticsDbContext(contextId))
            {
                var releaseService = new Mock <IReleaseService>();

                releaseService
                .Setup(s => s.GetLatestPublishedRelease(releaseSubject.Release.PublicationId))
                .Returns(releaseSubject.Release);

                var service = BuildSubjectService(context, releaseService: releaseService.Object);
                var result  = await service.IsSubjectForLatestPublishedRelease(releaseSubject.SubjectId);

                Assert.True(result);
            }
        }
        public async Task Get_WithReleaseAndSubjectName_SubjectNameNotFound()
        {
            var releaseSubject = new ReleaseSubject
            {
                Release = new Release(),
                Subject = new Subject
                {
                    Name = "Test subject"
                }
            };

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

            await using (var context = StatisticsDbUtils.InMemoryStatisticsDbContext(contextId))
            {
                await context.AddAsync(releaseSubject);

                await context.SaveChangesAsync();
            }

            await using (var context = StatisticsDbUtils.InMemoryStatisticsDbContext(contextId))
            {
                var service = BuildSubjectService(context);
                var result  = await service.Get(releaseSubject.ReleaseId, "Not the subject");

                Assert.Null(result);
            }
        }
        public async Task DeleteReleaseSubject_SoftDeleteOrphanedSubject()
        {
            var releaseSubject = new ReleaseSubject
            {
                Release = new Release(),
                Subject = new Subject(),
            };

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

            await using (var statisticsDbContext = StatisticsDbUtils.InMemoryStatisticsDbContext(contextId))
            {
                await statisticsDbContext.AddAsync(releaseSubject);

                await statisticsDbContext.SaveChangesAsync();
            }

            await using (var statisticsDbContext = StatisticsDbUtils.InMemoryStatisticsDbContext(contextId))
            {
                var service = BuildReleaseSubjectService(statisticsDbContext);
                await service.DeleteReleaseSubject(releaseSubject.ReleaseId, releaseSubject.SubjectId, true);
            }

            await using (var statisticsDbContext = StatisticsDbUtils.InMemoryStatisticsDbContext(contextId))
            {
                Assert.Empty(statisticsDbContext.ReleaseSubject.ToList());

                var subjects = statisticsDbContext.Subject.IgnoreQueryFilters().ToList();
                Assert.Single(subjects);
                Assert.Equal(releaseSubject.Subject.Id, subjects[0].Id);
                Assert.True(subjects[0].SoftDeleted);
            }
        }
Example #5
0
        public async Task CheckComplete_SingleDataFileCompleted_AlreadyFinished()
        {
            await FinishedStatuses.ForEachAsync(async finishedStatus =>
            {
                var message = new ImportObservationsMessage
                {
                    ReleaseId    = Guid.NewGuid(),
                    NumBatches   = 1,
                    DataFileName = "my_data_file.csv",
                    TotalRows    = 2,
                    SubjectId    = Guid.NewGuid()
                };

                var importStatusService = new Mock <IImportStatusService>(Strict);

                var service = BuildFileImportService(
                    importStatusService: importStatusService.Object);

                importStatusService
                .Setup(s => s.GetImportStatus(message.ReleaseId, message.DataFileName))
                .ReturnsAsync(new ImportStatus
                {
                    Status = finishedStatus
                });

                var dbContext = StatisticsDbUtils.InMemoryStatisticsDbContext();

                await using (dbContext)
                {
                    await service.CheckComplete(message.ReleaseId, message, dbContext);
                }

                MockUtils.VerifyAllMocks(importStatusService);
            });
        }
Example #6
0
        public async Task Query_ReleaseId_SubjectNotFound()
        {
            var releaseSubject = new ReleaseSubject
            {
                Release = new Release(),
                Subject = new Subject(),
            };

            var query = new ObservationQueryContext
            {
                SubjectId = Guid.NewGuid(),
            };

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

            await using (var statisticsDbContext = StatisticsDbUtils.InMemoryStatisticsDbContext(contextId))
            {
                await statisticsDbContext.AddAsync(releaseSubject);

                await statisticsDbContext.SaveChangesAsync();
            }

            await using (var statisticsDbContext = StatisticsDbUtils.InMemoryStatisticsDbContext(contextId))
            {
                var service = BuildTableBuilderService(statisticsDbContext);

                var result = await service.Query(Guid.NewGuid(), query);

                Assert.True(result.IsLeft);
                Assert.IsType <NotFoundResult>(result.Left);
            }
        }
Example #7
0
        public async Task CheckComplete_SingleDataFileCompleted_HasIncorrectObservationCount()
        {
            var message = new ImportObservationsMessage
            {
                ReleaseId    = Guid.NewGuid(),
                NumBatches   = 1,
                DataFileName = "my_data_file.csv",
                TotalRows    = 3,
                SubjectId    = Guid.NewGuid()
            };

            var importStatusService = new Mock <IImportStatusService>(Strict);
            var batchService        = new Mock <IBatchService>(Strict);

            var service = BuildFileImportService(
                importStatusService: importStatusService.Object,
                batchService: batchService.Object);

            importStatusService
            .Setup(s => s.GetImportStatus(message.ReleaseId, message.DataFileName))
            .ReturnsAsync(new ImportStatus
            {
                Status = STAGE_4,
            });

            batchService
            .Setup(s => s.FailImport(message.ReleaseId, message.DataFileName, new List <ValidationError>
            {
                new ValidationError(
                    $"Number of observations inserted (2) " +
                    $"does not equal that expected ({message.TotalRows}) : Please delete & retry"
                    )
            }))
            .Returns(Task.CompletedTask);

            var dbContext = StatisticsDbUtils.InMemoryStatisticsDbContext();

            await using (dbContext)
            {
                await dbContext.Observation.AddRangeAsync(
                    new Observation
                {
                    SubjectId = message.SubjectId
                },
                    new Observation
                {
                    SubjectId = message.SubjectId
                });

                await dbContext.SaveChangesAsync();

                await service.CheckComplete(message.ReleaseId, message, dbContext);
            }

            MockUtils.VerifyAllMocks(importStatusService, batchService);
        }
        public async Task DeleteAllReleaseSubjects()
        {
            var release = new Release();

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

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

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

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

                await statisticsDbContext.SaveChangesAsync();
            }

            await using (var statisticsDbContext = StatisticsDbUtils.InMemoryStatisticsDbContext(contextId))
            {
                var subjectDeleter = new Mock <ReleaseSubjectRepository.SubjectDeleter>();

                subjectDeleter
                .Setup(
                    s =>
                    s.Delete(
                        It.Is <Subject>(subject => subject.Id == releaseSubject1.SubjectId),
                        It.IsAny <StatisticsDbContext>()
                        )
                    );
                subjectDeleter
                .Setup(
                    s => s.Delete(
                        It.Is <Subject>(subject => subject.Id == releaseSubject2.SubjectId),
                        It.IsAny <StatisticsDbContext>()
                        )
                    );

                var service = BuildReleaseSubjectService(statisticsDbContext, subjectDeleter: subjectDeleter.Object);
                await service.DeleteAllReleaseSubjects(release.Id);

                MockUtils.VerifyAllMocks(subjectDeleter);
            }

            await using (var statisticsDbContext = StatisticsDbUtils.InMemoryStatisticsDbContext(contextId))
            {
                Assert.Empty(statisticsDbContext.ReleaseSubject.ToList());
            }
        }
Example #9
0
        public async Task CheckComplete_LastBatchFileCompleted_HasErrors()
        {
            var message = new ImportObservationsMessage
            {
                ReleaseId    = Guid.NewGuid(),
                NumBatches   = 2,
                DataFileName = "my_data_file.csv",
                TotalRows    = 2,
                SubjectId    = Guid.NewGuid()
            };

            var importStatusService = new Mock <IImportStatusService>(Strict);
            var fileStorageService  = new Mock <IFileStorageService>();

            var service = BuildFileImportService(
                importStatusService: importStatusService.Object,
                fileStorageService: fileStorageService.Object);

            importStatusService
            .Setup(s => s.GetImportStatus(message.ReleaseId, message.DataFileName))
            .ReturnsAsync(new ImportStatus
            {
                Status = STAGE_4,
                Errors = "an error"
            });

            fileStorageService
            .Setup(s => s.GetNumBatchesRemaining(message.ReleaseId, message.DataFileName))
            .ReturnsAsync(0);

            importStatusService
            .Setup(s => s.UpdateStatus(
                       message.ReleaseId, message.DataFileName, FAILED, 100))
            .Returns(Task.CompletedTask);

            var dbContext = StatisticsDbUtils.InMemoryStatisticsDbContext();

            await using (dbContext)
            {
                await dbContext.Observation.AddRangeAsync(
                    new Observation
                {
                    SubjectId = message.SubjectId
                },
                    new Observation
                {
                    SubjectId = message.SubjectId
                });

                await dbContext.SaveChangesAsync();

                await service.CheckComplete(message.ReleaseId, message, dbContext);
            }

            MockUtils.VerifyAllMocks(importStatusService, fileStorageService);
        }
        public async Task FindPublicationForSubject_NotFoundReturnsNull()
        {
            var contextId = Guid.NewGuid().ToString();

            await using (var context = StatisticsDbUtils.InMemoryStatisticsDbContext(contextId))
            {
                var service = BuildSubjectService(context);
                var result  = await service.FindPublicationForSubject(Guid.NewGuid());

                Assert.Null(result);
            }
        }
        public async Task Get_WithReleaseAndSubjectName_ReleaseNotFound()
        {
            var contextId = Guid.NewGuid().ToString();

            await using (var context = StatisticsDbUtils.InMemoryStatisticsDbContext(contextId))
            {
                var service = BuildSubjectService(context);
                var result  = await service.Get(Guid.NewGuid(), "Test subject");

                Assert.Null(result);
            }
        }
Example #12
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 GetPublicationForSubject_NotFoundThrows()
        {
            var contextId = Guid.NewGuid().ToString();

            await using (var context = StatisticsDbUtils.InMemoryStatisticsDbContext(contextId))
            {
                var service = BuildSubjectService(context);

                await Assert.ThrowsAsync <InvalidOperationException>(
                    () => service.GetPublicationForSubject(Guid.NewGuid())
                    );
            }
        }
Example #14
0
        public async Task Query_LatestRelease_SubjectNotFound()
        {
            var publication = new Publication
            {
                Id = Guid.NewGuid(),
            };

            var release = new Release
            {
                PublicationId = publication.Id,
                Publication   = publication,
            };

            var query = new ObservationQueryContext
            {
                SubjectId = Guid.NewGuid(),
            };

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

            await using (var statisticsDbContext = StatisticsDbUtils.InMemoryStatisticsDbContext(contextId))
            {
                var subjectService = new Mock <ISubjectService>();

                subjectService
                .Setup(s => s.GetPublicationForSubject(query.SubjectId))
                .ReturnsAsync(publication);

                var releaseService = new Mock <IReleaseService>();

                releaseService
                .Setup(s => s.GetLatestPublishedRelease(publication.Id))
                .Returns(release);

                var service = BuildTableBuilderService(
                    statisticsDbContext,
                    subjectService: subjectService.Object,
                    releaseService: releaseService.Object
                    );

                var result = await service.Query(query);

                Assert.True(result.IsLeft);
                Assert.IsType <NotFoundResult>(result.Left);
                MockUtils.VerifyAllMocks(subjectService, releaseService);
            }
        }
Example #15
0
        public async Task CheckComplete_LastBatchFileCompleted_Aborting()
        {
            await AbortingStatuses.ForEachAsync(async abortingStatus =>
            {
                var message = new ImportObservationsMessage
                {
                    ReleaseId    = Guid.NewGuid(),
                    NumBatches   = 2,
                    DataFileName = "my_data_file.csv",
                    TotalRows    = 2,
                    SubjectId    = Guid.NewGuid()
                };

                var importStatusService = new Mock <IImportStatusService>(Strict);
                var fileStorageService  = new Mock <IFileStorageService>();

                var service = BuildFileImportService(
                    importStatusService: importStatusService.Object,
                    fileStorageService: fileStorageService.Object);

                var currentStatus = new ImportStatus
                {
                    Status = abortingStatus
                };

                importStatusService
                .Setup(s => s.GetImportStatus(message.ReleaseId, message.DataFileName))
                .ReturnsAsync(currentStatus);

                importStatusService
                .Setup(s => s.UpdateStatus(
                           message.ReleaseId, message.DataFileName, currentStatus.GetFinishingStateOfAbortProcess(), 100))
                .Returns(Task.CompletedTask);

                var dbContext = StatisticsDbUtils.InMemoryStatisticsDbContext();

                await using (dbContext)
                {
                    await service.CheckComplete(message.ReleaseId, message, dbContext);
                }

                MockUtils.VerifyAllMocks(importStatusService, fileStorageService);
            });
        }
Example #16
0
        public async Task CheckComplete_BatchedFilesStillProcessing()
        {
            var message = new ImportObservationsMessage
            {
                ReleaseId    = Guid.NewGuid(),
                NumBatches   = 2,
                DataFileName = "my_data_file.csv",
                TotalRows    = 2,
                SubjectId    = Guid.NewGuid(),
                BatchNo      = 1
            };

            var importStatusService = new Mock <IImportStatusService>(Strict);
            var fileStorageService  = new Mock <IFileStorageService>();

            var service = BuildFileImportService(
                importStatusService: importStatusService.Object,
                fileStorageService: fileStorageService.Object);

            importStatusService
            .Setup(s => s.GetImportStatus(message.ReleaseId, message.DataFileName))
            .ReturnsAsync(new ImportStatus
            {
                Status = STAGE_4
            });

            fileStorageService
            .Setup(s => s.GetNumBatchesRemaining(message.ReleaseId, message.DataFileName))
            .ReturnsAsync(1);

            importStatusService
            .Setup(s => s.UpdateStatus(
                       message.ReleaseId, message.DataFileName, STAGE_4, 50))
            .Returns(Task.CompletedTask);

            var dbContext = StatisticsDbUtils.InMemoryStatisticsDbContext();

            await using (dbContext)
            {
                await service.CheckComplete(message.ReleaseId, message, dbContext);
            }

            MockUtils.VerifyAllMocks(importStatusService, fileStorageService);
        }
        public async Task DeleteReleaseSubject_NonOrphanedSubject()
        {
            var subject = new Subject();

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

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

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

                await statisticsDbContext.SaveChangesAsync();
            }

            await using (var statisticsDbContext = StatisticsDbUtils.InMemoryStatisticsDbContext(contextId))
            {
                var service = BuildReleaseSubjectService(statisticsDbContext);
                await service.DeleteReleaseSubject(releaseSubject2.ReleaseId, releaseSubject2.SubjectId);
            }

            await using (var statisticsDbContext = StatisticsDbUtils.InMemoryStatisticsDbContext(contextId))
            {
                var releaseSubjects = statisticsDbContext.ReleaseSubject.ToList();
                Assert.Single(releaseSubjects);
                Assert.Equal(subject.Id, releaseSubjects[0].SubjectId);

                var subjects = statisticsDbContext.Subject.ToList();
                Assert.Single(subjects);
                Assert.Equal(subject.Id, subjects[0].Id);
            }
        }
        public async Task Get_WithSubjectId()
        {
            var subject   = new Subject();
            var contextId = Guid.NewGuid().ToString();

            await using (var context = StatisticsDbUtils.InMemoryStatisticsDbContext(contextId))
            {
                await context.AddAsync(subject);

                await context.SaveChangesAsync();
            }

            await using (var context = StatisticsDbUtils.InMemoryStatisticsDbContext(contextId))
            {
                var service = BuildSubjectService(context);
                var result  = await service.Get(subject.Id);

                Assert.NotNull(result);
                Assert.Equal(subject.Id, result.Id);
            }
        }
        public async Task DeleteReleaseSubject_NotFound()
        {
            var releaseSubject = new ReleaseSubject
            {
                Release = new Release(),
                Subject = new Subject(),
            };

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

            await using (var statisticsDbContext = StatisticsDbUtils.InMemoryStatisticsDbContext(contextId))
            {
                await statisticsDbContext.AddAsync(releaseSubject);

                await statisticsDbContext.SaveChangesAsync();
            }

            await using (var statisticsDbContext = StatisticsDbUtils.InMemoryStatisticsDbContext(contextId))
            {
                var service = BuildReleaseSubjectService(statisticsDbContext);
                // Try to delete non-existing ReleaseSubject
                await service.DeleteReleaseSubject(Guid.NewGuid(), Guid.NewGuid());
            }

            await using (var statisticsDbContext = StatisticsDbUtils.InMemoryStatisticsDbContext(contextId))
            {
                // Check that other ReleaseSubject has not been deleted
                var releaseSubjects = statisticsDbContext.ReleaseSubject.ToList();
                Assert.Single(releaseSubjects);
                Assert.Equal(releaseSubject.SubjectId, releaseSubjects[0].SubjectId);

                var subjects = statisticsDbContext.Subject.ToList();
                Assert.Single(subjects);
                Assert.Equal(releaseSubject.SubjectId, subjects[0].Id);
            }
        }
        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);
                }
        }
Example #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);
            }
        }
Example #22
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);
            }
        }
Example #23
0
        public async Task Query_ReleaseId()
        {
            var releaseSubject = new ReleaseSubject
            {
                Release = new Release(),
                Subject = new Subject
                {
                    Id = Guid.NewGuid()
                },
            };

            var indicator1Id = Guid.NewGuid();
            var indicator2Id = Guid.NewGuid();

            var query = new ObservationQueryContext
            {
                SubjectId  = releaseSubject.Subject.Id,
                Indicators = new[] { indicator1Id, indicator2Id, },
                Locations  = new LocationQuery
                {
                    Country = new[] { "england" }
                },
                TimePeriod = new TimePeriodQuery
                {
                    StartYear = 2019,
                    StartCode = TimeIdentifier.AcademicYear,
                    EndYear   = 2020,
                    EndCode   = TimeIdentifier.AcademicYear,
                }
            };

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

            await using (var statisticsDbContext = StatisticsDbUtils.InMemoryStatisticsDbContext(contextId))
            {
                await statisticsDbContext.AddAsync(releaseSubject);

                await statisticsDbContext.SaveChangesAsync();
            }

            await using (var statisticsDbContext = StatisticsDbUtils.InMemoryStatisticsDbContext(contextId))
            {
                var observations = new List <Observation>
                {
                    new Observation
                    {
                        Measures = new Dictionary <Guid, string>
                        {
                            { indicator1Id, "123" },
                            { indicator2Id, "456" },
                        },
                        FilterItems    = new List <ObservationFilterItem>(),
                        Year           = 2019,
                        TimeIdentifier = TimeIdentifier.AcademicYear,
                    },
                    new Observation
                    {
                        Measures = new Dictionary <Guid, string>
                        {
                            { indicator1Id, "789" },
                            { Guid.NewGuid(), "1123" },
                            { Guid.NewGuid(), "1456" },
                        },
                        FilterItems    = new List <ObservationFilterItem>(),
                        Year           = 2020,
                        TimeIdentifier = TimeIdentifier.AcademicYear,
                    },
                };

                var subjectMeta = new ResultSubjectMetaViewModel
                {
                    Indicators = new List <IndicatorMetaViewModel>
                    {
                        new IndicatorMetaViewModel
                        {
                            Label = "Test indicator"
                        }
                    },
                };

                var observationService = new Mock <IObservationService>();

                observationService
                .Setup(s => s.FindObservations(query))
                .Returns(observations);

                var resultSubjectMetaService = new Mock <IResultSubjectMetaService>();

                resultSubjectMetaService
                .Setup(
                    s => s.GetSubjectMeta(
                        releaseSubject.ReleaseId,
                        It.IsAny <SubjectMetaQueryContext>(),
                        It.IsAny <IQueryable <Observation> >()
                        )
                    )
                .ReturnsAsync(subjectMeta);

                var service = BuildTableBuilderService(
                    statisticsDbContext,
                    observationService: observationService.Object,
                    resultSubjectMetaService: resultSubjectMetaService.Object
                    );

                var result = await service.Query(releaseSubject.ReleaseId, query);

                Assert.True(result.IsRight);

                var observationResults = result.Right.Results.ToList();

                Assert.Equal(2, observationResults.Count);

                Assert.Equal("2019_AY", observationResults[0].TimePeriod);
                Assert.Equal(2, observationResults[0].Measures.Count);
                Assert.Equal("123", observationResults[0].Measures[indicator1Id.ToString()]);
                Assert.Equal("456", observationResults[0].Measures[indicator2Id.ToString()]);

                Assert.Equal("2020_AY", observationResults[1].TimePeriod);
                Assert.Single(observationResults[1].Measures);
                Assert.Equal("789", observationResults[1].Measures[indicator1Id.ToString()]);

                Assert.Equal(subjectMeta, result.Right.SubjectMeta);

                MockUtils.VerifyAllMocks(observationService, resultSubjectMetaService);
            }
        }
Example #24
0
        public async Task GetSubjectsWithNoFootnotes_FootnoteForMultipleSubjects()
        {
            var release = new Release();

            var releaseSubject1 = new ReleaseSubject
            {
                Release = release,
                Subject = new Subject
                {
                    Name = "Test subject 1"
                }
            };
            var releaseSubject2 = new ReleaseSubject
            {
                Release = release,
                Subject = new Subject
                {
                    Name = "Test subject 2"
                }
            };
            var releaseSubject3 = new ReleaseSubject
            {
                Release = release,
                Subject = new Subject
                {
                    Name = "Test subject 3"
                }
            };
            var releaseSubject4 = new ReleaseSubject
            {
                Release = release,
                Subject = new Subject
                {
                    Name = "Test subject 4"
                }
            };
            var releaseSubject5 = new ReleaseSubject
            {
                Release = release,
                Subject = new Subject
                {
                    Name = "Test subject 5",
                }
            };
            var releaseSubjectWithNoFootnotes = new ReleaseSubject
            {
                Release = release,
                Subject = new Subject
                {
                    Name = "Test subject with no footnotes"
                }
            };

            var footnote = new Footnote
            {
                Content  = "Test footnote 1",
                Releases = new List <ReleaseFootnote>
                {
                    new ReleaseFootnote
                    {
                        Release = release
                    },
                },
                Subjects = new List <SubjectFootnote>
                {
                    new SubjectFootnote
                    {
                        Subject = releaseSubject1.Subject,
                    }
                },
                Filters = new List <FilterFootnote>
                {
                    new FilterFootnote
                    {
                        Filter = new Filter
                        {
                            Subject = releaseSubject2.Subject
                        },
                    }
                },
                FilterGroups = new List <FilterGroupFootnote>
                {
                    new FilterGroupFootnote
                    {
                        FilterGroup = new FilterGroup
                        {
                            Filter = new Filter
                            {
                                Subject = releaseSubject3.Subject
                            },
                        }
                    }
                },
                FilterItems = new List <FilterItemFootnote>
                {
                    new FilterItemFootnote
                    {
                        FilterItem = new FilterItem
                        {
                            FilterGroup = new FilterGroup
                            {
                                Filter = new Filter
                                {
                                    Subject = releaseSubject4.Subject
                                },
                            }
                        }
                    }
                },
                Indicators = new List <IndicatorFootnote>
                {
                    new IndicatorFootnote
                    {
                        Indicator = new Indicator
                        {
                            IndicatorGroup = new IndicatorGroup
                            {
                                Subject = releaseSubject5.Subject
                            }
                        }
                    }
                }
            };

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

            await using (var context = StatisticsDbUtils.InMemoryStatisticsDbContext(contextId))
            {
                await context.AddRangeAsync(release);

                await context.AddRangeAsync(
                    releaseSubject1,
                    releaseSubject2,
                    releaseSubject3,
                    releaseSubject4,
                    releaseSubject5,
                    releaseSubjectWithNoFootnotes
                    );

                await context.AddRangeAsync(footnote);

                await context.SaveChangesAsync();
            }

            await using (var context = StatisticsDbUtils.InMemoryStatisticsDbContext(contextId))
            {
                var repository = BuildFootnoteRepository(context);
                var results    = await repository.GetSubjectsWithNoFootnotes(release.Id);

                Assert.Single(results);

                Assert.Equal(releaseSubjectWithNoFootnotes.Subject.Id, results[0].Id);
                Assert.Equal(releaseSubjectWithNoFootnotes.Subject.Name, results[0].Name);
            }
        }
Example #25
0
        public async Task GetFootnotes_FiltersCriteriaBySubjectId()
        {
            var release      = new Release();
            var otherRelease = new Release();

            var releaseSubject = new ReleaseSubject
            {
                Release = release,
                Subject = new Subject
                {
                    Name = "Test subject for release"
                }
            };

            var otherReleaseSubject = new ReleaseSubject
            {
                Release = otherRelease,
                Subject = new Subject
                {
                    Name = "Test subject for other release"
                }
            };

            var footnote = new Footnote
            {
                Content  = "Test footnote",
                Releases = new List <ReleaseFootnote>
                {
                    new ReleaseFootnote
                    {
                        Release = release
                    },
                    new ReleaseFootnote
                    {
                        Release = otherRelease
                    }
                },
                Subjects = new List <SubjectFootnote>
                {
                    new SubjectFootnote
                    {
                        Subject = releaseSubject.Subject
                    },
                    new SubjectFootnote
                    {
                        Subject = otherReleaseSubject.Subject
                    },
                },
                Filters = new List <FilterFootnote>
                {
                    new FilterFootnote
                    {
                        Filter = new Filter
                        {
                            Subject = releaseSubject.Subject
                        }
                    },
                    new FilterFootnote
                    {
                        Filter = new Filter
                        {
                            Subject = otherReleaseSubject.Subject
                        }
                    }
                },
                FilterGroups = new List <FilterGroupFootnote>
                {
                    new FilterGroupFootnote
                    {
                        FilterGroup = new FilterGroup
                        {
                            Filter = new Filter
                            {
                                Subject = releaseSubject.Subject
                            }
                        }
                    },
                    new FilterGroupFootnote
                    {
                        FilterGroup = new FilterGroup
                        {
                            Filter = new Filter
                            {
                                Subject = otherReleaseSubject.Subject
                            }
                        }
                    }
                },
                FilterItems = new List <FilterItemFootnote>
                {
                    new FilterItemFootnote
                    {
                        FilterItem = new FilterItem
                        {
                            FilterGroup = new FilterGroup
                            {
                                Filter = new Filter
                                {
                                    Subject = releaseSubject.Subject
                                }
                            }
                        }
                    },
                    new FilterItemFootnote
                    {
                        FilterItem = new FilterItem
                        {
                            FilterGroup = new FilterGroup
                            {
                                Filter = new Filter
                                {
                                    Subject = otherReleaseSubject.Subject,
                                }
                            }
                        }
                    }
                },
                Indicators = new List <IndicatorFootnote>
                {
                    new IndicatorFootnote
                    {
                        Indicator = new Indicator
                        {
                            IndicatorGroup = new IndicatorGroup
                            {
                                Subject = releaseSubject.Subject
                            }
                        }
                    },
                    new IndicatorFootnote
                    {
                        Indicator = new Indicator
                        {
                            IndicatorGroup = new IndicatorGroup
                            {
                                Subject = otherReleaseSubject.Subject
                            }
                        }
                    }
                }
            };

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

            await using (var context = StatisticsDbUtils.InMemoryStatisticsDbContext(contextId))
            {
                await context.AddRangeAsync(release, otherRelease);

                await context.AddRangeAsync(releaseSubject, otherReleaseSubject);

                await context.AddAsync(footnote);

                await context.SaveChangesAsync();
            }

            await using (var context = StatisticsDbUtils.InMemoryStatisticsDbContext(contextId))
            {
                var repository = BuildFootnoteRepository(context);
                var results    = repository.GetFootnotes(release.Id, releaseSubject.SubjectId).ToList();

                Assert.Single(results);
                Assert.Equal("Test footnote", results[0].Content);

                var footnoteReleases = results[0].Releases.ToList();

                Assert.Single(footnoteReleases);
                Assert.Equal(releaseSubject.ReleaseId, footnoteReleases[0].ReleaseId);

                var footnoteSubjects = results[0].Subjects.ToList();

                Assert.Single(footnoteSubjects);
                Assert.Equal(releaseSubject.SubjectId, footnoteSubjects[0].SubjectId);

                var footnoteFilters = results[0].Filters.ToList();

                Assert.Single(footnoteSubjects);
                Assert.Equal(releaseSubject.SubjectId, footnoteFilters[0].Filter.SubjectId);

                var footnoteFilterGroups = results[0].FilterGroups.ToList();

                Assert.Single(footnoteFilterGroups);
                Assert.Equal(releaseSubject.SubjectId, footnoteFilterGroups[0].FilterGroup.Filter.SubjectId);

                var footnoteFilterItems = results[0].FilterItems.ToList();

                Assert.Single(footnoteFilterItems);
                Assert.Equal(releaseSubject.SubjectId, footnoteFilterItems[0].FilterItem.FilterGroup.Filter.SubjectId);

                var footnoteIndicators = results[0].Indicators.ToList();

                Assert.Single(footnoteIndicators);
                Assert.Equal(releaseSubject.SubjectId, footnoteIndicators[0].Indicator.IndicatorGroup.SubjectId);
            }
        }
Example #26
0
        public async Task GetFootnotes_FiltersBySubjectIds()
        {
            var release = new Release();

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

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

            var releaseSubject3 = new ReleaseSubject
            {
                Release = release,
                Subject = new Subject()
            };

            var footnote1 = new Footnote
            {
                Content  = "Test footnote 1",
                Releases = new List <ReleaseFootnote>
                {
                    new ReleaseFootnote
                    {
                        Release = release
                    },
                },
                Subjects = new List <SubjectFootnote>
                {
                    new SubjectFootnote
                    {
                        Subject = releaseSubject1.Subject
                    }
                },
            };

            var footnote2 = new Footnote
            {
                Content  = "Test footnote 2",
                Releases = new List <ReleaseFootnote>
                {
                    new ReleaseFootnote
                    {
                        Release = release
                    },
                },
                Subjects = new List <SubjectFootnote>
                {
                    new SubjectFootnote
                    {
                        Subject = releaseSubject2.Subject
                    }
                },
            };

            var footnote3 = new Footnote
            {
                Content  = "Test footnote 3",
                Releases = new List <ReleaseFootnote>
                {
                    new ReleaseFootnote
                    {
                        Release = release
                    },
                },
                Subjects = new List <SubjectFootnote>
                {
                    new SubjectFootnote
                    {
                        Subject = releaseSubject3.Subject
                    }
                },
            };

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

            await using (var context = StatisticsDbUtils.InMemoryStatisticsDbContext(contextId))
            {
                await context.AddAsync(release);

                await context.AddRangeAsync(releaseSubject1, releaseSubject2, releaseSubject3);

                await context.AddRangeAsync(footnote1, footnote2, footnote3);

                await context.SaveChangesAsync();
            }

            await using (var context = StatisticsDbUtils.InMemoryStatisticsDbContext(contextId))
            {
                var repository = BuildFootnoteRepository(context);
                var results    = repository.GetFootnotes(
                    release.Id,
                    new List <Guid> {
                    releaseSubject1.SubjectId, releaseSubject3.SubjectId
                }
                    )
                                 .ToList();

                Assert.Equal(2, results.Count);

                Assert.Equal("Test footnote 1", results[0].Content);
                Assert.Equal("Test footnote 3", results[1].Content);
            }
        }
Example #27
0
        public async Task GetFootnotes_FiltersByRelease()
        {
            var release      = new Release();
            var otherRelease = new Release();

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

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

            var footnote1 = new Footnote
            {
                Content  = "Test footnote 1",
                Releases = new List <ReleaseFootnote>
                {
                    new ReleaseFootnote
                    {
                        Release = release
                    },
                    // Check that footnote is still fetched
                    // even if it also linked to another release
                    new ReleaseFootnote
                    {
                        Release = otherRelease
                    }
                },
                Subjects = new List <SubjectFootnote>
                {
                    new SubjectFootnote
                    {
                        Subject = releaseSubject1.Subject
                    }
                },
            };

            var footnote2 = new Footnote
            {
                Content  = "Test footnote 2",
                Releases = new List <ReleaseFootnote>
                {
                    new ReleaseFootnote
                    {
                        Release = release
                    },
                },
                Subjects = new List <SubjectFootnote>
                {
                    new SubjectFootnote
                    {
                        Subject = releaseSubject2.Subject
                    }
                },
            };

            var footnoteForOtherRelease = new Footnote
            {
                Content  = "Test footnote for other release",
                Releases = new List <ReleaseFootnote>
                {
                    new ReleaseFootnote
                    {
                        Release = new Release()
                    },
                },
                Subjects = new List <SubjectFootnote>
                {
                    new SubjectFootnote
                    {
                        Subject = new Subject()
                    }
                },
            };

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

            await using (var context = StatisticsDbUtils.InMemoryStatisticsDbContext(contextId))
            {
                await context.AddAsync(release);

                await context.AddRangeAsync(releaseSubject1, releaseSubject2);

                await context.AddRangeAsync(footnote1, footnote2, footnoteForOtherRelease);

                await context.SaveChangesAsync();
            }

            await using (var context = StatisticsDbUtils.InMemoryStatisticsDbContext(contextId))
            {
                var repository = BuildFootnoteRepository(context);
                var results    = repository.GetFootnotes(release.Id).ToList();

                Assert.Equal(2, results.Count);

                Assert.Equal("Test footnote 1", results[0].Content);

                var footnote1Releases = results[0].Releases.ToList();

                Assert.Single(footnote1Releases);
                Assert.Equal(release.Id, footnote1Releases[0].ReleaseId);

                var footnote1Subjects = results[0].Subjects.ToList();

                Assert.Single(footnote1Subjects);
                Assert.Equal(releaseSubject1.SubjectId, footnote1Subjects[0].SubjectId);

                Assert.Equal("Test footnote 2", results[1].Content);

                var footnote2Releases = results[1].Releases.ToList();

                Assert.Single(footnote2Releases);
                Assert.Equal(release.Id, footnote2Releases[0].ReleaseId);

                var footnote2Subjects = results[1].Subjects.ToList();
                Assert.Single(footnote2Subjects);
                Assert.Equal(releaseSubject2.SubjectId, footnote2Subjects[0].SubjectId);
            }
        }