From a435ce5fd504877f1fd0b97e069daa6e2ecedf9a Mon Sep 17 00:00:00 2001 From: Pouya Kermanshahi Date: Fri, 27 Dec 2019 10:30:58 -0800 Subject: [PATCH 1/5] This Adds Unit Test for Comments Controller. Also some of the database connection code are changd in source code. --- ratemyprofessors.sln | 12 +- ratemyprofessors/ProfessorCache.cs | 7 +- ratemyprofessors/Startup.cs | 5 +- ratemyprofessors/ratemyprofessors.csproj | 2 +- .../CommentsControllerTest.cs | 499 ++++++++++++++++++ .../ratemyprofessorsTests.csproj | 24 + 6 files changed, 543 insertions(+), 6 deletions(-) create mode 100644 ratemyprofessorsTests/CommentsControllerTest.cs create mode 100644 ratemyprofessorsTests/ratemyprofessorsTests.csproj diff --git a/ratemyprofessors.sln b/ratemyprofessors.sln index cf05d8d..8590438 100644 --- a/ratemyprofessors.sln +++ b/ratemyprofessors.sln @@ -1,9 +1,11 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 15 -VisualStudioVersion = 15.0.27703.2042 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.29519.87 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ratemyprofessors", "ratemyprofessors\ratemyprofessors.csproj", "{6C93947B-F731-4AB7-98CE-53DAC00068C3}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ratemyprofessors", "ratemyprofessors\ratemyprofessors.csproj", "{6C93947B-F731-4AB7-98CE-53DAC00068C3}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ratemyprofessorsTests", "ratemyprofessorsTests\ratemyprofessorsTests.csproj", "{FA13E8EA-3296-4746-AA25-17F759815C81}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -15,6 +17,10 @@ Global {6C93947B-F731-4AB7-98CE-53DAC00068C3}.Debug|Any CPU.Build.0 = Debug|Any CPU {6C93947B-F731-4AB7-98CE-53DAC00068C3}.Release|Any CPU.ActiveCfg = Release|Any CPU {6C93947B-F731-4AB7-98CE-53DAC00068C3}.Release|Any CPU.Build.0 = Release|Any CPU + {FA13E8EA-3296-4746-AA25-17F759815C81}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {FA13E8EA-3296-4746-AA25-17F759815C81}.Debug|Any CPU.Build.0 = Debug|Any CPU + {FA13E8EA-3296-4746-AA25-17F759815C81}.Release|Any CPU.ActiveCfg = Release|Any CPU + {FA13E8EA-3296-4746-AA25-17F759815C81}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/ratemyprofessors/ProfessorCache.cs b/ratemyprofessors/ProfessorCache.cs index 389d13d..80a8fa4 100644 --- a/ratemyprofessors/ProfessorCache.cs +++ b/ratemyprofessors/ProfessorCache.cs @@ -24,9 +24,14 @@ public ProfessorCache(IConfiguration configuration) } private void Update() { + //var OptionsBuilder = new DbContextOptionsBuilder(); + //OptionsBuilder.UseSqlServer(_configuration.GetConnectionString("DefaultConnection")); + //List ProfsL; + var OptionsBuilder = new DbContextOptionsBuilder(); - OptionsBuilder.UseSqlServer(_configuration.GetConnectionString("DefaultConnection")); + OptionsBuilder.UseInMemoryDatabase(databaseName: "cache database"); List ProfsL; + using (var DB = new DataBaseContext(OptionsBuilder.Options)) { ProfsL = DB.Professors diff --git a/ratemyprofessors/Startup.cs b/ratemyprofessors/Startup.cs index 1837af6..c75e87b 100644 --- a/ratemyprofessors/Startup.cs +++ b/ratemyprofessors/Startup.cs @@ -28,8 +28,11 @@ public Startup(IConfiguration configuration) // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { + //services.AddDbContext(options => + // options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); + services.AddDbContext(options => - options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); + options.UseInMemoryDatabase(databaseName: "base_project_db")); services.AddSingleton(); diff --git a/ratemyprofessors/ratemyprofessors.csproj b/ratemyprofessors/ratemyprofessors.csproj index 375933f..857f88a 100644 --- a/ratemyprofessors/ratemyprofessors.csproj +++ b/ratemyprofessors/ratemyprofessors.csproj @@ -1,4 +1,4 @@ - + netcoreapp2.1 diff --git a/ratemyprofessorsTests/CommentsControllerTest.cs b/ratemyprofessorsTests/CommentsControllerTest.cs new file mode 100644 index 0000000..0d27895 --- /dev/null +++ b/ratemyprofessorsTests/CommentsControllerTest.cs @@ -0,0 +1,499 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Configuration; +using Microsoft.AspNetCore.Hosting; +using Moq; +using ratemyprofessors.Controllers; +using ratemyprofessors.Models; +using System; +using System.Collections.Generic; +using Xunit; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.Primitives; + +namespace ratemyprofessorsTests +{ + public class CommentsControllerTest + { + + private List GetTestComments() + { + List comments = new List(); + for (int i = 0; i < 3; i++) + { + Comment comment = new Comment() + { + ID = Guid.NewGuid(), + Professor = new Professor() + { + ID = Guid.NewGuid() + }, + Email = new Email() + { + Verified = true, + Address = $"test{i}@example.com" + }, + Verfied = true + }; + comments.Add(comment); + } + + return comments; + } + + + private CommentsController ConfigureCommentsController(string databaseName, IList comments) + { + var option = new DbContextOptionsBuilder().UseInMemoryDatabase(databaseName: databaseName).Options; + + var context = new DataBaseContext(option); + + if(comments != null) + { + context.Comments.AddRange(comments); + context.SaveChanges(); + } + + + var mockConf = new Mock(); + var mockHost = new Mock(); + + return new CommentsController(context, mockConf.Object, mockHost.Object); + } + + private CommentsController ConfigureCommentsController(string databaseName, Comment comment) + { + return ConfigureCommentsController(databaseName, new List() { comment }); + } + + + + + #region Test GetComment That Returns List + + [Fact] + public async void Check_GetComment_Returns_OKObjectResult() + { + + var controller = ConfigureCommentsController("comments_width_ok_result", GetTestComments()); + + var result = await controller.GetComment(); + + Assert.IsType(result); + + } + + [Fact] + public async void Check_Getomments_Returns_NotFound_Width_EmptyList() + { + var controller = ConfigureCommentsController("not_found_error", new List()); + + var result = await controller.GetComment(); + + Assert.IsType(result); + + } + + [Fact] + public async void Check_GetComment_Returns_Valid_CommentList() + { + var controller = ConfigureCommentsController("valid_comment_list", GetTestComments()); + + var result = await controller.GetComment(); + + var viewResult = Assert.IsAssignableFrom(result); + + var model = Assert.IsAssignableFrom>(viewResult.Value); + + Assert.Equal(GetTestComments().Count, model.Count); + + } + + #endregion + + + + + #region Test GetComment By Professor Id + + [Fact] + public async void Check_GetCommentByProfessorId_Returns_BadRequest_On_ModelError() + { + var controller = ConfigureCommentsController("bad_request_by_professorId", new List()); + + controller.ModelState.AddModelError("samepleError", "an error"); + + var result = await controller.GetComments(Guid.NewGuid()); + + Assert.IsType(result); + + } + + [Fact] + public async void Check_GetCommentByProfessorId_NotFound() + { + + + var comment = new Comment() + { + Email = new Email() + { + Verified = true + }, + Professor = new Professor() + { + ID = Guid.NewGuid() + }, + Verfied = true + }; + + + var controller = ConfigureCommentsController("not_found_by_professorId", comment); + var result = await controller.GetComments(Guid.NewGuid()); + + Assert.IsType(result); + } + + [Fact] + public async void Check_CetCommentsByProfessorId_Returns_Ok() + { + + var guid = Guid.NewGuid(); + + var comment = new Comment() + { + + Email = new Email() + { + Verified = true + }, + Professor = new Professor() + { + ID = guid + }, + Verfied = true + }; + + + + var controller = ConfigureCommentsController("ok_by_professorId", comment); + + var result = await controller.GetComments(guid); + + var okResult = Assert.IsAssignableFrom(result); + + var retrievedData = Assert.IsAssignableFrom>(okResult.Value); + + Assert.Single(retrievedData); + + } + + #endregion + + + + + #region Test GetCommentsAvg + + // CommentsAvg Test + + [Fact] + public async void Check_CommetnsAvg_Returns_BadRquest_On_ModelError() + { + var controller = ConfigureCommentsController("bad_request_comment_avg", new List()); + + controller.ModelState.AddModelError("test", "test error"); + + var result = await controller.GetCommentsAvg(Guid.NewGuid()); + + Assert.IsType(result); + } + + [Fact] + public async void Check_CommentsAvg_Returns_NotFound() + { + var controller = ConfigureCommentsController("not_found_comment_avg", GetTestComments()); + + var result = await controller.GetCommentsAvg(Guid.NewGuid()); + + Assert.IsType(result); + } + + [Fact] + public async void Check_CommentsAvg_ReturnsOK() + { + var comments = GetTestComments(); + + var guid = comments.Find(ct => true).Professor.ID; + + var controller = ConfigureCommentsController("ok_comment_avg", comments); + + var result = await controller.GetCommentsAvg(guid); + + Assert.IsType(result); + } + #endregion + + + + + #region Test GetComment + //Test GetComment + + [Fact] + public async void Check_GetComment_Returns_BadRequest_On_ModelError() + { + var controller = ConfigureCommentsController("bad_request_get_comment", new List()); + + controller.ModelState.AddModelError("test", "test error"); + + var result = await controller.GetComment(Guid.NewGuid()); + + Assert.IsType(result); + } + + [Fact] + public async void Check_GetComment_Returns_NotFound_On_InvalidId() + { + var controller = ConfigureCommentsController("not_found_on_InvalidId", GetTestComments()); + + var result = await controller.GetComment(Guid.NewGuid()); + + Assert.IsType(result); + + } + + [Fact] + public async void Check_GetComment_Returns_NotFound_On_UnVerifiedComment() + { + var guid = Guid.NewGuid(); + var comment = new Comment() + { + ID = guid, + Verfied = false + }; + + var comments = GetTestComments(); + comments.Add(comment); + + var controller = ConfigureCommentsController("not_found_on_unverfied", comments); + var result = await controller.GetComment(guid); + + Assert.IsType(result); + } + + [Fact] + public async void Check_GetComment_Returns_Ok_Data_With_ValidData() + { + var comments = GetTestComments(); + var comment = comments.Find(ct => true); + + var controller = ConfigureCommentsController("returns_ok_width_valid_data", comments); + var result = await controller.GetComment(comment.ID); + + var okResult = Assert.IsType(result); + var data = Assert.IsType(okResult.Value); + + Assert.Equal(comment.ID, data.ID); + + } + #endregion + + + + #region Test RatePlus + // Test RateplusComment + + [Fact] + public async void Check_RatePlus_Returns_BadRequest_On_ModelError() + { + var controller = ConfigureCommentsController("bad_request_model_error", GetTestComments()); + controller.ModelState.AddModelError("test", "test error"); + var result = await controller.RatePlusComment(Guid.NewGuid()); + + Assert.IsType(result); + } + + [Fact] + public async void Check_RatePlus_Returns_NotFound_On_InvalidId() + { + var controller = ConfigureCommentsController("not_found_invalidId", GetTestComments()); + var result = await controller.RatePlusComment(Guid.NewGuid()); + + Assert.IsType(result); + } + + [Fact] + public async void Check_RatePlus_Returns_NotFound_With_ValidData() + { + var comments = GetTestComments(); + var comment = comments.Find(ct => true); + + var controller = ConfigureCommentsController("ok_with_validData", comments); + var result = await controller.RatePlusComment(comment.ID); + + var okResult = Assert.IsType(result); + var data = Assert.IsType(okResult.Value); + Assert.Equal(1, data.Like); + } + #endregion + + + + #region Test RateNegetive + // Test RateNegativeComment + + [Fact] + public async void Check_RateNegetive_Returns_BadRequest_On_ModelError() + { + var controller = ConfigureCommentsController("bad_request_negative_modelError", new Comment()); + controller.ModelState.AddModelError("test", "test Error"); + var result = await controller.RateNegetiveComment(Guid.NewGuid()); + + Assert.IsType(result); + } + + [Fact] + public async void Check_RateNegetive_Returns_NotFound_OnModelError() + { + var controller = ConfigureCommentsController("not_found_negetive_invalidId", GetTestComments()); + var result = await controller.RateNegetiveComment(Guid.NewGuid()); + + Assert.IsType(result); + } + + [Fact] + public async void Check_RateNegetive_Returns_Ok_With_ValidData() + { + var comments = GetTestComments(); + var comment = comments.Find(ct => true); + + var controller = ConfigureCommentsController("ok_negative_validData", comments); + var result = await controller.RateNegetiveComment(comment.ID); + + var okResult = Assert.IsType(result); + var data = Assert.IsType(okResult.Value); + Assert.Equal(1, data.DisLike); + } + #endregion + + + + #region Test PostComment + + // Test PostComment + [Fact] + public async void Check_PostComment_Returns_BadRequest_On_ModelError() + { + var controller = ConfigureCommentsController("bad_request_postComment_modelError", new Comment()); + controller.ModelState.AddModelError("test", "test error"); + var result = await controller.PostComment(new Comment()); + + Assert.IsType(result); + } + + [Fact] + public async void Check_PostComment_Returns_Ok_On_DuplicateComment() + { + var comments = GetTestComments(); + var comment = comments.Find(ct => true); + //comment.Email.Address = "test@example.com"; + + + var controller = ConfigureCommentsController("Ok_postComment_duplicate_email", comments); + controller.ControllerContext = new ControllerContext(); + controller.ControllerContext.HttpContext = new DefaultHttpContext(); + + var result = await controller.PostComment(comment); + + var okResult = Assert.IsType(result); + } + + [Fact] + public async void Check_PostComment_Returns_Ok_On_UnVerified_Email() + { + var comments = GetTestComments(); + var comment = comments.Find(ct => true); + + var controller = ConfigureCommentsController("returns_ok_on_new_EmailAddress", comments); + controller.ControllerContext = new ControllerContext(); + controller.ControllerContext.HttpContext = new DefaultHttpContext(); + + var testComment = new Comment() + { + Email = new Email() + { + Address = comment.Email.Address, + Verified = false + }, + Professor = new Professor() + { + ID = Guid.NewGuid() + } + + }; + + var result = await controller.PostComment(testComment); + + Assert.IsType(result); + + } + + [Fact] + public async void Check_Returns_Ok_On_VerfiedDuplicateEmail() + { + var comments = GetTestComments(); + var comment = comments.Find(ct => true); + + var controller = ConfigureCommentsController("check_returns_ok_on_verfiedduplicateemail", comments); + controller.ControllerContext = new ControllerContext(); + controller.ControllerContext.HttpContext = new DefaultHttpContext(); + + var testComment = new Comment() + { + Email = new Email() + { + Address = comment.Email.Address, + Verified = true + }, + Professor = new Professor() + { + ID = Guid.NewGuid() + } + + }; + + var result = await controller.PostComment(testComment); + + Assert.IsType(result); + } + #endregion + + + #region Test Resend + // Test Resend + + [Fact] + public async void Check_Resend_Returns_NotFound() + { + var controller = ConfigureCommentsController("not_found_Resend", GetTestComments()); + var result = await controller.Resend("dummy email"); + + Assert.IsType(result); + } + + [Fact] + public async void Check_Rsend_Returns_Ok_On_VerifiedEmail() + { + var comments = GetTestComments(); + var address = comments.Find(ct => true).Email.Address; + + var controller = ConfigureCommentsController("ok_resend", GetTestComments()); + var result = await controller.Resend(address); + + Assert.IsType(result); + } + #endregion + + } +} diff --git a/ratemyprofessorsTests/ratemyprofessorsTests.csproj b/ratemyprofessorsTests/ratemyprofessorsTests.csproj new file mode 100644 index 0000000..8f7e807 --- /dev/null +++ b/ratemyprofessorsTests/ratemyprofessorsTests.csproj @@ -0,0 +1,24 @@ + + + + netcoreapp3.0 + + false + + + + + + + + + + + + + + + + + + From 22ca61d75d6e09b2b8738b0954752306ed7bed37 Mon Sep 17 00:00:00 2001 From: Pouya Kermanshahi Date: Fri, 27 Dec 2019 10:49:55 -0800 Subject: [PATCH 2/5] This adds ContactController test to the test project. --- .../ContactControllerTest.cs | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 ratemyprofessorsTests/ContactControllerTest.cs diff --git a/ratemyprofessorsTests/ContactControllerTest.cs b/ratemyprofessorsTests/ContactControllerTest.cs new file mode 100644 index 0000000..8d74979 --- /dev/null +++ b/ratemyprofessorsTests/ContactControllerTest.cs @@ -0,0 +1,46 @@ +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; +using ratemyprofessors.Controllers; +using ratemyprofessors.Models; +using System; +using System.Collections.Generic; +using System.Text; +using Xunit; + +namespace ratemyprofessorsTests +{ + public class ContactControllerTest + { + + private ContactController ConfigureContactController(string databaseName) + { + var options = new DbContextOptionsBuilder().UseInMemoryDatabase(databaseName).Options; + var database = new DataBaseContext(options); + + return new ContactController(database); + } + + [Fact] + public async void Check_PostContact_Returns_BadRequest_On_ModelError() + { + + var controller = ConfigureContactController("bad_request_model_error"); + controller.ModelState.AddModelError("test", "test error"); + + var result = await controller.PostContact(new ContactUs()); + + Assert.IsType(result); + + } + + [Fact] + public async void Check_PostContact_Returns_Ok() + { + var controller = ConfigureContactController("ok_post_contact"); + var result = await controller.PostContact(new ContactUs()); + + Assert.IsType(result); + } + + } +} From a718aa789b33a253ebc866b696d1a0780fbb4ec5 Mon Sep 17 00:00:00 2001 From: Pouya Kermanshahi Date: Fri, 27 Dec 2019 13:48:28 -0800 Subject: [PATCH 3/5] Tests for GetFacs, GetCourseProf and GetCourse are written. --- .../CommentsControllerTest.cs | 2 +- .../CoursesControllerTest.cs | 217 ++++++++++++++++++ ratemyprofessorsTests/EnumerableHelper.cs | 21 ++ 3 files changed, 239 insertions(+), 1 deletion(-) create mode 100644 ratemyprofessorsTests/CoursesControllerTest.cs create mode 100644 ratemyprofessorsTests/EnumerableHelper.cs diff --git a/ratemyprofessorsTests/CommentsControllerTest.cs b/ratemyprofessorsTests/CommentsControllerTest.cs index 0d27895..277cc21 100644 --- a/ratemyprofessorsTests/CommentsControllerTest.cs +++ b/ratemyprofessorsTests/CommentsControllerTest.cs @@ -324,7 +324,7 @@ public async void Check_RatePlus_Returns_NotFound_On_InvalidId() } [Fact] - public async void Check_RatePlus_Returns_NotFound_With_ValidData() + public async void Check_RatePlus_Returns_Ok_With_ValidData() { var comments = GetTestComments(); var comment = comments.Find(ct => true); diff --git a/ratemyprofessorsTests/CoursesControllerTest.cs b/ratemyprofessorsTests/CoursesControllerTest.cs new file mode 100644 index 0000000..032c9a8 --- /dev/null +++ b/ratemyprofessorsTests/CoursesControllerTest.cs @@ -0,0 +1,217 @@ +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; +using ratemyprofessors.Controllers; +using ratemyprofessors.Models; +using System; +using System.Collections.Generic; +using System.Text; +using Xunit; + +namespace ratemyprofessorsTests +{ + public class CoursesControllerTest + { + + private DataBaseContext ConfigureDatabase(string databaseName) + { + var options = new DbContextOptionsBuilder().UseInMemoryDatabase(databaseName).Options; + + return new DataBaseContext(options); + } + + private CoursesController ConfigureCoursesController(string databaseName, List courses) + { + + var database = ConfigureDatabase(databaseName); + + if (courses != null && courses.Count > 0) + { + database.Courses.AddRange(courses); + database.SaveChanges(); + } + + return new CoursesController(database); + } + + private List GetTestFaculties() + { + List faculties = new List(); + + for (int i = 0; i < 3; i++) + { + var f = new Faculty() + { + Name = "test" + i + }; + + faculties.Add(f); + } + + return faculties; + } + + + [Fact] + public void Check_Facs_Returns_Data() + { + var facility = GetTestFaculties(); + + var database = ConfigureDatabase("ok_facs"); + database.Faculties.AddRange(facility); + database.SaveChanges(); + + var controller = new CoursesController(database); + + var result = controller.GetFacs(); + + Assert.IsAssignableFrom>(result); + } + + + #region Test CourseProf + private Professor GetTestProfessor() + { + Professor pr = new Professor() + { + ProfCourses = new List() + { + + new ProfCourse() + { + ID = Guid.NewGuid(), + Course = new Course() + { + Approved = true + } + + }, + new ProfCourse() + { + ID = Guid.NewGuid(), + Course = new Course() + { + Approved = true + } + } + } + + }; + + return pr; + } + + [Fact] + public async void Check_CourseProf_Returns_BadRequest_On_ModelError() + { + var controller = ConfigureCoursesController("bad_request_on_modelError", null); + + controller.ModelState.AddModelError("test", "test error"); + var result = await controller.GetCourseProf(Guid.NewGuid()); + + Assert.IsType(result); + } + + + [Fact] + public async void Check_CourseProf_Returns_NotFound_On_InvalidId() + { + var database = ConfigureDatabase("not_found_courseProf"); + + database.Professors.Add(GetTestProfessor()); + database.SaveChanges(); + + var controller = new CoursesController(database); + var result = await controller.GetCourseProf(Guid.NewGuid()); + + Assert.IsType(result); + } + + [Fact] + public async void Check_CourseProf_Returns_Ok_With_ValidData() + { + var database = ConfigureDatabase("ok_courseProf"); + + var professor = GetTestProfessor(); + + database.Professors.Add(professor); + database.SaveChanges(); + + var controller = new CoursesController(database); + var result = await controller.GetCourseProf(professor.ID); + + var okResult = Assert.IsType(result); + var data = Assert.IsAssignableFrom>(okResult.Value); + + + var coursesNum = (professor.ProfCourses as List).Count; + Assert.Equal(coursesNum, data.Count()); + } + #endregion + + + + #region Test GetCourse + [Fact] + public async void Check_GetCourse_Returns_BadRequest_On_ModelError() + { + var database = ConfigureDatabase("bad_request_getCourse"); + + var controller = new CoursesController(database); + controller.ModelState.AddModelError("test", "test error"); + + var result = await controller.GetCourse(string.Empty); + Assert.IsType(result); + } + + + + [Fact] + public async void Check_GetCourse_Returns_NotFound_on_Model_On_InvalidAliasName() + { + + var faci = new Faculty() + { + AliasName = "sdsdsds" + }; + + var database = ConfigureDatabase("not_found_faculity_invalidId"); + database.Faculties.Add(faci); + database.SaveChanges(); + + var controller = new CoursesController(database); + + var result = await controller.GetCourse("dsd"); + + Assert.IsType(result); + } + + [Fact] + public async void Check_GetCourse_Returns_Ok_With_ValidData() + { + string name = "test"; + var faci = new Faculty() + { + AliasName = name, + Courses = new List() + { + new Course(){Approved = true}, + new Course(){Approved = false} + } + }; + + var database = ConfigureDatabase("ok_getcours"); + database.Faculties.Add(faci); + database.SaveChanges(); + + var controller = new CoursesController(database); + var result = await controller.GetCourse(name); + + var okResult = Assert.IsType(result); + var data = Assert.IsAssignableFrom>(okResult.Value); + + Assert.Equal(1, data.Count()); + } + #endregion + + } +} diff --git a/ratemyprofessorsTests/EnumerableHelper.cs b/ratemyprofessorsTests/EnumerableHelper.cs new file mode 100644 index 0000000..3ee20ca --- /dev/null +++ b/ratemyprofessorsTests/EnumerableHelper.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace ratemyprofessorsTests +{ + public static class EnumerableHelper + { + public static int Count(this IEnumerable data) + { + var enumerator = data.GetEnumerator(); + + int count = 0; + + while (enumerator.MoveNext()) + count++; + + return count; + } + } +} From 5ad480edeabe3c8343d32e3ad251a3f1a4fedb0c Mon Sep 17 00:00:00 2001 From: Pouya Kermanshahi Date: Sun, 29 Dec 2019 09:35:20 -0800 Subject: [PATCH 4/5] This adds tests for PotsCourse method in CoursesController. --- .../Controllers/CoursesController.cs | 2 + .../CoursesControllerTest.cs | 97 +++++++++++++++++++ 2 files changed, 99 insertions(+) diff --git a/ratemyprofessors/Controllers/CoursesController.cs b/ratemyprofessors/Controllers/CoursesController.cs index 354474a..07a4fc8 100644 --- a/ratemyprofessors/Controllers/CoursesController.cs +++ b/ratemyprofessors/Controllers/CoursesController.cs @@ -73,6 +73,8 @@ public IEnumerable GetAllCourses() return _context.Courses.Where(x => x.Approved); } // POST: api/Courses + + // TODO Check Facility IsFound And If Not Return BadRequest [HttpPost] public async Task PostCourses([FromBody] Course course) { diff --git a/ratemyprofessorsTests/CoursesControllerTest.cs b/ratemyprofessorsTests/CoursesControllerTest.cs index 032c9a8..5559ad2 100644 --- a/ratemyprofessorsTests/CoursesControllerTest.cs +++ b/ratemyprofessorsTests/CoursesControllerTest.cs @@ -213,5 +213,102 @@ public async void Check_GetCourse_Returns_Ok_With_ValidData() } #endregion + + #region Test GetAllCourses + [Fact] + public void Check_GetAllCourses_Returns_Approved_Courses() + { + var courses = new List() + { + new Course(){Approved = true}, + new Course(){Approved = false}, + new Course(){Approved = true} + }; + + var database = ConfigureDatabase("get_all_course"); + database.Courses.AddRange(courses); + database.SaveChanges(); + + var controller = new CoursesController(database); + var result = controller.GetAllCourses(); + + var data = Assert.IsAssignableFrom>(result); + Assert.Equal(2, data.Count()); + } + #endregion + + + #region Test PostCourse + [Fact] + public async void Check_PostCourse_Returns_BadRequest_On_ModelError() + { + var database = ConfigureDatabase("bad_request_postCourse"); + var controller = new CoursesController(database); + controller.ModelState.AddModelError("test", "test error"); + + var result = await controller.PostCourses(new Course()); + + Assert.IsType(result); + } + + [Fact] + public async void Check_PostCourse_Returns_BadRequest_On_InvalidFacId() + { + var database = ConfigureDatabase("bad_request_postCourse_invalidId"); + var controller = new CoursesController(database); + + + var result = await controller.PostCourses(new Course()); + + Assert.IsType(result); + } + + [Fact] + public async void Check_PostCourse_Returns_BadRequest_On_InavlidFacility() + { + var database = ConfigureDatabase("bad_request_postCourse_invalidFACILITY"); + var testCourse = new Course() + { + FacID = Guid.NewGuid().ToString(), + Profs = Guid.NewGuid() + ";" + Guid.NewGuid() + }; + + var controller = new CoursesController(database); + + var result = await controller.PostCourses(testCourse); + + Assert.IsType(result); + } + + [Fact] + public async void Check_PostCourse_Returns_BadRequest_On_ValidData() + { + var database = ConfigureDatabase("OK_postCourse"); + + var guid = Guid.NewGuid(); + + var fac = new Faculty() + { + ID = guid + }; + + var testCourse = new Course() + { + FacID = guid.ToString(), + Profs = Guid.NewGuid() + ";" + Guid.NewGuid() + }; + + database.Faculties.Add(fac); + database.SaveChanges(); + + var controller = new CoursesController(database); + + var result = await controller.PostCourses(testCourse); + + Assert.IsType(result); + + } + #endregion + } } From 8f8423f230892310f4c128c6b7b33d1ab385b75d Mon Sep 17 00:00:00 2001 From: Pouya Kermanshahi Date: Sun, 29 Dec 2019 12:37:16 -0800 Subject: [PATCH 5/5] This adds tests for postProfessor method in professorsController. --- .../Controllers/ProfessorsController.cs | 1 + .../ProfessorsControllerTest.cs | 191 ++++++++++++++++++ 2 files changed, 192 insertions(+) create mode 100644 ratemyprofessorsTests/ProfessorsControllerTest.cs diff --git a/ratemyprofessors/Controllers/ProfessorsController.cs b/ratemyprofessors/Controllers/ProfessorsController.cs index 64c66ad..ad6f3fa 100644 --- a/ratemyprofessors/Controllers/ProfessorsController.cs +++ b/ratemyprofessors/Controllers/ProfessorsController.cs @@ -121,6 +121,7 @@ public IActionResult GetProfessorsByFac([FromRoute] string id) // POST: api/Professors + // TODO Return BadRequest on model error. [HttpPost("NewProf")] public async Task PostProfessor([FromBody] Professor professor) { diff --git a/ratemyprofessorsTests/ProfessorsControllerTest.cs b/ratemyprofessorsTests/ProfessorsControllerTest.cs new file mode 100644 index 0000000..c1e8b4d --- /dev/null +++ b/ratemyprofessorsTests/ProfessorsControllerTest.cs @@ -0,0 +1,191 @@ +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Configuration; +using Moq; +using ratemyprofessors; +using ratemyprofessors.Controllers; +using ratemyprofessors.Models; +using System; +using System.Collections.Generic; +using System.Text; +using Xunit; + +namespace ratemyprofessorsTests +{ + public class ProfessorsControllerTest + { + + + + private DataBaseContext ConfigureDatabase(string databaseName) + { + var options = new DbContextOptionsBuilder().UseInMemoryDatabase(databaseName).Options; + return new DataBaseContext(options); + } + + private Mock GetMockedProfessorChache() + { + var mockConf = new Mock(); + return new Mock(mockConf.Object); + } + + #region Test GetById + + [Fact] + public async void Check_GetByID_Returns_NotFound_On_InvalidId() + { + var database = ConfigureDatabase("not_found_getbyId"); + + var controller = new ProfessorsController(database, GetMockedProfessorChache().Object); + + var result = await controller.GetByID(Guid.NewGuid()); + + Assert.IsType(result); + + } + + [Fact] + public async void Check_GetByID_Returns_Ok_With_ValidData() + { + var database = ConfigureDatabase("not_found_getbyId"); + + var guid = Guid.NewGuid(); + + var professors = new List() + { + new Professor() {ID = guid}, + new Professor() {ID = Guid.NewGuid()} + }; + + + + database.AddRange(professors); + database.SaveChanges(); + + var controller = new ProfessorsController(database, GetMockedProfessorChache().Object); + var result = await controller.GetByID(guid); + + var okResult = Assert.IsType(result); + var data = Assert.IsType(okResult.Value); + Assert.Equal(guid.ToString(), data.ID.ToString()); + } + #endregion + + + #region Test GetProfessors + + [Fact] + public async void Check_GetProfessors_Return_BadRequest_OnModelError() + { + var database = ConfigureDatabase("bad_request_on_modelError"); + var controller = new ProfessorsController(database, GetMockedProfessorChache().Object); + controller.ModelState.AddModelError("test", "test error"); + + var result = await controller.GetProfessors(Guid.NewGuid()); + + Assert.IsType(result); + } + + + [Fact] + public async void Chect_GetProfessors_Returns_NotFound_On_InvalidCourseId() + { + var database = ConfigureDatabase("not_found_getProfessors_InvalidId"); + + var controller = new ProfessorsController(database, GetMockedProfessorChache().Object); + var result = await controller.GetProfessors(Guid.NewGuid()); + + Assert.IsType(result); + } + + + [Fact] + public async void Check_GetProfessors_Returns_Ok_With_ValidData() + { + var database = ConfigureDatabase("get_professors_with_valid_data"); + + var guid = Guid.NewGuid(); + + var testCourse = new Course() + { + ID = guid, + ProfCourses = new List() + { + new ProfCourse() + { + Professor = new Professor() + { + Approved = true + } + }, + new ProfCourse() + { + Professor = new Professor() + { + Approved = false + } + }, + + new ProfCourse() + { + Professor = new Professor() + { + Approved = true + } + } + } + }; + + database.Courses.Add(testCourse); + database.SaveChanges(); + + var controller = new ProfessorsController(database, GetMockedProfessorChache().Object); + var result = await controller.GetProfessors(guid); + + var okResult = Assert.IsType(result); + var data = Assert.IsAssignableFrom>(okResult.Value); + Assert.Equal(2, data.Count()); + + } + + #endregion + + + #region Test PostProfessor + + [Fact] + public async void Check_PostProfessor_Returns_BadRequest_On_ModelError() + { + var database = ConfigureDatabase("bad_request_PostProfessor_modelError"); + var controller = new ProfessorsController(database, GetMockedProfessorChache().Object); + controller.ModelState.AddModelError("test", "test error"); + + var result = await controller.PostProfessor(new Professor()); + + Assert.IsType(result); + } + + + [Fact] + public async void Check_PostProfessor_Returns_Ok_With_ValidData() + { + + var testPorfessor = new Professor() + { + Courses = Guid.NewGuid() + ";" + Guid.NewGuid(), + Facs = Guid.NewGuid() + ";" + Guid.NewGuid() + }; + + + var databse = ConfigureDatabase("pk_PostPrfessor"); + var controller = new ProfessorsController(databse, GetMockedProfessorChache().Object); + + var result = await controller.PostProfessor(testPorfessor); + + Assert.IsType(result); + + } + + #endregion + } +}