1+ using System ;
2+ using System . Collections . Generic ;
13using System . Linq ;
4+ using System . Reflection ;
5+ using Humanizer ;
26using JsonApiDotNetCore . Builders ;
7+ using JsonApiDotNetCore . Configuration ;
38using JsonApiDotNetCore . Extensions ;
9+ using JsonApiDotNetCore . Graph ;
410using JsonApiDotNetCore . Internal ;
511using JsonApiDotNetCore . Models ;
612using Microsoft . EntityFrameworkCore ;
@@ -17,6 +23,11 @@ class TestContext : DbContext {
1723 public DbSet < DbResource > DbResources { get ; set ; }
1824 }
1925
26+ public ContextGraphBuilder_Tests ( )
27+ {
28+ JsonApiOptions . ResourceNameFormatter = new DefaultResourceNameFormatter ( ) ;
29+ }
30+
2031 [ Fact ]
2132 public void Can_Build_ContextGraph_Using_Builder ( )
2233 {
@@ -55,6 +66,22 @@ public void Resources_Without_Names_Specified_Will_Use_Default_Formatter()
5566 Assert . Equal ( "test-resources" , resource . EntityName ) ;
5667 }
5768
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+
5885 [ Fact ]
5986 public void Attrs_Without_Names_Specified_Will_Use_Default_Formatter ( )
6087 {
@@ -67,12 +94,56 @@ public void Attrs_Without_Names_Specified_Will_Use_Default_Formatter()
6794
6895 // assert
6996 var resource = graph . GetContextEntity ( typeof ( TestResource ) ) ;
70- Assert . Equal ( "attribute" , resource . Attributes . Single ( ) . PublicAttributeName ) ;
97+ Assert . Equal ( "compound- attribute" , resource . Attributes . Single ( ) . PublicAttributeName ) ;
7198 }
7299
73- public class TestResource : Identifiable
100+ [ Fact ]
101+ public void Attrs_Without_Names_Specified_Will_Use_Configured_Formatter ( )
74102 {
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 ) ;
76147 }
77148 }
78149}
0 commit comments