Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
355083c
Refactored tests for HttpReadOnly/NoHttpPost/Patch/Delete controller …
Jan 26, 2021
90274c6
Refactored tests for casing convention
Jan 26, 2021
4bdb3e8
Refactored tests for custom exception handler
Jan 26, 2021
cbe01c2
Refactored tests for nulls/default query string parameters; removed s…
Jan 26, 2021
0c5d5e8
Refactored tests for DisableQueryStringAttribute
Jan 27, 2021
7057c89
Refactored tests for resource injection
Jan 28, 2021
8756f25
Fixed assertions on DateTime/DateTimeOffset in tests
Jan 28, 2021
44653f8
Refactored tests for non-json:api controllers
Jan 28, 2021
22738b6
Refactored tests for ActionResult usage
Jan 28, 2021
e12d27f
Refactored tests for custom routes
Feb 2, 2021
ddcd4ac
Refactored tests for links rendering
Feb 2, 2021
8b443d8
Refactored tests for exception handling in serializer
Feb 2, 2021
64e09db
Refactored tests for serialization
Feb 2, 2021
8bf0989
Refactored tests for resource hooks
Feb 2, 2021
255ed36
General cleanup
Feb 3, 2021
c7000b8
Extracted test building blocks in shared project
Feb 4, 2021
db9778e
Auto-adjust namespaces
Feb 4, 2021
d9a2ac7
Refactored tests for service discovery
Feb 4, 2021
53f8c82
Refactored tests for no EF Core
Feb 4, 2021
86bac20
Renamed IntegrationTestContext to ExampleIntegrationTestContext becau…
Feb 4, 2021
d94235d
Adjusted test names for ModelStateValidationTests
Feb 4, 2021
c8a6012
Adjusted test names for pagination
Feb 4, 2021
cb983b9
Adjusted test names for meta
Feb 4, 2021
5ad5c76
Fixed broken tests; simpler bootstrap setup
Feb 4, 2021
838a158
Adjusted test names for hooks
Feb 4, 2021
d3ff09a
Enable concurrent testruns (makes running all tests 60% faster)
Feb 4, 2021
64c5b4a
Removed unused using
Feb 4, 2021
e2a2bb0
Merge branch 'master' into refactor-tests
Feb 4, 2021
bcb7320
Cleanup tests for hooks
Feb 4, 2021
60ac856
Cleanup example models
Feb 4, 2021
de40fb7
Revert "Enable concurrent testruns (makes running all tests 60% faste…
Feb 4, 2021
bd11b52
fixed failing testrunner
Feb 4, 2021
b0ac024
Enable running integration tests in parallel
Feb 4, 2021
f343d08
test
Feb 4, 2021
38c127a
Disable duplicate builds
Feb 4, 2021
9ee3fc5
Revert "Disable duplicate builds"
Feb 4, 2021
59fccd8
Addressed review feedback
Feb 5, 2021
21b94c5
Moved integration tests for filter/include/page/sort/fields into Quer…
Feb 5, 2021
a044cf0
Move types into separate files
Feb 5, 2021
78bc0c3
Moved unit tests for query string parameters into ExampleTests projec…
Feb 5, 2021
c2da909
Refactored query string integration tests to use private models. Chan…
Feb 5, 2021
e594391
Refactored query string unit tests to use private models.
Feb 5, 2021
f2fd733
Cleanup models in example project
Feb 6, 2021
f97b81f
Addressed review feedback
Feb 8, 2021
2516d2e
Rename
Feb 8, 2021
114dc07
Refactored tests for logging
Feb 8, 2021
37487cf
Refactored tests for meta
Feb 8, 2021
ea404bf
renamed ResourceHook tests and fixed a few wrong assertions
maurei Feb 8, 2021
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Refactored tests for meta
  • Loading branch information
Bart Koelman committed Feb 8, 2021
commit 37487cfec208e136191f1908de13c09ea9db70fe
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using JsonApiDotNetCore.Configuration;
using JsonApiDotNetCore.Controllers;
using JsonApiDotNetCore.Services;
using Microsoft.Extensions.Logging;

