Skip to content

Commit

Permalink
Adding code coverage and more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ardalis committed Jul 19, 2019
1 parent 6cc2f0a commit 650d341
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -116,6 +116,7 @@ _TeamCity*
# Visual Studio code coverage results
*.coverage
*.coveragexml
CodeCoverage/

# NCrunch
_NCrunch_*
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Expand Up @@ -5,4 +5,4 @@ COPY . ./
ADD https://github.com/ufoscout/docker-compose-wait/releases/download/2.5.0/wait /wait
RUN chmod +x /wait

CMD /wait && dotnet test --logger trx --results-directory /var/temp
CMD /wait && dotnet test --logger trx --results-directory /var/temp /p:CollectCoverage=true /p:CoverletOutputFormat=cobertura && mv /app/tests/Ardalis.Specification.UnitTests/coverage.cobertura.xml /var/temp/coverage.unit.cobertura.xml && mv /app/tests/Ardalis.Specification.IntegrationTests/coverage.cobertura.xml /var/temp/coverage.integration.cobertura.xml
1 change: 1 addition & 0 deletions GetCoverage.bat
@@ -0,0 +1 @@
reportgenerator "-reports:.\TestResults\coverage.integration.cobertura.xml;.\TestResults\coverage.unit.cobertura.xml" "-targetdir:CodeCoverage"
Expand Up @@ -7,6 +7,11 @@
</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" />
Expand Down
Expand Up @@ -17,6 +17,7 @@ public class DatabaseCommunicationTests
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()
{
Expand All @@ -32,6 +33,7 @@ public DatabaseCommunicationTests()
_dbContext.Database.EnsureCreated();

_blogRepository = new EfRepository<Blog>(_dbContext);
_postRepository = new EfRepository<Post>(_dbContext);
}

[Fact]
Expand Down Expand Up @@ -74,17 +76,45 @@ public async Task GetBlogUsingEFRepositoryAndSpecShouldIncludePosts()
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 postRepo = new EfRepository<Post>(_dbContext);
var result = (await postRepo.ListAsync(new PostsByBlogPaginatedSpec(pageIndex * pageSize, pageSize, BlogBuilder.VALID_BLOG_ID))).ToList();
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);
}

}
}
@@ -0,0 +1,12 @@
using Ardalis.Specification.IntegrationTests.SampleClient;

namespace Ardalis.Specification.IntegrationTests.SampleSpecs
{
public class BlogWithPostsUsingStringSpec : BaseSpecification<Blog>
{
public BlogWithPostsUsingStringSpec(int id) : base(b => b.Id == id)
{
base.IncludeStrings.Add("Posts");
}
}
}
Expand Up @@ -10,4 +10,19 @@ public PostsByBlogPaginatedSpec(int skip, int take, int blogId)
ApplyPaging(skip, take);
}
}
public class PostsByBlogOrderedSpec : BaseSpecification<Post>
{
public PostsByBlogOrderedSpec(int blogId, bool isAscending = true)
: base(p => p.BlogId == blogId)
{
if(isAscending)
{
ApplyOrderBy(p => p.Id);
}
else
{
ApplyOrderByDescending(p => p.Id);
}
}
}
}

0 comments on commit 650d341

Please sign in to comment.