diff --git a/src/dwCheckApi.DTO/ViewModels/CharacterViewModel.cs b/src/dwCheckApi.DTO/ViewModels/CharacterViewModel.cs index a8ed611..6797b0e 100644 --- a/src/dwCheckApi.DTO/ViewModels/CharacterViewModel.cs +++ b/src/dwCheckApi.DTO/ViewModels/CharacterViewModel.cs @@ -4,12 +4,7 @@ namespace dwCheckApi.DTO.ViewModels { public class CharacterViewModel : BaseViewModel { - public CharacterViewModel() - { - Books = new SortedDictionary(); - } - public string CharacterName { get; set; } - public SortedDictionary Books { get; set; } + public SortedDictionary Books { get; set; } = new(); } } \ No newline at end of file diff --git a/tests/dwCheckApi.Tests/ViewModelMappers/BookViewModelMapperTests.cs b/tests/dwCheckApi.Tests/ViewModelMappers/BookViewModelMapperTests.cs index 26b4bed..0d37924 100644 --- a/tests/dwCheckApi.Tests/ViewModelMappers/BookViewModelMapperTests.cs +++ b/tests/dwCheckApi.Tests/ViewModelMappers/BookViewModelMapperTests.cs @@ -1,24 +1,18 @@ +using System; using System.Collections.Generic; using System.Linq; using dwCheckApi.DTO.Helpers; using dwCheckApi.DTO.ViewModels; using dwCheckApi.Entities; using Xunit; -using Xunit.Abstractions; namespace dwCheckApi.Tests.ViewModelMappers { public class BookViewModelMapperTests { - public BookViewModelMapperTests(ITestOutputHelper output) - { - } - [Fact] public void Given_BookDbModel_Returns_ViewModel() { - // What is this test even testing? - // Arrange const int idForTest = 1; var dbBook = GetTestBookById(idForTest); @@ -34,6 +28,65 @@ public void Given_BookDbModel_Returns_ViewModel() Assert.Equal(testViewModel.BookIsbn10, viewModel.BookIsbn10); Assert.Equal(testViewModel.BookIsbn13, viewModel.BookIsbn13); } + + [Fact] + public void Given_BookDbModels_Returns_ViewModels() + { + // Arrange + const int idForTest = 1; + var dbBooks = GetTestBooks(); + // Act + var viewModels = BookViewModelHelpers.ConvertToViewModels(dbBooks); + + // Assert + Assert.NotEmpty(viewModels); + Assert.Equal(viewModels.Count, dbBooks.Count); + + for (var i = 0; i < viewModels.Count; i++) + { + Assert.Equal(viewModels[i].BookName, dbBooks[i].BookName); + Assert.Equal(viewModels[i].BookDescription, dbBooks[i].BookDescription); + Assert.Equal(viewModels[i].BookIsbn10, dbBooks[i].BookIsbn10); + Assert.Equal(viewModels[i].BookIsbn13, dbBooks[i].BookIsbn13); + } + } + + [Fact] + public void Given_BookDbModels_Returns_BaseViewModels() + { + // Arrange + const int idForTest = 1; + var dbBooks = GetTestBooks(); + // Act + var viewModels = BookViewModelHelpers.ConvertToBaseViewModels(dbBooks); + + // Assert + Assert.NotEmpty(viewModels); + Assert.Equal(viewModels.Count, dbBooks.Count); + + for (var i = 0; i < viewModels.Count; i++) + { + Assert.Equal(viewModels[i].BookId, dbBooks[i].BookId); + Assert.Equal(viewModels[i].BookOrdinal, dbBooks[i].BookOrdinal); + Assert.Equal(viewModels[i].BookName, dbBooks[i].BookName); + Assert.Equal(viewModels[i].BookDescription, dbBooks[i].BookDescription); + } + } + + [Fact] + public void Given_BookDbModel_Returns_BookCoverViewModel() + { + // Arrange + const int idForTest = 1; + var dbBook = GetTestBookById(idForTest); + // Act + var viewModel = BookViewModelHelpers.ConvertToBookCoverViewModel(dbBook); + + // Assert + Assert.NotNull(viewModel); + Assert.Equal(viewModel.bookId, dbBook.BookId); + Assert.Equal(viewModel.BookCoverImage, dbBook.BookCoverImageUrl); + } private Book GetTestBookById(int id) { @@ -42,6 +95,18 @@ private Book GetTestBookById(int id) private List GetTestBooks() { + var testSeries = new Series + { + SeriesName = "A test series", + SeriesId = 2 + }; + + var testCharacter = new Character + { + CharacterName = Guid.NewGuid().ToString(), + CharacterId = 4 + }; + var mockData = new List(); mockData.Add(new Book { @@ -51,7 +116,26 @@ private List GetTestBooks() BookDescription = "Test entry for unit tests only", BookIsbn10 = "1234567890", BookIsbn13 = "1234567890123", - BookCoverImage = new List().ToArray() + BookCoverImage = new List().ToArray(), + BookSeries = new List + { + new() + { + BookId = 1, + SeriesId = testSeries.SeriesId, + Series = testSeries, + Ordinal = 3 + } + }, + BookCharacter = new List + { + new() + { + BookId = 1, + CharacterId = testCharacter.CharacterId, + Character = testCharacter + } + } }); return mockData; diff --git a/tests/dwCheckApi.Tests/ViewModelMappers/CharacterViewModelMapperTests.cs b/tests/dwCheckApi.Tests/ViewModelMappers/CharacterViewModelMapperTests.cs new file mode 100644 index 0000000..e2b3f3e --- /dev/null +++ b/tests/dwCheckApi.Tests/ViewModelMappers/CharacterViewModelMapperTests.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using dwCheckApi.DTO.Helpers; +using Xunit; + +namespace dwCheckApi.Tests.ViewModelMappers +{ + public class CharacterViewModelMapperTests + { + [Fact] + public void Given_CharacterDbModel_Returns_ViewModel() + { + // Arrange + var characterName = Guid.NewGuid().ToString(); + var books = new Dictionary + { + // intentionally added out of order ot test the ordering of the final Dictionary + { 2, Guid.NewGuid().ToString() }, + { 1, Guid.NewGuid().ToString() } + }; + + // Act + var response = CharacterViewModelHelpers.ConvertToViewModel(characterName, books); + + // Assert + Assert.Equal(response.CharacterName, characterName); + Assert.Equal(response.Books.Count, books.Count); + + var first = response.Books.First(); + Assert.Equal(1, first.Key); + } + } +} \ No newline at end of file diff --git a/tests/dwCheckApi.Tests/ViewModelMappers/SeriesViewModelMapperTests.cs b/tests/dwCheckApi.Tests/ViewModelMappers/SeriesViewModelMapperTests.cs new file mode 100644 index 0000000..b90d942 --- /dev/null +++ b/tests/dwCheckApi.Tests/ViewModelMappers/SeriesViewModelMapperTests.cs @@ -0,0 +1,71 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using dwCheckApi.DTO.Helpers; +using dwCheckApi.Entities; +using Xunit; + +namespace dwCheckApi.Tests.ViewModelMappers +{ + public class SeriesViewModelMapperTests + { + [Fact] + public void Given_SeriesDbModel_Returns_ViewModel() + { + // Arrange + var dbBook = new Book + { + BookName = Guid.NewGuid().ToString() + }; + var dbSeries = new Series + { + SeriesId = 1, + SeriesName = Guid.NewGuid().ToString(), + BookSeries = new List + { + new() + { + Book = dbBook + } + } + }; + + // Act + var viewModel = SeriesViewModelHelpers.ConvertToViewModel(dbSeries); + + // Assert + Assert.Equal(dbSeries.SeriesId, viewModel.SeriesId); + Assert.Equal(dbSeries.SeriesName, viewModel.SeriesName); + Assert.Equal(dbSeries.BookSeries.First().Book.BookName, viewModel.BookNames.First()); + } + + [Fact] + public void Given_ListOfSeriesDbModel_Returns_ListOfViewModel() + { + // Arrange + var dbSeries = new List + { + new() + { + SeriesId = 1, + SeriesName = Guid.NewGuid().ToString(), + BookSeries = new List() + } + }; + + // Act + var viewModels = SeriesViewModelHelpers.ConvertToViewModels(dbSeries); + + // Assert + Assert.NotNull(viewModels); + Assert.NotEmpty(viewModels); + Assert.Equal(dbSeries.Count, viewModels.Count); + + for (var i = 0; i < viewModels.Count; i++) + { + Assert.Equal(dbSeries[i].SeriesId, viewModels[i].SeriesId); + Assert.Equal(dbSeries[i].SeriesName, viewModels[i].SeriesName); + } + } + } +} \ No newline at end of file