Skip to content

Commit 255ed36

Browse files
author
Bart Koelman
committed
General cleanup
1 parent 8bf0989 commit 255ed36

21 files changed

+58
-112
lines changed

src/Examples/JsonApiDotNetCoreExample/Controllers/PassportsController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace JsonApiDotNetCoreExample.Controllers
88
{
99
public sealed class PassportsController : JsonApiController<Passport>
1010
{
11-
public PassportsController(IJsonApiOptions options, ILoggerFactory loggerFactory, IResourceService<Passport, int> resourceService)
11+
public PassportsController(IJsonApiOptions options, ILoggerFactory loggerFactory, IResourceService<Passport> resourceService)
1212
: base(options, loggerFactory, resourceService)
1313
{
1414
}

src/Examples/JsonApiDotNetCoreExample/Controllers/TagsController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public sealed class TagsController : JsonApiController<Tag>
1111
public TagsController(
1212
IJsonApiOptions options,
1313
ILoggerFactory loggerFactory,
14-
IResourceService<Tag, int> resourceService)
14+
IResourceService<Tag> resourceService)
1515
: base(options, loggerFactory, resourceService)
1616
{ }
1717
}

src/Examples/JsonApiDotNetCoreExample/Data/AppDbContext.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,10 @@ public sealed class AppDbContext : DbContext
1212
public DbSet<TodoItem> TodoItems { get; set; }
1313
public DbSet<Passport> Passports { get; set; }
1414
public DbSet<Person> People { get; set; }
15-
public DbSet<TodoItemCollection> TodoItemCollections { get; set; }
1615
public DbSet<Article> Articles { get; set; }
1716
public DbSet<Author> AuthorDifferentDbContextName { get; set; }
1817
public DbSet<User> Users { get; set; }
19-
public DbSet<PersonRole> PersonRoles { get; set; }
2018
public DbSet<ArticleTag> ArticleTags { get; set; }
21-
public DbSet<Tag> Tags { get; set; }
2219
public DbSet<Blog> Blogs { get; set; }
2320

2421
public AppDbContext(DbContextOptions<AppDbContext> options, ISystemClock systemClock) : base(options)

src/Examples/JsonApiDotNetCoreExample/Startups/Startup.cs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,38 +14,30 @@ namespace JsonApiDotNetCoreExample
1414
{
1515
public class Startup : EmptyStartup
1616
{
17+
private static readonly Version _postgresCiBuildVersion = new Version(9, 6);
1718
private readonly string _connectionString;
1819

19-
public Startup(IConfiguration configuration) : base(configuration)
20+
public Startup(IConfiguration configuration)
21+
: base(configuration)
2022
{
2123
string postgresPassword = Environment.GetEnvironmentVariable("PGPASSWORD") ?? "postgres";
2224
_connectionString = configuration["Data:DefaultConnection"].Replace("###", postgresPassword);
2325
}
2426

2527
public override void ConfigureServices(IServiceCollection services)
2628
{
27-
ConfigureClock(services);
29+
services.AddSingleton<ISystemClock, SystemClock>();
2830

2931
services.AddDbContext<AppDbContext>(options =>
3032
{
3133
options.EnableSensitiveDataLogging();
32-
options.UseNpgsql(_connectionString, innerOptions => innerOptions.SetPostgresVersion(new Version(9, 6)));
33-
},
34-
// TODO: Remove ServiceLifetime.Transient, after all integration tests have been converted to use IntegrationTestContext.
35-
ServiceLifetime.Transient);
34+
options.UseNpgsql(_connectionString, postgresOptions => postgresOptions.SetPostgresVersion(_postgresCiBuildVersion));
35+
});
3636

3737
services.AddJsonApi<AppDbContext>(ConfigureJsonApiOptions, discovery => discovery.AddCurrentAssembly());
38-
39-
// once all tests have been moved to WebApplicationFactory format we can get rid of this line below
40-
services.AddClientSerialization();
4138
}
4239

