public void GetBucketsDescending_AreDescending()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: "TestAppDatabase")
                          .Options;

            var stub = Substitute.For <IHostingEnvironment>();
            ApplicationDbContext context = new ApplicationDbContext(options, stub);

            context.Database.EnsureCreated();
            context.Posts.AddRange(GenerateDoublePosts());
            context.SaveChanges();

            IBlogPostRepository repo  = new EFBlogPostRepository(context);
            ArchivesViewModel   model = new ArchivesViewModel(repo);
            var buckets = model.BucketsOfPostsDescending;

            Assert.IsTrue(buckets.Count > 0);
            Assert.IsTrue(buckets.Count == 9);

            for (int i = 0; i < buckets.Count - 1; i++)
            {
                Assert.IsTrue(buckets[i].MonthAndYear > buckets[i + 1].MonthAndYear);
            }

            context.Database.EnsureDeleted();
        }
        public void GetStaticPageById_NoRealId_ReturnsNull()
        {
            EFBlogPostRepository repo = new EFBlogPostRepository(SharedDbContext);

            var page = repo.GetStaticPageById(-4);

            Assert.IsNull(page);
        }
        public void GetPostByUrlTitle_IsNotThere_ReturnsNull()
        {
            EFBlogPostRepository repo = new EFBlogPostRepository(SharedDbContext);

            var notAPost = repo.GetPostByUrlTitle("Not-A-Real-Title");

            Assert.IsNull(notAPost);
        }
        public void GetStaticPagesByPriorityAscending_GetsAllPages()
        {
            EFBlogPostRepository repo = new EFBlogPostRepository(SharedDbContext);

            var pages = repo.GetStaticPagesByPriorityAscending();

            Assert.AreEqual(pages.Count(), 9);
        }
        public void GetMostRecentDrafts_TooManyInArgument_ReturnsLessThanFull()
        {
            EFBlogPostRepository repo = new EFBlogPostRepository(SharedDbContext);

            var drafts = repo.GetMostRecentDrafts(25).ToList();

            Assert.IsTrue(drafts.Count < 25);
        }
        public void GetPostByUrlTitle_IsThere_ReturnsRealPost()
        {
            EFBlogPostRepository repo = new EFBlogPostRepository(SharedDbContext);

            var post = repo.GetPostByUrlTitle("Post-Title-No-2");

            Assert.IsNotNull(post);
            Assert.AreEqual(post.PageTitle, "Post Title No 2");
        }
        public void GetPostByUrlTitle_AnyCaseWorks()
        {
            EFBlogPostRepository repo = new EFBlogPostRepository(SharedDbContext);

            var shouldGetPost = repo.GetPostByUrlTitle("POsT-tiTLe-NO-5");

            Assert.IsNotNull(shouldGetPost);
            Assert.AreEqual(shouldGetPost.PageTitle, "Post Title No 5");
        }
        public void GetStaticPageByUrlTitle_AnyCaseWorks()
        {
            EFBlogPostRepository repo = new EFBlogPostRepository(SharedDbContext);

            var shouldGetPage = repo.GetStaticPageByUrlTitle("STAtIc-pAgE-NO-5");

            Assert.IsNotNull(shouldGetPage);
            Assert.AreEqual(shouldGetPage.PageTitle, "Static Page No 5");
        }
        public void GetStaticPageByUrlTitle_IsThere_ReturnsRealPost()
        {
            EFBlogPostRepository repo = new EFBlogPostRepository(SharedDbContext);

            var page = repo.GetStaticPageByUrlTitle("Static-Page-No-2");

            Assert.IsNotNull(page);
            Assert.AreEqual(page.PageTitle, "Static Page No 2");
        }
        public void GetStaticPagesByPriorityAscending_OrderedByMainNav()
        {
            EFBlogPostRepository repo = new EFBlogPostRepository(SharedDbContext);

            var pages = repo.GetStaticPagesByPriorityAscending().ToList();

            for (int i = 0; i < pages.Count; i++)
            {
                Assert.IsTrue(pages[i].MainNavPriority <= pages[i].MainNavPriority);
            }
        }
        public void GetMostRecentDrafts_CountIsCorrect()
        {
            EFBlogPostRepository repo = new EFBlogPostRepository(SharedDbContext);

            var drafts = repo.GetMostRecentDrafts(7).ToList();

            Assert.AreEqual(7, drafts.Count);

            drafts = repo.GetMostRecentDrafts(4).ToList();
            Assert.AreEqual(4, drafts.Count);
        }
        public void GetFooterStaticPages_CorrectOrder()
        {
            EFBlogPostRepository repo = new EFBlogPostRepository(SharedDbContext);

            var pages = repo.GetFooterStaticPages().ToList();

            for (int i = 0; i < 3; i++)
            {
                Assert.IsTrue(pages[i].FooterPriority < pages[i + 1].FooterPriority);
            }
        }
        public void GetStaticPageById_GetsRealId()
        {
            EFBlogPostRepository repo = new EFBlogPostRepository(SharedDbContext);

            var page1 = repo.StaticPages.First();
            var id    = page1.StaticPageId;

            var page2 = repo.GetStaticPageById(id);

            Assert.AreEqual(page1, page2);
        }
        public void GetAllStaticPages_RetrievesAll()
        {
            EFBlogPostRepository repo = new EFBlogPostRepository(SharedDbContext);

            var pages  = repo.StaticPages;
            var titles = pages.Select(p => p.PageTitle).ToList();

            for (int i = 1; i < 10; i++)
            {
                Assert.Contains($"Static Page No {i}", titles);
            }
        }
        public void GetMostRecentDrafts_OrderIsCorrect()
        {
            EFBlogPostRepository repo = new EFBlogPostRepository(SharedDbContext);

            var ordered = repo.GetMostRecentDrafts(5).ToList();

            Assert.AreEqual(ordered.Count, 5);
            for (int i = 0; i < 4; i++)
            {
                Assert.IsTrue(ordered[i].LastEdit >= ordered[i + 1].LastEdit);
            }
        }
        public void GetMostRecentPosts_CountIsCorrect()
        {
            EFBlogPostRepository repo = new EFBlogPostRepository(SharedDbContext);

            var posts = repo.GetMostRecentPosts(8).ToList();

            Assert.AreEqual(posts.Count, 8);


            posts = repo.GetMostRecentPosts(6).ToList();
            Assert.AreEqual(posts.Count, 6);
        }
        public void GetAllPostsDescending_CorrectOrder()
        {
            EFBlogPostRepository repo = new EFBlogPostRepository(SharedDbContext);

            var shouldBeDescending = repo.GetAllPostsDescending().ToList();

            for (int i = 0; i < shouldBeDescending.Count - 1; i++)
            {
                Assert.IsTrue(shouldBeDescending[i].DateTimePublished
                              >= shouldBeDescending[i + 1].DateTimePublished);
            }
        }
        public void DeleteStaticPage_CountIsLess()
        {
            EFBlogPostRepository repo = new EFBlogPostRepository(SharedDbContext);

            var page = repo.StaticPages.First();

            var countBefore = repo.StaticPages.Count();

            repo.DeleteStaticPage(page);

            Assert.IsTrue(repo.StaticPages.Count() < countBefore);
        }
        public void GetDrafts_DraftsAreThere()
        {
            EFBlogPostRepository repo = new EFBlogPostRepository(SharedDbContext);

            Assert.AreEqual(repo.Drafts.Count(), 9);

            var titles = repo.Drafts.Select(p => p.PageTitle).ToList();

            for (int i = 1; i < 10; i++)
            {
                Assert.Contains($"Draft Title No {i}", titles);
            }
        }
        public void GetAllCategoriesUsed_DraftsToo_RightCountAndVals()
        {
            EFBlogPostRepository repo = new EFBlogPostRepository(SharedDbContext);

            var cats = repo.GetAllCategoriesUsed(false);

            Assert.AreEqual(cats.Count, 20);
            Assert.Contains("OtherDraftCat", cats);
            for (int i = 1; i < 10; i++)
            {
                Assert.Contains("DraftCat" + i, cats);
            }
        }
        public void GetAllCategoriesUsed_PostsOnly_RightCountAndVals()
        {
            EFBlogPostRepository repo = new EFBlogPostRepository(SharedDbContext);

            var cats = repo.GetAllCategoriesUsed(true);

            Assert.AreEqual(cats.Count, 10);
            Assert.Contains("OtherCat", cats);
            for (int i = 1; i < 10; i++)
            {
                Assert.Contains("Cat" + i, cats);
            }
        }
        public void DeleteStaticPage_Removes()
        {
            EFBlogPostRepository repo = new EFBlogPostRepository(SharedDbContext);

            var page = repo.StaticPages.First();
            var id   = page.StaticPageId;

            repo.DeleteStaticPage(page);

            var shouldBeNull = repo.GetStaticPageById(id);

            Assert.IsNull(shouldBeNull);
        }
        public void PublishDraftToPost_AddsToPosts()
        {
            EFBlogPostRepository repo = new EFBlogPostRepository(SharedDbContext);

            var draft = repo.Drafts.First();
            var title = draft.PageTitle;

            repo.PublishDraftToPost(draft);

            var shouldBePost = repo.Posts.Where(d => d.PageTitle == title).FirstOrDefault();

            Assert.IsNotNull(shouldBePost);
        }
        public void UnPublishPostToDraft_RemovesPost()
        {
            EFBlogPostRepository repo = new EFBlogPostRepository(SharedDbContext);

            var published = repo.Posts.First();
            var title     = published.PageTitle;

            repo.UnPublishPostToDraft(published);

            var shouldBeNull = repo.Posts.Where(p => p.PageTitle == title).FirstOrDefault();

            Assert.IsNull(shouldBeNull);
        }
        public void PublishDraftToPost_RemovesFromDrafts()
        {
            EFBlogPostRepository repo = new EFBlogPostRepository(SharedDbContext);

            var draft = repo.Drafts.First();
            var id    = draft.BlogPostTemplateId;

            repo.PublishDraftToPost(draft);

            var shouldBeNull = repo.Drafts.Where(d => d.BlogPostTemplateId == id).FirstOrDefault();

            Assert.IsNull(shouldBeNull);
        }
        public void DeleteDraft_RemovesFromSet()
        {
            EFBlogPostRepository repo = new EFBlogPostRepository(SharedDbContext);

            var id    = repo.Drafts.Select(d => d.BlogPostTemplateId).First();
            var draft = repo.GetDraftById(id);
            var title = draft.PageTitle;

            repo.DeleteDraft(draft);

            var shouldBeNull = repo.GetDraftById(id);

            Assert.IsNull(shouldBeNull);
        }
        public void SaveStaticPage_IdZeroAndUrlAlreadyExists_CountIsSame()
        {
            EFBlogPostRepository repo = new EFBlogPostRepository(SharedDbContext);
            var page = new StaticPage
            {
                PageTitle    = "Static Page No 4",
                StaticPageId = 0,
                FullContent  = "content"
            };
            var countBefore = repo.StaticPages.Count();
            var result      = repo.SaveStaticPage(page);

            Assert.AreEqual(countBefore, repo.StaticPages.Count());
        }
        public void SaveStaticPage_IdZeroAndUrlAlreadyExists_ReturnsFalse()
        {
            EFBlogPostRepository repo = new EFBlogPostRepository(SharedDbContext);
            var page = new StaticPage
            {
                PageTitle    = "Static Page No 4",
                StaticPageId = 0,
                FullContent  = "content"
            };

            var result = repo.SaveStaticPage(page);

            Assert.IsFalse(result);
        }
        public void SaveStaticPage_ZeroId_ReturnsTrue()
        {
            EFBlogPostRepository repo = new EFBlogPostRepository(SharedDbContext);
            var page = new StaticPage
            {
                PageTitle    = "Title",
                StaticPageId = 0,
                FullContent  = "content"
            };

            var result = repo.SaveStaticPage(page);

            Assert.IsTrue(result);
        }
        public void SaveStaticPage_ZeroId_AddsNew()
        {
            EFBlogPostRepository repo = new EFBlogPostRepository(SharedDbContext);
            var page = new StaticPage
            {
                PageTitle    = "Title",
                StaticPageId = 0,
                FullContent  = "content"
            };

            var countBefore = repo.StaticPages.Count();

            repo.SaveStaticPage(page);
            Assert.IsTrue(repo.StaticPages.Count() > countBefore);
        }