-
-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Code coverage: exclude obsolete members, prevent usage of stale coverage results in local build * Add tests to increase coverage * Add justification comment for coverage exclusion
- Loading branch information
Showing
7 changed files
with
256 additions
and
3 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
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
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,229 @@ | ||
using System.Net; | ||
using System.Text.Json; | ||
using FluentAssertions; | ||
using JsonApiDotNetCore.Configuration; | ||
using JsonApiDotNetCore.Serialization.Objects; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using NoEntityFrameworkExample.Models; | ||
using TestBuildingBlocks; | ||
using Xunit; | ||
|
||
namespace NoEntityFrameworkTests; | ||
|
||
public sealed class TagTests : IntegrationTest, IClassFixture<NoLoggingWebApplicationFactory<Tag>> | ||
{ | ||
private readonly NoLoggingWebApplicationFactory<Tag> _factory; | ||
|
||
protected override JsonSerializerOptions SerializerOptions | ||
{ | ||
get | ||
{ | ||
var options = _factory.Services.GetRequiredService<IJsonApiOptions>(); | ||
return options.SerializerOptions; | ||
} | ||
} | ||
|
||
public TagTests(NoLoggingWebApplicationFactory<Tag> factory) | ||
{ | ||
_factory = factory; | ||
} | ||
|
||
[Fact] | ||
public async Task Can_get_primary_resources() | ||
{ | ||
// Arrange | ||
const string route = "/api/tags"; | ||
|
||
// Act | ||
(HttpResponseMessage httpResponse, Document responseDocument) = await ExecuteGetAsync<Document>(route); | ||
|
||
// Assert | ||
httpResponse.ShouldHaveStatusCode(HttpStatusCode.OK); | ||
|
||
responseDocument.Data.ManyValue.ShouldHaveCount(3); | ||
|
||
responseDocument.Meta.Should().ContainTotal(3); | ||
} | ||
|
||
[Fact] | ||
public async Task Can_filter_in_primary_resources() | ||
{ | ||
// Arrange | ||
const string route = "/api/tags?filter=equals(name,'Personal')"; | ||
|
||
// Act | ||
(HttpResponseMessage httpResponse, Document responseDocument) = await ExecuteGetAsync<Document>(route); | ||
|
||
// Assert | ||
httpResponse.ShouldHaveStatusCode(HttpStatusCode.OK); | ||
|
||
responseDocument.Data.ManyValue.ShouldHaveCount(1); | ||
responseDocument.Data.ManyValue[0].Attributes.ShouldContainKey("name").With(value => value.Should().Be("Personal")); | ||
|
||
responseDocument.Meta.Should().ContainTotal(1); | ||
} | ||
|
||
[Fact] | ||
public async Task Can_filter_in_related_resources() | ||
{ | ||
// Arrange | ||
const string route = "/api/tags?filter=has(todoItems,equals(description,'Check emails'))"; | ||
|
||
// Act | ||
(HttpResponseMessage httpResponse, Document responseDocument) = await ExecuteGetAsync<Document>(route); | ||
|
||
// Assert | ||
httpResponse.ShouldHaveStatusCode(HttpStatusCode.OK); | ||
|
||
responseDocument.Data.ManyValue.ShouldHaveCount(1); | ||
responseDocument.Data.ManyValue[0].Attributes.ShouldContainKey("name").With(value => value.Should().Be("Business")); | ||
|
||
responseDocument.Meta.Should().ContainTotal(1); | ||
} | ||
|
||
[Fact] | ||
public async Task Can_sort_on_attribute_in_primary_resources() | ||
{ | ||
// Arrange | ||
const string route = "/api/tags?sort=-id"; | ||
|
||
// Act | ||
(HttpResponseMessage httpResponse, Document responseDocument) = await ExecuteGetAsync<Document>(route); | ||
|
||
// Assert | ||
httpResponse.ShouldHaveStatusCode(HttpStatusCode.OK); | ||
|
||
responseDocument.Data.ManyValue.ShouldHaveCount(3); | ||
responseDocument.Data.ManyValue[0].Id.Should().Be("3"); | ||
responseDocument.Data.ManyValue[1].Id.Should().Be("2"); | ||
responseDocument.Data.ManyValue[2].Id.Should().Be("1"); | ||
} | ||
|
||
[Fact] | ||
public async Task Can_sort_on_count_in_primary_resources() | ||
{ | ||
// Arrange | ||
const string route = "/api/tags?sort=-count(todoItems),id"; | ||
|
||
// Act | ||
(HttpResponseMessage httpResponse, Document responseDocument) = await ExecuteGetAsync<Document>(route); | ||
|
||
// Assert | ||
httpResponse.ShouldHaveStatusCode(HttpStatusCode.OK); | ||
|
||
responseDocument.Data.ManyValue.ShouldHaveCount(3); | ||
responseDocument.Data.ManyValue[0].Id.Should().Be("1"); | ||
responseDocument.Data.ManyValue[1].Id.Should().Be("2"); | ||
responseDocument.Data.ManyValue[2].Id.Should().Be("3"); | ||
} | ||
|
||
[Fact] | ||
public async Task Can_paginate_in_primary_resources() | ||
{ | ||
// Arrange | ||
const string route = "/api/tags?page[size]=1&page[number]=2&sort=id"; | ||
|
||
// Act | ||
(HttpResponseMessage httpResponse, Document responseDocument) = await ExecuteGetAsync<Document>(route); | ||
|
||
// Assert | ||
httpResponse.ShouldHaveStatusCode(HttpStatusCode.OK); | ||
|
||
responseDocument.Data.ManyValue.ShouldHaveCount(1); | ||
responseDocument.Data.ManyValue[0].Attributes.ShouldContainKey("name").With(value => value.Should().Be("Family")); | ||
|
||
responseDocument.Meta.Should().ContainTotal(3); | ||
} | ||
|
||
[Fact] | ||
public async Task Can_select_fields_in_primary_resources() | ||
{ | ||
// Arrange | ||
const string route = "/api/tags?fields[tags]=todoItems"; | ||
|
||
// Act | ||
(HttpResponseMessage httpResponse, Document responseDocument) = await ExecuteGetAsync<Document>(route); | ||
|
||
// Assert | ||
httpResponse.ShouldHaveStatusCode(HttpStatusCode.OK); | ||
|
||
responseDocument.Data.ManyValue.ShouldNotBeEmpty(); | ||
responseDocument.Data.ManyValue.Should().AllSatisfy(resource => resource.Attributes.Should().BeNull()); | ||
responseDocument.Data.ManyValue.Should().AllSatisfy(resource => resource.Relationships.ShouldOnlyContainKeys("todoItems")); | ||
} | ||
|
||
[Fact] | ||
public async Task Can_include_in_primary_resources() | ||
{ | ||
// Arrange | ||
const string route = "/api/tags?include=todoItems.owner"; | ||
|
||
// Act | ||
(HttpResponseMessage httpResponse, Document responseDocument) = await ExecuteGetAsync<Document>(route); | ||
|
||
// Assert | ||
httpResponse.ShouldHaveStatusCode(HttpStatusCode.OK); | ||
|
||
responseDocument.Data.ManyValue.Should().NotBeEmpty(); | ||
responseDocument.Included.Should().NotBeEmpty(); | ||
} | ||
|
||
[Fact] | ||
public async Task Can_get_primary_resource() | ||
{ | ||
// Arrange | ||
const string route = "/api/tags/1"; | ||
|
||
// Act | ||
(HttpResponseMessage httpResponse, Document responseDocument) = await ExecuteGetAsync<Document>(route); | ||
|
||
// Assert | ||
httpResponse.ShouldHaveStatusCode(HttpStatusCode.OK); | ||
|
||
responseDocument.Data.SingleValue.ShouldNotBeNull(); | ||
responseDocument.Data.SingleValue.Id.Should().Be("1"); | ||
} | ||
|
||
[Fact] | ||
public async Task Can_get_secondary_resources() | ||
{ | ||
// Arrange | ||
const string route = "/api/tags/1/todoItems?sort=id"; | ||
|
||
// Act | ||
(HttpResponseMessage httpResponse, Document responseDocument) = await ExecuteGetAsync<Document>(route); | ||
|
||
// Assert | ||
httpResponse.ShouldHaveStatusCode(HttpStatusCode.OK); | ||
|
||
responseDocument.Data.ManyValue.ShouldHaveCount(3); | ||
responseDocument.Data.ManyValue[0].Attributes.ShouldContainKey("description").With(value => value.Should().Be("Make homework")); | ||
responseDocument.Data.ManyValue[1].Attributes.ShouldContainKey("description").With(value => value.Should().Be("Book vacation")); | ||
responseDocument.Data.ManyValue[2].Attributes.ShouldContainKey("description").With(value => value.Should().Be("Cook dinner")); | ||
|
||
responseDocument.Meta.Should().ContainTotal(3); | ||
} | ||
|
||
[Fact] | ||
public async Task Can_get_ToMany_relationship() | ||
{ | ||
// Arrange | ||
const string route = "/api/tags/2/relationships/todoItems"; | ||
|
||
// Act | ||
(HttpResponseMessage httpResponse, Document responseDocument) = await ExecuteGetAsync<Document>(route); | ||
|
||
// Assert | ||
httpResponse.ShouldHaveStatusCode(HttpStatusCode.OK); | ||
|
||
responseDocument.Data.ManyValue.ShouldHaveCount(1); | ||
responseDocument.Data.ManyValue[0].Id.Should().Be("3"); | ||
|
||
responseDocument.Meta.Should().ContainTotal(1); | ||
} | ||
|
||
protected override HttpClient CreateClient() | ||
{ | ||
return _factory.CreateClient(); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
|
||
// https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/MSBuildIntegration.md#excluding-from-coverage | ||
// Justification: This assembly contains building blocks for writing tests. It does not contain code that ships. | ||
[assembly: ExcludeFromCodeCoverage] |
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,16 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<RunSettings> | ||
<RunConfiguration> | ||
<CollectSourceInformation>true</CollectSourceInformation> | ||
</RunConfiguration> | ||
<DataCollectionRunSettings> | ||
<DataCollectors> | ||
<DataCollector friendlyName="XPlat Code Coverage"> | ||
<Configuration> | ||
<ExcludeByAttribute>ObsoleteAttribute,GeneratedCodeAttribute</ExcludeByAttribute> | ||
<DeterministicReport>true</DeterministicReport> | ||
</Configuration> | ||
</DataCollector> | ||
</DataCollectors> | ||
</DataCollectionRunSettings> | ||
</RunSettings> |