Skip to content

Add example projects and tests #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 43 commits into from
Jan 12, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
99e7d50
Add example projects and tests
mrnkr Jan 1, 2021
c53096a
throw proper error when fetching relationships
mrnkr Jan 2, 2021
b42b381
relationship creation tests
mrnkr Jan 2, 2021
c815c29
fix: unsupported relationships message
mrnkr Jan 2, 2021
c3f9dcf
test to-many relationship creation
mrnkr Jan 2, 2021
f348ff9
test unsupported filters
mrnkr Jan 2, 2021
1b49d01
test for readonly attributes
mrnkr Jan 2, 2021
fc94301
suggested changes to validation and IModel implementation
mrnkr Jan 3, 2021
ab7fed5
cleanup last commit
mrnkr Jan 3, 2021
970692b
remove unnecessary MongoDBRefs
mrnkr Jan 3, 2021
3ec805d
added update relationship tests
mrnkr Jan 3, 2021
0a3604e
address review comments
mrnkr Jan 4, 2021
ca2588d
update README.md
mrnkr Jan 4, 2021
1bcef1f
address review comments
mrnkr Jan 6, 2021
e384b4e
bring latest changes in IntegrationTestContext
mrnkr Jan 6, 2021
aa140cb
Fixed handling DateTimes with ambiguous kind.
Jan 6, 2021
310b972
Added test for resource meta
Jan 6, 2021
f3e63b2
Cleanup solution: Move GettingStarted to the root and make it the def…
Jan 6, 2021
09456b0
Simplified code
Jan 6, 2021
465d720
fix README.md
mrnkr Jan 6, 2021
054501b
remove unnecessary test for sparse fieldsets
mrnkr Jan 6, 2021
2446ae4
remove unnecessary tests from CreateResourceTests
mrnkr Jan 6, 2021
a7b8713
address review comments
mrnkr Jan 6, 2021
c97d773
fix: custom query parameter support
mrnkr Jan 7, 2021
78f058c
fix: sort on HasMany test passing
mrnkr Jan 7, 2021
72430bc
added tests for relationship support in SparseFieldSetTests
mrnkr Jan 7, 2021
99192c1
remove unnecessary ReadWrite tests
mrnkr Jan 7, 2021
afb71a5
fix: sort ids descending test
mrnkr Jan 7, 2021
a3742bd
cleanup sparse fieldset tests
mrnkr Jan 7, 2021
5341ba9
move MongoDatabaseExtensions to test project
mrnkr Jan 8, 2021
27f72f7
fix: sparse fieldset test
mrnkr Jan 8, 2021
e4b94bd
cleanup tests
mrnkr Jan 8, 2021
aae4eeb
latest requested changes minus ReadWrite tests
mrnkr Jan 9, 2021
0f3272b
add missing ReadWrite tests
mrnkr Jan 9, 2021
a9e1bc8
Fixed failing tests
Jan 10, 2021
4e5bf56
address review comments
mrnkr Jan 10, 2021
33add8b
Fixed: do not return relationship links in response json
Jan 11, 2021
2a73164
Fixed: the Task returned from Collection.InsertOneAsync was not await…
Jan 11, 2021
11646fc
address review comments
mrnkr Jan 11, 2021
461eb32
Cleanup IoC registrations to properly detect when non-string IDs are …
Jan 12, 2021
4e50bc3
Removed unused types
Jan 12, 2021
d7c7520
Updated example
Jan 12, 2021
3451619
rename public static class ServiceCollectionExtensions.cs to ServiceC…
mrnkr Jan 12, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions JsonApiDotNetCore.MongoDb.sln
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ Global
{11CC33C8-27D7-44D2-B402-76E3A33285A0} = {AA148569-62FF-4E1A-8E16-0E529FA38040}
{24CE53FA-9C49-4E20-A060-4A43DFB8C8F1} = {19A533AA-E006-496D-A476-364DF2B637A1}
{FD312677-2A62-4B8F-A965-879B059F1755} = {7E29AA10-F938-4CF8-9CAB-7ACD2D6DC784}
{1AB13DD0-1E72-40C4-9EED-D4FF83701B4A} = {AA148569-62FF-4E1A-8E16-0E529FA38040}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {83EBEC34-0CE5-4D16-93CF-EF5B5CCBC538}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
using System.Threading.Tasks;
using MongoDB.Driver;

