Skip to content

Commit

Permalink
Added unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JonPSmith committed Mar 13, 2018
1 parent a8fbeca commit 4f74cdb
Show file tree
Hide file tree
Showing 10 changed files with 705 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"dotnet-test-explorer.testProjectPath": "./Tests/"
}
14 changes: 14 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,20 @@
"${workspaceFolder}/RazorPageApp/RazorPageApp.csproj"
],
"problemMatcher": "$msCompile"
},
// thanks to https://stackoverflow.com/questions/47066356/how-does-one-debug-an-mstest-in-vscode
{
"label": "Run tests",
"command": "dotnet test ${workspaceFolder}/Tests/Tests.csproj",
"type": "shell",
"group": {
"kind": "test",
"isDefault": true
},
"presentation": {
"reveal": "silent"
},
"problemMatcher": "$msCompile"
}
]
}
6 changes: 6 additions & 0 deletions GenericLibraries.sln
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GenericLibsBase", "GenericL
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ServiceLayer", "ServiceLayer\ServiceLayer.csproj", "{0B6C08D4-D744-4E46-AFA0-692E35885BA3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "Tests\Tests.csproj", "{6D3D7458-6669-4348-91D5-54E78D50CB13}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -39,6 +41,10 @@ Global
{0B6C08D4-D744-4E46-AFA0-692E35885BA3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0B6C08D4-D744-4E46-AFA0-692E35885BA3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0B6C08D4-D744-4E46-AFA0-692E35885BA3}.Release|Any CPU.Build.0 = Release|Any CPU
{6D3D7458-6669-4348-91D5-54E78D50CB13}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6D3D7458-6669-4348-91D5-54E78D50CB13}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6D3D7458-6669-4348-91D5-54E78D50CB13}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6D3D7458-6669-4348-91D5-54E78D50CB13}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
31 changes: 31 additions & 0 deletions Tests/Helpers/AutoMapperHelpers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (c) 2018 Jon P Smith, GitHub: JonPSmith, web: http://www.thereformedprogrammer.net/
// Licensed under MIT licence. See License.txt in the project root for license information.

using AutoMapper;

namespace Tests.Helpers
{
public static class AutoMapperHelpers
{
public static IMapper CreateMapper<T>() where T : Profile, new()
{
var config = new MapperConfiguration(cfg =>
{
cfg.AddProfile(new T());
});
var mapper = config.CreateMapper();
return mapper;
}

public static IMapper CreateMapper<T1, T2>() where T1 : Profile, new() where T2 : Profile, new()
{
var config = new MapperConfiguration(cfg =>
{
cfg.AddProfile(new T1());
cfg.AddProfile(new T2());
});
var mapper = config.CreateMapper();
return mapper;
}
}
}
153 changes: 153 additions & 0 deletions Tests/Helpers/DddEfTestData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
// Copyright (c) 2016 Jon P Smith, GitHub: JonPSmith, web: http://www.thereformedprogrammer.net/
// Licensed under MIT licence. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Linq;
using DataLayer.Dtos;
using DataLayer.EfClasses;
using DataLayer.EfCode;

namespace Tests.Helpers
{
public static class DddEfTestData
{
public const string DummyUserId = "UnitTestUserId";
public static readonly DateTime DummyBookStartDate = new DateTime(2010, 1, 1);

public static void SeedDatabaseDummyBooks(this EfCoreContext context, int numBooks = 10)
{
context.Books.AddRange(CreateDummyBooks(numBooks));
context.SaveChanges();
}

public static Book CreateDummyBookOneAuthor()
{

var book = new Book
(
"Book Title",
"Book Description",
DummyBookStartDate,
"Book Publisher",
123,
null,
new[] { new Author ("Test Author") }
);

return book;
}

public static List<Book> CreateDummyBooks(int numBooks = 10, bool stepByYears = false)
{
var result = new List<Book>();
var commonAuthor = new Author("CommonAuthor");
for (int i = 0; i < numBooks; i++)
{
var book = new Book
(
$"Book{i:D4} Title",
$"Book{i:D4} Description",
stepByYears ? DummyBookStartDate.AddYears(i) : DummyBookStartDate.AddDays(i),
"Publisher",
(short)(i + 1),
$"Image{i:D4}",
new[] { new Author( $"Author{i:D4}"), commonAuthor}
);
for (int j = 0; j < i; j++)
{
book.AddReview((j % 5) + 1, null, j.ToString());
}

result.Add(book);
}

return result;
}

public static void SeedDatabaseFourBooks(this EfCoreContext context)
{
context.Books.AddRange(CreateFourBooks());
context.SaveChanges();
}

public static List<Book> CreateFourBooks()
{
var martinFowler = new Author("Martin Fowler");

var books = new List<Book>();

var book1 = new Book
(
"Refactoring",
"Improving the design of existing code",
new DateTime(1999, 7, 8),
null,
40,
null,
new[] { martinFowler }
);
books.Add(book1);

var book2 = new Book
(
"Patterns of Enterprise Application Architecture",
"Written in direct response to the stiff challenges",
new DateTime(2002, 11, 15),
null,
53,
null,
new []{martinFowler}
);
books.Add(book2);

var book3 = new Book
(
"Domain-Driven Design",
"Linking business needs to software design",
new DateTime(2003, 8, 30),
null,
56,
null,
new[] { new Author("Eric Evans") }
);
books.Add(book3);

var book4 = new Book
(
"Quantum Networking",
"Entangled quantum networking provides faster-than-light data communications",
new DateTime(2057, 1, 1),
"Future Published",
220,
null,
new[] { new Author("Future Person") }
);
book4.AddReview(5,
"I look forward to reading this book, if I am still alive!", "Jon P Smith");
book4.AddReview(5,
"I write this book if I was still alive!", "Albert Einstein"); book4.AddPromotion(219, "Save $1 if you order 40 years ahead!");
book4.AddPromotion(219, "Save 1$ by buying 40 years ahead");

books.Add(book4);

return books;
}

public static void SeedDummyOrder(this EfCoreContext context, DateTime orderDate = new DateTime())
{
if (orderDate == new DateTime())
orderDate = DateTime.Today;
var books = context.Books.ToList();
context.AddRange(BuildDummyOrder(DummyUserId, orderDate, books.First()));
context.SaveChanges();
}

private static Order BuildDummyOrder(string userId, DateTime orderDate, Book bookOrdered)
{
var deliverDay = orderDate.AddDays(5);
var bookOrders = new List<OrderBooksDto>() { new OrderBooksDto(1, bookOrdered, 1) };
return Order.CreateOrderFactory(userId, deliverDay, bookOrders)?.Result;
}
}
}
30 changes: 30 additions & 0 deletions Tests/Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AutoMapper" Version="6.2.2" />
<PackageReference Include="EfCore.TestSupport" Version="1.3.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="2.0.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.6.1" />
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
</ItemGroup>

<ItemGroup>
<Folder Include="UnitTests\DataLayer\" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\DataLayer\DataLayer.csproj" />
<ProjectReference Include="..\GenericLibsBase\GenericLibsBase.csproj" />
<ProjectReference Include="..\RazorPageApp\RazorPageApp.csproj" />
<ProjectReference Include="..\ServiceLayer\ServiceLayer.csproj" />
</ItemGroup>

</Project>
Loading

0 comments on commit 4f74cdb

Please sign in to comment.