Skip to content

Commit 34cffeb

Browse files
maureiwisepotato
authored andcommitted
Fix DiscoveryTests and NoEntityFrameworkExample project/tests (#590)
* fix: DiscoveryTests project * fix: NoEntityFrameworkExample project + tests
1 parent 97d94d2 commit 34cffeb

File tree

5 files changed

+41
-55
lines changed

5 files changed

+41
-55
lines changed

benchmarks/Serialization/JsonApiDeserializer_Benchmarks.cs

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
using Moq;
1414
using Newtonsoft.Json;
1515
using Newtonsoft.Json.Serialization;
16-
1716
namespace Benchmarks.Serialization
1817
{
1918
[MarkdownExporter]

src/Examples/NoEntityFrameworkExample/Startup.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF
5757

5858
context.Database.EnsureCreated();
5959

60-
app.UseMvc();
60+
app.UseJsonApi();
6161
}
6262
}
6363
}

src/JsonApiDotNetCore/Data/DefaultResourceRepository.cs

+4-29
Original file line numberDiff line numberDiff line change
@@ -367,27 +367,6 @@ protected void LoadCurrentRelationships(TResource oldEntity, RelationshipAttribu
367367
}
368368
}
369369

370-
/// <summary>
371-
/// The relationshipValue parameter contains the dependent side of the relationship (Tags).
372-
/// We can't directly add them to the left entity (Article): we need to
373-
/// use the join table (ArticleTags). This methods assigns the relationship value to entity
374-
/// by taking care of that
375-
/// </summary>
376-
private void AssignHasManyThrough(TResource entity, HasManyThroughAttribute hasManyThrough, IList relationshipValue)
377-
{
378-
var pointers = relationshipValue.Cast<IIdentifiable>();
379-
var throughRelationshipCollection = Activator.CreateInstance(hasManyThrough.ThroughProperty.PropertyType) as IList;
380-
hasManyThrough.ThroughProperty.SetValue(entity, throughRelationshipCollection);
381-
382-
foreach (var pointer in pointers)
383-
{
384-
var throughInstance = Activator.CreateInstance(hasManyThrough.ThroughType);
385-
hasManyThrough.LeftProperty.SetValue(throughInstance, entity);
386-
hasManyThrough.RightProperty.SetValue(throughInstance, pointer);
387-
throughRelationshipCollection.Add(throughInstance);
388-
}
389-
}
390-
391370
/// <summary>
392371
/// Given a iidentifiable relationshipvalue, verify if an entity of the underlying
393372
/// type with the same ID is already attached to the dbContext, and if so, return it.
@@ -423,19 +402,15 @@ public class DefaultResourceRepository<TResource> : DefaultResourceRepository<TR
423402
{
424403
public DefaultResourceRepository(ITargetedFields targetedFields,
425404
IDbContextResolver contextResolver,
426-
IResourceGraph resourceContextProvider,
405+
IResourceGraph resourceGraph,
427406
IGenericServiceFactory genericServiceFactory)
428-
: base(targetedFields, contextResolver, resourceContextProvider, genericServiceFactory)
429-
{
430-
}
407+
: base(targetedFields, contextResolver, resourceGraph, genericServiceFactory) { }
431408

432409
public DefaultResourceRepository(ITargetedFields targetedFields,
433410
IDbContextResolver contextResolver,
434-
IResourceGraph resourceContextProvider,
411+
IResourceGraph resourceGraph,
435412
IGenericServiceFactory genericServiceFactory,
436413
ILoggerFactory loggerFactory = null)
437-
: base(targetedFields, contextResolver, resourceContextProvider, genericServiceFactory, loggerFactory)
438-
{
439-
}
414+
: base(targetedFields, contextResolver, resourceGraph, genericServiceFactory, loggerFactory) { }
440415
}
441416
}

test/DiscoveryTests/ServiceDiscoveryFacadeTests.cs