namespace JsonApiDotNetCore.MongoDb.Repositories
{
public class MongoDatabaseExtensions
public static class MongoDatabaseExtensions
{

public static IMongoCollection<TResource> GetCollection<TResource>(this IMongoDatabase db)
{
return db.GetCollection<TResource>(typeof(TResource).Name);
}

public static async Task ClearCollectionAsync<TResource>(this IMongoDatabase db)
{
var collection = GetCollection<TResource>(db);
await collection.DeleteManyAsync(Builders<TResource>.Filter.Empty);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -209,14 +209,14 @@ protected override IHostBuilder CreateHostBuilder()
return Host.CreateDefaultBuilder(null)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.ConfigureTestServices(services =>
webBuilder.ConfigureServices(services =>
{
_beforeServicesConfiguration?.Invoke(services);
});

webBuilder.UseStartup<TStartup>();

webBuilder.ConfigureTestServices(services =>
webBuilder.ConfigureServices(services =>
{
_afterServicesConfiguration?.Invoke(services);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@
using FluentAssertions.Extensions;
using Humanizer;
using JsonApiDotNetCore.Configuration;
using JsonApiDotNetCore.MongoDb;
using JsonApiDotNetCore.MongoDb.Repositories;
using JsonApiDotNetCore.Serialization.Objects;
using Microsoft.Extensions.DependencyInjection;
using MongoDB.Driver;
using Newtonsoft.Json;
using Xunit;

namespace JsonApiDotNetCoreMongoDbExampleTests.IntegrationTests.Filtering
Expand Down Expand Up @@ -58,9 +55,9 @@ public async Task Can_filter_equality_on_type(string propertyName, object value)

await _testContext.RunOnDatabaseAsync(async db =>
{
var collection = db.GetCollection<FilterableResource>(nameof(FilterableResource));
await collection.DeleteManyAsync(Builders<FilterableResource>.Filter.Empty);
await collection.InsertManyAsync(new[] {resource, new FilterableResource()});
await db.ClearCollectionAsync<FilterableResource>();
await db.GetCollection<FilterableResource>()
.InsertManyAsync(new[] {resource, new FilterableResource()});
});

var attributeName = propertyName.Camelize();
Expand All @@ -84,9 +81,9 @@ public async Task Can_filter_equality_on_type_Decimal()

await _testContext.RunOnDatabaseAsync(async db =>
{
var collection = db.GetCollection<FilterableResource>(nameof(FilterableResource));
await collection.DeleteManyAsync(Builders<FilterableResource>.Filter.Empty);
await collection.InsertManyAsync(new[] { resource, new FilterableResource() });
await db.ClearCollectionAsync<FilterableResource>();
await db.GetCollection<FilterableResource>()
.InsertManyAsync(new[] {resource, new FilterableResource()});
});

var route = $"/filterableResources?filter=equals(someDecimal,'{resource.SomeDecimal}')";
Expand All @@ -109,9 +106,9 @@ public async Task Can_filter_equality_on_type_Guid()

await _testContext.RunOnDatabaseAsync(async db =>
{
var collection = db.GetCollection<FilterableResource>(nameof(FilterableResource));
await collection.DeleteManyAsync(Builders<FilterableResource>.Filter.Empty);
await collection.InsertManyAsync(new[] { resource, new FilterableResource() });
await db.ClearCollectionAsync<FilterableResource>();
await db.GetCollection<FilterableResource>()
.InsertManyAsync(new[] {resource, new FilterableResource()});
});

var route = $"/filterableResources?filter=equals(someGuid,'{resource.SomeGuid}')";
Expand All @@ -134,9 +131,9 @@ public async Task Can_filter_equality_on_type_DateTime()

await _testContext.RunOnDatabaseAsync(async db =>
{
var collection = db.GetCollection<FilterableResource>(nameof(FilterableResource));
await collection.DeleteManyAsync(Builders<FilterableResource>.Filter.Empty);
await collection.InsertManyAsync(new[] { resource, new FilterableResource() });
await db.ClearCollectionAsync<FilterableResource>();
await db.GetCollection<FilterableResource>()
.InsertManyAsync(new[] {resource, new FilterableResource()});
});

var route = $"/filterableResources?filter=equals(someDateTime,'{resource.SomeDateTime:O}')";
Expand All @@ -162,9 +159,9 @@ public async Task Can_filter_equality_on_type_DateTimeOffset()

await _testContext.RunOnDatabaseAsync(async db =>
{
var collection = db.GetCollection<FilterableResource>(nameof(FilterableResource));
await collection.DeleteManyAsync(Builders<FilterableResource>.Filter.Empty);
await collection.InsertManyAsync(new[] { resource, new FilterableResource() });
await db.ClearCollectionAsync<FilterableResource>();
await db.GetCollection<FilterableResource>()
.InsertManyAsync(new[] {resource, new FilterableResource()});
});

var route = $"/filterableResources?filter=equals(someDateTimeOffset,'{WebUtility.UrlEncode(resource.SomeDateTimeOffset.ToString("O"))}')";
Expand All @@ -187,9 +184,9 @@ public async Task Can_filter_equality_on_type_TimeSpan()

await _testContext.RunOnDatabaseAsync(async db =>
{
var collection = db.GetCollection<FilterableResource>(nameof(FilterableResource));
await collection.DeleteManyAsync(Builders<FilterableResource>.Filter.Empty);
await collection.InsertManyAsync(new[] { resource, new FilterableResource() });
await db.ClearCollectionAsync<FilterableResource>();
await db.GetCollection<FilterableResource>()
.InsertManyAsync(new[] {resource, new FilterableResource()});
});

var route = $"/filterableResources?filter=equals(someTimeSpan,'{resource.SomeTimeSpan}')";
Expand All @@ -212,9 +209,9 @@ public async Task Cannot_filter_equality_on_incompatible_value()

await _testContext.RunOnDatabaseAsync(async db =>
{
var collection = db.GetCollection<FilterableResource>(nameof(FilterableResource));
await collection.DeleteManyAsync(Builders<FilterableResource>.Filter.Empty);
await collection.InsertManyAsync(new[] { resource, new FilterableResource() });
await db.ClearCollectionAsync<FilterableResource>();
await db.GetCollection<FilterableResource>()
.InsertManyAsync(new[] {resource, new FilterableResource()});
});

var route = "/filterableResources?filter=equals(someInt32,'ABC')";
Expand Down Expand Up @@ -268,9 +265,9 @@ public async Task Can_filter_is_null_on_type(string propertyName)

await _testContext.RunOnDatabaseAsync(async db =>
{
var collection = db.GetCollection<FilterableResource>(nameof(FilterableResource));
await collection.DeleteManyAsync(Builders<FilterableResource>.Filter.Empty);
await collection.InsertManyAsync(new[] { resource, otherResource });
await db.ClearCollectionAsync<FilterableResource>();
await db.GetCollection<FilterableResource>()
.InsertManyAsync(new[] {resource, otherResource});
});

var attributeName = propertyName.Camelize();
Expand Down Expand Up @@ -318,9 +315,9 @@ public async Task Can_filter_is_not_null_on_type(string propertyName)

await _testContext.RunOnDatabaseAsync(async db =>
{
var collection = db.GetCollection<FilterableResource>(nameof(FilterableResource));
await collection.DeleteManyAsync(Builders<FilterableResource>.Filter.Empty);
await collection.InsertManyAsync(new[] { resource, new FilterableResource() });
await db.ClearCollectionAsync<FilterableResource>();
await db.GetCollection<FilterableResource>()
.InsertManyAsync(new[] {resource, new FilterableResource()});
});

var attributeName = propertyName.Camelize();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
using System.Threading.Tasks;
using FluentAssertions;
using JsonApiDotNetCore.Configuration;
using JsonApiDotNetCore.MongoDb.Repositories;
using JsonApiDotNetCore.Serialization.Objects;
using JsonApiDotNetCoreMongoDbExample;
using JsonApiDotNetCoreMongoDbExample.Models;
using Microsoft.Extensions.DependencyInjection;
using MongoDB.Driver;
using Xunit;
using Tag = JsonApiDotNetCoreMongoDbExample.Models.Tag;

Expand Down Expand Up @@ -43,9 +43,8 @@ public async Task Can_filter_in_primary_resources()

await _testContext.RunOnDatabaseAsync(async db =>
{
var collection = db.GetCollection<Article>(nameof(Article));
await collection.DeleteManyAsync(Builders<Article>.Filter.Empty);
await collection.InsertManyAsync(articles);
await db.ClearCollectionAsync<Article>();
await db.GetCollection<Article>().InsertManyAsync(articles);
});

var route = "/api/v1/articles?filter=equals(caption,'Two')";
Expand All @@ -71,7 +70,7 @@ public async Task Cannot_filter_in_single_primary_resource()

await _testContext.RunOnDatabaseAsync(async db =>
{
await db.GetCollection<Article>(nameof(Article)).InsertOneAsync(article);
await db.GetCollection<Article>().InsertOneAsync(article);
});

var route = $"/api/v1/articles/{article.StringId}?filter=equals(caption,'Two')";
Expand Down Expand Up @@ -115,12 +114,11 @@ public async Task Cannot_filter_on_HasOne_relationship()

await _testContext.RunOnDatabaseAsync(async db =>
{
var collection = db.GetCollection<Article>(nameof(Article));
await collection.DeleteManyAsync(Builders<Article>.Filter.Empty);
await collection.InsertManyAsync(articles);
await db.ClearCollectionAsync<Article>();
await db.GetCollection<Article>().InsertManyAsync(articles);
});

var route = "/api/v1/articles?include=author&filter=equals(author.lastName,'Smith')";
var route = "/api/v1/articles?filter=equals(author.lastName,'Smith')";

// Act
var (httpResponse, responseDocument) = await _testContext.ExecuteGetAsync<ErrorDocument>(route);
Expand Down Expand Up @@ -155,9 +153,8 @@ public async Task Cannot_filter_on_HasMany_relationship()

await _testContext.RunOnDatabaseAsync(async db =>
{
var collection = db.GetCollection<Blog>(nameof(Blog));
await collection.DeleteManyAsync(Builders<Blog>.Filter.Empty);
await collection.InsertManyAsync(blogs);
await db.ClearCollectionAsync<Blog>();
await db.GetCollection<Blog>().InsertManyAsync(blogs);
});

var route = "/api/v1/blogs?filter=greaterThan(count(articles),'0')";
Expand Down Expand Up @@ -202,9 +199,8 @@ public async Task Cannot_filter_on_HasManyThrough_relationship()

await _testContext.RunOnDatabaseAsync(async db =>
{
var collection = db.GetCollection<Article>(nameof(Article));
await collection.DeleteManyAsync(Builders<Article>.Filter.Empty);
await collection.InsertManyAsync(articles);
await db.ClearCollectionAsync<Article>();
await db.GetCollection<Article>().InsertManyAsync(articles);
});

var route = "/api/v1/articles?filter=has(tags)";
Expand Down
Loading