public async Task GetAsync_OwnerNotFound_ThrowsException()
        {
            TestingObject <ReportService> testingObject = this.GetTestingObject();

            Guid reportIdArg = Guid.NewGuid();

            var mockDbContext = testingObject.GetDependency <Mock <IDatabaseContext> >();

            var report = new ReportMetadataSql
            {
                Created = DateTime.UtcNow.AddYears(-2),
                OwnerId = Guid.NewGuid()
            };

            mockDbContext
            .Setup(m => m.FindSingleAsync <ReportMetadataSql>(It.Is <Guid>(id => id == reportIdArg)))
            .ReturnsAsync(report);

            mockDbContext
            .Setup(m => m.FindSingleAsync <UserSql>(It.Is <Guid>(id => id == report.OwnerId)))
            .ReturnsAsync((UserSql)null);

            ReportService reportService = testingObject.GetResolvedTestingObject();
            await Assert.ThrowsAsync <Exception>(async()
                                                 => await reportService.GetAsync(reportIdArg));
        }
示例#2
0
        public async Task <ReportMetadata> GetAsync(Guid reportId)
        {
            if (reportId == Guid.Empty)
            {
                throw new ArgumentException("Report id must be provided", nameof(reportId));
            }

            ReportMetadataSql reportSql = await this.DbContext.FindSingleAsync <ReportMetadataSql>(reportId);

            if (reportSql == null)
            {
                throw new Exception($"Report '{reportId}' was not found.");
            }


            // if report is older than 3 years, dont return it
            if (reportSql.Created.AddYears(3) < DateTime.UtcNow)
            {
                throw new Exception($"Report '{reportId}' is too old and will not be retrieved.");
            }

            UserSql ownerSql = await this.DbContext.FindSingleAsync <UserSql>(reportSql.OwnerId);

            if (ownerSql == null)
            {
                throw new Exception($"User '{reportSql.OwnerId}' not found.");
            }


            // if owner of report is disabled, dont return it
            if (!ownerSql.Enabled)
            {
                throw new Exception($"Report '{reportId}' is owned by a disabled user and will not be retrieved.");
            }

            User   owner          = UserSql.ToEntity(ownerSql);
            string authorFullName = UserUtil.GetFullName(owner.FirstName, owner.LastName);

            // modify title based on whether it's been updated
            if (reportSql.LastUpdated.HasValue)
            {
                reportSql.Title += " (Revision)";
            }
            else
            {
                reportSql.Title += " (Original)";
            }

            return(new ReportMetadata
            {
                Id = reportSql.Id,
                LastUpdated = reportSql.LastUpdated,
                LastRevisionById = reportSql.LastRevisionById,
                Title = reportSql.Title,
                Created = reportSql.Created,
                AuthorFullName = authorFullName
            });
        }
        public async Task GetAsync_Success_Updated_ReturnsCorrectResult()
        {
            TestingObject <ReportService> testingObject = this.GetTestingObject();

            Guid reportIdArg = Guid.NewGuid();

            var mockDbContext = testingObject.GetDependency <Mock <IDatabaseContext> >();

            var report = new ReportMetadataSql
            {
                Created     = DateTime.UtcNow.AddYears(-2),
                OwnerId     = Guid.NewGuid(),
                LastUpdated = DateTime.UtcNow,
                Title       = "Report Title"
            };

            mockDbContext
            .Setup(m => m.FindSingleAsync <ReportMetadataSql>(It.Is <Guid>(id => id == reportIdArg)))
            .ReturnsAsync(report);

            var owner = new UserSql
            {
                Enabled   = true,
                FirstName = "Mary",
                LastName  = "Jane"
            };

            mockDbContext
            .Setup(m => m.FindSingleAsync <UserSql>(It.Is <Guid>(id => id == report.OwnerId)))
            .ReturnsAsync(owner);

            ReportService reportService = testingObject.GetResolvedTestingObject();

            ReportMetadata result = await reportService.GetAsync(reportIdArg);

            Assert.EndsWith("(Revision)", result.Title);
        }