Skip to content

Commit 0a630b0

Browse files
committed
Merge branch 'master' of github.com:Guzzter/BookCollection
2 parents d8fec4c + 430f936 commit 0a630b0

19 files changed

+261
-120
lines changed

BookCollection.Tests/BookCollection.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292
<Compile Include="DAL\BookSeederTest.cs" />
9393
<Compile Include="Helpers\ConvertersTest.cs" />
9494
<Compile Include="Helpers\ValidatorsTest.cs" />
95+
<Compile Include="MockExtensions.cs" />
9596
<Compile Include="Properties\AssemblyInfo.cs" />
9697
<Compile Include="Controllers\HomeControllerTest.cs" />
9798
</ItemGroup>

BookCollection.Tests/Controllers/HomeControllerTest.cs

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,101 @@
66
using Microsoft.VisualStudio.TestTools.UnitTesting;
77
using BookCollection;
88
using BookCollection.Controllers;
9+
using BookCollection.DAL;
10+
using BookCollection.Logging;
11+
using Moq;
12+
using BookCollection.Models;
13+
using BookCollection.ViewModels;
914

1015
namespace BookCollection.Tests.Controllers
1116
{
1217
[TestClass]
1318
public class HomeControllerTest
1419
{
20+
Mock<IBookContext> _bookContext = new Mock<IBookContext>();
21+
Mock<IBookRepository> _bookRepo = new Mock<IBookRepository>();
22+
Mock<ILogger> _log = new Mock<ILogger>();
23+
24+
[TestInitialize]
25+
public void Init()
26+
{
27+
_bookContext = new Mock<IBookContext>();
28+
_bookRepo = new Mock<IBookRepository>();
29+
_log = new Mock<ILogger>();
30+
31+
}
32+
33+
/// <summary>
34+
/// Generic helper to create a IQueryable list of certain type
35+
/// </summary>
36+
/// <typeparam name="T">Type needed</typeparam>
37+
/// <param name="count">Amount of items needed</param>
38+
/// <param name="prefix">Prefix title that can be used in a Name, Lastname or Title property to identify a unique item</param>
39+
/// <returns></returns>
40+
private IQueryable<T> GetDummies<T>(int count, string prefix = null)
41+
{
42+
var items = new List<T>();
43+
if (string.IsNullOrWhiteSpace(prefix))
44+
{
45+
prefix = typeof(T).Name;
46+
}
47+
for (int i = 1; i <= count; i++)
48+
{
49+
var obj = Activator.CreateInstance<T>();
50+
if (typeof(T).GetProperty("Title") != null)
51+
{
52+
typeof(T).GetProperty("Title").SetValue(obj, prefix + " " + i);
53+
}
54+
if (typeof(T).GetProperty("Name") != null)
55+
{
56+
typeof(T).GetProperty("Name").SetValue(obj, prefix + " " + i);
57+
}
58+
if (typeof(T).GetProperty("Lastname") != null)
59+
{
60+
typeof(T).GetProperty("Lastname").SetValue(obj, prefix + " " + i);
61+
}
62+
63+
items.Add(obj);
64+
}
65+
66+
return items.ToArray().AsQueryable();
67+
}
68+
69+
1570
[TestMethod]
1671
public void Index()
1772
{
73+
var books = GetDummies<Book>(5, "Book Title");
74+
/*_bookContext.Setup(bc => bc.Query<Book>()).Returns(books);
75+
var authors = GetDummies<Author>(3);
76+
_bookContext.Setup(bc => bc.Query<Author>()).Returns(authors);
77+
var subjects = GetDummies<Subject>(5);
78+
_bookContext.Setup(bc => bc.Query<Subject>()).Returns(subjects);
79+
var pubs = GetDummies<Publisher>(2);
80+
_bookContext.Setup(bc => bc.Query<Publisher>()).Returns(pubs);
81+
*/
82+
_bookRepo.Setup(br => br.GetMostRecentBooks(5)).Returns(books.Take(5));
83+
1884
// Arrange
19-
HomeController controller = new HomeController();
85+
HomeController controller = new HomeController(_bookRepo.Object, _bookContext.Object);
2086

2187
// Act
2288
ViewResult result = controller.Index() as ViewResult;
2389

2490
// Assert
2591
Assert.IsNotNull(result);
92+
93+
var tagCloud = result.ViewBag.CatTagCloud as IEnumerable<CategoryGroup>;
94+
Assert.IsNotNull(tagCloud);
95+
96+
var serieTagCloud = result.ViewBag.SerieTagCloud as IEnumerable<CategoryGroup>;
97+
Assert.IsNotNull(serieTagCloud);
98+
99+
var mostRecent = (result.ViewBag.MostRecent as IEnumerable<Book>).ToList();
100+
Assert.IsNotNull(mostRecent);
101+
Assert.AreEqual(5, mostRecent.Count());
102+
Assert.AreEqual("Book Title 1", mostRecent[0].Title);
103+
26104
}
27105

28106
/*[TestMethod]

BookCollection.Tests/DAL/BookSeederTest.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using BookCollection.Logging;
66
using Moq;
77
using BookCollection.Models;
8+
using BookCollection.DAL.SeedData;
89

910
namespace BookCollection.Tests.DAL
1011
{
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using Moq;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace BookCollection.Tests
9+
{
10+
public static class MockExtensions
11+
{
12+
public static void SetupIQueryable<T>(this Mock<T> mock, IQueryable queryable)
13+
where T : class, IQueryable
14+
{
15+
mock.Setup(r => r.GetEnumerator()).Returns(queryable.GetEnumerator());
16+
mock.Setup(r => r.Provider).Returns(queryable.Provider);
17+
mock.Setup(r => r.ElementType).Returns(queryable.ElementType);
18+
mock.Setup(r => r.Expression).Returns(queryable.Expression);
19+
}
20+
}
21+
}

BookCollection/BookCollection.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@
214214
<Compile Include="Helpers\Converters.cs" />
215215
<Compile Include="Helpers\CustomHtmlHelper.cs" />
216216
<Compile Include="Helpers\CustomWebViewPage.cs" />
217-
<Compile Include="Helpers\DownloadFileActionResult.cs" />
217+
<Compile Include="Helpers\DownloadExcelFileActionResult.cs" />
218218
<Compile Include="Helpers\ImageResult.cs" />
219219
<Compile Include="Helpers\ImageUpload.cs" />
220220
<Compile Include="Helpers\Validators.cs" />

BookCollection/Controllers/AuthorsController.cs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ namespace BookCollection.Controllers
1414
{
1515
public class AuthorsController : BaseController
1616
{
17+
public AuthorsController(IBookRepository rep, IBookContext bc) : base(rep, bc) { /* constructor forward to BaseController */ }
18+
1719
// GET: Authors
1820
public ActionResult Index(string sortOrder, string currentFilter, string searchString, int? page, bool noPaging = false)
1921
{
@@ -31,7 +33,7 @@ public ActionResult Index(string sortOrder, string currentFilter, string searchS
3133

3234
ViewBag.CurrentFilter = searchString;
3335

34-
var authors = from s in db.Authors
36+
var authors = from s in db.Query<Author>()
3537
select s;
3638
if (!string.IsNullOrEmpty(searchString))
3739
{
@@ -59,14 +61,14 @@ public ActionResult Details(int? id)
5961
{
6062
return RedirectToAction("Index");
6163
}
62-
Author author = db.Authors.Find(id);
64+
Author author = db.Find<Author>(id);
6365
if (author == null)
6466
{
6567
return HttpNotFound();
6668
}
6769
else
6870
{
69-
author.Books = db.Books.Where(b => b.AuthorID == author.AuthorID).ToList();
71+
author.Books = db.Query<Book>().Where(b => b.AuthorID == author.AuthorID).ToList();
7072
}
7173
return View(author);
7274
}
@@ -86,7 +88,7 @@ public ActionResult Create([Bind(Include = "AuthorID,Lastname,Firstname")] Autho
8688
{
8789
if (ModelState.IsValid)
8890
{
89-
db.Authors.Add(author);
91+
db.Add(author);
9092
db.SaveChanges();
9193
return RedirectToAction("Index");
9294
}
@@ -101,7 +103,7 @@ public ActionResult Edit(int? id)
101103
{
102104
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
103105
}
104-
Author author = db.Authors.Find(id);
106+
Author author = db.Find<Author>(id);
105107
if (author == null)
106108
{
107109
return HttpNotFound();
@@ -118,7 +120,7 @@ public ActionResult Edit([Bind(Include = "AuthorID,Lastname,Firstname")] Author
118120
{
119121
if (ModelState.IsValid)
120122
{
121-
db.Entry(author).State = EntityState.Modified;
123+
db.SetState(author, EntityState.Modified);
122124
db.SaveChanges();
123125
return RedirectToAction("Index");
124126
}
@@ -132,7 +134,7 @@ public ActionResult Delete(int? id)
132134
{
133135
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
134136
}
135-
Author author = db.Authors.Find(id);
137+
Author author = db.Find<Author>(id);
136138
if (author == null)
137139
{
138140
return HttpNotFound();
@@ -145,8 +147,8 @@ public ActionResult Delete(int? id)
145147
[ValidateAntiForgeryToken]
146148
public ActionResult DeleteConfirmed(int id)
147149
{
148-
Author author = db.Authors.Find(id);
149-
db.Authors.Remove(author);
150+
Author author = db.Find<Author>(id);
151+
db.Remove(author);
150152
db.SaveChanges();
151153
return RedirectToAction("Index");
152154
}

BookCollection/Controllers/BaseController.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ namespace BookCollection.Controllers
1010
public abstract class BaseController : Controller
1111
{
1212
protected readonly IBookRepository repo;
13-
protected readonly BookContext db;
13+
protected readonly IBookContext db;
1414

15-
protected BaseController() : this(new BookRepository(), new BookContext())
15+
/*protected BaseController() : this(new BookRepository(), new BookContext())
1616
{
1717
1818
}
19-
20-
protected BaseController(IBookRepository rep, BookContext bc)
19+
*/
20+
protected BaseController(IBookRepository rep, IBookContext bc)
2121
{
2222
repo = rep;
2323
repo.SetContext(bc);

BookCollection/Controllers/BooksController.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public class BooksController : Controller
1717
{
1818
private readonly IBookContext _db;
1919

20+
//TODO use base controller
2021
public BooksController(IBookContext dbContext)
2122
{
2223
_db = dbContext;

BookCollection/Controllers/CategoriesController.cs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ namespace BookCollection.Controllers
1414
{
1515
public class CategoriesController : BaseController
1616
{
17+
public CategoriesController(IBookRepository rep, IBookContext bc) : base(rep, bc) { /* constructor forward to BaseController */ }
18+
1719
// GET: Categories
1820
public ActionResult Index(string sortOrder, string currentFilter, string searchString, int? page, bool noPaging = false)
1921
{
@@ -31,7 +33,7 @@ public ActionResult Index(string sortOrder, string currentFilter, string searchS
3133

3234
ViewBag.CurrentFilter = searchString;
3335

34-
var cats = from s in db.Categories
36+
var cats = from s in db.Query<Category>()
3537
select s;
3638
if (!string.IsNullOrEmpty(searchString))
3739
{
@@ -59,13 +61,13 @@ public ActionResult Details(int? id)
5961
{
6062
return RedirectToAction("Index");
6163
}
62-
Category category = db.Categories.Find(id);
64+
Category category = db.Find<Category>(id);
6365
if (category == null)
6466
{
6567
return HttpNotFound();
6668
}else
6769
{
68-
category.Books = db.Books.Where(b => b.CategoryID == category.CategoryID).ToList();
70+
category.Books = db.Query<Book>().Where(b => b.CategoryID == category.CategoryID).ToList();
6971
}
7072
return View(category);
7173
}
@@ -76,14 +78,14 @@ public ActionResult DetailsForName(string name)
7678
{
7779
return RedirectToAction("Index");
7880
}
79-
Category category = db.Categories.FirstOrDefault(b => b.Title == name);
81+
Category category = db.Query<Category>().FirstOrDefault(b => b.Title == name);
8082
if (category == null)
8183
{
8284
return HttpNotFound();
8385
}
8486
else
8587
{
86-
category.Books = db.Books.Where(b => b.CategoryID == category.CategoryID).ToList();
88+
category.Books = db.Query<Book>().Where(b => b.CategoryID == category.CategoryID).ToList();
8789
}
8890
return View("Details", category);
8991
}
@@ -103,7 +105,7 @@ public ActionResult Create([Bind(Include = "CategoryID,Title")] Category categor
103105
{
104106
if (ModelState.IsValid)
105107
{
106-
db.Categories.Add(category);
108+
db.Add(category);
107109
db.SaveChanges();
108110
return RedirectToAction("Index");
109111
}
@@ -118,7 +120,7 @@ public ActionResult Edit(int? id)
118120
{
119121
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
120122
}
121-
Category category = db.Categories.Find(id);
123+
Category category = db.Find<Category>(id);
122124
if (category == null)
123125
{
124126
return HttpNotFound();
@@ -135,7 +137,7 @@ public ActionResult Edit([Bind(Include = "CategoryID,Title")] Category category)
135137
{
136138
if (ModelState.IsValid)
137139
{
138-
db.Entry(category).State = EntityState.Modified;
140+
db.SetState(category, EntityState.Modified);
139141
db.SaveChanges();
140142
return RedirectToAction("Index");
141143
}
@@ -149,7 +151,7 @@ public ActionResult Delete(int? id)
149151
{
150152
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
151153
}
152-
Category category = db.Categories.Find(id);
154+
Category category = db.Find<Category>(id);
153155
if (category == null)
154156
{
155157
return HttpNotFound();
@@ -162,8 +164,8 @@ public ActionResult Delete(int? id)
162164
[ValidateAntiForgeryToken]
163165
public ActionResult DeleteConfirmed(int id)
164166
{
165-
Category category = db.Categories.Find(id);
166-
db.Categories.Remove(category);
167+
Category category = db.Find<Category>(id);
168+
db.Remove(category);
167169
db.SaveChanges();
168170
return RedirectToAction("Index");
169171
}

0 commit comments

Comments
 (0)