Esempio n. 1
0
        public void GetOrCreateChapterAsyncShouldReturnChapterIfItExistsForTheCurrentPeriod()
        {
            //Arrange
            var existingPeriod = new Period {
                Id = _random.NextPositive()
            };
            var courseCode      = Guid.NewGuid().ToString();
            var existingChapter = new ChapterBuilder().WithId()
                                  .WithCourse(courseCode)
                                  .WithPeriod(existingPeriod).Build();

            _periodRepositoryMock.Setup(repo => repo.GetCurrentPeriodAsync()).ReturnsAsync(existingPeriod);

            _chapterRepositoryMock.Setup(repo => repo.GetSingleAsync(It.IsAny <string>(), It.IsAny <int>(), It.IsAny <int>()))
            .ReturnsAsync(existingChapter);

            //Act
            var result = _service.GetOrCreateChapterAsync(courseCode, existingChapter.Number).Result;

            //Assert
            _periodRepositoryMock.Verify(repo => repo.GetCurrentPeriodAsync(), Times.Once);
            _chapterRepositoryMock.Verify(repo => repo.GetSingleAsync(courseCode, existingChapter.Number, existingPeriod.Id), Times.Once);
            _chapterRepositoryMock.Verify(repo => repo.AddAsync(It.IsAny <Chapter>()), Times.Never);
            Assert.That(result, Is.EqualTo(existingChapter));
        }
Esempio n. 2
0
        public void LoadChapterWithTestsAsyncShouldLoadTheChapterItsExercisesAndItsTests()
        {
            //Arrange
            var existingPeriod  = new Period();
            var courseCode      = Guid.NewGuid().ToString();
            var existingChapter = new ChapterBuilder().WithId()
                                  .WithCourse(courseCode)
                                  .WithPeriod(existingPeriod).Build();

            _periodRepositoryMock.Setup(repo => repo.GetCurrentPeriodAsync()).ReturnsAsync(existingPeriod);

            _chapterRepositoryMock.Setup(repo => repo.LoadWithExercisesAndTestsAsync(It.IsAny <int>(), It.IsAny <int>(), It.IsAny <int>()))
            .ReturnsAsync(existingChapter);

            //Act
            var result = _service.LoadChapterWithTestsAsync(existingChapter.CourseId, existingChapter.Number).Result;

            //Assert
            _periodRepositoryMock.Verify(repo => repo.GetCurrentPeriodAsync(), Times.Once);

            _chapterRepositoryMock.Verify();

            _chapterRepositoryMock.Verify(repo => repo.LoadWithExercisesAndTestsAsync(existingChapter.CourseId, existingChapter.Number, existingPeriod.Id), Times.Once);

            Assert.That(result, Is.EqualTo(existingChapter));
        }
        public async Task Setup()
        {
            var reader = AccountBuilder.InLibrary(LibraryId).As(Role.Reader).Build();

            _chapter  = ChapterBuilder.WithLibrary(LibraryId).WithContents().WithoutAnyAssignment().Build();
            _response = await Client.PostObject($"/libraries/{LibraryId}/books/{_chapter.BookId}/chapters/{_chapter.ChapterNumber}/assign", new { AccountId = reader.Id, Type = "write" });
        }
Esempio n. 4
0
        public async Task Setup()
        {
            var _newContents = RandomData.Words(12);
            var chapter      = ChapterBuilder.WithLibrary(LibraryId).WithoutContents().Build();

            _response = await Client.DeleteAsync($"/libraries/{LibraryId}/books/{chapter.BookId}/chapters/{chapter.ChapterNumber}/contents?language={RandomData.Locale}");
        }
Esempio n. 5
0
        public async Task Setup()
        {
            var book    = BookBuilder.WithLibrary(LibraryId).Build();
            var chapter = ChapterBuilder.WithLibrary(LibraryId).Build();

            _response = await Client.PostString($"/libraries/{-RandomData.Number}/books/{book.Id}/chapters/{chapter.Id}/contents?language={RandomData.Locale}", RandomData.String, RandomData.Locale);
        }
