Skip to content

Fix DiscoveryTests and NoEntityFrameworkExample project/tests #590

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 3 commits into from
Oct 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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: 0 additions & 1 deletion benchmarks/Serialization/JsonApiDeserializer_Benchmarks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
using Moq;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

namespace Benchmarks.Serialization
{
[MarkdownExporter]
Expand Down
2 changes: 1 addition & 1 deletion src/Examples/NoEntityFrameworkExample/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF

context.Database.EnsureCreated();

app.UseMvc();
app.UseJsonApi();
}
}
}
33 changes: 4 additions & 29 deletions src/JsonApiDotNetCore/Data/DefaultResourceRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -367,27 +367,6 @@ protected void LoadCurrentRelationships(TResource oldEntity, RelationshipAttribu
}
}

/// <summary>
/// The relationshipValue parameter contains the dependent side of the relationship (Tags).
/// We can't directly add them to the left entity (Article): we need to
/// use the join table (ArticleTags). This methods assigns the relationship value to entity
/// by taking care of that
/// </summary>
private void AssignHasManyThrough(TResource entity, HasManyThroughAttribute hasManyThrough, IList relationshipValue)
{
var pointers = relationshipValue.Cast<IIdentifiable>();
var throughRelationshipCollection = Activator.CreateInstance(hasManyThrough.ThroughProperty.PropertyType) as IList;
hasManyThrough.ThroughProperty.SetValue(entity, throughRelationshipCollection);

foreach (var pointer in pointers)
{
var throughInstance = Activator.CreateInstance(hasManyThrough.ThroughType);
hasManyThrough.LeftProperty.SetValue(throughInstance, entity);
hasManyThrough.RightProperty.SetValue(throughInstance, pointer);
throughRelationshipCollection.Add(throughInstance);
}
}

/// <summary>
/// Given a iidentifiable relationshipvalue, verify if an entity of the underlying
/// type with the same ID is already attached to the dbContext, and if so, return it.
Expand Down Expand Up @@ -423,19 +402,15 @@ public class DefaultResourceRepository<TResource> : DefaultResourceRepository<TR
{
public DefaultResourceRepository(ITargetedFields targetedFields,
IDbContextResolver contextResolver,
IResourceGraph resourceContextProvider,
IResourceGraph resourceGraph,
IGenericServiceFactory genericServiceFactory)
: base(targetedFields, contextResolver, resourceContextProvider, genericServiceFactory)
{
}
: base(targetedFields, contextResolver, resourceGraph, genericServiceFactory) { }

public DefaultResourceRepository(ITargetedFields targetedFields,
IDbContextResolver contextResolver,
IResourceGraph resourceContextProvider,
IResourceGraph resourceGraph,
IGenericServiceFactory genericServiceFactory,
ILoggerFactory loggerFactory = null)
: base(targetedFields, contextResolver, resourceContextProvider, genericServiceFactory, loggerFactory)
{
}
: base(targetedFields, contextResolver, resourceGraph, genericServiceFactory, loggerFactory) { }
}
}
57 changes: 35 additions & 22 deletions test/DiscoveryTests/ServiceDiscoveryFacadeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
using JsonApiDotNetCore.Graph;
using JsonApiDotNetCore.Hooks;
using JsonApiDotNetCore.Internal.Contracts;
using JsonApiDotNetCore.Internal.Generics;
using JsonApiDotNetCore.Managers.Contracts;
using JsonApiDotNetCore.Models;
using JsonApiDotNetCore.Query;
using JsonApiDotNetCore.Serialization;
using JsonApiDotNetCore.Serialization.Server.Builders;
using JsonApiDotNetCore.Services;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -27,6 +32,18 @@ public ServiceDiscoveryFacadeTests()
var dbResolverMock = new Mock<IDbContextResolver>();
dbResolverMock.Setup(m => m.GetContext()).Returns(new Mock<DbContext>().Object);
TestModelRepository._dbContextResolver = dbResolverMock.Object;
_services.AddSingleton<IJsonApiOptions>(new JsonApiOptions());
_services.AddScoped((_) => new Mock<ILinkBuilder>().Object);
_services.AddScoped((_) => new Mock<ICurrentRequest>().Object);
_services.AddScoped((_) => new Mock<IPageService>().Object);
_services.AddScoped((_) => new Mock<ISparseFieldsService>().Object);
_services.AddScoped((_) => new Mock<IFilterService>().Object);
_services.AddScoped((_) => new Mock<IIncludeService>().Object);
_services.AddScoped((_) => new Mock<ISortService>().Object);
_services.AddScoped((_) => new Mock<ITargetedFields>().Object);
_services.AddScoped((_) => new Mock<IResourceGraph>().Object);
_services.AddScoped((_) => new Mock<IGenericServiceFactory>().Object);
_services.AddScoped((_) => new Mock<IResourceContextProvider>().Object);
}

