# HG changeset patch # User Steven Hollidge # Date 1333563620 -3600 # Node ID aef06698d9e272bbaf329da8672aea335fe0d718 # Parent 0d4aff4f699d88d5f6527046ecf7dc51b02be0b2 Added the library tests diff -r 0d4aff4f699d -r aef06698d9e2 .hgignore --- a/.hgignore Tue Apr 03 23:32:40 2012 +0100 +++ b/.hgignore Wed Apr 04 19:20:20 2012 +0100 @@ -5,4 +5,6 @@ *.csproj.user */obj/* */_ReSharper* -Stocks/packages/* +*.DotSettings.user +*/packages/* +*.stats diff -r 0d4aff4f699d -r aef06698d9e2 Library/.nuget/NuGet.Config --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Library/.nuget/NuGet.Config Wed Apr 04 19:20:20 2012 +0100 @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff -r 0d4aff4f699d -r aef06698d9e2 Library/.nuget/NuGet.exe Binary file Library/.nuget/NuGet.exe has changed diff -r 0d4aff4f699d -r aef06698d9e2 Library/.nuget/NuGet.targets --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Library/.nuget/NuGet.targets Wed Apr 04 19:20:20 2012 +0100 @@ -0,0 +1,71 @@ + + + + $(MSBuildProjectDirectory)\..\ + + + $([System.IO.Path]::Combine($(SolutionDir), ".nuget")) + $([System.IO.Path]::Combine($(ProjectDir), "packages.config")) + $([System.IO.Path]::Combine($(SolutionDir), "packages")) + + + $(SolutionDir).nuget + packages.config + $(SolutionDir)packages + + + $(NuGetToolsPath)\nuget.exe + "$(NuGetExePath)" + mono --runtime=v4.0.30319 $(NuGetExePath) + + $(TargetDir.Trim('\\')) + + + "" + + + false + + + false + + + $(NuGetCommand) install "$(PackagesConfig)" -source $(PackageSources) -o "$(PackagesDir)" + $(NuGetCommand) pack "$(ProjectPath)" -p Configuration=$(Configuration) -o "$(PackageOutputDir)" -symbols + + + + RestorePackages; + $(BuildDependsOn); + + + + + $(BuildDependsOn); + BuildPackage; + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff -r 0d4aff4f699d -r aef06698d9e2 Library/LibrarySystem.Tests/Core/BookTests.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Library/LibrarySystem.Tests/Core/BookTests.cs Wed Apr 04 19:20:20 2012 +0100 @@ -0,0 +1,42 @@ +using System; +using LibrarySystem.Core; +using LibrarySystem.Interfaces; +using NUnit.Framework; + +namespace LibrarySystem.Tests.Core +{ + [TestFixture] + class BookTests + { + [Test] + public void Create_WithNullIdAndNullPublication_ShouldThrowArgumentNullException() + { + Assert.Throws(() => Book.Create(null, null)); + } + + [Test] + public void Create_WithNullIdAndValidPublication_ShouldThrowArgumentNullException() + { + var publisher = Publisher.Create("dummy"); + var publication = Publication.Create("dummy", "dummy", publisher, "dummy"); + Assert.Throws(() => Book.Create(null, publication)); + } + + [Test] + public void Create_WithNullPublication_ShouldThrowArgumentNullException() + { + var validId = "1"; + Assert.Throws(() => Book.Create(validId, null)); + } + + [Test] + public void Create_WithValidParametersPublication_ShouldReturnBook() + { + var publisher = Publisher.Create("dummy"); + var publication = Publication.Create("dummy","dummy", publisher, "dummy"); + var validId = "1"; + IBook book = Book.Create(validId, publication); + Assert.NotNull(book); + } + } +} \ No newline at end of file diff -r 0d4aff4f699d -r aef06698d9e2 Library/LibrarySystem.Tests/Core/LibraryTests.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Library/LibrarySystem.Tests/Core/LibraryTests.cs Wed Apr 04 19:20:20 2012 +0100 @@ -0,0 +1,374 @@ +using System; +using System.Collections.Generic; +using LibrarySystem.Core; +using LibrarySystem.Interfaces; +using NUnit.Framework; + +namespace LibrarySystem.Tests.Core +{ + [TestFixture] + public class LibraryTests + { + #region Factory methods to setup fixed scenario for testing + + // Factory that creates: + // A library object with 10 books (2 x Lord of the Flies, 3 x 1984, 5 x Brave New World) + private ILibrary CreateLibraryScenario() + { + #region Publisher + + IPublisher penguin = Publisher.Create("Penguin"); + + #endregion + + #region Publications + + IPublication lordOfTheFlies = Publication.Create("William Golding", "0140283331", penguin, "Lord of the Flies"); + + IPublication nineteenEightyFour = Publication.Create("George Orwell", "0452284236", penguin, "Nineteen Eighty-Four"); + + IPublication braveNewWorld = Publication.Create("Aldous Huxley", "0060850523", penguin, "Brave New World"); + + #endregion + + #region Books + + // Lord of the flies + IBook book1 = Book.Create("0000001", lordOfTheFlies); + IBook book2 = Book.Create("0000002", lordOfTheFlies); + IBook book3 = Book.Create("0000003", lordOfTheFlies); + + // 1984 + IBook book4 = Book.Create("0000004", nineteenEightyFour); + IBook book5 = Book.Create("0000005", nineteenEightyFour); + + // Brave new world + IBook book6 = Book.Create("0000006", braveNewWorld); + IBook book7 = Book.Create("0000007", braveNewWorld); + IBook book8 = Book.Create("0000008", braveNewWorld); + IBook book9 = Book.Create("0000009", braveNewWorld); + IBook book10 = Book.Create("0000010", braveNewWorld); + + #endregion + + #region Library + + ILibrary library = Library.Create("Leicester Square"); + library.AddStock(book1, book2, book3, book4, book5, book6, book7, book8, book9, book10); + + #endregion + + return library; + } + + public IBook CreateBook(string bookId = "dummy") + { + IPublisher publisher = Publisher.Create("dummy"); + IPublication publication = Publication.Create("dummy", "dummy", publisher, "dummy"); + IBook book = Book.Create(bookId, publication); + return book; + } + + #endregion + + #region Create + + [Test] + public void Create_WithNullLocation_ThrowsArgumentNullException() + { + Assert.Throws(() => Library.Create(null)); + } + + #endregion + + #region AddStock + + [Test] + public void AddStock_WithNullBook_ShouldThrowArgumentNullException() + { + var library = CreateLibraryScenario(); + Assert.Throws(() => library.AddStock(null)); + } + + [Test] + public void AddStock_WithOneValidBookAndOneNullBook_ShouldThrowArgumentNullException() + { + var library = CreateLibraryScenario(); + var validBook = CreateBook(); + Assert.Throws(() => library.AddStock(validBook, null)); + } + + [Test] + public void AddStock_WithValidBook_ShouldIncreaseStockListCountByOne() + { + //arrange + var library = CreateLibraryScenario(); + var expected = library.StockList.Count + 1; + var dummyBook = CreateBook(); + + //act + library.AddStock(dummyBook); + var actual = library.StockList.Count; + + //assert + Assert.AreEqual(expected, actual); + } + + [Test] + public void AddStock_WithBookIdAlreadyInStock_ShouldThrowException() + { + //arrange + var library = CreateLibraryScenario(); + var idThatIsAlreadyInStock = "0000001"; + var dummyBook = CreateBook(idThatIsAlreadyInStock); + + //act + Assert.Throws( + () => library.AddStock(dummyBook), + string.Format("Book with {0} is already in stock", idThatIsAlreadyInStock)); + } + + [Test] + public void AddStock_WithValidBook_ShouldMakeBookAvailable() + { + //arrange + var library = CreateLibraryScenario(); + var id = "0000001"; + var dummyBook = CreateBook(); + + //act + library.AddStock(dummyBook); + var lookupBook = ((List) library.StockList).Find(s => s.Book.Id == id); + var actual = lookupBook.IsAvailable; + + //assert + Assert.IsTrue(actual); + } + + #endregion + + #region IsBookAvailableByISBN + + [Test] + public void IsBookAvailableByISBN_WithNullISBN_ShouldThrowArgumentNullExpection() + { + var library = CreateLibraryScenario(); + Assert.Throws(() => library.IsBookAvailableByISBN(null)); + } + + [Test] + public void IsBookAvailableByISBN_WithValidISBN_ShouldReturnTrue() + { + var library = CreateLibraryScenario(); + + // Lord of the flies + var validISBN = "0140283331"; + + var actual = library.IsBookAvailableByISBN(validISBN); + + Assert.IsTrue(actual); + } + + [Test] + public void IsBookAvailableByISBN_WithUnknownISBN_ShouldReturnFalse() + { + var library = CreateLibraryScenario(); + + // rubbish data + var unknownISBN = "X"; + + var actual = library.IsBookAvailableByISBN(unknownISBN); + + Assert.IsFalse(actual); + } + + #endregion + + #region FindBooks + + [Test] + public void FindBooks_WhenNullSearchString_ShouldThrowArgumentNullExpection() + { + var library = CreateLibraryScenario(); + Assert.Throws(() => library.FindBooks(null)); + } + + + [Test] + public void FindBooks_WithValidSearchTitleString_ShouldReturnCorrectNumberOfBooks() + { + var library = CreateLibraryScenario(); + + // good search string + var searchString = "Flies"; + + // number of Lord of the Flies books expected + var expected = 3; + var actual = library.FindBooks(searchString).Count; + + Assert.AreEqual(expected, actual); + } + + [Test] + public void FindBooks_WithValidSearchTitleStringLowerCase_ShouldReturnCorrectNumberOfBooks() + { + var library = CreateLibraryScenario(); + + // good search string + var searchString = "flies"; + + // number of Lord of the Flies books expected + var expected = 3; + var actual = library.FindBooks(searchString).Count; + + Assert.AreEqual(expected, actual); + } + + [Test] + public void FindBooks_WithValidSearchStringMultiplePublications_ShouldReturnCorrectNumberOfBooks() + { + var library = CreateLibraryScenario(); + + // good search string + var searchString = "e"; + + // number of Lord of the Flies books expected + var expected = 10; + var actual = library.FindBooks(searchString).Count; + + Assert.AreEqual(expected, actual); + } + + [Test] + public void FindBooks_WithValidSearchAuthorString_ShouldReturnCorrectNumberOfBooks() + { + var library = CreateLibraryScenario(); + + // number of Brave New World books expected + var expected = 5; + var searchString = "ldous"; + + var actual = library.FindBooks(searchString).Count; + + Assert.AreEqual(expected, actual); + } + + [Test] + public void FindBooks_WithInvalidSearchString_ShouldBeEmpty() + { + var library = CreateLibraryScenario(); + + // rubbish data + var searchString = "XYZ"; + + var actual = library.FindBooks(searchString); + + Assert.IsEmpty(actual); + } + + #endregion + + #region GetBookById + + [Test] + public void GetBookById_WhenNullId_ShouldThrowArgumentNullExpection() + { + var library = CreateLibraryScenario(); + Assert.Throws(() => library.GetBookById(null)); + } + + [Test] + public void GetBookById_WhenValidId_ShouldReturnBook() + { + var library = CreateLibraryScenario(); + var validBookId = "0000001"; + var book = library.GetBookById(validBookId); + Assert.IsNotNull(book); + } + + [Test] + public void GetBookById_WhenValidId_ShouldReturnNull() + { + var library = CreateLibraryScenario(); + var invalidBookId = "X"; + var book = library.GetBookById(invalidBookId); + Assert.IsNull(book); + } + + #endregion + + #region LendBook + + [Test] + public void LendBook_ShouldMakeBookInLibraryStockListUnavailable() + { + //arrange + var library = CreateLibraryScenario(); + var person = CreatePerson(); + var bookId = "0000001"; + + // act + library.LendBook(person, bookId); + var lookupBook = ((List) library.StockList).Find(s => s.Book.Id == bookId); + var actual = lookupBook.IsAvailable; + + // assert + Assert.IsFalse(actual); + } + + private IPerson CreatePerson() + { + var person = Person.Create("dummy", "dummy"); + return person; + } + + [Test] + public void LendBook_WithNullPerson_ShouldThrowArgumentNullException() + { + //arrange + var library = CreateLibraryScenario(); + var id = "dummy"; + + // act & assert + Assert.Throws(() => library.LendBook(null, id)); + } + + [Test] + public void LendBook_WithNullBook_ShouldThrowArgumentNullException() + { + //arrange + var library = CreateLibraryScenario(); + var person = CreatePerson(); + + // act & assert + Assert.Throws(() => library.LendBook(person, null)); + } + + [Test] + public void LendBook_WithNullBookAndNullPerson_ShouldThrowArgumentNullException() + { + //arrange + var library = CreateLibraryScenario(); + + // act & assert + Assert.Throws(() => library.LendBook(null, null)); + } + + [Test] + public void LendBook_WithUnknownBook_ShouldThrowArgumentException() + { + //arrange + var library = CreateLibraryScenario(); + var person = CreatePerson(); + var id = "dummy"; + + // act & assert + Assert.Throws( + () => library.LendBook(person, id), + string.Format("Book id {0} is not in stock", id)); + } + + #endregion + + } +} diff -r 0d4aff4f699d -r aef06698d9e2 Library/LibrarySystem.Tests/Core/PersonTests.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Library/LibrarySystem.Tests/Core/PersonTests.cs Wed Apr 04 19:20:20 2012 +0100 @@ -0,0 +1,35 @@ +using System; +using LibrarySystem.Core; +using NUnit.Framework; + +namespace LibrarySystem.Tests.Core +{ + [TestFixture] + class PersonTests + { + [Test] + public void Create_WithValidParams_ShouldReturnPerson() + { + var person = Person.Create("dummy", "dummy"); + Assert.IsNotNull(person); + } + + [Test] + public void Create_WithNullAddress_ShouldThrowAgrumentNullException() + { + Assert.Throws(() => Person.Create(null, "dummy")); + } + + [Test] + public void Create_WithNullName_ShouldThrowAgrumentNullException() + { + Assert.Throws(() => Person.Create("dummy", null)); + } + + [Test] + public void Create_WithNullAddressAndName_ShouldThrowAgrumentNullException() + { + Assert.Throws(() => Person.Create(null, null)); + } + } +} diff -r 0d4aff4f699d -r aef06698d9e2 Library/LibrarySystem.Tests/Core/PublicationTests.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Library/LibrarySystem.Tests/Core/PublicationTests.cs Wed Apr 04 19:20:20 2012 +0100 @@ -0,0 +1,45 @@ +using System; +using LibrarySystem.Core; +using NUnit.Framework; + +namespace LibrarySystem.Tests.Core +{ + [TestFixture] + class PublicationTests + { + [Test] + public void Create_WithValidParams_ShouldReturnPublication() + { + var publisher = Publisher.Create("dummy"); + var publication = Publication.Create("dummy","dummy", publisher, "dummy"); + Assert.IsNotNull(publication); + } + + [Test] + public void Create_WithNullAuthor_ShouldThrowAgrumentNullException() + { + var publisher = Publisher.Create("dummy"); + Assert.Throws(() => Publication.Create(null, "dummy", publisher, "dummy")); + } + + [Test] + public void Create_WithNullISBN_ShouldThrowAgrumentNullException() + { + var publisher = Publisher.Create("dummy"); + Assert.Throws(() => Publication.Create("dummy", null, publisher, "dummy")); + } + + [Test] + public void Create_WithNullPublisher_ShouldThrowAgrumentNullException() + { + Assert.Throws(() => Publication.Create("dummy","dummy", null, "dummy")); + } + + [Test] + public void Create_WithNullTitle_ShouldThrowAgrumentNullException() + { + var publisher = Publisher.Create("dummy"); + Assert.Throws(() => Publication.Create("dummy", "dummy", publisher, null)); + } + } +} diff -r 0d4aff4f699d -r aef06698d9e2 Library/LibrarySystem.Tests/Core/PublisherTests.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Library/LibrarySystem.Tests/Core/PublisherTests.cs Wed Apr 04 19:20:20 2012 +0100 @@ -0,0 +1,23 @@ +using System; +using LibrarySystem.Core; +using NUnit.Framework; + +namespace LibrarySystem.Tests.Core +{ + [TestFixture] + class PublisherTests + { + [Test] + public void Create_WithValidName_ShouldReturnPublisher() + { + var publisher = Publisher.Create("dummy"); + Assert.IsNotNull(publisher); + } + + [Test] + public void Create_WithNullName_ShouldThrowAgrumentNullException() + { + Assert.Throws(() => Publisher.Create(null)); + } + } +} diff -r 0d4aff4f699d -r aef06698d9e2 Library/LibrarySystem.Tests/Core/StockTests.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Library/LibrarySystem.Tests/Core/StockTests.cs Wed Apr 04 19:20:20 2012 +0100 @@ -0,0 +1,26 @@ +using System; +using LibrarySystem.Core; +using NUnit.Framework; + +namespace LibrarySystem.Tests.Core +{ + [TestFixture] + class StockTests + { + [Test] + public void Create_WithValidName_ShouldReturnPublisher() + { + var publisher = Publisher.Create("dummy"); + var publication = Publication.Create("dummy","dummy", publisher, "dummy"); + var book = Book.Create("dummy", publication); + var stock = Stock.Create(book, true); + Assert.IsNotNull(stock); + } + + [Test] + public void Create_WithNullBook_ShouldThrowAgrumentNullException() + { + Assert.Throws(() => Stock.Create(null, true)); + } + } +} diff -r 0d4aff4f699d -r aef06698d9e2 Library/LibrarySystem.Tests/LibrarySystem.Tests.csproj --- a/Library/LibrarySystem.Tests/LibrarySystem.Tests.csproj Tue Apr 03 23:32:40 2012 +0100 +++ b/Library/LibrarySystem.Tests/LibrarySystem.Tests.csproj Wed Apr 04 19:20:20 2012 +0100 @@ -12,6 +12,8 @@ LibrarySystem.Tests v4.0 512 + ..\..\Library\ + true true @@ -38,8 +40,13 @@ - + + + + + + @@ -51,6 +58,7 @@ +