Mercurial > silverbladetech
comparison Library/LibrarySystem.Tests/Core/LibraryTests.cs @ 43:aef06698d9e2 Test
Added the library tests
author | Steven Hollidge <stevenhollidge@hotmail.com> |
---|---|
date | Wed, 04 Apr 2012 19:20:20 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
42:0d4aff4f699d | 43:aef06698d9e2 |
---|---|
1 using System; | |
2 using System.Collections.Generic; | |
3 using LibrarySystem.Core; | |
4 using LibrarySystem.Interfaces; | |
5 using NUnit.Framework; | |
6 | |
7 namespace LibrarySystem.Tests.Core | |
8 { | |
9 [TestFixture] | |
10 public class LibraryTests | |
11 { | |
12 #region Factory methods to setup fixed scenario for testing | |
13 | |
14 // Factory that creates: | |
15 // A library object with 10 books (2 x Lord of the Flies, 3 x 1984, 5 x Brave New World) | |
16 private ILibrary CreateLibraryScenario() | |
17 { | |
18 #region Publisher | |
19 | |
20 IPublisher penguin = Publisher.Create("Penguin"); | |
21 | |
22 #endregion | |
23 | |
24 #region Publications | |
25 | |
26 IPublication lordOfTheFlies = Publication.Create("William Golding", "0140283331", penguin, "Lord of the Flies"); | |
27 | |
28 IPublication nineteenEightyFour = Publication.Create("George Orwell", "0452284236", penguin, "Nineteen Eighty-Four"); | |
29 | |
30 IPublication braveNewWorld = Publication.Create("Aldous Huxley", "0060850523", penguin, "Brave New World"); | |
31 | |
32 #endregion | |
33 | |
34 #region Books | |
35 | |
36 // Lord of the flies | |
37 IBook book1 = Book.Create("0000001", lordOfTheFlies); | |
38 IBook book2 = Book.Create("0000002", lordOfTheFlies); | |
39 IBook book3 = Book.Create("0000003", lordOfTheFlies); | |
40 | |
41 // 1984 | |
42 IBook book4 = Book.Create("0000004", nineteenEightyFour); | |
43 IBook book5 = Book.Create("0000005", nineteenEightyFour); | |
44 | |
45 // Brave new world | |
46 IBook book6 = Book.Create("0000006", braveNewWorld); | |
47 IBook book7 = Book.Create("0000007", braveNewWorld); | |
48 IBook book8 = Book.Create("0000008", braveNewWorld); | |
49 IBook book9 = Book.Create("0000009", braveNewWorld); | |
50 IBook book10 = Book.Create("0000010", braveNewWorld); | |
51 | |
52 #endregion | |
53 | |
54 #region Library | |
55 | |
56 ILibrary library = Library.Create("Leicester Square"); | |
57 library.AddStock(book1, book2, book3, book4, book5, book6, book7, book8, book9, book10); | |
58 | |
59 #endregion | |
60 | |
61 return library; | |
62 } | |
63 | |
64 public IBook CreateBook(string bookId = "dummy") | |
65 { | |
66 IPublisher publisher = Publisher.Create("dummy"); | |
67 IPublication publication = Publication.Create("dummy", "dummy", publisher, "dummy"); | |
68 IBook book = Book.Create(bookId, publication); | |
69 return book; | |
70 } | |
71 | |
72 #endregion | |
73 | |
74 #region Create | |
75 | |
76 [Test] | |
77 public void Create_WithNullLocation_ThrowsArgumentNullException() | |
78 { | |
79 Assert.Throws<ArgumentNullException>(() => Library.Create(null)); | |
80 } | |
81 | |
82 #endregion | |
83 | |
84 #region AddStock | |
85 | |
86 [Test] | |
87 public void AddStock_WithNullBook_ShouldThrowArgumentNullException() | |
88 { | |
89 var library = CreateLibraryScenario(); | |
90 Assert.Throws<ArgumentNullException>(() => library.AddStock(null)); | |
91 } | |
92 | |
93 [Test] | |
94 public void AddStock_WithOneValidBookAndOneNullBook_ShouldThrowArgumentNullException() | |
95 { | |
96 var library = CreateLibraryScenario(); | |
97 var validBook = CreateBook(); | |
98 Assert.Throws<ArgumentNullException>(() => library.AddStock(validBook, null)); | |
99 } | |
100 | |
101 [Test] | |
102 public void AddStock_WithValidBook_ShouldIncreaseStockListCountByOne() | |
103 { | |
104 //arrange | |
105 var library = CreateLibraryScenario(); | |
106 var expected = library.StockList.Count + 1; | |
107 var dummyBook = CreateBook(); | |
108 | |
109 //act | |
110 library.AddStock(dummyBook); | |
111 var actual = library.StockList.Count; | |
112 | |
113 //assert | |
114 Assert.AreEqual(expected, actual); | |
115 } | |
116 | |
117 [Test] | |
118 public void AddStock_WithBookIdAlreadyInStock_ShouldThrowException() | |
119 { | |
120 //arrange | |
121 var library = CreateLibraryScenario(); | |
122 var idThatIsAlreadyInStock = "0000001"; | |
123 var dummyBook = CreateBook(idThatIsAlreadyInStock); | |
124 | |
125 //act | |
126 Assert.Throws<Exception>( | |
127 () => library.AddStock(dummyBook), | |
128 string.Format("Book with {0} is already in stock", idThatIsAlreadyInStock)); | |
129 } | |
130 | |
131 [Test] | |
132 public void AddStock_WithValidBook_ShouldMakeBookAvailable() | |
133 { | |
134 //arrange | |
135 var library = CreateLibraryScenario(); | |
136 var id = "0000001"; | |
137 var dummyBook = CreateBook(); | |
138 | |
139 //act | |
140 library.AddStock(dummyBook); | |
141 var lookupBook = ((List<IStock>) library.StockList).Find(s => s.Book.Id == id); | |
142 var actual = lookupBook.IsAvailable; | |
143 | |
144 //assert | |
145 Assert.IsTrue(actual); | |
146 } | |
147 | |
148 #endregion | |
149 | |
150 #region IsBookAvailableByISBN | |
151 | |
152 [Test] | |
153 public void IsBookAvailableByISBN_WithNullISBN_ShouldThrowArgumentNullExpection() | |
154 { | |
155 var library = CreateLibraryScenario(); | |
156 Assert.Throws<ArgumentNullException>(() => library.IsBookAvailableByISBN(null)); | |
157 } | |
158 | |
159 [Test] | |
160 public void IsBookAvailableByISBN_WithValidISBN_ShouldReturnTrue() | |
161 { | |
162 var library = CreateLibraryScenario(); | |
163 | |
164 // Lord of the flies | |
165 var validISBN = "0140283331"; | |
166 | |
167 var actual = library.IsBookAvailableByISBN(validISBN); | |
168 | |
169 Assert.IsTrue(actual); | |
170 } | |
171 | |
172 [Test] | |
173 public void IsBookAvailableByISBN_WithUnknownISBN_ShouldReturnFalse() | |
174 { | |
175 var library = CreateLibraryScenario(); | |
176 | |
177 // rubbish data | |
178 var unknownISBN = "X"; | |
179 | |
180 var actual = library.IsBookAvailableByISBN(unknownISBN); | |
181 | |
182 Assert.IsFalse(actual); | |
183 } | |
184 | |
185 #endregion | |
186 | |
187 #region FindBooks | |
188 | |
189 [Test] | |
190 public void FindBooks_WhenNullSearchString_ShouldThrowArgumentNullExpection() | |
191 { | |
192 var library = CreateLibraryScenario(); | |
193 Assert.Throws<ArgumentNullException>(() => library.FindBooks(null)); | |
194 } | |
195 | |
196 | |
197 [Test] | |
198 public void FindBooks_WithValidSearchTitleString_ShouldReturnCorrectNumberOfBooks() | |
199 { | |
200 var library = CreateLibraryScenario(); | |
201 | |
202 // good search string | |
203 var searchString = "Flies"; | |
204 | |
205 // number of Lord of the Flies books expected | |
206 var expected = 3; | |
207 var actual = library.FindBooks(searchString).Count; | |
208 | |
209 Assert.AreEqual(expected, actual); | |
210 } | |
211 | |
212 [Test] | |
213 public void FindBooks_WithValidSearchTitleStringLowerCase_ShouldReturnCorrectNumberOfBooks() | |
214 { | |
215 var library = CreateLibraryScenario(); | |
216 | |
217 // good search string | |
218 var searchString = "flies"; | |
219 | |
220 // number of Lord of the Flies books expected | |
221 var expected = 3; | |
222 var actual = library.FindBooks(searchString).Count; | |
223 | |
224 Assert.AreEqual(expected, actual); | |
225 } | |
226 | |
227 [Test] | |
228 public void FindBooks_WithValidSearchStringMultiplePublications_ShouldReturnCorrectNumberOfBooks() | |
229 { | |
230 var library = CreateLibraryScenario(); | |
231 | |
232 // good search string | |
233 var searchString = "e"; | |
234 | |
235 // number of Lord of the Flies books expected | |
236 var expected = 10; | |
237 var actual = library.FindBooks(searchString).Count; | |
238 | |
239 Assert.AreEqual(expected, actual); | |
240 } | |
241 | |
242 [Test] | |
243 public void FindBooks_WithValidSearchAuthorString_ShouldReturnCorrectNumberOfBooks() | |
244 { | |
245 var library = CreateLibraryScenario(); | |
246 | |
247 // number of Brave New World books expected | |
248 var expected = 5; | |
249 var searchString = "ldous"; | |
250 | |
251 var actual = library.FindBooks(searchString).Count; | |
252 | |
253 Assert.AreEqual(expected, actual); | |
254 } | |
255 | |
256 [Test] | |
257 public void FindBooks_WithInvalidSearchString_ShouldBeEmpty() | |
258 { | |
259 var library = CreateLibraryScenario(); | |
260 | |
261 // rubbish data | |
262 var searchString = "XYZ"; | |
263 | |
264 var actual = library.FindBooks(searchString); | |
265 | |
266 Assert.IsEmpty(actual); | |
267 } | |
268 | |
269 #endregion | |
270 | |
271 #region GetBookById | |
272 | |
273 [Test] | |
274 public void GetBookById_WhenNullId_ShouldThrowArgumentNullExpection() | |
275 { | |
276 var library = CreateLibraryScenario(); | |
277 Assert.Throws<ArgumentNullException>(() => library.GetBookById(null)); | |
278 } | |
279 | |
280 [Test] | |
281 public void GetBookById_WhenValidId_ShouldReturnBook() | |
282 { | |
283 var library = CreateLibraryScenario(); | |
284 var validBookId = "0000001"; | |
285 var book = library.GetBookById(validBookId); | |
286 Assert.IsNotNull(book); | |
287 } | |
288 | |
289 [Test] | |
290 public void GetBookById_WhenValidId_ShouldReturnNull() | |
291 { | |
292 var library = CreateLibraryScenario(); | |
293 var invalidBookId = "X"; | |
294 var book = library.GetBookById(invalidBookId); | |
295 Assert.IsNull(book); | |
296 } | |
297 | |
298 #endregion | |
299 | |
300 #region LendBook | |
301 | |
302 [Test] | |
303 public void LendBook_ShouldMakeBookInLibraryStockListUnavailable() | |
304 { | |
305 //arrange | |
306 var library = CreateLibraryScenario(); | |
307 var person = CreatePerson(); | |
308 var bookId = "0000001"; | |
309 | |
310 // act | |
311 library.LendBook(person, bookId); | |
312 var lookupBook = ((List<IStock>) library.StockList).Find(s => s.Book.Id == bookId); | |
313 var actual = lookupBook.IsAvailable; | |
314 | |
315 // assert | |
316 Assert.IsFalse(actual); | |
317 } | |
318 | |
319 private IPerson CreatePerson() | |
320 { | |
321 var person = Person.Create("dummy", "dummy"); | |
322 return person; | |
323 } | |
324 | |
325 [Test] | |
326 public void LendBook_WithNullPerson_ShouldThrowArgumentNullException() | |
327 { | |
328 //arrange | |
329 var library = CreateLibraryScenario(); | |
330 var id = "dummy"; | |
331 | |
332 // act & assert | |
333 Assert.Throws<ArgumentNullException>(() => library.LendBook(null, id)); | |
334 } | |
335 | |
336 [Test] | |
337 public void LendBook_WithNullBook_ShouldThrowArgumentNullException() | |
338 { | |
339 //arrange | |
340 var library = CreateLibraryScenario(); | |
341 var person = CreatePerson(); | |
342 | |
343 // act & assert | |
344 Assert.Throws<ArgumentNullException>(() => library.LendBook(person, null)); | |
345 } | |
346 | |
347 [Test] | |
348 public void LendBook_WithNullBookAndNullPerson_ShouldThrowArgumentNullException() | |
349 { | |
350 //arrange | |
351 var library = CreateLibraryScenario(); | |
352 | |
353 // act & assert | |
354 Assert.Throws<ArgumentNullException>(() => library.LendBook(null, null)); | |
355 } | |
356 | |
357 [Test] | |
358 public void LendBook_WithUnknownBook_ShouldThrowArgumentException() | |
359 { | |
360 //arrange | |
361 var library = CreateLibraryScenario(); | |
362 var person = CreatePerson(); | |
363 var id = "dummy"; | |
364 | |
365 // act & assert | |
366 Assert.Throws<Exception>( | |
367 () => library.LendBook(person, id), | |
368 string.Format("Book id {0} is not in stock", id)); | |
369 } | |
370 | |
371 #endregion | |
372 | |
373 } | |
374 } |