1
+ using System ;
2
+ using System . Collections . Generic ;
1
3
using System . Linq ;
4
+ using System . Reflection ;
5
+ using Humanizer ;
2
6
using JsonApiDotNetCore . Builders ;
7
+ using JsonApiDotNetCore . Configuration ;
3
8
using JsonApiDotNetCore . Extensions ;
9
+ using JsonApiDotNetCore . Graph ;
4
10
using JsonApiDotNetCore . Internal ;
5
11
using JsonApiDotNetCore . Models ;
6
12
using Microsoft . EntityFrameworkCore ;
@@ -17,6 +23,11 @@ class TestContext : DbContext {
17
23
public DbSet < DbResource > DbResources { get ; set ; }
18
24
}
19
25
26
+ public ContextGraphBuilder_Tests ( )
27
+ {
28
+ JsonApiOptions . ResourceNameFormatter = new DefaultResourceNameFormatter ( ) ;
29
+ }
30
+
20
31
[ Fact ]
21
32
public void Can_Build_ContextGraph_Using_Builder ( )
22
33
{
@@ -55,6 +66,22 @@ public void Resources_Without_Names_Specified_Will_Use_Default_Formatter()
55
66
Assert . Equal ( "test-resources" , resource . EntityName ) ;
56
67
}
57
68
69
+ [ Fact ]
70
+ public void Resources_Without_Names_Specified_Will_Use_Configured_Formatter ( )
71
+ {
72
+ // arrange
73
+ JsonApiOptions . ResourceNameFormatter = new CamelCaseNameFormatter ( ) ;
74
+ var builder = new ContextGraphBuilder ( ) ;
75
+ builder . AddResource < TestResource > ( ) ;
76
+
77
+ // act
78
+ var graph = builder . Build ( ) ;
79
+
80
+ // assert
81
+ var resource = graph . GetContextEntity ( typeof ( TestResource ) ) ;
82
+ Assert . Equal ( "testResources" , resource . EntityName ) ;
83
+ }
84
+
58
85
[ Fact ]
59
86
public void Attrs_Without_Names_Specified_Will_Use_Default_Formatter ( )
60
87
{
@@ -67,12 +94,56 @@ public void Attrs_Without_Names_Specified_Will_Use_Default_Formatter()
67
94
68
95
// assert
69
96
var resource = graph . GetContextEntity ( typeof ( TestResource ) ) ;
70
- Assert . Equal ( "attribute" , resource . Attributes . Single ( ) . PublicAttributeName ) ;
97
+ Assert . Equal ( "compound- attribute" , resource . Attributes . Single ( ) . PublicAttributeName ) ;
71
98
}
72
99
73
- public class TestResource : Identifiable
100
+ [ Fact ]
101
+ public void Attrs_Without_Names_Specified_Will_Use_Configured_Formatter ( )
74
102
{
75
- [ Attr ] public string Attribute { get ; set ; }
103
+ // arrange
104
+ JsonApiOptions . ResourceNameFormatter = new CamelCaseNameFormatter ( ) ;
105
+ var builder = new ContextGraphBuilder ( ) ;
106
+ builder . AddResource < TestResource > ( ) ;
107
+
108
+ // act
109
+ var graph = builder . Build ( ) ;
110
+
111
+ // assert
112
+ var resource = graph . GetContextEntity ( typeof ( TestResource ) ) ;
113
+ Assert . Equal ( "compoundAttribute" , resource . Attributes . Single ( ) . PublicAttributeName ) ;
114
+ }
115
+
116
+ [ Fact ]
117
+ public void Relationships_Without_Names_Specified_Will_Use_Default_Formatter ( )
118
+ {
119
+ // arrange
120
+ var builder = new ContextGraphBuilder ( ) ;
121
+ builder . AddResource < TestResource > ( ) ;
122
+
123
+ // act
124
+ var graph = builder . Build ( ) ;
125
+
126
+ // assert
127
+ var resource = graph . GetContextEntity ( typeof ( TestResource ) ) ;
128
+ Assert . Equal ( "related-resource" , resource . Relationships . Single ( r => r . IsHasOne ) . PublicRelationshipName ) ;
129
+ Assert . Equal ( "related-resources" , resource . Relationships . Single ( r => r . IsHasMany ) . PublicRelationshipName ) ;
130
+ }
131
+
132
+ public class TestResource : Identifiable {
133
+ [ Attr ] public string CompoundAttribute { get ; set ; }
134
+ [ HasOne ] public RelatedResource RelatedResource { get ; set ; }
135
+ [ HasMany ] public List < RelatedResource > RelatedResources { get ; set ; }
136
+ }
137
+
138
+ public class RelatedResource : Identifiable { }
139
+
140
+ public class CamelCaseNameFormatter : IResourceNameFormatter
141
+ {
142
+ public string FormatPropertyName ( PropertyInfo property ) => ToCamelCase ( property . Name ) ;
143
+
144
+ public string FormatResourceName ( Type resourceType ) => ToCamelCase ( resourceType . Name . Pluralize ( ) ) ;
145
+
146
+ private string ToCamelCase ( string str ) => Char . ToLowerInvariant ( str [ 0 ] ) + str . Substring ( 1 ) ;
76
147
}
77
148
}
78
149
}
0 commit comments