43-
protected virtual void ConfigureClock(IServiceCollection services)
44-
{
45-
services.AddSingleton<ISystemClock, SystemClock>();
46-
}
47-
48-
protected virtual void ConfigureJsonApiOptions(JsonApiOptions options)
40+
protected void ConfigureJsonApiOptions(JsonApiOptions options)
4941
{
5042
options.IncludeExceptionStackTraceInErrors = true;
5143
options.Namespace = "api/v1";
@@ -63,7 +55,7 @@ public override void Configure(IApplicationBuilder app, IWebHostEnvironment envi
6355
var appDbContext = scope.ServiceProvider.GetRequiredService<AppDbContext>();
6456
appDbContext.Database.EnsureCreated();
6557
}
66-
58+
6759
app.UseRouting();
6860
app.UseJsonApi();
6961
app.UseEndpoints(endpoints => endpoints.MapControllers());

test/JsonApiDotNetCoreExampleTests/AppDbContextExtensions.cs

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -57,27 +57,5 @@ private static async Task ClearTablesAsync(this DbContext dbContext, params Type
5757
}
5858
}
5959
}
60-
61-
public static void ClearTable<TEntity>(this DbContext dbContext) where TEntity : class
62-
{
63-
var entityType = dbContext.Model.FindEntityType(typeof(TEntity));
64-
if (entityType == null)
65-
{
66-
throw new InvalidOperationException($"Table for '{typeof(TEntity).Name}' not found.");
67-
}
68-
69-
string tableName = entityType.GetTableName();
70-
71-
// PERF: We first try to clear the table, which is fast and usually succeeds, unless foreign key constraints are violated.
72-
// In that case, we recursively delete all related data, which is slow.
73-
try
74-
{
75-
dbContext.Database.ExecuteSqlRaw("delete from \"" + tableName + "\"");
76-
}
77-
catch (PostgresException)
78-
{
79-
dbContext.Database.ExecuteSqlRaw("truncate table \"" + tableName + "\" cascade");
80-
}
81-
}
8260
}
8361
}
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
using Bogus;
33
using JsonApiDotNetCoreExample.Data;
44
using JsonApiDotNetCoreExample.Models;
5+
using JsonApiDotNetCoreExampleTests.IntegrationTests;
56
using Microsoft.Extensions.DependencyInjection;
67
using Person = JsonApiDotNetCoreExample.Models.Person;
78