+35-22
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@
66
using JsonApiDotNetCore.Graph;
77
using JsonApiDotNetCore.Hooks;
88
using JsonApiDotNetCore.Internal.Contracts;
9+
using JsonApiDotNetCore.Internal.Generics;
10+
using JsonApiDotNetCore.Managers.Contracts;
911
using JsonApiDotNetCore.Models;
12+
using JsonApiDotNetCore.Query;
13+
using JsonApiDotNetCore.Serialization;
14+
using JsonApiDotNetCore.Serialization.Server.Builders;
1015
using JsonApiDotNetCore.Services;
1116
using Microsoft.EntityFrameworkCore;
1217
using Microsoft.Extensions.DependencyInjection;
@@ -27,6 +32,18 @@ public ServiceDiscoveryFacadeTests()
2732
var dbResolverMock = new Mock<IDbContextResolver>();
2833
dbResolverMock.Setup(m => m.GetContext()).Returns(new Mock<DbContext>().Object);
2934
TestModelRepository._dbContextResolver = dbResolverMock.Object;
35+
_services.AddSingleton<IJsonApiOptions>(new JsonApiOptions());
36+
_services.AddScoped((_) => new Mock<ILinkBuilder>().Object);
37+
_services.AddScoped((_) => new Mock<ICurrentRequest>().Object);
38+
_services.AddScoped((_) => new Mock<IPageService>().Object);
39+
_services.AddScoped((_) => new Mock<ISparseFieldsService>().Object);
40+
_services.AddScoped((_) => new Mock<IFilterService>().Object);
41+
_services.AddScoped((_) => new Mock<IIncludeService>().Object);
42+
_services.AddScoped((_) => new Mock<ISortService>().Object);
43+
_services.AddScoped((_) => new Mock<ITargetedFields>().Object);
44+
_services.AddScoped((_) => new Mock<IResourceGraph>().Object);
45+
_services.AddScoped((_) => new Mock<IGenericServiceFactory>().Object);
46+
_services.AddScoped((_) => new Mock<IResourceContextProvider>().Object);
3047
}
3148

3249
private ServiceDiscoveryFacade _facade => new ServiceDiscoveryFacade(_services, _resourceGraphBuilder);
@@ -63,13 +80,7 @@ public void AddCurrentAssembly_Adds_Resources_To_Graph()
6380
[Fact]
6481
public void AddCurrentAssembly_Adds_Services_To_Container()
6582
{
66-
// arrange, act
67-
_services.AddSingleton<IJsonApiOptions>(new JsonApiOptions());
68-
69-
_services.AddScoped((_) => new Mock<ILinkBuilder>().Object);
70-
_services.AddScoped((_) => new Mock<IRequestContext>().Object);
71-
_services.AddScoped((_) => new Mock<IPageQueryService>().Object);
72-
_services.AddScoped((_) => new Mock<IResourceGraph>().Object);
83+
// arrange, act
7384
_facade.AddCurrentAssembly();
7485

7586
// assert
@@ -93,26 +104,28 @@ public class TestModel : Identifiable { }
93104

94105
public class TestModelService : DefaultResourceService<TestModel>
95106
{
96-
private static IResourceRepository<TestModel> _repo = new Mock<IResourceRepository<TestModel>>().Object;
97-
private static IJsonApiContext _jsonApiContext = new Mock<IJsonApiContext>().Object;
107+
private static IResourceRepository<TestModel> _repo = new Mock<IResourceRepository<TestModel>>().Object;
98108

99-
public TestModelService(
100-
IResourceRepository<TestModel> repository,
101-
IJsonApiOptions options,
102-
IRequestContext currentRequest,
103-
IPageQueryService pageService,
104-
IResourceGraph resourceGraph,
105-
ILoggerFactory loggerFactory = null,
106-
IResourceHookExecutor hookExecutor = null) : base(repository, options, currentRequest, pageService, resourceGraph, loggerFactory, hookExecutor)
107-
{
108-
}
109+
public TestModelService(ISortService sortService,
110+
IFilterService filterService,
111+
IJsonApiOptions options,
112+
IIncludeService includeService,
113+
ISparseFieldsService sparseFieldsService,
114+
IPageService pageManager,
115+
IResourceContextProvider provider,
116+
IResourceHookExecutor hookExecutor = null,
117+
ILoggerFactory loggerFactory = null)
118+
: base(sortService, filterService, _repo, options, includeService, sparseFieldsService, pageManager, provider, hookExecutor, loggerFactory) { }
109119
}
110120

111121
public class TestModelRepository : DefaultResourceRepository<TestModel>
112122
{
113-
internal static IDbContextResolver _dbContextResolver;
114-
private static IJsonApiContext _jsonApiContext = new Mock<IJsonApiContext>().Object;
115-
public TestModelRepository() : base(_jsonApiContext, _dbContextResolver) { }
123+
internal static IDbContextResolver _dbContextResolver;
124+
125+
public TestModelRepository(ITargetedFields targetedFields,
126+
IResourceGraph resourceGraph,
127+
IGenericServiceFactory genericServiceFactory)
128+
: base(targetedFields, _dbContextResolver, resourceGraph, genericServiceFactory) { }
116129
}
117130
}
118131
}

test/NoEntityFrameworkTests/TestFixture.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,13 @@ public IResponseDeserializer GetDeserializer()
4646
.AddResource<Person>()
4747
.AddResource<Author>()
4848
.AddResource<Passport>()
49-
.AddResource<TodoItemClient>("todo-items")
49+
.AddResource<TodoItemClient>("custom-todo-items")
5050
.AddResource<TodoItemCollectionClient, Guid>().Build();
5151
return new ResponseDeserializer(resourceGraph);
5252
}
5353

5454
public T GetService<T>() => (T)_services.GetService(typeof(T));
5555

56-
5756
public void Dispose()
5857
{
5958
Server.Dispose();

0 commit comments

Comments
 (0)