Skip to content

Commit 08b786a

Browse files
authored
Unit test hook discovery, fix typo in hooks docs (#624)
* fix: typo * test: add unit test generic subclass hook discovery * refactor: cleanup hook discovery test
1 parent a801d95 commit 08b786a

File tree

2 files changed

+31
-13
lines changed

2 files changed

+31
-13
lines changed

src/JsonApiDotNetCore/Hooks/IResourceHookExecutor.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public interface IResourceHookExecutor : IReadHookExecutor, IUpdateHookExecutor,
1919
public interface ICreateHookExecutor
2020
{
2121
/// <summary>
22-
/// Executes the Before Cycle by firing the appropiate hooks if they are implemented.
22+
/// Executes the Before Cycle by firing the appropriate hooks if they are implemented.
2323
/// The returned set will be used in the actual operation in <see cref="DefaultResourceService{T}"/>.
2424
/// <para />
2525
/// Fires the <see cref="ResourceDefinition{T}.BeforeCreate"/>
@@ -34,7 +34,7 @@ public interface ICreateHookExecutor
3434
/// <typeparam name="TResource">The type of the root entities</typeparam>
3535
IEnumerable<TResource> BeforeCreate<TResource>(IEnumerable<TResource> entities, ResourcePipeline pipeline) where TResource : class, IIdentifiable;
3636
/// <summary>
37-
/// Executes the After Cycle by firing the appropiate hooks if they are implemented.
37+
/// Executes the After Cycle by firing the appropriate hooks if they are implemented.
3838
/// <para />
3939
/// Fires the <see cref="ResourceDefinition{T}.AfterCreate"/>
4040
/// hook where T = <typeparamref name="TResource"/> for values in parameter <paramref name="entities"/>.
@@ -51,7 +51,7 @@ public interface ICreateHookExecutor
5151
public interface IDeleteHookExecutor
5252
{
5353
/// <summary>
54-
/// Executes the Before Cycle by firing the appropiate hooks if they are implemented.
54+
/// Executes the Before Cycle by firing the appropriate hooks if they are implemented.
5555
/// The returned set will be used in the actual operation in <see cref="DefaultResourceService{T}"/>.
5656
/// <para />
5757
/// Fires the <see cref="ResourceDefinition{T}.BeforeDelete"/>
@@ -68,7 +68,7 @@ public interface IDeleteHookExecutor
6868
/// <typeparam name="TResource">The type of the root entities</typeparam>
6969
IEnumerable<TResource> BeforeDelete<TResource>(IEnumerable<TResource> entities, ResourcePipeline pipeline) where TResource : class, IIdentifiable;
7070
/// <summary>
71-
/// Executes the After Cycle by firing the appropiate hooks if they are implemented.
71+
/// Executes the After Cycle by firing the appropriate hooks if they are implemented.
7272
/// <para />
7373
/// Fires the <see cref="ResourceDefinition{T}.AfterDelete"/>
7474
/// hook where T = <typeparamref name="TResource"/> for values in parameter <paramref name="entities"/>.
@@ -85,7 +85,7 @@ public interface IDeleteHookExecutor
8585
public interface IReadHookExecutor
8686
{
8787
/// <summary>
88-
/// Executes the Before Cycle by firing the appropiate hooks if they are implemented.
88+
/// Executes the Before Cycle by firing the appropriate hooks if they are implemented.
8989
/// <para />
9090
/// Fires the <see cref="ResourceDefinition{T}.BeforeRead"/>
9191
/// hook where T = <typeparamref name="TResource"/> for the requested
@@ -97,7 +97,7 @@ public interface IReadHookExecutor
9797
/// <typeparam name="TResource">The type of the request entity</typeparam>
9898
void BeforeRead<TResource>(ResourcePipeline pipeline, string stringId = null) where TResource : class, IIdentifiable;
9999
/// <summary>
100-
/// Executes the After Cycle by firing the appropiate hooks if they are implemented.
100+
/// Executes the After Cycle by firing the appropriate hooks if they are implemented.
101101
/// <para />
102102
/// Fires the <see cref="ResourceDefinition{T}.AfterRead"/> for every unique
103103
/// entity type occuring in parameter <paramref name="entities"/>.
@@ -114,7 +114,7 @@ public interface IReadHookExecutor
114114
public interface IUpdateHookExecutor
115115
{
116116
/// <summary>
117-
/// Executes the Before Cycle by firing the appropiate hooks if they are implemented.
117+
/// Executes the Before Cycle by firing the appropriate hooks if they are implemented.
118118
/// The returned set will be used in the actual operation in <see cref="DefaultResourceService{T}"/>.
119119
/// <para />
120120
/// Fires the <see cref="ResourceDefinition{T}.BeforeUpdate(IDiffableEntityHashSet{T}, ResourcePipeline)"/>
@@ -135,7 +135,7 @@ public interface IUpdateHookExecutor
135135
/// <typeparam name="TResource">The type of the root entities</typeparam>
136136
IEnumerable<TResource> BeforeUpdate<TResource>(IEnumerable<TResource> entities, ResourcePipeline pipeline) where TResource : class, IIdentifiable;
137137
/// <summary>
138-
/// Executes the After Cycle by firing the appropiate hooks if they are implemented.
138+
/// Executes the After Cycle by firing the appropriate hooks if they are implemented.
139139
/// <para />
140140
/// Fires the <see cref="ResourceDefinition{T}.AfterUpdate"/>
141141
/// hook where T = <typeparamref name="TResource"/> for values in parameter <paramref name="entities"/>.
@@ -155,7 +155,7 @@ public interface IUpdateHookExecutor
155155
public interface IOnReturnHookExecutor
156156
{
157157
/// <summary>
158-
/// Executes the On Cycle by firing the appropiate hooks if they are implemented.
158+
/// Executes the On Cycle by firing the appropriate hooks if they are implemented.
159159
/// <para />
160160
/// Fires the <see cref="ResourceDefinition{T}.OnReturn"/> for every unique
161161
/// entity type occuring in parameter <paramref name="entities"/>.

test/UnitTests/ResourceHooks/DiscoveryTests.cs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ private IServiceProvider MockProvider<TResource>(object service) where TResource
2929
}
3030

3131
[Fact]
32-
public void Hook_Discovery()
32+
public void HookDiscovery_StandardResourceDefinition_CanDiscover()
3333
{
3434
// Arrange & act
3535
var hookConfig = new HooksDiscovery<Dummy>(MockProvider<Dummy>(new DummyResourceDefinition()));
@@ -42,7 +42,7 @@ public class AnotherDummy : Identifiable { }
4242
public abstract class ResourceDefintionBase<T> : ResourceDefinition<T> where T : class, IIdentifiable
4343
{
4444
public ResourceDefintionBase(IResourceGraph resourceGraph) : base(resourceGraph) { }
45-
public override IEnumerable<T> BeforeDelete(IEntityHashSet<T> affected, ResourcePipeline pipeline) { return affected; }
45+
public override IEnumerable<T> BeforeDelete(IEntityHashSet<T> entities, ResourcePipeline pipeline) { return entities; }
4646
public override void AfterDelete(HashSet<T> entities, ResourcePipeline pipeline, bool succeeded) { }
4747
}
4848

@@ -52,7 +52,7 @@ public AnotherDummyResourceDefinition() : base(new ResourceGraphBuilder().AddRes
5252
}
5353

5454
[Fact]
55-
public void Hook_Discovery_With_Inheritance()
55+
public void HookDiscovery_InheritanceSubclass_CanDiscover()
5656
{
5757
// Arrange & act
5858
var hookConfig = new HooksDiscovery<AnotherDummy>(MockProvider<AnotherDummy>(new AnotherDummyResourceDefinition()));
@@ -73,15 +73,33 @@ public override void AfterDelete(HashSet<YetAnotherDummy> entities, ResourcePipe
7373
}
7474

7575
[Fact]
76-
public void LoadDatabaseValues_Attribute_Not_Allowed()
76+
public void HookDiscovery_WronglyUsedLoadDatabaseValueAttribute_ThrowsJsonApiSetupException()
7777
{
7878
// assert
7979
Assert.Throws<JsonApiSetupException>(() =>
8080
{
8181
// Arrange & act
8282
var hookConfig = new HooksDiscovery<YetAnotherDummy>(MockProvider<YetAnotherDummy>(new YetAnotherDummyResourceDefinition()));
8383
});
84+
}
85+
86+
[Fact]
87+
public void HookDiscovery_InheritanceWithGenericSubclass_CanDiscover()
88+
{
89+
// Arrange & act
90+
var hookConfig = new HooksDiscovery<AnotherDummy>(MockProvider<AnotherDummy>(new GenericDummyResourceDefinition<AnotherDummy>()));
91+
92+
// Assert
93+
Assert.Contains(ResourceHook.BeforeDelete, hookConfig.ImplementedHooks);
94+
Assert.Contains(ResourceHook.AfterDelete, hookConfig.ImplementedHooks);
95+
}
96+
97+
public class GenericDummyResourceDefinition<TResource> : ResourceDefinition<TResource> where TResource : class, IIdentifiable<int>
98+
{
99+
public GenericDummyResourceDefinition() : base(new ResourceGraphBuilder().AddResource<TResource>().Build()) { }
84100

101+
public override IEnumerable<TResource> BeforeDelete(IEntityHashSet<TResource> entities, ResourcePipeline pipeline) { return entities; }
102+
public override void AfterDelete(HashSet<TResource> entities, ResourcePipeline pipeline, bool succeeded) { }
85103
}
86104
}
87105
}

0 commit comments

Comments
 (0)