Skip to content

Commit

Permalink
BookLib: Add integration test SortFilterAndChangeLendToStatusTest
Browse files Browse the repository at this point in the history
  • Loading branch information
jbe2277 committed Aug 2, 2024
1 parent b5a342b commit 3b8ae88
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Waf.Applications;
using System.Waf.UnitTesting;
using Test.BookLibrary.Library.Applications.Services;
using Test.BookLibrary.Library.Applications.Views;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Waf.Applications;
using System.Waf.Applications.Services;
using System.Waf.UnitTesting.Mocks;
using Test.BookLibrary.Library.Applications.Services;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Test.BookLibrary.Library.Applications.Services;
using Test.BookLibrary.Library.Applications.Views;
using Waf.BookLibrary.Library.Applications.Controllers;
using Waf.BookLibrary.Library.Domain;

namespace Test.BookLibrary.Library.Applications.Integration;

[TestClass]
public class IntegrationTest : ApplicationsTest
{
protected override void OnInitialize()
{
base.OnInitialize();
var dbContextService = Get<MockDBContextService>();
dbContextService.ContextCreated = x =>
{
var harry = new Person { Firstname = "Harry", Lastname = "Potter" };
var ron = new Person { Firstname = "Ron", Lastname = "Weasley" };
x.Set<Person>().Add(harry);
x.Set<Person>().Add(ron);
x.Set<Book>().Add(new Book { Title = "The Lord of the Rings - The Fellowship of the Ring", Author = "J.R.R. Tolkien", LendTo = harry });
x.Set<Book>().Add(new Book { Title = "Star Wars - Heir to the Empire", Author = "Timothy Zahn", LendTo = ron });
x.Set<Book>().Add(new Book { Title = "Serenity, Vol 1: Those Left Behind", Author = "Joss Whedon, Brett Matthews, Will Conrad" });
};
}

[TestMethod]
public void SortFilterAndChangeLendToStatusTest()
{
var moduleController = Get<ModuleController>();
moduleController.Initialize();
moduleController.Run();

var shellView = Get<MockShellView>();
Assert.IsTrue(shellView.IsVisible);
var bookListView = (MockBookListView)shellView.ViewModel.ShellService.BookListView!;
var bookView = (MockBookView)shellView.ViewModel.ShellService.BookView!;

var books = bookListView.ViewModel.Books!;
bookListView.SingleSelect(books[0]);

// Sort Book list by Title
Assert.IsTrue(books[0].Book.Title.StartsWith("The Lord"));
bookListView.ViewModel.Sort = x => x.OrderBy(y => y.Book.Title);
Assert.IsTrue(books[0].Book.Title.StartsWith("Serenity"));

// Filter Book list by search text
bookListView.ViewModel.FilterText = "Star";
var book = books.Single().Book;
Assert.IsTrue(book.Title.StartsWith("Star"));
bookListView.SingleSelect(books[0]);

Assert.AreSame(book, bookView.ViewModel.Book);

// Check that the Star Wars book was lend to Ron. Then change the status to not lend to anyone.
var ron = book.LendTo!;
Assert.AreEqual("Ron", ron.Firstname);
MockLendToView.ShowDialogAction = v =>
{
Assert.AreSame(book, v.ViewModel.Book);
Assert.AreSame(ron, v.ViewModel.SelectedPerson);
Assert.IsTrue(v.ViewModel.IsLendTo);
v.ViewModel.IsLendTo = false;
v.ViewModel.OkCommand.Execute(null);
};
bookView.ViewModel.LendToCommand!.Execute(book);
Assert.IsNull(book.LendTo);

// Check that The Lord of the Rings book was lend to Harry.
bookListView.ViewModel.FilterText = "";
var book2 = books.Select(x => x.Book).First(x => x.Title.StartsWith("The Lord"));
var harry = book2.LendTo!;
bookListView.SingleSelect(books.Single(x => x.Book == book2));
Assert.AreEqual("Harry", bookView.ViewModel.Book?.LendTo?.Firstname);

// Switch to Persons(List)View and select Harry
var personListView = (MockPersonListView)shellView.ViewModel.ShellService.PersonListView!;
var personView = (MockPersonView)shellView.ViewModel.ShellService?.PersonView!;
var persons = personListView.ViewModel.Persons!;
Assert.AreEqual(2, persons.Count);
Assert.AreSame(harry, persons.Single(x => x.Firstname == "Harry"));
personListView.SingleSelect(harry);
Assert.AreSame(harry, personView.ViewModel.Person);

// Select all Persons and remove all of them
foreach (var x in persons.Where(x => x != harry)) personListView.ViewModel.AddSelectedPerson(x);
personListView.ViewModel.RemoveCommand!.Execute(null);
Assert.AreEqual(0, persons.Count);

// Check that The Lord of the Rings book is not lend to anyone.
Assert.IsNull(book2.LendTo);

// Use a search string which does not exist -> Books list is empty now
bookListView.ViewModel.FilterText = "Star Trek";
Assert.AreEqual(0, books.Count);
bookListView.SingleSelect(null);
Assert.IsNull(bookView.ViewModel.Book);

moduleController.Shutdown();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ namespace Test.BookLibrary.Library.Applications.Services;
[Export, Export(typeof(IDBContextService))]
public class MockDBContextService : IDBContextService
{
public Action<DbContext>? ContextCreated { get; set; }

public DbContext GetBookLibraryContext(out string dataSourcePath)
{
dataSourcePath = @"C:\Test.db";
Expand All @@ -19,6 +21,7 @@ public DbContext GetBookLibraryContext(out string dataSourcePath)
modelBuilder.Entity<Book>().Ignore(x => x.Errors).Ignore(x => x.HasErrors);
modelBuilder.Entity<Person>().Ignore(x => x.Errors).Ignore(x => x.HasErrors);
});
ContextCreated?.Invoke(context);
return context;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.ComponentModel.Composition;
using System.Waf.UnitTesting.Mocks;
using Waf.BookLibrary.Library.Applications.DataModels;
using Waf.BookLibrary.Library.Applications.ViewModels;
using Waf.BookLibrary.Library.Applications.Views;

Expand All @@ -11,4 +12,11 @@ public class MockBookListView : MockView<BookListViewModel>, IBookListView
public bool FirstCellHasFocus { get; set; }

public void FocusFirstCell() => FirstCellHasFocus = true;

public void SingleSelect(BookDataModel? book)
{
ViewModel.SelectedBook = book;
foreach (var x in ViewModel.SelectedBooks.ToArray()) ViewModel.RemoveSelectedBook(x);
if (book is not null) ViewModel.AddSelectedBook(book);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Waf.UnitTesting.Mocks;
using Waf.BookLibrary.Library.Applications.ViewModels;
using Waf.BookLibrary.Library.Applications.Views;
using Waf.BookLibrary.Library.Domain;

namespace Test.BookLibrary.Library.Applications.Views;

Expand All @@ -11,4 +12,11 @@ public class MockPersonListView : MockView<PersonListViewModel>, IPersonListView
public bool FirstCellHasFocus { get; set; }

public void FocusFirstCell() => FirstCellHasFocus = true;

public void SingleSelect(Person? person)
{
ViewModel.SelectedPerson = person;
foreach (var x in ViewModel.SelectedPersons.ToArray()) ViewModel.RemoveSelectedPerson(x);
if (person is not null) ViewModel.AddSelectedPerson(person);
}
}

0 comments on commit 3b8ae88

Please sign in to comment.