コード例 #1
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 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));
        }
コード例 #2
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 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);
        }
コード例 #3
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 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));
        }
コード例 #4
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 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));
        }