public async void Get_ReturnsACourseWithASpecificId_ThrowIfCourseDoesNotExist() { //Arrange var connectionBuilder = new SqliteConnectionStringBuilder { DataSource = ":memory:" }; var connection = new SqliteConnection(connectionBuilder.ToString()); var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseSqlite(connection) .Options; using var context = new ApplicationDbContext(options); context.Database.OpenConnection(); context.Database.EnsureCreated(); var courseRepository = new CourseRepository(context); //Act var course = CourseHelpers.GetCourse(); await courseRepository.Insert(course); var returnedCourse = await courseRepository.Get(x => x.Id == 5); //Assert Assert.Equal(5, returnedCourse.Id); await Assert.ThrowsAsync <CoursesException>( () => courseRepository.Get(x => x.Id == 1)); }
public async void GetAll_GetsAllSavedCourses() { // Arrange var connectionBuilder = new SqliteConnectionStringBuilder { DataSource = ":memory:" }; var connection = new SqliteConnection(connectionBuilder.ToString()); var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseSqlite(connection) .Options; using var context = new ApplicationDbContext(options); context.Database.OpenConnection(); context.Database.EnsureCreated(); var courseRepository = new CourseRepository(context); var courses = CourseHelpers.GetCourses(); //Act context.AddRange(courses); context.SaveChanges(); var returnedCourses = await courseRepository.GetAll(); //Assert Assert.Equal(courses.Count(), returnedCourses.Count()); }
public async void Insert_SavesDataAndThrowsForNullData() { //Arrange var connectionBuilder = new SqliteConnectionStringBuilder { DataSource = ":memory:" }; var connection = new SqliteConnection(connectionBuilder.ToString()); var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseSqlite(connection) .Options; using var context = new ApplicationDbContext(options); context.Database.OpenConnection(); context.Database.EnsureCreated(); var courseRepository = new CourseRepository(context); var course = CourseHelpers.GetCourse(); //Act await courseRepository.Insert(course); var returnedCourses = await courseRepository.GetAll(); //Assert Assert.Single(returnedCourses); await Assert.ThrowsAsync <CoursesException>( () => courseRepository.Insert(null)); }
public async void Update_ModifiesASavedData() { //Arrange var connectionBuilder = new SqliteConnectionStringBuilder { DataSource = ":memory:" }; var connection = new SqliteConnection(connectionBuilder.ToString()); var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseSqlite(connection) .Options; using var context = new ApplicationDbContext(options); context.Database.OpenConnection(); context.Database.EnsureCreated(); var courseRepository = new CourseRepository(context); var course = CourseHelpers.GetCourse(); //Act await courseRepository.Insert(course); course.Title = "Java Getting Started"; await courseRepository.Update(course); var returnedCourse = await courseRepository.Get(x => x.Id == 5); //Assert Assert.Equal("Java Getting Started", returnedCourse.Title); }
public async void Delete_RemovesACourseFromDatabase_ThrowsForNonNullObject() { //Arrange var connectionBuilder = new SqliteConnectionStringBuilder { DataSource = ":memory:" }; var connection = new SqliteConnection(connectionBuilder.ToString()); var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseSqlite(connection) .Options; using var context = new ApplicationDbContext(options); context.Database.OpenConnection(); context.Database.EnsureCreated(); var courseRepository = new CourseRepository(context); var course = CourseHelpers.GetCourse(); await courseRepository.Insert(course); //Act await courseRepository.Delete(course); var returnedCourses = await courseRepository.GetAll(); //Assert Assert.Empty(returnedCourses); await Assert.ThrowsAsync <ArgumentNullException>( () => courseRepository.Delete(null)); }
public async void AddCourses_AddsCoursesWithSameIdFromTheListOfSudentCourses() { //Arrange var connectionBuilder = new SqliteConnectionStringBuilder { DataSource = ":memory:" }; var connection = new SqliteConnection(connectionBuilder.ToString()); var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseSqlite(connection) .Options; using var context = new ApplicationDbContext(options); context.Database.OpenConnection(); context.Database.EnsureCreated(); var teacherRepository = new TeacherRepository(context); var courseRepository = new CourseRepository(context); var teacher = TeacherHelpers.GetTeacher(); var courses = CourseHelpers.GetCourses(); //Act foreach (var course in courses) { await courseRepository.Insert(course); } await teacherRepository.Insert(teacher); await teacherRepository.AddManyCourses(new List <int> { 1, 2, 3 }, "5"); var numberOfCoursesAfterInsertion = (await teacherRepository.GetCourses("5")).Count(); //Assert Assert.Equal(3, numberOfCoursesAfterInsertion); await Assert.ThrowsAsync <AppUserException>( () => teacherRepository.AddManyCourses(new List <int> { 1, 2, 3 }, "5")); await Assert.ThrowsAsync <AppUserException>( () => teacherRepository.AddManyCourses(new List <int> { 10, 29, 13 }, "5")); }
public async void RemoveCourse_RemovesAcourseWithSameIdFromTheListOfSudentCourses() { //Arrange var connectionBuilder = new SqliteConnectionStringBuilder { DataSource = ":memory:" }; var connection = new SqliteConnection(connectionBuilder.ToString()); var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseSqlite(connection) .Options; using var context = new ApplicationDbContext(options); context.Database.OpenConnection(); context.Database.EnsureCreated(); var studentRepository = new StudentRepository(context); var courseRepository = new CourseRepository(context); var student = StudentHelpers.GetStudent(); var courses = CourseHelpers.GetCourses(); //Act foreach (var course in courses) { await courseRepository.Insert(course); } await studentRepository.Insert(student); await studentRepository.AddCourse(1, "5"); await studentRepository.AddCourse(2, "5"); var numberOfCoursesAfterInsertion = (await studentRepository.GetCourses("5")).Count(); await studentRepository.RemoveCourse(1, "5"); var numberOfCoursesAfterRemoval = (await studentRepository.GetCourses("5")).Count(); //Assert Assert.Equal(2, numberOfCoursesAfterInsertion); Assert.Equal(1, numberOfCoursesAfterRemoval); await studentRepository.RemoveCourse(2, "5"); await Assert.ThrowsAsync <AppUserException>( () => studentRepository.GetCourses("5")); }