public void DeleteArticle_Valid_Id() { using (TransactionScope transaction = new TransactionScope()) { articleRepository = new ArticlesRepository(); int count = articleRepository.GetAllArticles().Count(); //Count articles pre-editting of existing article SMCC.Models.Article originalArticle = new SMCC.Models.Article(); SMCC.Models.Article deleteArticle = new SMCC.Models.Article(); List <SMCC.Models.Article> existArticles = new List <SMCC.Models.Article>(); try { //Set your controller ControllerContext with fake context articleController.ControllerContext = MockContext().Object; originalArticle = articleRepository.GetArticle(1); var result = articleController.Delete(1) as ViewResult; deleteArticle = (SMCC.Models.Article)result.ViewData.Model; articleController.DeleteConfirmed(deleteArticle.ArticleId); } finally { articleRepository = new ArticlesRepository(); existArticles = articleRepository.GetAllArticles().ToList(); Assert.AreEqual(count - 1, existArticles.Count()); //Record count should be deducted by 1 after deletion CollectionAssert.DoesNotContain(existArticles, originalArticle); // Article should no longer be present in db. } } }
public void CreateArticle_Null_Content_Invalid() { using (TransactionScope transaction = new TransactionScope()) { articleRepository = new ArticlesRepository(); int count = articleRepository.GetAllArticles().Count(); //Count articles pre-creation of new article SMCC.Models.Article newArticle = new SMCC.Models.Article(); SMCC.Models.Article createdArticle = new SMCC.Models.Article(); List <SMCC.Models.Article> existArticles = new List <SMCC.Models.Article>(); try { //Set your controller ControllerContext with fake context articleController.ControllerContext = MockContext().Object; newArticle.Title = "A"; newArticle.Text = null; articleController.Create(newArticle); } finally { articleRepository = new ArticlesRepository(); existArticles = articleRepository.GetAllArticles().ToList(); createdArticle = articleRepository.GetArticle(newArticle.ArticleId); Assert.AreEqual(count, existArticles.Count()); CollectionAssert.DoesNotContain(existArticles, createdArticle); } } }
//Searching And Paging // GET: Articles public ActionResult Index(string searchString, string currentFilter, int?page) { ArticlesRepository ArticlesRepo = new ArticlesRepository(); if (searchString != null) { page = 1; } else { searchString = currentFilter; } ViewBag.currentFilter = searchString; var articles = from posts in ArticlesRepo.GetAllArticles() select posts; if (!String.IsNullOrEmpty(searchString)) { articles = articles.Where(search => search.Title.Contains(searchString)); } int pageSize = 3; int pageNumber = (page ?? 1); return(View(articles.ToPagedList(pageNumber, pageSize))); //return View(ArticlesRepo.GetAllArticles()); }
public void DeleteArticle_Negative_Id_Invalid() { using (TransactionScope transaction = new TransactionScope()) { articleRepository = new ArticlesRepository(); int count = articleRepository.GetAllArticles().Count(); //Count articles pre-deletion of article List <SMCC.Models.Article> existArticles = new List <SMCC.Models.Article>(); var result = (HttpNotFoundResult)articleController.Delete(-1); articleRepository = new ArticlesRepository(); existArticles = articleRepository.GetAllArticles().ToList(); Assert.AreEqual(404, result.StatusCode); //404 - Page Not Found Assert.AreEqual(count, existArticles.Count()); //Record count should remain the same after editting } }
public void EditArticle_Long_Valid() { using (TransactionScope transaction = new TransactionScope()) { articleRepository = new ArticlesRepository(); int count = articleRepository.GetAllArticles().Count(); //Count articles pre-editting of existing article SMCC.Models.Article originalArticle = new SMCC.Models.Article(); SMCC.Models.Article modArticle = new SMCC.Models.Article(); List <SMCC.Models.Article> existArticles = new List <SMCC.Models.Article>(); try { //Set your controller ControllerContext with fake context articleController.ControllerContext = MockContext().Object; originalArticle = articleRepository.GetArticle(1); var result = articleController.Edit(1) as ViewResult; modArticle = (SMCC.Models.Article)result.ViewData.Model; modArticle.Title = ""; for (int i = 0; i < 50; i++) { modArticle.Title += "A"; } modArticle.Text = "A"; articleController.Edit(modArticle); } finally { articleRepository = new ArticlesRepository(); existArticles = articleRepository.GetAllArticles().ToList(); Assert.AreEqual(count, existArticles.Count()); //Record count should remain the same after editting SMCC.Models.Article originalArticle_Modified = new SMCC.Models.Article(); originalArticle_Modified = articleRepository.GetArticle(1); Assert.IsFalse(AssertSameArticles(originalArticle, originalArticle_Modified)); //Article content should change, hence return false Assert.IsTrue(AssertModifiedArticles(originalArticle, originalArticle_Modified)); } } }
public JsonResult GetTags(string text) { var repo = new ArticlesRepository(); List <string> tags = new List <string>(); foreach (var article in repo.GetAllArticles()) { var tagsInArticle = repo.Hashtags(article); foreach (var tag in tagsInArticle) { if (!tags.Contains(tag.HashtagWord)) { tags.Add(tag.HashtagWord); } } } var word = text.Split(' ').Last(); var newTags = tags.Where(x => x.ToLower().Contains(word.ToLower())).ToList(); return(Json(newTags, JsonRequestBehavior.AllowGet)); }
public void CreateArticle_Long_Valid() { using (TransactionScope transaction = new TransactionScope()) { articleRepository = new ArticlesRepository(); int count = articleRepository.GetAllArticles().Count(); //Count articles pre-creation of new article SMCC.Models.Article newArticle = new SMCC.Models.Article(); SMCC.Models.Article createdArticle = new SMCC.Models.Article(); List <SMCC.Models.Article> existArticles = new List <SMCC.Models.Article>(); try { //Set your controller ControllerContext with fake context articleController.ControllerContext = MockContext().Object; for (int i = 0; i < 50; i++) { newArticle.Title += "A"; } newArticle.Text = "A"; articleController.Create(newArticle); } finally { articleRepository = new ArticlesRepository(); existArticles = articleRepository.GetAllArticles().ToList(); createdArticle = articleRepository.GetArticle(newArticle.ArticleId); Assert.AreEqual(count + 1, existArticles.Count()); CollectionAssert.Contains(existArticles, createdArticle); } } }
// GET: Articles/Edit/5 public ActionResult Edit(int Id) { ArticlesRepository ArticlesRepo = new ArticlesRepository(); return(View(ArticlesRepo.GetAllArticles().Find(article => article.Id == Id))); }