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 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")); }