Esempio n. 6
0
        public void ToChapterSummaryModel_ShouldCorrectlyConvertValidChapter(int numberOfTests,
                                                                             int numberOfPassingTests,
                                                                             int numberOfFailingTests,
                                                                             int numberOfUsers)
        {
            //Arrange
            var chapter             = new ChapterBuilder().WithId().WithExercises(5, numberOfTests).Build();
            var userExerciseResults = GenerateAssignmentResults(chapter, numberOfPassingTests, numberOfFailingTests);

            //Act
            var model = _converter.ToChapterSummaryModel(chapter, userExerciseResults);

            //Assert
            Assert.That(model, Is.Not.Null);
            Assert.That(model.Id, Is.EqualTo(chapter.Id));
            Assert.That(model.Number, Is.EqualTo(chapter.Number));
            Assert.That(model.ExerciseSummaries, Is.Not.Null);
            Assert.That(model.ExerciseSummaries.Count, Is.EqualTo(chapter.Exercises.Count));

            foreach (var exercise in chapter.Exercises)
            {
                var userExerciseSummary = model.ExerciseSummaries.FirstOrDefault(summary => summary.ExerciseId == exercise.Id);
                Assert.That(userExerciseSummary, Is.Not.Null);
                Assert.That(userExerciseSummary.Code, Is.EqualTo(exercise.Code));
                Assert.That(userExerciseSummary.NumberOfPassedTests, Is.EqualTo(numberOfPassingTests));
                Assert.That(userExerciseSummary.NumberOfFailedTests, Is.EqualTo(numberOfFailingTests));
                Assert.That(userExerciseSummary.NumberOfTests, Is.EqualTo(numberOfTests));
            }
        }
Esempio n. 7
0
        public void GetChapterSummaryShouldReturnContentsIfParamatersAreValid()
        {
            //Arrange
            var existingChapter     = new ChapterBuilder().WithId().Build();
            var chapterContents     = new ChapterSummaryModel();
            var userExerciseResults = new List <AssignmentResultDto>();

            _chapterServiceMock.Setup(service => service.LoadChapterWithTestsAsync(It.IsAny <int>(), It.IsAny <int>()))
            .ReturnsAsync(existingChapter);
            _chapterServiceMock.Setup(service => service.GetResultsForUserAsync(It.IsAny <int>(), It.IsAny <int>(), It.IsAny <DateTime?>()))
            .ReturnsAsync(userExerciseResults);
            _chapterConverterMock.Setup(converter => converter.ToChapterSummaryModel(It.IsAny <Chapter>(),
                                                                                     It.IsAny <IList <AssignmentResultDto> >()))
            .Returns(chapterContents);

            //Act
            var actionResult = _controller.GetChapterSummary(existingChapter.CourseId, existingChapter.Number, _userId, null).Result as OkObjectResult;

            //Assert
            Assert.That(actionResult, Is.Not.Null);
            _chapterServiceMock.Verify(service => service.LoadChapterWithTestsAsync(existingChapter.CourseId, existingChapter.Number), Times.Once);

            _chapterServiceMock.Verify(service => service.GetResultsForUserAsync(existingChapter.Id, _userId, null), Times.Once);

            _chapterConverterMock.Verify(converter => converter.ToChapterSummaryModel(existingChapter, userExerciseResults), Times.Once);
            Assert.That(actionResult.Value, Is.EqualTo(chapterContents));
        }
        public async Task Setup()
        {
            _chapter = ChapterBuilder.WithLibrary(LibraryId).Private().WithContents().Build();
            var content = ChapterBuilder.Contents.Single(x => x.ChapterId == _chapter.Id);

            _response = await Client.GetAsync($"/libraries/{LibraryId}/books/{_chapter.BookId}/chapters/{_chapter.Id}/contents?language={content.Language}");
        }
