public async Task CreateReport_WhenParamsAreValid() { //Arrange var options = Utils.GetOptions(nameof(CreateReport_WhenParamsAreValid)); var title = "Report Title"; var summary = "Report Summary"; var description = "Report Description"; var author = TestModelsSeeder.SeedUser(); var industry = TestModelsSeeder.SeedIndustry(); var imgURL = "imageurl"; var tag = TestModelsSeeder.SeedTag(); using (var arrangeContext = new InsightHubContext(options)) { await arrangeContext.Users.AddAsync(author); await arrangeContext.Industries.AddAsync(industry); await arrangeContext.Tags.AddAsync(tag); await arrangeContext.SaveChangesAsync(); } //Act & Assert using var assertContext = new InsightHubContext(options); var sutTags = new TagServices(assertContext); var sutReports = new ReportServices(assertContext, sutTags); var act = await sutReports.CreateReport(title, summary, description, author.Email, imgURL, industry.Name, tag.Name); var result = assertContext.Reports.FirstOrDefault(t => t.Title == title); Assert.AreEqual(title, result.Title); Assert.AreEqual(description, result.Description); Assert.AreEqual(tag.Name, result.ReportTags.First().Tag.Name); }
public async Task ReturnCorrectDeletedTags_When_ParamsValid() { //Arrange var options = Utils.GetOptions(nameof(ReturnCorrectDeletedTags_When_ParamsValid)); var firstTag = TestModelsSeeder.SeedTag(); var secondTag = TestModelsSeeder.SeedTag2(); using (var arrangeContext = new InsightHubContext(options)) { arrangeContext.Tags.Add(firstTag); arrangeContext.Tags.Add(secondTag); await arrangeContext.SaveChangesAsync(); } //Act & Assert using var assertContext = new InsightHubContext(options); var sut = new TagServices(assertContext); await sut.DeleteTag(1); await sut.DeleteTag(2); var act = await sut.GetDeletedTags(); var result = act.ToArray(); Assert.AreEqual(2, result.Length); Assert.AreEqual(firstTag.Name, result[0].Name); Assert.AreEqual(secondTag.Name, result[1].Name); }
public async Task ThrowArgumentException_When_TitleAlreadyExists() { //Arrange var options = Utils.GetOptions(nameof(ThrowArgumentException_When_TitleAlreadyExists)); var summary = "Report Summary"; var description = "Report Description"; var author = TestModelsSeeder.SeedUser(); var industry = TestModelsSeeder.SeedIndustry(); var imgURL = "imageurl"; var tag = TestModelsSeeder.SeedTag(); var report = TestModelsSeeder.SeedReport(); using (var arrangeContext = new InsightHubContext(options)) { await arrangeContext.Users.AddAsync(author); await arrangeContext.Industries.AddAsync(industry); await arrangeContext.Tags.AddAsync(tag); await arrangeContext.Reports.AddAsync(report); await arrangeContext.SaveChangesAsync(); } //Act & Assert using var assertContext = new InsightHubContext(options); var sutTags = new TagServices(assertContext); var sutReports = new ReportServices(assertContext, sutTags); await Assert.ThrowsExceptionAsync <ArgumentException>(() => sutReports.CreateReport(report.Title, summary, description, author.Email, imgURL, industry.Name, tag.Name)); }
public async Task ReturnCorrectTags_WithSortAndSearch_When_ParamValid() { //Arrange var options = Utils.GetOptions(nameof(ReturnCorrectTags_WithSortAndSearch_When_ParamValid)); var firstTag = TestModelsSeeder.SeedTag(); var secondTag = TestModelsSeeder.SeedTag2(); var thirdTag = TestModelsSeeder.SeedTag3(); using (var arrangeContext = new InsightHubContext(options)) { arrangeContext.Tags.Add(firstTag); arrangeContext.Tags.Add(secondTag); arrangeContext.Tags.Add(thirdTag); await arrangeContext.SaveChangesAsync(); } //Act & Assert using var assertContext = new InsightHubContext(options); var sut = new TagServices(assertContext); var act = await sut.GetTags("name_desc", "Test"); var result = act.ToArray(); Assert.AreEqual(3, result.Count()); Assert.AreEqual(thirdTag.Name, result[0].Name); }
public async Task CorrectlyUpdateData_WhenParamsValid() { //Arrange var options = Utils.GetOptions(nameof(CorrectlyUpdateData_WhenParamsValid)); var user = TestModelsSeeder.SeedUser(); var originalModifiedDate = user.ModifiedOn; using (var arrangeContext = new InsightHubContext(options)) { arrangeContext.Users.Add(user); await arrangeContext.SaveChangesAsync(); } //Act & Assert using var assertContext = new InsightHubContext(options); var sut = new UserServices(assertContext); var act = await sut.UpdateUser(1, "Test First Name", "Test Last Name", true, "Test Ban Reason"); var result = assertContext.Users.FirstOrDefault(); Assert.AreEqual("Test First Name", result.FirstName); Assert.AreEqual("Test Last Name", result.LastName); Assert.IsTrue(result.IsBanned); Assert.AreEqual("Test Ban Reason", result.BanReason); Assert.AreNotEqual(originalModifiedDate, result.ModifiedOn); }
public async Task AddSubscription_WhenParamsAreValid() { //Arrange var options = Utils.GetOptions(nameof(AddSubscription_WhenParamsAreValid)); var industry = TestModelsSeeder.SeedIndustry(); var user = TestModelsSeeder.SeedUser(); using (var arrangeContext = new InsightHubContext(options)) { await arrangeContext.Industries.AddAsync(industry); await arrangeContext.Users.AddAsync(user); await arrangeContext.SaveChangesAsync(); } //Act & Assert using var assertContext = new InsightHubContext(options); var sut = new IndustryServices(assertContext); await sut.AddSubscription(user.Id, industry.Id); var result = await assertContext.IndustrySubscriptions.AnyAsync(ui => ui.UserId == user.Id && ui.IndustryId == industry.Id); Assert.IsTrue(result); }
public async Task ThrowArgumentException_WhenReportNotExists() { //Arrange var options = Utils.GetOptions(nameof(ThrowArgumentException_WhenReportNotExists)); var report1 = TestModelsSeeder.SeedReport(); var industry1 = TestModelsSeeder.SeedIndustry(); var user = TestModelsSeeder.SeedUser(); var tag = TestModelsSeeder.SeedTag(); using (var arrangeContext = new InsightHubContext(options)) { await arrangeContext.Reports.AddAsync(report1); await arrangeContext.Tags.AddAsync(tag); await arrangeContext.Industries.AddAsync(industry1); await arrangeContext.Users.AddAsync(user); await arrangeContext.SaveChangesAsync(); } //Act & Assert using var assertContext = new InsightHubContext(options); var sutTag = new TagServices(assertContext); var sut = new ReportServices(assertContext, sutTag); await Assert.ThrowsExceptionAsync <ArgumentNullException>(() => sut.ApproveReport(5)); }
public async Task ToggleReport_ToFeatured_When_NotFeatured() { //Arrange var options = Utils.GetOptions(nameof(ToggleReport_ToFeatured_When_NotFeatured)); var report1 = TestModelsSeeder.SeedReport(); var industry1 = TestModelsSeeder.SeedIndustry(); var user = TestModelsSeeder.SeedUser(); var tag = TestModelsSeeder.SeedTag(); using (var arrangeContext = new InsightHubContext(options)) { await arrangeContext.Reports.AddAsync(report1); await arrangeContext.Tags.AddAsync(tag); await arrangeContext.Industries.AddAsync(industry1); await arrangeContext.Users.AddAsync(user); await arrangeContext.SaveChangesAsync(); } //Act & Assert using var assertContext = new InsightHubContext(options); var sutTag = new TagServices(assertContext); var sut = new ReportServices(assertContext, sutTag); Assert.IsFalse(assertContext.Reports.First(u => u.Id == 1).IsFeatured); await sut.ToggleFeatured(1); Assert.IsTrue(assertContext.Reports.First(u => u.Id == 1).IsFeatured); }
public async Task GetsCorrectReport_When_ParamsValid() { //Arrange var options = Utils.GetOptions(nameof(GetsCorrectReport_When_ParamsValid)); var report1 = TestModelsSeeder.SeedReport(); var industry1 = TestModelsSeeder.SeedIndustry(); var user = TestModelsSeeder.SeedUser(); var tag = TestModelsSeeder.SeedTag(); using (var arrangeContext = new InsightHubContext(options)) { await arrangeContext.Reports.AddAsync(report1); await arrangeContext.Tags.AddAsync(tag); await arrangeContext.Industries.AddAsync(industry1); await arrangeContext.Users.AddAsync(user); await arrangeContext.SaveChangesAsync(); } //Act & Assert using var assertContext = new InsightHubContext(options); var sutTags = new TagServices(assertContext); var sut = new ReportServices(assertContext, sutTags); var result = await sut.GetReport(1); Assert.AreEqual(report1.Title, result.Title); Assert.AreEqual(report1.Summary, result.Summary); Assert.AreEqual(report1.Description, result.Description); Assert.AreEqual(report1.Author.Email, result.Author.Split(" ").Last()); }
public async Task DoNotThrowArgumentException_When_AlreadyExists() { //Arrange var options = Utils.GetOptions(nameof(DoNotThrowArgumentException_When_AlreadyExists)); var industry = TestModelsSeeder.SeedIndustry(); var user = TestModelsSeeder.SeedUser(); using (var arrangeContext = new InsightHubContext(options)) { await arrangeContext.Industries.AddAsync(industry); await arrangeContext.Users.AddAsync(user); await arrangeContext.SaveChangesAsync(); } //Act & Assert using var assertContext = new InsightHubContext(options); var sut = new IndustryServices(assertContext); await sut.AddSubscription(user.Id, industry.Id); await sut.AddSubscription(user.Id, industry.Id); var result = await assertContext.IndustrySubscriptions.AnyAsync(ui => ui.UserId == user.Id && ui.IndustryId == industry.Id); Assert.IsTrue(result); Assert.AreEqual(1, assertContext.IndustrySubscriptions.Count()); }
public async Task ApproveReport_When_ParamsValid() { //Arrange var options = Utils.GetOptions(nameof(ApproveReport_When_ParamsValid)); var report1 = TestModelsSeeder.SeedReport(); var industry1 = TestModelsSeeder.SeedIndustry(); var user = TestModelsSeeder.SeedUser(); var tag = TestModelsSeeder.SeedTag(); report1.IsPending = true; using (var arrangeContext = new InsightHubContext(options)) { await arrangeContext.Reports.AddAsync(report1); await arrangeContext.Tags.AddAsync(tag); await arrangeContext.Industries.AddAsync(industry1); await arrangeContext.Users.AddAsync(user); await arrangeContext.SaveChangesAsync(); } //Act & Assert using var assertContext = new InsightHubContext(options); var sutTag = new TagServices(assertContext); var sut = new ReportServices(assertContext, sutTag); await sut.ApproveReport(1); Assert.IsFalse(assertContext.Reports.First(u => u.Id == 1).IsPending); }
public async Task ReturnOnlyActiveUsers_WhenSearching() { //Arrange var options = Utils.GetOptions(nameof(ReturnOnlyActiveUsers_WhenSearching)); var user1 = TestModelsSeeder.SeedUser(); var user2 = TestModelsSeeder.SeedUser(); user2.Id = 2; user1.IsBanned = false; user1.IsPending = false; user2.IsBanned = true; user2.IsPending = false; using (var arrangeContext = new InsightHubContext(options)) { arrangeContext.Users.Add(user1); arrangeContext.Users.Add(user2); await arrangeContext.SaveChangesAsync(); } //Act & Assert using var assertContext = new InsightHubContext(options); var sut = new UserServices(assertContext); var result = (await sut.GetUsers("first")).ToList(); Assert.AreEqual(1, result.Count); Assert.AreEqual(user1.Email, result[0].Email); }
public async Task ReturnOnlyPendingUsers() { //Arrange var options = Utils.GetOptions(nameof(ReturnOnlyPendingUsers)); var user1 = TestModelsSeeder.SeedUser(); var user2 = TestModelsSeeder.SeedUser2(); var user3 = TestModelsSeeder.SeedUser3(); user1.IsPending = true; user2.IsPending = true; user3.IsPending = false; using (var arrangeContext = new InsightHubContext(options)) { arrangeContext.Users.Add(user1); arrangeContext.Users.Add(user2); arrangeContext.Users.Add(user3); await arrangeContext.SaveChangesAsync(); } //Act & Assert using var assertContext = new InsightHubContext(options); var sut = new UserServices(assertContext); var act = await sut.GetPendingUsers(null); var result = act.ToList(); Assert.AreEqual(2, result.Count); Assert.AreEqual(user1.Email, result[0].Email); Assert.AreEqual(user2.Email, result[1].Email); }
public async Task ReturnNotDeletedIndustries_WithSearch_When_ParamValid() { //Arrange var options = Utils.GetOptions(nameof(ReturnNotDeletedIndustries_WithSearch_When_ParamValid)); var industry = TestModelsSeeder.SeedIndustry(); var industry2 = TestModelsSeeder.SeedIndustry2(); var industry3 = TestModelsSeeder.SeedIndustry3(); industry.IsDeleted = true; using (var arrangeContext = new InsightHubContext(options)) { await arrangeContext.Industries.AddAsync(industry); await arrangeContext.Industries.AddAsync(industry2); await arrangeContext.Industries.AddAsync(industry3); await arrangeContext.SaveChangesAsync(); } //Act & Assert using var assertContext = new InsightHubContext(options); var sut = new IndustryServices(assertContext); var result = (await sut.GetAllIndustries(null, "3")).ToArray(); Assert.AreEqual(1, result.Length); Assert.AreEqual(industry3.Name, result[0].Name); }
public async Task ReturnNotDeletedIndustries() { //Arrange var options = Utils.GetOptions(nameof(ReturnNotDeletedIndustries)); var industry = TestModelsSeeder.SeedIndustry(); var industry2 = TestModelsSeeder.SeedIndustry2(); var industry3 = TestModelsSeeder.SeedIndustry3(); industry.IsDeleted = true; using (var arrangeContext = new InsightHubContext(options)) { await arrangeContext.Industries.AddAsync(industry); await arrangeContext.Industries.AddAsync(industry2); await arrangeContext.Industries.AddAsync(industry3); await arrangeContext.SaveChangesAsync(); } //Act & Assert using var assertContext = new InsightHubContext(options); var sut = new IndustryServices(assertContext); var result = await sut.GetAllIndustries(null, null); Assert.AreEqual(2, result.Count); }
/// <summary> /// Creates a new Tag in the context. /// </summary> /// <param name="name">The name of the new Tag.</param> /// <returns>Tag Model on success. Throws ArgumentException if a Tag with this name already exists.</returns> public async Task <TagModel> CreateTag(string name) { name = name.Trim(); var tagDTO = TagMapper.MapModelFromInput(name); if (!await _context.Tags.AnyAsync(t => t.Name.ToLower() == name.ToLower())) { var tag = TagMapper.MapEntityFromModel(tagDTO); await _context.Tags.AddAsync(tag); await _context.SaveChangesAsync(); tagDTO = TagMapper.MapModelFromEntity(tag); return(tagDTO); } throw new ArgumentException($"Tag with name {name} already exists."); }
public async Task ReturnPendingReports_WithSearch_When_ParamValid() { //Arrange var options = Utils.GetOptions(nameof(ReturnPendingReports_WithSearch_When_ParamValid)); var report1 = TestModelsSeeder.SeedReport(); var industry1 = TestModelsSeeder.SeedIndustry(); var user = TestModelsSeeder.SeedUser(); var tag = TestModelsSeeder.SeedTag(); var tag2 = TestModelsSeeder.SeedTag2(); var tag3 = TestModelsSeeder.SeedTag3(); var report2 = TestModelsSeeder.SeedReport2(); var industry2 = TestModelsSeeder.SeedIndustry2(); var report3 = TestModelsSeeder.SeedReport3(); var industry3 = TestModelsSeeder.SeedIndustry3(); report1.IsPending = true; report2.IsPending = true; using (var arrangeContext = new InsightHubContext(options)) { await arrangeContext.Reports.AddAsync(report1); await arrangeContext.Tags.AddAsync(tag); await arrangeContext.Industries.AddAsync(industry1); await arrangeContext.Reports.AddAsync(report2); await arrangeContext.Tags.AddAsync(tag2); await arrangeContext.Industries.AddAsync(industry2); await arrangeContext.Reports.AddAsync(report3); await arrangeContext.Tags.AddAsync(tag3); await arrangeContext.Industries.AddAsync(industry3); await arrangeContext.Users.AddAsync(user); await arrangeContext.SaveChangesAsync(); } //Act & Assert using var assertContext = new InsightHubContext(options); var sutTags = new TagServices(assertContext); var sut = new ReportServices(assertContext, sutTags); var act = await sut.GetPendingReports(null, "TestReport"); var result = act.ToArray(); Assert.AreEqual(2, result.Length); Assert.AreEqual(report1.Title, result[0].Title); Assert.AreEqual(report1.Summary, result[0].Summary); Assert.IsTrue(result[0].Title.Contains("TestReport")); Assert.AreEqual(report2.Title, result[1].Title); Assert.AreEqual(report2.Summary, result[1].Summary); Assert.IsTrue(result[1].Title.Contains("TestReport")); }
public async Task GetMostDownloadedReports_When_ParamsValid() { //Arrange var options = Utils.GetOptions(nameof(GetMostDownloadedReports_When_ParamsValid)); var report1 = TestModelsSeeder.SeedReport(); var industry1 = TestModelsSeeder.SeedIndustry(); var user = TestModelsSeeder.SeedUser(); var tag = TestModelsSeeder.SeedTag(); var tag2 = TestModelsSeeder.SeedTag2(); var tag3 = TestModelsSeeder.SeedTag3(); var report2 = TestModelsSeeder.SeedReport2(); var industry2 = TestModelsSeeder.SeedIndustry2(); var report3 = TestModelsSeeder.SeedReport3(); var industry3 = TestModelsSeeder.SeedIndustry3(); using (var arrangeContext = new InsightHubContext(options)) { await arrangeContext.Reports.AddAsync(report1); await arrangeContext.Tags.AddAsync(tag); await arrangeContext.Industries.AddAsync(industry1); await arrangeContext.Reports.AddAsync(report2); await arrangeContext.Tags.AddAsync(tag2); await arrangeContext.Industries.AddAsync(industry2); await arrangeContext.Reports.AddAsync(report3); await arrangeContext.Tags.AddAsync(tag3); await arrangeContext.Industries.AddAsync(industry3); await arrangeContext.Users.AddAsync(user); await arrangeContext.SaveChangesAsync(); } //Act & Assert using var assertContext = new InsightHubContext(options); var sutTags = new TagServices(assertContext); var sut = new ReportServices(assertContext, sutTags); var act = await sut.GetMostDownloadedReports(); var result = act.ToArray(); Assert.AreEqual(3, result.Length); Assert.AreEqual(report2.Title, result[0].Title); Assert.AreEqual(report2.Summary, result[0].Summary); Assert.IsTrue(result[0].DownloadsCount > result[1].DownloadsCount); Assert.AreEqual(report1.Title, result[1].Title); Assert.AreEqual(report1.Summary, result[1].Summary); Assert.IsTrue(result[1].DownloadsCount > result[2].DownloadsCount); Assert.AreEqual(report3.Title, result[2].Title); Assert.AreEqual(report3.Summary, result[2].Summary); }
public async Task ReturnNotDeletedReport_WithSortAndSearchAndIndustry_When_ParamValid() { //Arrange var options = Utils.GetOptions(nameof(ReturnNotDeletedReport_WithSortAndSearchAndIndustry_When_ParamValid)); var report1 = TestModelsSeeder.SeedReport(); var industry1 = TestModelsSeeder.SeedIndustry(); var user = TestModelsSeeder.SeedUser(); var tag = TestModelsSeeder.SeedTag(); var tag2 = TestModelsSeeder.SeedTag2(); var tag3 = TestModelsSeeder.SeedTag3(); var report2 = TestModelsSeeder.SeedReport2(); var industry2 = TestModelsSeeder.SeedIndustry2(); var report3 = TestModelsSeeder.SeedReport3(); var industry3 = TestModelsSeeder.SeedIndustry3(); using (var arrangeContext = new InsightHubContext(options)) { await arrangeContext.Reports.AddAsync(report1); await arrangeContext.Tags.AddAsync(tag); await arrangeContext.Industries.AddAsync(industry1); await arrangeContext.Reports.AddAsync(report2); await arrangeContext.Tags.AddAsync(tag2); await arrangeContext.Industries.AddAsync(industry2); await arrangeContext.Reports.AddAsync(report3); await arrangeContext.Tags.AddAsync(tag3); await arrangeContext.Industries.AddAsync(industry3); await arrangeContext.Users.AddAsync(user); await arrangeContext.SaveChangesAsync(); } //Act & Assert using var assertContext = new InsightHubContext(options); var sutTags = new TagServices(assertContext); var sut = new ReportServices(assertContext, sutTags); var act = await sut.GetReports("title_desc", "TestReport", null, "Industry", null); var result = act.ToList(); Assert.AreEqual(3, result.Count()); Assert.AreEqual(report3.Title, result[0].Title); Assert.IsTrue(result[0].Industry.Contains("Industry")); Assert.AreEqual(report2.Title, result[1].Title); Assert.IsTrue(result[1].Industry.Contains("Industry")); Assert.AreEqual(report1.Title, result[2].Title); Assert.IsTrue(result[2].Industry.Contains("Industry")); }
/// <summary> /// Creates a new Industry in the context. /// </summary> /// <param name="name">The Name of the new Industry.</param> /// <param name="imgUrl">Link to the Image to be displayed in the new Industry's page.</param> /// <returns>IndustryModel on success. Throws ArgumentException if Industry already exists.</returns> public async Task <IndustryModel> CreateIndustry(string name, string imgUrl) { if (name == null) { throw new ArgumentNullException("Name can NOT be null."); } var dto = IndustryMapper.MapModelFromInput(name, imgUrl); if (await _context.Industries.AnyAsync(i => i.Name == name)) { throw new ArgumentException($"Industry with name {name} already exists."); } var industry = IndustryMapper.MapEntityFromModel(dto); await _context.Industries.AddAsync(industry); await _context.SaveChangesAsync(); dto = IndustryMapper.MapModelFromEntity(industry); return(dto); }
/// <summary> /// Updates the properties of an existing User. /// </summary> /// <param name="id">The ID of the target User.</param> /// <param name="firstName">The new First Name for the User.</param> /// <param name="lastName">The new Last Name for the User.</param> /// <param name="isBanned">The new IsBanned property.</param> /// <param name="banReason">The new BanReason property.</param> /// <returns>User Model on success. Throws ArgumentNullException if User does not exist.</returns> public async Task <UserModel> UpdateUser(int id, string firstName, string lastName, bool isBanned, string banReason) { var user = await _context.Users .Include(u => u.DownloadedReports) .Include(u => u.IndustrySubscriptions) .FirstOrDefaultAsync(u => u.Id == id && !u.IsBanned); ValidateUserExists(user); user.FirstName = firstName; user.LastName = lastName; user.IsBanned = isBanned; user.BanReason = banReason; var userDTO = UserMapper.MapModelFromEntity(user); user.ModifiedOn = DateTime.UtcNow; await _context.SaveChangesAsync(); return(userDTO); }
/// <summary> /// Creates a new Report from given Strings and places it in Pending. /// </summary> /// <param name="title">The title of the new Report. Between 5 and 100 characters.</param> /// <param name="summary">The summary of the new Report. Between 5 and 300 characters.</param> /// <param name="description">The description of the new Report. Between 5 and 5000 characters.</param> /// <param name="author">The author of the new Report. Automatically generated from Identity and the logged in User.</param> /// <param name="imgUrl">The URL for the report's image, which appears on the report's card.</param> /// <param name="industry">The Industry under which the new Report will be classified. Has to match an existing Industry in the context.</param> /// <param name="tags">The Tags to be added to the new Report. If a tag does not exist, it will be created automatically.</param> /// <returns>On success - A Report Model, mapped from the new Report. If the Report already exists - Throws Argument Exception</returns> public async Task <ReportModel> CreateReport(string title, string summary, string description, string author, string imgUrl, string industry, string tags) { var reportModel = ReportMapper.MapModelFromInput(title, summary, description, imgUrl, author, industry, tags); // Throw if Report with this title exists. if (await _context.Reports .Include(r => r.Author) .Include(r => r.Industry) .Include(r => r.ReportTags) .AnyAsync(r => r.Title == title)) { throw new ArgumentException($"Report with title {title} already exists."); } //Create Report var report = new Report() { Title = reportModel.Title, Description = reportModel.Description, Summary = reportModel.Summary, ImgUrl = reportModel.ImgUrl, CreatedOn = DateTime.UtcNow }; await _context.Reports.AddAsync(report); //Map Author, Industry, set Pending report.AuthorId = _context.Users.FirstOrDefault(u => u.NormalizedEmail == reportModel.Author.ToUpper()).Id; report.IndustryId = _context.Industries.FirstOrDefault(i => i.Name.ToUpper() == reportModel.Industry.ToUpper()).Id; report.IsPending = true; await _context.SaveChangesAsync(); //Map Tags, Create new Tags if any do not exist. await AddTagsToReport(report, reportModel.Tags); await _context.SaveChangesAsync(); //Return Report Model reportModel = ReportMapper.MapModelFromEntity(report); return(reportModel); }
public async Task GetsCorrectReports_When_ParamsValid() { //Arrange var options = Utils.GetOptions(nameof(GetsCorrectReports_When_ParamsValid)); var report1 = TestModelsSeeder.SeedReport(); var industry1 = TestModelsSeeder.SeedIndustry(); var user = TestModelsSeeder.SeedUser(); var tag = TestModelsSeeder.SeedTag(); var tag2 = TestModelsSeeder.SeedTag2(); var tag3 = TestModelsSeeder.SeedTag3(); var report2 = TestModelsSeeder.SeedReport2(); var industry2 = TestModelsSeeder.SeedIndustry2(); var report3 = TestModelsSeeder.SeedReport3(); var industry3 = TestModelsSeeder.SeedIndustry3(); report1.IsDeleted = true; using (var arrangeContext = new InsightHubContext(options)) { await arrangeContext.Reports.AddAsync(report1); await arrangeContext.Tags.AddAsync(tag); await arrangeContext.Industries.AddAsync(industry1); await arrangeContext.Reports.AddAsync(report2); await arrangeContext.Tags.AddAsync(tag2); await arrangeContext.Industries.AddAsync(industry2); await arrangeContext.Reports.AddAsync(report3); await arrangeContext.Tags.AddAsync(tag3); await arrangeContext.Industries.AddAsync(industry3); await arrangeContext.Users.AddAsync(user); await arrangeContext.SaveChangesAsync(); } //Act & Assert using var assertContext = new InsightHubContext(options); var sutTags = new TagServices(assertContext); var sut = new ReportServices(assertContext, sutTags); var result = await sut.GetReports(null, null, null, null, null); Assert.AreEqual(2, result.Count); }
public async Task UpdateReport_WhenParamsAreValid() { //Arrange var options = Utils.GetOptions(nameof(UpdateReport_WhenParamsAreValid)); var newTitle = "New Report Title"; var newSummary = "New Report Summary"; var newDescription = "New Report Description"; var newIndustry = TestModelsSeeder.SeedIndustry2(); var newImgURL = "imageurl"; var newTag = TestModelsSeeder.SeedTag2(); var report1 = TestModelsSeeder.SeedReport(); var industry1 = TestModelsSeeder.SeedIndustry(); var user = TestModelsSeeder.SeedUser(); var tag = TestModelsSeeder.SeedTag(); using (var arrangeContext = new InsightHubContext(options)) { await arrangeContext.Users.AddAsync(user); await arrangeContext.Industries.AddAsync(industry1); await arrangeContext.Industries.AddAsync(newIndustry); await arrangeContext.Tags.AddAsync(tag); await arrangeContext.Tags.AddAsync(newTag); await arrangeContext.Reports.AddAsync(report1); await arrangeContext.SaveChangesAsync(); } //Act & Assert using var assertContext = new InsightHubContext(options); var sutTags = new TagServices(assertContext); var sutReports = new ReportServices(assertContext, sutTags); var act = await sutReports.UpdateReport(1, newTitle, newSummary, newDescription, newImgURL, newIndustry.Name, newTag.Name); var result = assertContext.Reports.FirstOrDefault(t => t.Title == newTitle); Assert.AreEqual(newTitle, result.Title); Assert.AreEqual(newSummary, result.Summary); Assert.AreEqual(newDescription, result.Description); Assert.AreEqual(newIndustry.Name, result.Industry.Name); Assert.AreEqual(newImgURL, result.ImgUrl); Assert.AreEqual(newTag.Name, result.ReportTags.First().Tag.Name); }
public async Task ThrowArgumentException_WhenTitleAlreadyExists() { //Arrange var options = Utils.GetOptions(nameof(ThrowArgumentException_WhenTitleAlreadyExists)); var newSummary = "New Report Summary"; var newDescription = "New Report Description"; var newIndustry = TestModelsSeeder.SeedIndustry2(); var newImgURL = "imageurl"; var newTag = TestModelsSeeder.SeedTag2(); var report1 = TestModelsSeeder.SeedReport(); var industry1 = TestModelsSeeder.SeedIndustry(); var user = TestModelsSeeder.SeedUser(); var tag = TestModelsSeeder.SeedTag(); var report2 = TestModelsSeeder.SeedReport2(); var industry2 = TestModelsSeeder.SeedIndustry3(); var tag2 = TestModelsSeeder.SeedTag3(); using (var arrangeContext = new InsightHubContext(options)) { await arrangeContext.Users.AddAsync(user); await arrangeContext.Industries.AddAsync(industry1); await arrangeContext.Industries.AddAsync(industry2); await arrangeContext.Industries.AddAsync(newIndustry); await arrangeContext.Tags.AddAsync(tag); await arrangeContext.Tags.AddAsync(newTag); await arrangeContext.Tags.AddAsync(tag2); await arrangeContext.Reports.AddAsync(report1); await arrangeContext.Reports.AddAsync(report2); await arrangeContext.SaveChangesAsync(); } //Act & Assert using var assertContext = new InsightHubContext(options); var sutTags = new TagServices(assertContext); var sutReports = new ReportServices(assertContext, sutTags); await Assert.ThrowsExceptionAsync <ArgumentException>(() => sutReports.UpdateReport(1, report2.Title, newSummary, newDescription, newImgURL, newIndustry.Name, newTag.Name)); }
public async Task Throw_WhenIdIsInvalid() { //Arrange var options = Utils.GetOptions(nameof(Throw_WhenIdIsInvalid)); var industry = TestModelsSeeder.SeedIndustry(); using (var arrangeContext = new InsightHubContext(options)) { await arrangeContext.AddAsync(industry); await arrangeContext.SaveChangesAsync(); } //Act & Assert using var assertContext = new InsightHubContext(options); var sut = new IndustryServices(assertContext); await Assert.ThrowsExceptionAsync <ArgumentNullException>(() => sut.UpdateIndustry(5, "NewName", "NewTestURL")); }
public async Task Throw_When_UserNotBanned() { //Arrange var options = Utils.GetOptions(nameof(Throw_When_UserNotBanned)); var user = TestModelsSeeder.SeedUser(); user.IsBanned = false; using (var arrangeContext = new InsightHubContext(options)) { arrangeContext.Users.Add(user); await arrangeContext.SaveChangesAsync(); } //Act & Assert using var assertContext = new InsightHubContext(options); var sut = new UserServices(assertContext); await Assert.ThrowsExceptionAsync <ArgumentException>(async() => await sut.UnbanUser(user.Id)); }
public async Task Throw_WhenTagAlreadyDeleted() { //Arrange var options = Utils.GetOptions(nameof(Throw_WhenTagAlreadyDeleted)); var tag = TestModelsSeeder.SeedTag(); tag.IsDeleted = true; using (var arrangeContext = new InsightHubContext(options)) { arrangeContext.Tags.Add(tag); await arrangeContext.SaveChangesAsync(); } //Act & Assert using var assertContext = new InsightHubContext(options); var sut = new TagServices(assertContext); await Assert.ThrowsExceptionAsync <ArgumentException>(async() => await sut.DeleteTag(tag.Id)); }
public async Task Throw_When_IndustryAlreadyExists() { //Arrange var options = Utils.GetOptions(nameof(Throw_When_IndustryAlreadyExists)); var industry = TestModelsSeeder.SeedIndustry(); var industryName = industry.Name; using (var arrangeContext = new InsightHubContext(options)) { await arrangeContext.Industries.AddAsync(industry); await arrangeContext.SaveChangesAsync(); } //Act & Assert using var assertContext = new InsightHubContext(options); var sut = new IndustryServices(assertContext); await Assert.ThrowsExceptionAsync <ArgumentException>(() => sut.CreateIndustry(industryName, "testURL")); }
public async Task DeleteTag_When_ParamsValid() { //Arrange var options = Utils.GetOptions(nameof(DeleteTag_When_ParamsValid)); var tag = TestModelsSeeder.SeedTag(); using (var arrangeContext = new InsightHubContext(options)) { arrangeContext.Tags.Add(tag); await arrangeContext.SaveChangesAsync(); } //Act & Assert using var assertContext = new InsightHubContext(options); var sut = new TagServices(assertContext); await sut.DeleteTag(1); Assert.IsTrue(assertContext.Tags.FirstOrDefault().IsDeleted); }