public void ValidParamsCalledTwiceWithin1Minute_UsesCachedVersionOnSecondCall() { var readerCreator = mocks.DynamicMock<IDnaDataReaderCreator>(); var reader = mocks.DynamicMock<IDnaDataReader>(); var cacheManager = mocks.DynamicMock<ICacheManager>(); DateTime startSampleTime = DateTime.Now.AddMinutes(-1); int minutes = 1; // URL Default int totalActiveForums = 1; DateTime closeDate = DateTime.Now.AddDays(1.0); int count = 5; DateTime lastRatedDate = DateTime.Now.AddMinutes(-0.5); int siteId = 1; string urlName = "h2g2"; string title = "Test Title"; int totalPosts = 20; string url = "https://local.bbc.co.uk/dna/h2g2/comments"; int upRatings = 20; int downRatings = 10; StubDatabaseCall(readerCreator, reader, totalActiveForums, closeDate, count, lastRatedDate, siteId, urlName, title, totalPosts, url, upRatings, downRatings); CommentForumRatingActivity cachedRatingActivity = new CommentForumRatingActivity(); cachedRatingActivity.ClosingDate = new DateTimeHelper(closeDate); cachedRatingActivity.Count = count; cachedRatingActivity.LastRatedDate = new DateTimeHelper(lastRatedDate); cachedRatingActivity.SiteId = siteId; cachedRatingActivity.SiteName = urlName; cachedRatingActivity.Title = title; cachedRatingActivity.TotalPosts = totalPosts; cachedRatingActivity.URL = url; cachedRatingActivity.Up = upRatings; cachedRatingActivity.Down = downRatings; CommentForumsRatingActivityList cachedActivityRatingList = new CommentForumsRatingActivityList(); cachedActivityRatingList.CommentForumsRatingActivity = new List<CommentForumRatingActivity>(); cachedActivityRatingList.CommentForumsRatingActivity.Add(cachedRatingActivity); cachedActivityRatingList.DateChecked = new DateTimeHelper(DateTime.Now); cachedActivityRatingList.Minutes = minutes; cachedActivityRatingList.StartDate = new DateTimeHelper(startSampleTime); cacheManager.Stub(x => x.GetData(Arg<string>.Is.Anything)).Return(null).Repeat.Once(); // Not cached first time cacheManager.Stub(x => x.GetData(Arg<string>.Is.Anything)).Return(cachedActivityRatingList).Repeat.Once(); // Cached second time mocks.ReplayAll(); Comments comments = new Comments(null, readerCreator, cacheManager, null); CommentForumsRatingActivityList activity = comments.GetCommentForumsRatingActivity(minutes, startSampleTime.ToString()); VerifyActivityList(totalActiveForums, minutes, closeDate, startSampleTime, count, lastRatedDate, siteId, urlName, title, totalPosts, url, upRatings, downRatings, activity); activity = comments.GetCommentForumsRatingActivity(minutes, startSampleTime.ToString()); VerifyActivityList(totalActiveForums, minutes, closeDate, startSampleTime, count, lastRatedDate, siteId, urlName, title, totalPosts, url, upRatings, downRatings, activity); cacheManager.AssertWasCalled(x => x.GetData(Arg<string>.Is.Anything), options => options.Repeat.Times(2)); cacheManager.AssertWasCalled(x => x.Add(Arg<string>.Is.Anything, Arg<object>.Is.Anything, Arg<CacheItemPriority>.Is.Anything, Arg<ICacheItemRefreshAction>.Is.Anything, Arg<ICacheItemExpiration>.Is.Anything), options => options.Repeat.Once()); }
/// <summary> /// Get the latest rating activity on all comment forums across all sites /// </summary> /// <param name="minutes">Activity over the last 'X' minutes. Will top out at 60 mins if greater</param> /// <param name="startDate">Activity since the given start date. Will truncate to 60 mins if greater</param> /// <returns>A list of activity on forums within the given time span</returns> public CommentForumsRatingActivityList GetCommentForumsRatingActivity(int minutes, string startDate) { if (minutes > 60) { minutes = 60; } else if (minutes < 1) { minutes = 1; } DateTime startDateTime = DateTime.Now.AddMinutes(-minutes); if (startDate.Length > 0 && DateTime.TryParse(startDate, out startDateTime)) { if (startDateTime < DateTime.Now.AddHours(-1)) { startDateTime = DateTime.Now.AddHours(-1); } minutes = (int)(DateTime.Now - startDateTime).TotalMinutes; } CommentForumsRatingActivityList activityList = GetCachedRatingActivitylist(minutes); if (activityList != null) { return activityList; } activityList = new CommentForumsRatingActivityList(); activityList.CommentForumsRatingActivity = new List<CommentForumRatingActivity>(); activityList.DateChecked = new DateTimeHelper(DateTime.Now); activityList.StartDate = new DateTimeHelper(startDateTime); activityList.Minutes = minutes; using (IDnaDataReader reader = CreateReader("getcommentforumsactivity_ratings")) { reader.AddParameter("minutes", minutes); reader.AddParameter("startdate", startDateTime); reader.Execute(); while (reader.HasRows && reader.Read()) { CommentForumRatingActivity activity = new CommentForumRatingActivity(); activity.ClosingDate = new DateTimeHelper(reader.GetDateTime("ForumCloseDate")); activity.Count = reader.GetInt32("count"); activity.Up = reader.GetInt32("up"); activity.Down = reader.GetInt32("down"); activity.LastRatedDate = new DateTimeHelper(reader.GetDateTime("LastRatedDate")); activity.SiteId = reader.GetInt32("siteid"); activity.SiteName = reader.GetString("urlname"); activity.Title = reader.GetString("title"); activity.TotalPosts = reader.GetInt32("totalPosts"); activity.URL = reader.GetString("url"); activityList.CommentForumsRatingActivity.Add(activity); } } CacheRatingActivityList(activityList); return activityList; }