Esempio n. 9
0
        public void GetChapterStatisticsShouldNotUseCacheWhenDateIsTooFarInThePast()
        {
            //Arrange
            var existingChapter        = new ChapterBuilder().WithId().Build();
            var chapterStatistics      = new List <AssignmentStatisticsDto>();
            var chapterStatisticsModel = new ChapterStatisticsModel();
            var aLongTimeAgo           = DateTime.Now.AddSeconds(-ChapterController.CacheTimeInSeconds - 1);


            _chapterServiceMock.Setup(service => service.LoadChapterAsync(It.IsAny <int>(), It.IsAny <int>()))
            .ReturnsAsync(existingChapter);

            _chapterServiceMock.Setup(service => service.GetChapterStatisticsAsync(It.IsAny <int>(), It.IsAny <DateTime?>()))
            .ReturnsAsync(chapterStatistics);

            _chapterConverterMock.Setup(converter => converter.ToChapterStatisticsModel(It.IsAny <Chapter>(), It.IsAny <IList <AssignmentStatisticsDto> >()))
            .Returns(chapterStatisticsModel);

            //Act
            var actionResult = _controller.GetChapterStatistics(existingChapter.CourseId, existingChapter.Number, aLongTimeAgo).Result as OkObjectResult;

            //Assert
            Assert.That(actionResult, Is.Not.Null);
            object cachedEntry;

            _memoryCacheMock.Verify(cache => cache.TryGetValue(It.IsAny <object>(), out cachedEntry), Times.Never);

            _chapterServiceMock.Verify(service => service.LoadChapterAsync(existingChapter.CourseId, existingChapter.Number), Times.Once);
            _chapterServiceMock.Verify(service => service.GetChapterStatisticsAsync(existingChapter.Id, aLongTimeAgo.ToUniversalTime()), Times.Once);
            _chapterConverterMock.Verify(converter => converter.ToChapterStatisticsModel(existingChapter, chapterStatistics), Times.Once);
            Assert.That(actionResult.Value, Is.EqualTo(chapterStatisticsModel));
        }
Esempio n. 10
0
        public void GetChapterStatisticsShouldReturnStatisticsIfParamatersAreValid()
        {
            //Arrange
            var existingChapter        = new ChapterBuilder().WithId().Build();
            var chapterStatistics      = new List <AssignmentStatisticsDto>();
            var chapterStatisticsModel = new ChapterStatisticsModel();
            var date = DateTime.Now.AddDays(-1);


            _chapterServiceMock.Setup(service => service.LoadChapterAsync(It.IsAny <int>(), It.IsAny <int>()))
            .ReturnsAsync(existingChapter);

            _chapterServiceMock.Setup(service => service.GetChapterStatisticsAsync(It.IsAny <int>(), It.IsAny <DateTime?>()))
            .ReturnsAsync(chapterStatistics);

            _chapterConverterMock.Setup(converter => converter.ToChapterStatisticsModel(It.IsAny <Chapter>(), It.IsAny <IList <AssignmentStatisticsDto> >()))
            .Returns(chapterStatisticsModel);

            //Act
            var actionResult = _controller.GetChapterStatistics(existingChapter.CourseId, existingChapter.Number, date).Result as OkObjectResult;

            //Assert
            Assert.That(actionResult, Is.Not.Null);
            _chapterServiceMock.Verify(service => service.LoadChapterAsync(existingChapter.CourseId, existingChapter.Number), Times.Once);
            _chapterServiceMock.Verify(service => service.GetChapterStatisticsAsync(existingChapter.Id, date.ToUniversalTime()), Times.Once);
            _chapterConverterMock.Verify(converter => converter.ToChapterStatisticsModel(existingChapter, chapterStatistics), Times.Once);
            Assert.That(actionResult.Value, Is.EqualTo(chapterStatisticsModel));
        }
        public async Task Setup()
        {
            _expected = ChapterBuilder.WithLibrary(LibraryId).Build(4).First();

            _response = await Client.GetAsync($"/libraries/{LibraryId}/books/{_expected.BookId}/chapters/{_expected.ChapterNumber}");

            _assert = ChapterAssert.FromResponse(_response, LibraryId);
        }
        public async Task Setup()
        {
            _chapter = ChapterBuilder.WithLibrary(LibraryId).Build();

            _response = await Client.PostString($"/libraries/{LibraryId}/books/{_chapter.BookId}/chapters/{_chapter.ChapterNumber}/contents", RandomData.String, null);

            _assert = new ChapterContentAssert(_response, Library);
        }
        public async Task Setup()
        {
            _chapter  = ChapterBuilder.WithLibrary(LibraryId).Public().Build();
            _contents = RandomData.String;

            _response = await Client.PostString($"/libraries/{LibraryId}/books/{_chapter.BookId}/chapters/{_chapter.ChapterNumber}/contents?language={RandomData.Locale}", _contents, RandomData.Locale);

            _assert = new ChapterContentAssert(_response, LibraryId);
        }
        public async Task Setup()
        {
            var _newContents = RandomData.Words(12);
            var chapter      = ChapterBuilder.WithLibrary(LibraryId).WithContents().Build();

            _content = ChapterBuilder.Contents.Single(x => x.ChapterId == chapter.Id);

            _response = await Client.DeleteAsync($"/libraries/{LibraryId}/books/{chapter.BookId}/chapters/{chapter.ChapterNumber}/contents?language={_content.Language}");
        }