8-
namespace JsonApiDotNetCoreExampleTests.IntegrationTests.ResourceHooks
9+
namespace JsonApiDotNetCoreExampleTests
910
{
10-
internal sealed class HookFakers : FakerContainer
11+
internal sealed class ExampleFakers : FakerContainer
1112
{
1213
private readonly IServiceProvider _serviceProvider;
1314

@@ -58,7 +59,7 @@ internal sealed class HookFakers : FakerContainer
5859
public Faker<Person> Person => _lazyPersonFaker.Value;
5960
public Faker<Tag> Tag => _lazyTagFaker.Value;
6061

61-
public HookFakers(IServiceProvider serviceProvider)
62+
public ExampleFakers(IServiceProvider serviceProvider)
6263
{
6364
_serviceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider));
6465

test/JsonApiDotNetCoreExampleTests/IntegrationTests/FakerContainer.cs renamed to test/JsonApiDotNetCoreExampleTests/FakerContainer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using System.Reflection;
55
using Xunit;
66

7-
namespace JsonApiDotNetCoreExampleTests.IntegrationTests
7+
namespace JsonApiDotNetCoreExampleTests
88
{
99
internal abstract class FakerContainer
1010
{

test/JsonApiDotNetCoreExampleTests/IntegrationTests/Pagination/PaginationWithTotalCountTests.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using System.Linq;
33
using System.Net;
44
using System.Threading.Tasks;
5-
using Bogus;
65
using FluentAssertions;
76
using FluentAssertions.Extensions;
87
using JsonApiDotNetCore.Configuration;
@@ -12,7 +11,6 @@
1211
using JsonApiDotNetCoreExample.Models;
1312
using Microsoft.Extensions.DependencyInjection;
1413
using Xunit;
15-
using Person = JsonApiDotNetCoreExample.Models.Person;
1614

1715
namespace JsonApiDotNetCoreExampleTests.IntegrationTests.Pagination
1816
{
@@ -21,7 +19,7 @@ public sealed class PaginationWithTotalCountTests : IClassFixture<IntegrationTes
2119
private const int _defaultPageSize = 5;
2220

2321
private readonly IntegrationTestContext<Startup, AppDbContext> _testContext;
24-
private readonly Faker<TodoItem> _todoItemFaker = new Faker<TodoItem>();
22+
private readonly ExampleFakers _fakers;
2523

2624
public PaginationWithTotalCountTests(IntegrationTestContext<Startup, AppDbContext> testContext)
2725
{
@@ -36,6 +34,8 @@ public PaginationWithTotalCountTests(IntegrationTestContext<Startup, AppDbContex
3634

3735
options.DisableTopPagination = false;
3836
options.DisableChildrenPagination = false;
37+
38+
_fakers = new ExampleFakers(testContext.Factory.Services);
3939
}
4040

4141
[Fact]
@@ -714,7 +714,7 @@ public async Task Renders_correct_top_level_links_for_page_number(int pageNumber
714714
};
715715

716716
const int totalCount = 3 * _defaultPageSize + 3;
717-
var todoItems = _todoItemFaker.Generate(totalCount);
717+
var todoItems = _fakers.TodoItem.Generate(totalCount);
718718

719719
foreach (var todoItem in todoItems)
720720
{
@@ -739,46 +739,46 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
739739
// Assert
740740
httpResponse.Should().HaveStatusCode(HttpStatusCode.OK);
741741

742-
Assert.Equal("http://localhost" + route, responseDocument.Links.Self);
742+
responseDocument.Links.Self.Should().Be("http://localhost" + route);
743743

744744
if (firstLink != null)
745745
{
746746
var expected = "http://localhost" + SetPageNumberInUrl(routePrefix, firstLink.Value);
747-
Assert.Equal(expected, responseDocument.Links.First);
747+
responseDocument.Links.First.Should().Be(expected);
748748
}
749749
else
750750
{
751-
Assert.Null(responseDocument.Links.First);
751+
responseDocument.Links.First.Should().BeNull();
752752
}
753753

754754
if (prevLink != null)
755755
{
756756
var expected = "http://localhost" + SetPageNumberInUrl(routePrefix, prevLink.Value);
757-
Assert.Equal(expected, responseDocument.Links.Prev);
757+
responseDocument.Links.Prev.Should().Be(expected);
758758
}
759759
else
760760
{
761-
Assert.Null(responseDocument.Links.Prev);
761+
responseDocument.Links.Prev.Should().BeNull();
762762
}
763763

764764
if (nextLink != null)
765765
{
766766
var expected = "http://localhost" + SetPageNumberInUrl(routePrefix, nextLink.Value);
767-
Assert.Equal(expected, responseDocument.Links.Next);
767+
responseDocument.Links.Next.Should().Be(expected);
768768
}
769769
else
770770
{
771-
Assert.Null(responseDocument.Links.Next);
771+
responseDocument.Links.Next.Should().BeNull();
772772
}
773773

774774
if (lastLink != null)
775775
{
776776
var expected = "http://localhost" + SetPageNumberInUrl(routePrefix, lastLink.Value);
777-
Assert.Equal(expected, responseDocument.Links.Last);
777+
responseDocument.Links.Last.Should().Be(expected);
778778
}
779779
else
780780
{
781-
Assert.Null(responseDocument.Links.Last);
781+
responseDocument.Links.Last.Should().BeNull();
782782
}
783783

784784
static string SetPageNumberInUrl(string url, int pageNumber)

test/JsonApiDotNetCoreExampleTests/IntegrationTests/Pagination/PaginationWithoutTotalCountTests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System.Net;
22
using System.Threading.Tasks;
3-
using Bogus;
43
using FluentAssertions;
54
using JsonApiDotNetCore.Configuration;
65
using JsonApiDotNetCore.Serialization.Objects;
@@ -17,8 +16,7 @@ public sealed class PaginationWithoutTotalCountTests : IClassFixture<Integration
1716
private const int _defaultPageSize = 5;
1817

1918
private readonly IntegrationTestContext<Startup, AppDbContext> _testContext;
20-
21-
private readonly Faker<Article> _articleFaker = new Faker<Article>();
19+
private readonly ExampleFakers _fakers;
2220

2321
public PaginationWithoutTotalCountTests(IntegrationTestContext<Startup, AppDbContext> testContext)
2422
{
@@ -29,6 +27,8 @@ public PaginationWithoutTotalCountTests(IntegrationTestContext<Startup, AppDbCon
2927
options.IncludeTotalResourceCount = false;
3028
options.DefaultPageSize = new PageSize(_defaultPageSize);
3129
options.AllowUnknownQueryStringParameters = true;
30+
31+
_fakers = new ExampleFakers(testContext.Factory.Services);
3232
}
3333

3434
[Fact]
@@ -113,7 +113,7 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
113113
public async Task When_page_number_is_specified_in_query_string_with_partially_filled_page_it_should_render_pagination_links()
114114
{
115115
// Arrange
116-
var articles = _articleFaker.Generate(12);
116+
var articles = _fakers.Article.Generate(12);
117117

118118
await _testContext.RunOnDatabaseAsync(async dbContext =>
119119
{
@@ -145,7 +145,7 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
145145
public async Task When_page_number_is_specified_in_query_string_with_full_page_it_should_render_pagination_links()
146146
{
147147
// Arrange
148-
var articles = _articleFaker.Generate(_defaultPageSize * 3);
148+
var articles = _fakers.Article.Generate(_defaultPageSize * 3);
149149

150150
await _testContext.RunOnDatabaseAsync(async dbContext =>
151151
{
@@ -179,7 +179,7 @@ public async Task When_page_number_is_specified_in_query_string_with_full_page_o
179179
// Arrange
180180
var author = new Author
181181
{
182-
Articles = _articleFaker.Generate(_defaultPageSize * 3)
182+
Articles = _fakers.Article.Generate(_defaultPageSize * 3)
183183
};
184184

185185
await _testContext.RunOnDatabaseAsync(async dbContext =>

test/JsonApiDotNetCoreExampleTests/IntegrationTests/Pagination/RangeValidationTests.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System.Net;
22
using System.Threading.Tasks;
3-
using Bogus;
43
using FluentAssertions;
54
using JsonApiDotNetCore.Configuration;
65
using JsonApiDotNetCore.Serialization.Objects;
@@ -15,7 +14,7 @@ namespace JsonApiDotNetCoreExampleTests.IntegrationTests.Pagination
1514
public sealed class RangeValidationTests : IClassFixture<IntegrationTestContext<Startup, AppDbContext>>
1615
{
1716
private readonly IntegrationTestContext<Startup, AppDbContext> _testContext;
18-
private readonly Faker<TodoItem> _todoItemFaker = new Faker<TodoItem>();
17+
private readonly ExampleFakers _fakers;
1918

2019
private const int _defaultPageSize = 5;
2120

@@ -27,6 +26,8 @@ public RangeValidationTests(IntegrationTestContext<Startup, AppDbContext> testCo
2726
options.DefaultPageSize = new PageSize(_defaultPageSize);
2827
options.MaximumPageSize = null;
2928
options.MaximumPageNumber = null;
29+
30+
_fakers = new ExampleFakers(testContext.Factory.Services);
3031
}
3132

3233
[Fact]
@@ -84,7 +85,7 @@ public async Task When_page_number_is_positive_it_must_succeed()
8485
public async Task When_page_number_is_too_high_it_must_return_empty_set_of_resources()
8586
{
8687
// Arrange
87-
var todoItems = _todoItemFaker.Generate(3);
88+
var todoItems = _fakers.TodoItem.Generate(3);
8889

8990
await _testContext.RunOnDatabaseAsync(async dbContext =>
9091
{

test/JsonApiDotNetCoreExampleTests/IntegrationTests/ResourceHooks/ResourceHookTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public sealed class ResourceHookTests
2424
: IClassFixture<IntegrationTestContext<ResourceHooksStartup<AppDbContext>, AppDbContext>>
2525
{
2626
private readonly IntegrationTestContext<ResourceHooksStartup<AppDbContext>, AppDbContext> _testContext;
27-
private readonly HookFakers _fakers;
27+
private readonly ExampleFakers _fakers;
2828

2929
public ResourceHookTests(IntegrationTestContext<ResourceHooksStartup<AppDbContext>, AppDbContext> testContext)
3030
{
@@ -43,7 +43,7 @@ public ResourceHookTests(IntegrationTestContext<ResourceHooksStartup<AppDbContex
4343
options.DisableTopPagination = false;
4444
options.DisableChildrenPagination = false;
4545

46-
_fakers = new HookFakers(testContext.Factory.Services);
46+
_fakers = new ExampleFakers(testContext.Factory.Services);
4747
}
4848

4949
[Fact]

test/JsonApiDotNetCoreExampleTests/IntegrationTests/RestrictedControllers/BlockingHttpDeleteController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace JsonApiDotNetCoreExampleTests.IntegrationTests.RestrictedControllers
1212
public sealed class BlockingHttpDeleteController : JsonApiController<Sofa>
1313
{
1414
public BlockingHttpDeleteController(IJsonApiOptions options, ILoggerFactory loggerFactory,
15-
IResourceService<Sofa, int> resourceService)
15+
IResourceService<Sofa> resourceService)
1616
: base(options, loggerFactory, resourceService)
1717
{
1818
}

test/JsonApiDotNetCoreExampleTests/IntegrationTests/RestrictedControllers/BlockingHttpPatchController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace JsonApiDotNetCoreExampleTests.IntegrationTests.RestrictedControllers
1010
public sealed class BlockingHttpPatchController : JsonApiController<Chair>
1111
{
1212
public BlockingHttpPatchController(IJsonApiOptions options, ILoggerFactory loggerFactory,
13-
IResourceService<Chair, int> resourceService)
13+
IResourceService<Chair> resourceService)
1414
: base(options, loggerFactory, resourceService)
1515
{
1616
}

test/JsonApiDotNetCoreExampleTests/IntegrationTests/RestrictedControllers/BlockingHttpPostController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace JsonApiDotNetCoreExampleTests.IntegrationTests.RestrictedControllers
1010
public sealed class BlockingHttpPostController : JsonApiController<Table>
1111
{
1212
public BlockingHttpPostController(IJsonApiOptions options, ILoggerFactory loggerFactory,
13-
IResourceService<Table, int> resourceService)
13+
IResourceService<Table> resourceService)
1414
: base(options, loggerFactory, resourceService)
1515
{
1616
}

test/JsonApiDotNetCoreExampleTests/IntegrationTests/RestrictedControllers/BlockingWritesController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace JsonApiDotNetCoreExampleTests.IntegrationTests.RestrictedControllers
1111
public sealed class BlockingWritesController : JsonApiController<Bed>
1212
{
1313
public BlockingWritesController(IJsonApiOptions options, ILoggerFactory loggerFactory,
14-
IResourceService<Bed, int> resourceService)
14+
IResourceService<Bed> resourceService)
1515
: base(options, loggerFactory, resourceService)
1616
{
1717
}

0 commit comments

Comments
 (0)