Ejemplo n.º 1
0
        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 studentRepository = new StudentRepository(context);
            var student           = StudentHelpers.GetStudent();

            //Act
            await studentRepository.Insert(student);

            student.FirstName = "Frank";
            student.LastName  = "James";

            await studentRepository.Update(student);

            var returnedStudent = await studentRepository.Get(x => x.Id == "5");

            //Assert
            Assert.Equal("Frank", returnedStudent.FirstName);
            Assert.Equal("James", returnedStudent.LastName);
        }
Ejemplo n.º 2
0
        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 studentRepository = new StudentRepository(context);
            var course            = StudentHelpers.GetStudent();

            //Act
            await studentRepository.Insert(course);

            var returnedStudents = await studentRepository.GetAll();

            //Assert
            Assert.Single(returnedStudents);
            await Assert.ThrowsAsync <AppUserException>(
                () => studentRepository.Insert(null));
        }
Ejemplo n.º 3
0
        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 studentRepository = new StudentRepository(context);
            var student           = StudentHelpers.GetStudent();

            //Act
            await studentRepository.Insert(student);

            await studentRepository.Delete(student);

            var returnedCourses = await studentRepository.GetAll();

            //Assert
            Assert.Empty(returnedCourses);
            await Assert.ThrowsAsync <ArgumentNullException>(
                () => studentRepository.Delete(null));
        }
Ejemplo n.º 4
0
        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 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.AddManyCourses(new List <int> {
                1, 2, 3
            }, "5");

            var numberOfCoursesAfterInsertion = (await studentRepository.GetCourses("5")).Count();

            //Assert
            Assert.Equal(3, numberOfCoursesAfterInsertion);
            await Assert.ThrowsAsync <AppUserException>(
                () => studentRepository.AddManyCourses(new List <int> {
                1, 2, 3
            }, "5"));

            await Assert.ThrowsAsync <AppUserException>(
                () => studentRepository.AddManyCourses(new List <int> {
                10, 29, 13
            }, "5"));
        }
Ejemplo n.º 5
0
        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 studentRepository = new StudentRepository(context);
            var student           = StudentHelpers.GetStudent();
            //Act
            await studentRepository.Insert(student);

            var returnedCourse = await studentRepository.Get(x => x.Id == "5");

            //Assert
            Assert.Equal("5", returnedCourse.Id);
        }