Esempio n. 15
0
        public async Task Setup()
        {
            _chapter = ChapterBuilder.WithLibrary(LibraryId).Public().WithContents().Build();
            _content = ChapterBuilder.Contents.Single(x => x.ChapterId == _chapter.Id);

            _newContents = RandomData.String;

            _response = await Client.PutString($"/libraries/{LibraryId}/books/{_chapter.BookId}/chapters/{_chapter.Id}/contents?language={_content.Language}", _newContents);
        }
Esempio n. 16
0
        public async Task Setup()
        {
            _chapter = ChapterBuilder.WithLibrary(LibraryId).Public().WithContents().Build();
            _content = ChapterBuilder.Contents.Single(x => x.ChapterId == _chapter.Id);

            _response = await Client.GetAsync($"/libraries/{LibraryId}/books/{_chapter.BookId}/chapters/{_chapter.ChapterNumber}/contents?language={_content.Language}");

            _assert = new ChapterContentAssert(_response, LibraryId);
        }
        public async Task Setup()
        {
            _chapters = ChapterBuilder.WithLibrary(LibraryId).WithContents().Build(4);
            _book     = DatabaseConnection.GetBookById(_chapters.PickRandom().BookId);

            _response = await Client.GetAsync($"/libraries/{LibraryId}/books/{_book.Id}/chapters");

            _view = _response.GetContent <ListView <ChapterView> >().Result;
        }
Esempio n. 18
0
        public async Task Setup()
        {
            var chapters = ChapterBuilder.WithLibrary(LibraryId).WithContents(2).Build(4);

            _expected = chapters.PickRandom();

            _response = await Client.GetAsync($"/libraries/{LibraryId}/books/{_expected.BookId}/chapters/{_expected.ChapterNumber}");

            _assert = ChapterAssert.FromResponse(_response, LibraryId);
        }
        public async Task Setup()
        {
            var chapters = ChapterBuilder.WithLibrary(LibraryId).WithContents().Build(4);
            var chapter  = chapters.PickRandom();

            var chapter2 = new ChapterView {
                Title = RandomData.Name
            };

            _response = await Client.PutObject($"/libraries/{LibraryId}/books/{chapter.BookId}/chapters/{chapter.ChapterNumber}", chapter2);
        }
Esempio n. 20
0
        public void ToExerciseDetailModel_ShouldThrowArgumentExceptionIfCourseOfChapterOfExerciseIsNotLoaded()
        {
            //Arrange
            var chapter  = new ChapterBuilder().WithoutCourseLoaded().Build();
            var exercise = new ExerciseBuilder().WithChapter(chapter).Build();

            //Act + Assert
            Assert.That(
                () => _converter.ToExerciseDetailModel(exercise, new AssignmentResultDto(), new ExerciseTestRunInfoDto()),
                Throws.ArgumentException);
        }
Esempio n. 21
0
        public void ToExerciseDetailModel_ShouldThrowArgumentExceptionIfTestRunInfoIsNotProvided()
        {
            //Arrange
            var chapter  = new ChapterBuilder().WithCourse().Build();
            var exercise = new ExerciseBuilder().WithChapter(chapter).Build();

            //Act + Assert
            Assert.That(
                () => _converter.ToExerciseDetailModel(exercise, new AssignmentResultDto(), null),
                Throws.ArgumentNullException);
        }
Esempio n. 22
0
        public void GetOrCreateChapterAsync_ShouldThrowDataNotFoundExeptionWhenNoCurrentPeriodIsFound()
        {
            //Arrange
            var courseCode      = Guid.NewGuid().ToString();
            var existingChapter = new ChapterBuilder().WithId().Build();

            _periodRepositoryMock.Setup(repo => repo.GetCurrentPeriodAsync()).Throws <DataNotFoundException>();

            //Act + Assert
            Assert.That(() => _service.GetOrCreateChapterAsync(courseCode, existingChapter.Number).Result,
                        Throws.InstanceOf <AggregateException>().With.Matches((AggregateException ex) =>
                                                                              ex.InnerExceptions.OfType <DataNotFoundException>().Any()));
        }