namespace JsonApiDotNetCoreExampleTests.IntegrationTests.Meta
{
public sealed class ProductFamiliesController : JsonApiController<ProductFamily>
{
public ProductFamiliesController(IJsonApiOptions options, ILoggerFactory loggerFactory,
IResourceService<ProductFamily> resourceService)
: base(options, loggerFactory, resourceService)
{
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Collections.Generic;
using JsonApiDotNetCore.Resources;
using JsonApiDotNetCore.Resources.Annotations;

namespace JsonApiDotNetCoreExampleTests.IntegrationTests.Meta
{
public sealed class ProductFamily : Identifiable
{
[Attr]
public string Name { get; set; }

[HasMany]
public IList<SupportTicket> Tickets { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,45 +1,47 @@
using System.Collections.Generic;
using System.Net;
using System.Threading.Tasks;
using FluentAssertions;
using JsonApiDotNetCore.Resources;
using JsonApiDotNetCore.Serialization.Objects;
using JsonApiDotNetCoreExample;
using JsonApiDotNetCoreExample.Data;
using JsonApiDotNetCoreExample.Models;
using JsonApiDotNetCoreExampleTests.Startups;
using Microsoft.Extensions.DependencyInjection;
using TestBuildingBlocks;
using Xunit;

namespace JsonApiDotNetCoreExampleTests.IntegrationTests.Meta
{
public sealed class ResourceMetaTests : IClassFixture<ExampleIntegrationTestContext<Startup, AppDbContext>>
public sealed class ResourceMetaTests
: IClassFixture<ExampleIntegrationTestContext<TestableStartup<SupportDbContext>, SupportDbContext>>
{
private readonly ExampleIntegrationTestContext<Startup, AppDbContext> _testContext;
private readonly ExampleIntegrationTestContext<TestableStartup<SupportDbContext>, SupportDbContext> _testContext;
private readonly SupportFakers _fakers = new SupportFakers();

public ResourceMetaTests(ExampleIntegrationTestContext<Startup, AppDbContext> testContext)
public ResourceMetaTests(ExampleIntegrationTestContext<TestableStartup<SupportDbContext>, SupportDbContext> testContext)
{
_testContext = testContext;

testContext.ConfigureServicesAfterStartup(services =>
{
services.AddScoped<IResourceDefinition<SupportTicket>, SupportTicketDefinition>();
});
}

[Fact]
public async Task Returns_resource_meta_from_ResourceDefinition()
{
// Arrange
var todoItems = new[]
{
new TodoItem {Description = "Important: Pay the bills"},
new TodoItem {Description = "Plan my birthday party"},
new TodoItem {Description = "Important: Call mom"}
};
var tickets = _fakers.SupportTicket.Generate(3);
tickets[0].Description = "Critical: " + tickets[0].Description;
tickets[2].Description = "Critical: " + tickets[2].Description;

await _testContext.RunOnDatabaseAsync(async dbContext =>
{
await dbContext.ClearTableAsync<TodoItem>();
dbContext.TodoItems.AddRange(todoItems);

await dbContext.ClearTableAsync<SupportTicket>();
dbContext.SupportTickets.AddRange(tickets);
await dbContext.SaveChangesAsync();
});

var route = "/api/v1/todoItems";
var route = "/supportTickets";

// Act
var (httpResponse, responseDocument) = await _testContext.ExecuteGetAsync<Document>(route);
Expand All @@ -57,22 +59,18 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
public async Task Returns_resource_meta_from_ResourceDefinition_in_included_resources()
{
// Arrange
var person = new Person
{
TodoItems = new HashSet<TodoItem>
{
new TodoItem {Description = "Important: Pay the bills"}
}
};
var family = _fakers.ProductFamily.Generate();
family.Tickets = _fakers.SupportTicket.Generate(1);
family.Tickets[0].Description = "Critical: " + family.Tickets[0].Description;

await _testContext.RunOnDatabaseAsync(async dbContext =>
{
await dbContext.ClearTableAsync<TodoItem>();
dbContext.People.Add(person);
await dbContext.ClearTableAsync<ProductFamily>();
dbContext.ProductFamilies.Add(family);
await dbContext.SaveChangesAsync();
});

var route = $"/api/v1/people/{person.StringId}?include=todoItems";
var route = $"/productFamilies/{family.StringId}?include=tickets";

// Act
var (httpResponse, responseDocument) = await _testContext.ExecuteGetAsync<Document>(route);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,25 @@
using FluentAssertions;
using JsonApiDotNetCore.Configuration;
using JsonApiDotNetCore.Serialization;
using JsonApiDotNetCoreExample;
using JsonApiDotNetCoreExample.Data;
using JsonApiDotNetCoreExample.Models;
using JsonApiDotNetCoreExampleTests.Startups;
using Microsoft.Extensions.DependencyInjection;
using TestBuildingBlocks;
using Xunit;

namespace JsonApiDotNetCoreExampleTests.IntegrationTests.Meta
{
public sealed class ResponseMetaTests : IClassFixture<ExampleIntegrationTestContext<Startup, AppDbContext>>
public sealed class ResponseMetaTests
: IClassFixture<ExampleIntegrationTestContext<TestableStartup<SupportDbContext>, SupportDbContext>>
{
private readonly ExampleIntegrationTestContext<Startup, AppDbContext> _testContext;
private readonly ExampleIntegrationTestContext<TestableStartup<SupportDbContext>, SupportDbContext> _testContext;

public ResponseMetaTests(ExampleIntegrationTestContext<Startup, AppDbContext> testContext)
public ResponseMetaTests(ExampleIntegrationTestContext<TestableStartup<SupportDbContext>, SupportDbContext> testContext)
{
_testContext = testContext;

testContext.ConfigureServicesAfterStartup(services =>
{
services.AddSingleton<IResponseMeta, TestResponseMeta>();
services.AddSingleton<IResponseMeta, SupportResponseMeta>();
});

var options = (JsonApiOptions) testContext.Factory.Services.GetRequiredService<IJsonApiOptions>();
Expand All @@ -33,9 +32,12 @@ public ResponseMetaTests(ExampleIntegrationTestContext<Startup, AppDbContext> te
public async Task Returns_top_level_meta()
{
// Arrange
await _testContext.RunOnDatabaseAsync(async dbContext => { await dbContext.ClearTableAsync<Person>(); });
await _testContext.RunOnDatabaseAsync(async dbContext =>
{
await dbContext.ClearTableAsync<SupportTicket>();
});

var route = "/api/v1/people";
var route = "/supportTickets";

// Act
var (httpResponse, responseDocument) = await _testContext.ExecuteGetAsync<string>(route);
Expand All @@ -55,8 +57,8 @@ public async Task Returns_top_level_meta()
]
},
""links"": {
""self"": ""http://localhost/api/v1/people"",
""first"": ""http://localhost/api/v1/people""
""self"": ""http://localhost/supportTickets"",
""first"": ""http://localhost/supportTickets""
},
""data"": []
}");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Microsoft.EntityFrameworkCore;

namespace JsonApiDotNetCoreExampleTests.IntegrationTests.Meta
{
public sealed class SupportDbContext : DbContext
{
public DbSet<ProductFamily> ProductFamilies { get; set; }
public DbSet<SupportTicket> SupportTickets { get; set; }

public SupportDbContext(DbContextOptions<SupportDbContext> options)
: base(options)
{
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using Bogus;
using TestBuildingBlocks;

namespace JsonApiDotNetCoreExampleTests.IntegrationTests.Meta
{
internal sealed class SupportFakers : FakerContainer
{
private readonly Lazy<Faker<ProductFamily>> _lazyProductFamilyFaker = new Lazy<Faker<ProductFamily>>(() =>
new Faker<ProductFamily>()
.UseSeed(GetFakerSeed())
.RuleFor(productFamily => productFamily.Name, f => f.Commerce.ProductName()));

private readonly Lazy<Faker<SupportTicket>> _lazySupportTicketFaker = new Lazy<Faker<SupportTicket>>(() =>
new Faker<SupportTicket>()
.UseSeed(GetFakerSeed())
.RuleFor(supportTicket => supportTicket.Description, f => f.Lorem.Paragraph()));

public Faker<ProductFamily> ProductFamily => _lazyProductFamilyFaker.Value;
public Faker<SupportTicket> SupportTicket => _lazySupportTicketFaker.Value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace JsonApiDotNetCoreExampleTests.IntegrationTests.Meta
{
public sealed class TestResponseMeta : IResponseMeta
public sealed class SupportResponseMeta : IResponseMeta
{
public IReadOnlyDictionary<string, object> GetMeta()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using JsonApiDotNetCore.Resources;
using JsonApiDotNetCore.Resources.Annotations;

namespace JsonApiDotNetCoreExampleTests.IntegrationTests.Meta
{
public sealed class SupportTicket : Identifiable
{
[Attr]
public string Description { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
using System.Collections.Generic;
using JsonApiDotNetCore.Configuration;
using JsonApiDotNetCore.Resources;
using JsonApiDotNetCoreExample.Models;

namespace JsonApiDotNetCoreExample.Definitions
namespace JsonApiDotNetCoreExampleTests.IntegrationTests.Meta
{
public sealed class TodoItemDefinition : JsonApiResourceDefinition<TodoItem>
public sealed class SupportTicketDefinition : JsonApiResourceDefinition<SupportTicket>
{
public TodoItemDefinition(IResourceGraph resourceGraph) : base(resourceGraph)
public SupportTicketDefinition(IResourceGraph resourceGraph) : base(resourceGraph)
{
}

public override IDictionary<string, object> GetMeta(TodoItem resource)
public override IDictionary<string, object> GetMeta(SupportTicket resource)
{
if (resource.Description != null && resource.Description.StartsWith("Important:"))
if (resource.Description != null && resource.Description.StartsWith("Critical:"))
{
return new Dictionary<string, object>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using JsonApiDotNetCore.Configuration;
using JsonApiDotNetCore.Controllers;
using JsonApiDotNetCore.Services;
using Microsoft.Extensions.Logging;

namespace JsonApiDotNetCoreExampleTests.IntegrationTests.Meta
{
public sealed class SupportTicketsController : JsonApiController<SupportTicket>
{
public SupportTicketsController(IJsonApiOptions options, ILoggerFactory loggerFactory,
IResourceService<SupportTicket> resourceService)
: base(options, loggerFactory, resourceService)
{
}
}
}
Loading