private async Task <FileInfo> GetReleaseFileInfo(ReleaseFile releaseFile)
        {
            var blobExists = await _blobStorageService.CheckBlobExists(
                PrivateReleaseFiles,
                releaseFile.Path()
                );

            if (!blobExists)
            {
                return(releaseFile.ToFileInfoNotFound());
            }

            var blob = await _blobStorageService.GetBlob(PrivateReleaseFiles, releaseFile.Path());

            return(releaseFile.ToFileInfo(blob));
        }
        public void Path_ReleaseFile()
        {
            var releaseFile = new ReleaseFile
            {
                File = _file
            };

            Assert.Equal(_file.Path(), releaseFile.Path());
        }
Example #3
0
        public void Path()
        {
            var releaseFile = new ReleaseFile
            {
                File = new File
                {
                    Id       = Guid.NewGuid(),
                    RootPath = Guid.NewGuid(),
                    Filename = "ancillary.pdf",
                    Type     = Ancillary
                }
            };

            Assert.Equal(releaseFile.File.Path(), releaseFile.Path());
        }
        public async Task Stream()
        {
            var release = new Release();

            var releaseFile = new ReleaseFile
            {
                Release = release,
                File    = new File
                {
                    RootPath = Guid.NewGuid(),
                    Filename = "image.png",
                    Type     = Image
                }
            };

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

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

                await contentDbContext.AddAsync(releaseFile);

                await contentDbContext.SaveChangesAsync();
            }

            var blobStorageService = new Mock <IBlobStorageService>(MockBehavior.Strict);

            var blob = new BlobInfo(
                path: string.Empty,
                size: string.Empty,
                contentType: "image/png",
                contentLength: 0L,
                meta: null,
                created: null);

            blobStorageService.Setup(mock =>
                                     mock.GetBlob(PrivateReleaseFiles, releaseFile.Path()))
            .ReturnsAsync(blob);

            blobStorageService.Setup(mock =>
                                     mock.DownloadToStream(PrivateReleaseFiles, releaseFile.Path(),
                                                           It.IsAny <MemoryStream>(), null))
            .ReturnsAsync(new MemoryStream());

            await using (var contentDbContext = InMemoryApplicationDbContext(contentDbContextId))
            {
                var service = SetupReleaseImageService(contentDbContext: contentDbContext,
                                                       blobStorageService: blobStorageService.Object);

                var result = await service.Stream(release.Id, releaseFile.File.Id);

                Assert.True(result.IsRight);

                blobStorageService.Verify(
                    mock => mock.GetBlob(PrivateReleaseFiles, releaseFile.Path()),
                    Times.Once());

                blobStorageService.Verify(
                    mock =>
                    mock.DownloadToStream(PrivateReleaseFiles, releaseFile.Path(),
                                          It.IsAny <MemoryStream>(), null),
                    Times.Once());

                Assert.Equal("image/png", result.Right.ContentType);
                Assert.Equal("image.png", result.Right.FileDownloadName);
                Assert.IsType <MemoryStream>(result.Right.FileStream);
            }

            MockUtils.VerifyAllMocks(blobStorageService);
        }
Example #5
0
        public async Task ListSubjects()
        {
            var statisticsRelease = new Data.Model.Release();

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

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

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

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

                await statisticsDbContext.SaveChangesAsync();
            }

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

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

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

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

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

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

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

                await contentDbContext.AddRangeAsync(import1, import2);

                await contentDbContext.SaveChangesAsync();
            }

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

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

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

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

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

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

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

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

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

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

                    MockUtils.VerifyAllMocks(dataGuidanceSubjectService, fileInfoGetter);

                    var subjects = result.AssertRight();

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

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

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

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

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

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

                    Assert.Single(subjects[1].GeographicLevels);
                    Assert.Equal("National", subjects[1].GeographicLevels[0]);
                }
        }