Esempio n. 23
0
        public async Task Setup()
        {
            var chapters = ChapterBuilder.WithLibrary(LibraryId).Build(4);
            var chapter  = chapters.PickRandom();

            newChapter = new ChapterView {
                Title = RandomData.Name, BookId = chapter.BookId
            };

            _response = await Client.PutObject($"/libraries/{LibraryId}/books/{chapter.BookId}/chapters/{chapter.ChapterNumber}", newChapter);

            _assert = ChapterAssert.FromResponse(_response, LibraryId);
        }
Esempio n. 24
0
        public void ToChapterModel_ShouldCorrectlyCovertValidChapter()
        {
            //Arrange
            var chapter = new ChapterBuilder().WithId().Build();

            //Act
            var model = _converter.ToChapterModel(chapter);

            //Assert
            Assert.That(model, Is.Not.Null);
            Assert.That(model.Id, Is.EqualTo(chapter.Id));
            Assert.That(model.Number, Is.EqualTo(chapter.Number));
        }
Esempio n. 25
0
        public void ToChapterSummaryModel_ShouldThrowArgumentExceptionWhenTestsOfExercisesAreMissing()
        {
            //Arrange
            var chapter = new ChapterBuilder().WithExercises(1, 1).Build();

            chapter.Exercises.First().Tests = null;
            var userExerciseResults         = new List <AssignmentResultDto>();

            //     var averageExerciseResults = new List<AssignmentResultDto>();

            //Act + Assert
            Assert.That(() => _converter.ToChapterSummaryModel(chapter, userExerciseResults), Throws.InstanceOf <ArgumentException>());
        }
Esempio n. 26
0
        public void ToExerciseDetailModel_ShouldAlsoWorkWhenNoTestResultsAreProvided()
        {
            //Arrange
            var chapter  = new ChapterBuilder().WithCourse().Build();
            var exercise = new ExerciseBuilder().WithChapter(chapter).Build();

            //Act
            var model = _converter.ToExerciseDetailModel(exercise, null, new ExerciseTestRunInfoDto());

            //Assert
            Assert.That(model, Is.Not.Null);
            Assert.That(model.TestResults, Is.Not.Null);
            Assert.That(model.TestResults, Has.Count.Zero);
        }
Esempio n. 27
0
        public void ToExerciseDetailModel_ShouldHaveATestResultForEachTest()
        {
            //Arrange
            var chapter       = new ChapterBuilder().WithCourse().Build();
            var numberOfTests = _random.Next(2, 10);
            var exercise      = new ExerciseBuilder().WithChapter(chapter).WithRandomTests(numberOfTests).Build();

            //Act
            var model = _converter.ToExerciseDetailModel(exercise, new AssignmentResultDto(), new ExerciseTestRunInfoDto());

            //Assert
            Assert.That(model, Is.Not.Null);
            Assert.That(model.TestResults, Is.Not.Null);
            Assert.That(model.TestResults, Has.Count.EqualTo(exercise.Tests.Count));
        }
