public void Can_Update_New_TwitterReportLikesPePeriod()
        {
            //Arrange - create a report
            TwitterReportLikesByPeriod report = new TwitterReportLikesByPeriod();

            report.UserName = "******";

            //Arrange - creating a collection of tweets for update
            List <ITweet> tweetsCollection = new List <ITweet>();

            //Arrange - fill the collection from previous step
            for (int hour = 1; hour <= 3; hour++)
            {
                for (int i = 1; i <= 10; i++)
                {
                    var tweet = new Mock <ITweet>();
                    tweet.Setup(t => t.CreatedAt).Returns(new System.DateTime(2017, 11, 23, hour, 0, 0));
                    tweet.Setup(t => t.FavoriteCount).Returns(i * hour);

                    tweetsCollection.Add(tweet.Object);
                }
            }

            //Act
            report.Update(11, tweetsCollection);

            //Assert - main report
            Assert.AreEqual(report.TweetsCount, 30);
            Assert.AreEqual(report.reportParts.Count, 3);
            Assert.AreEqual(report.ReportAuthorID, 11);
            Assert.AreEqual(report.LikesMedian, 9);
            Assert.AreEqual(report.BestTimePeriod, 3);

            //Assert - first part
            Assert.AreEqual(report.reportParts[0].UserName, "TestName");
            Assert.AreEqual(report.reportParts[0].TweetsCount, 10);
            Assert.AreEqual(report.reportParts[0].ReportAuthorID, 11);
            Assert.AreEqual(report.reportParts[0].MainReport, report);
            Assert.AreEqual(report.reportParts[0].LikesMedian, 5.5);
            Assert.AreEqual(report.reportParts[0].LikesCount, 55);
            Assert.AreEqual(report.reportParts[0].Hour, 1);

            //Assert - second part
            Assert.AreEqual(report.reportParts[1].UserName, "TestName");
            Assert.AreEqual(report.reportParts[1].TweetsCount, 10);
            Assert.AreEqual(report.reportParts[1].ReportAuthorID, 11);
            Assert.AreEqual(report.reportParts[1].MainReport, report);
            Assert.AreEqual(report.reportParts[1].LikesMedian, 11);
            Assert.AreEqual(report.reportParts[1].LikesCount, 110);
            Assert.AreEqual(report.reportParts[1].Hour, 2);

            //Assert - third part
            Assert.AreEqual(report.reportParts[2].UserName, "TestName");
            Assert.AreEqual(report.reportParts[2].TweetsCount, 10);
            Assert.AreEqual(report.reportParts[2].ReportAuthorID, 11);
            Assert.AreEqual(report.reportParts[2].MainReport, report);
            Assert.AreEqual(report.reportParts[2].LikesMedian, 16.5);
            Assert.AreEqual(report.reportParts[2].LikesCount, 165);
            Assert.AreEqual(report.reportParts[2].Hour, 3);
        }
        public async Task <ActionResult> Index([Bind(Include = "UserName")] TwitterReportLikesByPeriod twitterReportLikesByPeriod)
        {
            if (await TweetsReportGenerator.UpdateReportAsync(twitterReportLikesByPeriod))
            {
                await repository.SaveReportAsync(twitterReportLikesByPeriod);
            }

            ModelState.Clear();
            return(View());
        }
Esempio n. 3
0
        public void DeleteReportByID(int ID)
        {
            TwitterReportLikesByPeriod dbEntry = context.ReportLikesByPeriod.Find(ID);

            if (dbEntry == null)
            {
                return;
            }
            context.ReportPartLikesByHour.RemoveRange(dbEntry.reportParts);
            context.ReportLikesByPeriod.Remove(dbEntry);
            context.SaveChanges();
        }
Esempio n. 4
0
        public async Task DeleteReportByIDAsync(int ID)
        {
            TwitterReportLikesByPeriod dbEntry = context.ReportLikesByPeriod.Find(ID);

            if (dbEntry == null)
            {
                return;
            }
            context.ReportPartLikesByHour.RemoveRange(dbEntry.reportParts);
            context.ReportLikesByPeriod.Remove(dbEntry);
            await context.SaveChangesAsync();
        }
        public async Task UpdateAsync_New_TwitterReportLikesPePeriod_With_Invalid_UserName()
        {
            //Arrange - create a report
            TwitterReportLikesByPeriod report = new TwitterReportLikesByPeriod();

            //Arrange - set incorrect name
            report.UserName = "******";

            //Act
            bool result = await TweetsReportGenerator.UpdateReportAsync(report);

            //Assert
            Assert.AreEqual(result, false);
        }
        public async Task <ActionResult> UpdateAsync(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            TwitterReportLikesByPeriod twitterReportLikesByPeriod = repository.Reports.FirstOrDefault(r => r.ID == id);

            if (twitterReportLikesByPeriod != null)
            {
                await repository.UpdateReportAsync(twitterReportLikesByPeriod);
            }

            return(PartialView("UpdateReports", GetLastUserReports(10)));
        }
Esempio n. 7
0
 public async Task SaveReportAsync(TwitterReportLikesByPeriod report)
 {
     if (report.ID == 0)
     {
         context.ReportLikesByPeriod.Add(report);
         if (report.reportParts != null && report.reportParts.Count > 0)
         {
             context.ReportPartLikesByHour.AddRange(report.reportParts);
         }
         await context.SaveChangesAsync();
     }
     else
     {
         await UpdateReportAsync(report);
     }
 }
Esempio n. 8
0
 public void SaveReport(TwitterReportLikesByPeriod report)
 {
     if (report.ID == 0)
     {
         context.ReportLikesByPeriod.Add(report);
         if (report.reportParts != null && report.reportParts.Count > 0)
         {
             context.ReportPartLikesByHour.AddRange(report.reportParts);
         }
         context.SaveChanges();
     }
     else
     {
         UpdateReport(report);
         return;
     }
 }
Esempio n. 9
0
        public void UpdateReport(TwitterReportLikesByPeriod report)
        {
            if (report == null)
            {
                return;
            }
            TwitterReportLikesByPeriod dbEntry = context.ReportLikesByPeriod.Find(report.ID);

            if (dbEntry == null)
            {
                return;
            }
            IEnumerable <TwitterReportPartLikesByHour> oldCollection = dbEntry.reportParts;

            TweetsReportGenerator.UpdateReport(dbEntry);
            if (oldCollection != null)
            {
                context.ReportPartLikesByHour.RemoveRange(oldCollection);
            }
            context.ReportPartLikesByHour.AddRange(dbEntry.reportParts);
            context.SaveChanges();
        }
Esempio n. 10
0
        public async Task UpdateReportAsync(TwitterReportLikesByPeriod report)
        {
            if (report == null)
            {
                return;
            }
            TwitterReportLikesByPeriod dbEntry = context.ReportLikesByPeriod.Find(report.ID);

            if (dbEntry == null)
            {
                return;
            }
            IEnumerable <TwitterReportPartLikesByHour> oldCollection = dbEntry.reportParts;
            await TweetsReportGenerator.UpdateReportAsync(dbEntry);

            if (oldCollection != null)
            {
                context.ReportPartLikesByHour.RemoveRange(oldCollection);
            }
            context.ReportPartLikesByHour.AddRange(dbEntry.reportParts);
            //db.ReportLikesByPeriod.Add(twitterReportLikesByPeriod);
            await context.SaveChangesAsync();
        }