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)); }
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 ChapterStatisticsModel ToChapterStatisticsModel(Chapter chapter, IList <AssignmentStatisticsDto> chapterStatistics) { var model = new ChapterStatisticsModel { Id = chapter.Id, Number = chapter.Number, ExerciseStatistics = new List <ExerciseStatisticsModel>() }; foreach (var exercise in chapter.Exercises.OrderBy(exercise => exercise.Code)) { var exerciseStatisticsModel = CreateExerciseStatisticsModel(exercise, chapterStatistics); model.ExerciseStatistics.Add(exerciseStatisticsModel); } return(model); }
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)); }
public void GetChapterStatisticsShouldUseCacheWhenDateIsCloseToNow() { //Arrange var courseId = _random.NextPositive(); var chapterNumber = _random.NextPositive(); var date = DateTime.Now.AddSeconds(-ChapterController.CacheTimeInSeconds + 1); var expectedCacheKey = $"GetChapterStatistics-{courseId}-{chapterNumber}"; object cachedChapterStatisticsModel = new ChapterStatisticsModel(); _memoryCacheMock.Setup(cache => cache.TryGetValue(It.IsAny <object>(), out cachedChapterStatisticsModel)) .Returns(true); //Act var actionResult = _controller.GetChapterStatistics(courseId, chapterNumber, date).Result as OkObjectResult; //Assert Assert.That(actionResult, Is.Not.Null); Assert.That(actionResult.Value, Is.EqualTo(cachedChapterStatisticsModel)); _memoryCacheMock.Verify(cache => cache.TryGetValue(expectedCacheKey, out cachedChapterStatisticsModel), Times.Once); _chapterServiceMock.Verify(service => service.LoadChapterAsync(It.IsAny <int>(), It.IsAny <int>()), Times.Never); }