Skip to content

Commit 02f98c7

Browse files
committed
add ContextGraphBuilder tests
1 parent 453d4fc commit 02f98c7

File tree

2 files changed

+60
-9
lines changed

2 files changed

+60
-9
lines changed

src/JsonApiDotNetCore/Builders/ContextGraphBuilder.cs

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,36 @@ public interface IContextGraphBuilder
2323
/// Add a json:api resource
2424
/// </summary>
2525
/// <typeparam name="TResource">The resource model type</typeparam>
26-
/// <param name="pluralizedTypeName">The pluralized name that should be exposed by the API</param>
27-
IContextGraphBuilder AddResource<TResource>(string pluralizedTypeName) where TResource : class, IIdentifiable<int>;
26+
/// <param name="pluralizedTypeName">
27+
/// The pluralized name that should be exposed by the API.
28+
/// If nothing is specified, the configured name formatter will be used.
29+
/// See <see cref="JsonApiOptions.ResourceNameFormatter" />.
30+
/// </param>
31+
IContextGraphBuilder AddResource<TResource>(string pluralizedTypeName = null) where TResource : class, IIdentifiable<int>;
2832

2933
/// <summary>
3034
/// Add a json:api resource
3135
/// </summary>
3236
/// <typeparam name="TResource">The resource model type</typeparam>
3337
/// <typeparam name="TId">The resource model identifier type</typeparam>
34-
/// <param name="pluralizedTypeName">The pluralized name that should be exposed by the API</param>
35-
IContextGraphBuilder AddResource<TResource, TId>(string pluralizedTypeName) where TResource : class, IIdentifiable<TId>;
38+
/// <param name="pluralizedTypeName">
39+
/// The pluralized name that should be exposed by the API.
40+
/// If nothing is specified, the configured name formatter will be used.
41+
/// See <see cref="JsonApiOptions.ResourceNameFormatter" />.
42+
/// </param>
43+
IContextGraphBuilder AddResource<TResource, TId>(string pluralizedTypeName = null) where TResource : class, IIdentifiable<TId>;
3644

3745
/// <summary>
3846
/// Add a json:api resource
3947
/// </summary>
4048
/// <param name="entityType">The resource model type</param>
4149
/// <param name="idType">The resource model identifier type</param>
42-
/// <param name="pluralizedTypeName">The pluralized name that should be exposed by the API</param>
43-
IContextGraphBuilder AddResource(Type entityType, Type idType, string pluralizedTypeName);
50+
/// <param name="pluralizedTypeName">
51+
/// The pluralized name that should be exposed by the API.
52+
/// If nothing is specified, the configured name formatter will be used.
53+
/// See <see cref="JsonApiOptions.ResourceNameFormatter" />.
54+
/// </param>
55+
IContextGraphBuilder AddResource(Type entityType, Type idType, string pluralizedTypeName = null);
4456

4557
/// <summary>
4658
/// Add all the models that are part of the provided <see cref="DbContext" />
@@ -80,18 +92,20 @@ public IContextGraph Build()
8092
}
8193

8294
/// <inheritdoc />
83-
public IContextGraphBuilder AddResource<TResource>(string pluralizedTypeName) where TResource : class, IIdentifiable<int>
95+
public IContextGraphBuilder AddResource<TResource>(string pluralizedTypeName = null) where TResource : class, IIdentifiable<int>
8496
=> AddResource<TResource, int>(pluralizedTypeName);
8597

8698
/// <inheritdoc />
87-
public IContextGraphBuilder AddResource<TResource, TId>(string pluralizedTypeName) where TResource : class, IIdentifiable<TId>
99+
public IContextGraphBuilder AddResource<TResource, TId>(string pluralizedTypeName = null) where TResource : class, IIdentifiable<TId>
88100
=> AddResource(typeof(TResource), typeof(TId), pluralizedTypeName);
89101

90102
/// <inheritdoc />
91-
public IContextGraphBuilder AddResource(Type entityType, Type idType, string pluralizedTypeName)
103+
public IContextGraphBuilder AddResource(Type entityType, Type idType, string pluralizedTypeName = null)
92104
{
93105
AssertEntityIsNotAlreadyDefined(entityType);
94106

107+
pluralizedTypeName = pluralizedTypeName ?? _resourceNameFormatter.FormatResourceName(entityType);
108+
95109
_entities.Add(GetEntity(pluralizedTypeName, entityType, idType));
96110

97111
return this;

test/UnitTests/Builders/ContextGraphBuilder_Tests.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System.Linq;
2+
using JsonApiDotNetCore.Builders;
13
using JsonApiDotNetCore.Extensions;
24
using JsonApiDotNetCore.Internal;
35
using JsonApiDotNetCore.Models;
@@ -37,5 +39,40 @@ public void Can_Build_ContextGraph_Using_Builder()
3739
Assert.Equal(typeof(NonDbResource), nonDbResource.EntityType);
3840
Assert.Equal(typeof(ResourceDefinition<NonDbResource>), nonDbResource.ResourceType);
3941
}
42+
43+
[Fact]
44+
public void Resources_Without_Names_Specified_Will_Use_Default_Formatter()
45+
{
46+
// arrange
47+
var builder = new ContextGraphBuilder();
48+
builder.AddResource<TestResource>();
49+
50+
// act
51+
var graph = builder.Build();
52+
53+
// assert
54+
var resource = graph.GetContextEntity(typeof(TestResource));
55+
Assert.Equal("test-resources", resource.EntityName);
56+
}
57+
58+
[Fact]
59+
public void Attrs_Without_Names_Specified_Will_Use_Default_Formatter()
60+
{
61+
// arrange
62+
var builder = new ContextGraphBuilder();
63+
builder.AddResource<TestResource>();
64+
65+
// act
66+
var graph = builder.Build();
67+
68+
// assert
69+
var resource = graph.GetContextEntity(typeof(TestResource));
70+
Assert.Equal("attribute", resource.Attributes.Single().PublicAttributeName);
71+
}
72+
73+
public class TestResource : Identifiable
74+
{
75+
[Attr] public string Attribute { get; set; }
76+
}
4077
}
4178
}

0 commit comments

Comments
 (0)