Skip to content

Commit

Permalink
Added unit tests for mappers
Browse files Browse the repository at this point in the history
  • Loading branch information
GaProgMan committed Feb 18, 2024
1 parent 627dc69 commit e222602
Show file tree
Hide file tree
Showing 4 changed files with 198 additions and 14 deletions.
7 changes: 1 addition & 6 deletions src/dwCheckApi.DTO/ViewModels/CharacterViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@ namespace dwCheckApi.DTO.ViewModels
{
public class CharacterViewModel : BaseViewModel
{
public CharacterViewModel()
{
Books = new SortedDictionary<int, string>();
}

public string CharacterName { get; set; }
public SortedDictionary<int, string> Books { get; set; }
public SortedDictionary<int, string> Books { get; set; } = new();
}
}
100 changes: 92 additions & 8 deletions tests/dwCheckApi.Tests/ViewModelMappers/BookViewModelMapperTests.cs
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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;

Check warning on line 36 in tests/dwCheckApi.Tests/ViewModelMappers/BookViewModelMapperTests.cs

View workflow job for this annotation

GitHub Actions / build

The variable 'idForTest' is assigned but its value is never used

Check warning on line 36 in tests/dwCheckApi.Tests/ViewModelMappers/BookViewModelMapperTests.cs

View workflow job for this annotation

GitHub Actions / build

The variable 'idForTest' is assigned but its value is never used
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;

Check warning on line 58 in tests/dwCheckApi.Tests/ViewModelMappers/BookViewModelMapperTests.cs

View workflow job for this annotation

GitHub Actions / build

The variable 'idForTest' is assigned but its value is never used

Check warning on line 58 in tests/dwCheckApi.Tests/ViewModelMappers/BookViewModelMapperTests.cs

View workflow job for this annotation

GitHub Actions / build

The variable 'idForTest' is assigned but its value is never used
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)
{
Expand All @@ -42,6 +95,18 @@ private Book GetTestBookById(int id)

private List<Book> 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<Book>();
mockData.Add(new Book
{
Expand All @@ -51,7 +116,26 @@ private List<Book> GetTestBooks()
BookDescription = "Test entry for unit tests only",
BookIsbn10 = "1234567890",
BookIsbn13 = "1234567890123",
BookCoverImage = new List<byte>().ToArray()
BookCoverImage = new List<byte>().ToArray(),
BookSeries = new List<BookSeries>
{
new()
{
BookId = 1,
SeriesId = testSeries.SeriesId,
Series = testSeries,
Ordinal = 3
}
},
BookCharacter = new List<BookCharacter>
{
new()
{
BookId = 1,
CharacterId = testCharacter.CharacterId,
Character = testCharacter
}
}
});

return mockData;
Expand Down
Original file line number Diff line number Diff line change
@@ -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<int, string>
{
// 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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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<BookSeries>
{
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<Series>
{
new()
{
SeriesId = 1,
SeriesName = Guid.NewGuid().ToString(),
BookSeries = new List<BookSeries>()
}
};

// 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);
}
}
}
}

0 comments on commit e222602

Please sign in to comment.