Exemplo n.º 1
0
        public FakeNewsServer(Dictionary <int, int[]> channelsByAccount)
        {
            var endDate   = DateTime.Now.AddHours(-1);
            var startDate = endDate.AddDays(-40);

            this.channelsByAccount = channelsByAccount;
            var channels = channelsByAccount.SelectMany(ca => ca.Value).Distinct();

            newsByChannel = channels.ToDictionary(c => c,
                                                  c => NewsMaker.MakeSomeNews(c, startDate, endDate, 1,
                                                                              random.Next(100) < 50
                                                                                  ? random.Next(1, 10)
                                                                                  : random.Next(2, 1440)));
        }
Exemplo n.º 2
0
        private Dictionary <int, List <News> > MakeSomeNews()
        {
            // генерировать новости
            var startDate = DateTime.Now.Date.AddDays(-30);
            var endDate   = DateTime.Now.AddHours(-6);
            var allNews   = new Dictionary <int, List <News> >
            {
                { 1, NewsMaker.MakeSomeNews(1, startDate, endDate, 1, 5) },
                { 3, NewsMaker.MakeSomeNews(3, startDate.AddDays(5), endDate, 1, 1440) },
                { 4, NewsMaker.MakeSomeNews(4, startDate, endDate, 1440, 1440) }
            };

            // запихнуть в хранилище
            foreach (var pair in allNews)
            {
                NewsLocalStorage.Instance.UpdateNews(pair.Key, pair.Value);
            }

            return(allNews);
        }
Exemplo n.º 3
0
        public void TestSaveLoad()
        {
            // перечитать
            NewsLocalStorage.ReInstantiate();

            var newsEmpty = NewsLocalStorage.Instance.GetNews(1);

            Assert.IsNull(newsEmpty, "no cache - no news");

            var startDate = DateTime.Now.Date.AddDays(-30);
            var endDate   = DateTime.Now.AddHours(-6);
            var allNews   = new Dictionary <int, List <News> >
            {
                { 1, NewsMaker.MakeSomeNews(1, startDate, endDate, 1, 1440) },
                { 3, NewsMaker.MakeSomeNews(3, startDate.AddDays(5), endDate, 1, 1440) },
                { 4, NewsMaker.MakeSomeNews(4, startDate, endDate, 1440, 1440) }
            };

            // запись новостей в пустое хранилище
            const int chanId = 1;

            NewsLocalStorage.Instance.UpdateNews(chanId, allNews[chanId]);
            var newsChan1 = NewsLocalStorage.Instance.GetNews(chanId);

            Assert.IsNotNull(newsChan1, "simple update & get: not null");
            Assert.AreEqual(allNews[chanId].Count, newsChan1.Count, "simple update & get: same count");
            var indexSpeciman = newsChan1.Count / 2;
            var newsOrig      = allNews[chanId][indexSpeciman];
            var newsNew       = newsChan1[indexSpeciman];

            Assert.IsTrue(newsOrig.ChannelId == newsNew.ChannelId &&
                          newsOrig.Body == newsNew.Body && newsOrig.Time == newsNew.Time &&
                          newsOrig.Title == newsNew.Title, "simple update & get: same news item");

            // добавить все новости
            NewsLocalStorage.Instance.UpdateNews(allNews[1]);
            NewsLocalStorage.Instance.UpdateNews(allNews[3]);
            NewsLocalStorage.Instance.UpdateNews(allNews[4]);

            // проверить количество новостей
            foreach (var pair in allNews)
            {
                Assert.AreEqual(pair.Value.Count, NewsLocalStorage.Instance.GetNews(pair.Key).Count, "updated news: got all");
            }

            // сохранить новости
            try
            {
                NewsLocalStorage.Instance.SaveNewsInFiles();
            }
            catch (Exception ex)
            {
                Assert.Fail("NewsLocalStorage.Instance.SaveNewsInFiles(): " + ex);
            }

            // перечитать
            NewsLocalStorage.ReInstantiate();

            // перечитать сохраненные новости
            foreach (var pair in allNews)
            {
                Assert.AreEqual(pair.Value.Count, NewsLocalStorage.Instance.GetNews(pair.Key).Count, "reloaded news: got all");
            }
        }