Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from ardalis/ardalis/integrationtests
Adding Integration Tests using Docker-hosted SQL Server
- Loading branch information
Showing
25 changed files
with
544 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.vs/ | ||
.git/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
FROM mcr.microsoft.com/dotnet/core/sdk:2.2 AS build | ||
WORKDIR / | ||
|
||
COPY . ./ | ||
ADD https://github.com/ufoscout/docker-compose-wait/releases/download/2.5.0/wait /wait | ||
#RUN chmod +x /wait | ||
RUN /bin/bash -c 'ls -la /wait; chmod +x /wait; ls -la /wait' | ||
|
||
# install the report generator tool | ||
RUN dotnet tool install dotnet-reportgenerator-globaltool --version 4.2.15 --tool-path /tools | ||
|
||
CMD /wait && dotnet test --logger trx --results-directory /var/temp /p:CollectCoverage=true /p:CoverletOutputFormat=cobertura && mv /tests/Ardalis.Specification.UnitTests/coverage.cobertura.xml /var/temp/coverage.unit.cobertura.xml && mv /tests/Ardalis.Specification.IntegrationTests/coverage.cobertura.xml /var/temp/coverage.integration.cobertura.xml && tools/reportgenerator -reports:/var/temp/coverage.*.cobertura.xml -targetdir:/var/temp/coverage -reporttypes:HtmlInline_AzurePipelines\;HTMLChart\;Cobertura | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
reportgenerator "-reports:.\TestResults\coverage.integration.cobertura.xml;.\TestResults\coverage.unit.cobertura.xml" "-targetdir:CodeCoverage" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
reportgenerator "-reports:.\TestResults\coverage.integration.cobertura.xml;.\TestResults\coverage.unit.cobertura.xml" "-targetdir:.\CodeCoverage" "-reportTypes:HtmlInline_AzurePipelines;Cobertura" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
docker-compose build | ||
docker-compose up --abort-on-container-exit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
docker-compose build | ||
docker-compose up --abort-on-container-exit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
version: '3.4' | ||
|
||
services: | ||
tests: | ||
build: | ||
context: . | ||
dockerfile: Dockerfile | ||
environment: | ||
WAIT_HOSTS: database:1433 | ||
volumes: | ||
- ./TestResults:/var/temp | ||
depends_on: | ||
- database | ||
|
||
database: | ||
image: mcr.microsoft.com/mssql/server:2017-CU8-ubuntu | ||
environment: | ||
SA_PASSWORD: "P@ssW0rd!" | ||
ACCEPT_EULA: "Y" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
tests/Ardalis.Specification.IntegrationTests/Ardalis.Specification.IntegrationTests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netcoreapp2.2</TargetFramework> | ||
|
||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="coverlet.msbuild" Version="2.3.1"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
<PackageReference Include="ReportGenerator" Version="4.2.5" /> | ||
<PackageReference Include="Dapper" Version="1.60.5" /> | ||
<PackageReference Include="FluentAssertions" Version="5.6.0" /> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.2.4" /> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.2.4" /> | ||
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.2.0" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.0.1" /> | ||
<PackageReference Include="xunit" Version="2.4.0" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\Ardalis.Specification\Ardalis.Specification.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
138 changes: 138 additions & 0 deletions
138
tests/Ardalis.Specification.IntegrationTests/DatabaseCommunicationTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
using Ardalis.Specification.IntegrationTests.SampleClient; | ||
using Ardalis.Specification.IntegrationTests.SampleSpecs; | ||
using Dapper; | ||
using FluentAssertions; | ||
using Microsoft.EntityFrameworkCore; | ||
using System.Data.SqlClient; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
|
||
namespace Ardalis.Specification.IntegrationTests | ||
{ | ||
public class DatabaseCommunicationTests | ||
{ | ||
// Run EF Migrations\DBUp script to prepare database before running your tests. | ||
const string ConnectionString = "Data Source=database;Initial Catalog=SampleDatabase;PersistSecurityInfo=True;User ID=sa;Password=P@ssW0rd!"; | ||
public SampleDbContext _dbContext; | ||
public EfRepository<Blog> _blogRepository; | ||
public EfRepository<Post> _postRepository; | ||
|
||
public DatabaseCommunicationTests() | ||
{ | ||
var optionsBuilder = new DbContextOptionsBuilder<SampleDbContext>(); | ||
optionsBuilder.UseSqlServer(ConnectionString); | ||
_dbContext = new SampleDbContext(optionsBuilder.Options); | ||
|
||
// Run this if you've made seed data or schema changes to force the container to rebuild the db | ||
// _dbContext.Database.EnsureDeleted(); | ||
|
||
// Note: If the database exists, this will do nothing, so it only creates it once. | ||
// This is fine since these tests all perform read-only operations | ||
_dbContext.Database.EnsureCreated(); | ||
|
||
_blogRepository = new EfRepository<Blog>(_dbContext); | ||
_postRepository = new EfRepository<Post>(_dbContext); | ||
} | ||
|
||
[Fact] | ||
public async Task CanConnectAndRunQuery() | ||
{ | ||
using (var conn = new SqlConnection(ConnectionString)) | ||
{ | ||
await conn.OpenAsync(); | ||
const string query = "SELECT 1 AS Data"; | ||
var result = (await conn.QueryAsync<int>(query)).FirstOrDefault(); | ||
result.Should().Be(1); | ||
} | ||
} | ||
|
||
[Fact] | ||
public async Task GetBlogUsingEF() | ||
{ | ||
var result = _dbContext.Blogs.FirstOrDefault(); | ||
|
||
Assert.Equal(BlogBuilder.VALID_BLOG_ID, result.Id); | ||
} | ||
|
||
[Fact] | ||
public async Task GetBlogUsingEFRepository() | ||
{ | ||
var result = await _blogRepository.GetByIdAsync(BlogBuilder.VALID_BLOG_ID); | ||
|
||
result.Should().NotBeNull(); | ||
result.Name.Should().Be(BlogBuilder.VALID_BLOG_NAME); | ||
result.Posts.Count.Should().Be(0); | ||
} | ||
|
||
[Fact] | ||
public async Task GetBlogUsingEFRepositoryAndSpecShouldIncludePosts() | ||
{ | ||
var result = (await _blogRepository.ListAsync(new BlogWithPostsSpec(BlogBuilder.VALID_BLOG_ID))).SingleOrDefault(); | ||
|
||
result.Should().NotBeNull(); | ||
result.Name.Should().Be(BlogBuilder.VALID_BLOG_NAME); | ||
result.Posts.Count.Should().BeGreaterThan(100); | ||
} | ||
|
||
[Fact] | ||
public async Task GetBlogUsingEFRepositoryAndSpecWithStringIncludeShouldIncludePosts() | ||
{ | ||
var result = (await _blogRepository.ListAsync(new BlogWithPostsUsingStringSpec(BlogBuilder.VALID_BLOG_ID))).SingleOrDefault(); | ||
|
||
result.Should().NotBeNull(); | ||
result.Name.Should().Be(BlogBuilder.VALID_BLOG_NAME); | ||
result.Posts.Count.Should().BeGreaterThan(100); | ||
} | ||
|
||
[Fact] | ||
public async Task GetSecondPageOfPostsUsingPostsByBlogPaginatedSpec() | ||
{ | ||
int pageSize = 10; | ||
int pageIndex = 1; // page 2 | ||
var result = (await _postRepository.ListAsync(new PostsByBlogPaginatedSpec(pageIndex * pageSize, pageSize, BlogBuilder.VALID_BLOG_ID))).ToList(); | ||
|
||
result.Count.Should().Be(pageSize); | ||
result.First().Id.Should().Be(309); | ||
result.Last().Id.Should().Be(318); | ||
} | ||
|
||
[Fact] | ||
public async Task GetPostsWithOrderedSpec() | ||
{ | ||
var result = (await _postRepository.ListAsync(new PostsByBlogOrderedSpec(BlogBuilder.VALID_BLOG_ID))).ToList(); | ||
|
||
result.First().Id.Should().Be(234); | ||
result.Last().Id.Should().Be(399); | ||
} | ||
|
||
[Fact] | ||
public async Task GetPostsWithOrderedSpecDescending() | ||
{ | ||
var result = (await _postRepository.ListAsync(new PostsByBlogOrderedSpec(BlogBuilder.VALID_BLOG_ID, false))).ToList(); | ||
|
||
result.First().Id.Should().Be(399); | ||
result.Last().Id.Should().Be(234); | ||
} | ||
|
||
// TODO: This could move to the Unit Tests project if specs were in separate project | ||
[Fact] | ||
public async Task EnableCacheShouldSetCacheKeyProperly() | ||
{ | ||
var spec = new BlogWithPostsSpec(BlogBuilder.VALID_BLOG_ID); | ||
|
||
spec.CacheKey.Should().Be($"BlogWithPostsSpec-{BlogBuilder.VALID_BLOG_ID}"); | ||
} | ||
|
||
[Fact] | ||
public async Task GroupByShouldWorkProperlyl() | ||
{ | ||
var spec = new PostsGroupedByIdSpec(); | ||
var result = (await _postRepository.ListAsync(spec)).ToList(); | ||
|
||
result.First().Id.Should().Be(301); | ||
result.Skip(1).Take(1).First().Id.Should().Be(303); | ||
} | ||
} | ||
} |
Oops, something went wrong.