Esempio n. 28
0
        public void GetChapterStatisticsShouldCacheCreatedModel()
        {
            //Arrange
            var existingChapter        = new ChapterBuilder().WithId().Build();
            var chapterStatistics      = new List <AssignmentStatisticsDto>();
            var chapterStatisticsModel = new ChapterStatisticsModel();
            var date             = DateTime.Now.AddSeconds(-ChapterController.CacheTimeInSeconds + 1);
            var expectedCacheKey = $"GetChapterStatistics-{existingChapter.CourseId}-{existingChapter.Number}";


            _chapterServiceMock.Setup(service => service.LoadChapterAsync(It.IsAny <int>(), It.IsAny <int>()))
            .ReturnsAsync(existingChapter);

            _chapterServiceMock.Setup(service => service.GetChapterStatisticsAsync(It.IsAny <int>(), It.IsAny <DateTime?>()))
            .ReturnsAsync(chapterStatistics);

            _chapterConverterMock.Setup(converter => converter.ToChapterStatisticsModel(It.IsAny <Chapter>(), It.IsAny <IList <AssignmentStatisticsDto> >()))
            .Returns(chapterStatisticsModel);

            object cachedChapterStatisticsModel;

            _memoryCacheMock.Setup(cache => cache.TryGetValue(It.IsAny <object>(), out cachedChapterStatisticsModel))
            .Returns(false);

            var cacheEntryMock = new Mock <ICacheEntry>();

            _memoryCacheMock.Setup(cache => cache.CreateEntry(It.IsAny <object>())).Returns(cacheEntryMock.Object);

            //Act
            var actionResult = _controller.GetChapterStatistics(existingChapter.CourseId, existingChapter.Number, date).Result as OkObjectResult;

            //Assert
            Assert.That(actionResult, Is.Not.Null);
            _memoryCacheMock.Verify(cache => cache.TryGetValue(expectedCacheKey, out cachedChapterStatisticsModel), Times.Once);
            _memoryCacheMock.Verify(cache => cache.CreateEntry(expectedCacheKey), Times.Once);

            Assert.That(actionResult.Value, Is.EqualTo(chapterStatisticsModel));
        }
Esempio n. 29
0
        public void ToExerciseDetailModel_ShouldCorrectlyConvertTestRunInfoFields()
        {
            //Arrange
            var chapter  = new ChapterBuilder().WithCourse().Build();
            var exercise = new ExerciseBuilder().WithChapter(chapter).Build();
            var utcNow   = DateTime.UtcNow;

            var testRunInfo = new ExerciseTestRunInfoDto
            {
                FirstRunDateTime = utcNow.AddDays(-1),
                LastRunDateTime  = utcNow,
                NumberOfRuns     = _random.NextPositive()
            };

            //Act
            var model = _converter.ToExerciseDetailModel(exercise, new AssignmentResultDto(), testRunInfo);

            //Assert
            Assert.That(model, Is.Not.Null);
            Assert.That(model.FirstRun, Is.EqualTo(testRunInfo.FirstRunDateTime));
            Assert.That(model.LastRun, Is.EqualTo(testRunInfo.LastRunDateTime));
            Assert.That(model.NumberOfRuns, Is.EqualTo(testRunInfo.NumberOfRuns));
        }
Esempio n. 30
0
        public void GetOrCreateChapterAsyncShouldCreateChapterIfItDoesNotExist()
        {
            //Arrange
            var existingPeriod = new Period()
            {
                Id = _random.NextPositive()
            };

            _periodRepositoryMock.Setup(repo => repo.GetCurrentPeriodAsync()).ReturnsAsync(existingPeriod);

            var existingCourse = new CourseBuilder().WithId().Build();

            _courseRepositoryMock.Setup(repo => repo.GetSingleAsync(It.IsAny <string>())).ReturnsAsync(existingCourse);

            _chapterRepositoryMock.Setup(repo => repo.GetSingleAsync(It.IsAny <string>(), It.IsAny <int>(), It.IsAny <int>()))
            .Throws <DataNotFoundException>();

            var addedChapter = new ChapterBuilder().WithId().Build();

            _chapterRepositoryMock.Setup(repo => repo.AddAsync(It.IsAny <Chapter>())).ReturnsAsync(addedChapter);


            //Act
            var result = _service.GetOrCreateChapterAsync(existingCourse.Code, addedChapter.Number).Result;

            //Assert
            _periodRepositoryMock.Verify(repo => repo.GetCurrentPeriodAsync(), Times.Once);
            _chapterRepositoryMock.Verify(repo => repo.GetSingleAsync(existingCourse.Code, addedChapter.Number, existingPeriod.Id), Times.Once);
            _chapterRepositoryMock.Verify(
                repo => repo.AddAsync(It.Is <Chapter>(ch =>
                                                      ch.PeriodId == existingPeriod.Id && ch.CourseId == existingCourse.Id &&
                                                      ch.Number == addedChapter.Number)), Times.Once);

            _courseRepositoryMock.Verify(repo => repo.GetSingleAsync(existingCourse.Code), Times.Once);

            Assert.That(result, Is.EqualTo(addedChapter));
        }