private ServiceDiscoveryFacade _facade => new ServiceDiscoveryFacade(_services, _resourceGraphBuilder);
Expand Down Expand Up @@ -63,13 +80,7 @@ public void AddCurrentAssembly_Adds_Resources_To_Graph()
[Fact]
public void AddCurrentAssembly_Adds_Services_To_Container()
{
// arrange, act
_services.AddSingleton<IJsonApiOptions>(new JsonApiOptions());

_services.AddScoped((_) => new Mock<ILinkBuilder>().Object);
_services.AddScoped((_) => new Mock<IRequestContext>().Object);
_services.AddScoped((_) => new Mock<IPageQueryService>().Object);
_services.AddScoped((_) => new Mock<IResourceGraph>().Object);
// arrange, act
_facade.AddCurrentAssembly();

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

public class TestModelService : DefaultResourceService<TestModel>
{
private static IResourceRepository<TestModel> _repo = new Mock<IResourceRepository<TestModel>>().Object;
private static IJsonApiContext _jsonApiContext = new Mock<IJsonApiContext>().Object;
private static IResourceRepository<TestModel> _repo = new Mock<IResourceRepository<TestModel>>().Object;

public TestModelService(
IResourceRepository<TestModel> repository,
IJsonApiOptions options,
IRequestContext currentRequest,
IPageQueryService pageService,
IResourceGraph resourceGraph,
ILoggerFactory loggerFactory = null,
IResourceHookExecutor hookExecutor = null) : base(repository, options, currentRequest, pageService, resourceGraph, loggerFactory, hookExecutor)
{
}
public TestModelService(ISortService sortService,
IFilterService filterService,
IJsonApiOptions options,
IIncludeService includeService,
ISparseFieldsService sparseFieldsService,
IPageService pageManager,
IResourceContextProvider provider,
IResourceHookExecutor hookExecutor = null,
ILoggerFactory loggerFactory = null)
: base(sortService, filterService, _repo, options, includeService, sparseFieldsService, pageManager, provider, hookExecutor, loggerFactory) { }
}

public class TestModelRepository : DefaultResourceRepository<TestModel>
{
internal static IDbContextResolver _dbContextResolver;
private static IJsonApiContext _jsonApiContext = new Mock<IJsonApiContext>().Object;
public TestModelRepository() : base(_jsonApiContext, _dbContextResolver) { }
internal static IDbContextResolver _dbContextResolver;

public TestModelRepository(ITargetedFields targetedFields,
IResourceGraph resourceGraph,
IGenericServiceFactory genericServiceFactory)
: base(targetedFields, _dbContextResolver, resourceGraph, genericServiceFactory) { }
}
}
}
3 changes: 1 addition & 2 deletions test/NoEntityFrameworkTests/TestFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,13 @@ public IResponseDeserializer GetDeserializer()
.AddResource<Person>()
.AddResource<Author>()
.AddResource<Passport>()
.AddResource<TodoItemClient>("todo-items")
.AddResource<TodoItemClient>("custom-todo-items")
.AddResource<TodoItemCollectionClient, Guid>().Build();
return new ResponseDeserializer(resourceGraph);
}

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


public void Dispose()
{
Server.Dispose();
Expand Down