Skip to content

Commit be0d510

Browse files
committed
add tests
1 parent 02f98c7 commit be0d510

File tree

2 files changed

+130
-3
lines changed

2 files changed

+130
-3
lines changed

test/UnitTests/Builders/ContextGraphBuilder_Tests.cs

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1+
using System;
2+
using System.Collections.Generic;
13
using System.Linq;
4+
using System.Reflection;
5+
using Humanizer;
26
using JsonApiDotNetCore.Builders;
7+
using JsonApiDotNetCore.Configuration;
38
using JsonApiDotNetCore.Extensions;
9+
using JsonApiDotNetCore.Graph;
410
using JsonApiDotNetCore.Internal;
511
using JsonApiDotNetCore.Models;
612
using 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
}

test/UnitTests/Graph/TypeLocator_Tests.cs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Reflection;
23
using JsonApiDotNetCore.Graph;
34
using JsonApiDotNetCore.Models;
45
using Xunit;
@@ -85,6 +86,61 @@ public void GetIdType_Correctly_Identifies_NonJsonApiResource()
8586
Assert.False(result.isJsonApiResource);
8687
Assert.Equal(exextedIdType, result.idType);
8788
}
89+
90+
[Fact]
91+
public void GetIdentifableTypes_Locates_Identifiable_Resource()
92+
{
93+
// arrange
94+
var resourceType = typeof(Model);
95+
96+
// act
97+
var results = TypeLocator.GetIdentifableTypes(resourceType.Assembly);
98+
99+
// assert
100+
Assert.Contains(results, r => r.ResourceType == resourceType);
101+
}
102+
103+
[Fact]
104+
public void GetIdentifableTypes__Only_Contains_IIdentifiable_Types()
105+
{
106+
// arrange
107+
var resourceType = typeof(Model);
108+
109+
// act
110+
var resourceDescriptors = TypeLocator.GetIdentifableTypes(resourceType.Assembly);
111+
112+
// assert
113+
foreach(var resourceDescriptor in resourceDescriptors)
114+
Assert.True(typeof(IIdentifiable).IsAssignableFrom(resourceDescriptor.ResourceType));
115+
}
116+
117+
[Fact]
118+
public void TryGetResourceDescriptor_Returns_True_If_Type_Is_IIdentfiable()
119+
{
120+
// arrange
121+
var resourceType = typeof(Model);
122+
123+
// act
124+
var isJsonApiResource = TypeLocator.TryGetResourceDescriptor(resourceType, out var descriptor);
125+
126+
// assert
127+
Assert.True(isJsonApiResource);
128+
Assert.Equal(resourceType, descriptor.ResourceType);
129+
Assert.Equal(typeof(int), descriptor.IdType);
130+
}
131+
132+
[Fact]
133+
public void TryGetResourceDescriptor_Returns_False_If_Type_Is_IIdentfiable()
134+
{
135+
// arrange
136+
var resourceType = typeof(String);
137+
138+
// act
139+
var isJsonApiResource = TypeLocator.TryGetResourceDescriptor(resourceType, out var descriptor);
140+
141+
// assert
142+
Assert.False(isJsonApiResource);
143+
}
88144
}
89145

90146

0 commit comments

Comments
 (0)