Skip to content

Commit 2af4a26

Browse files
author
Harro van der Kroft
committed
feat: updated tests for updating data with only get attribute set
1 parent 4fb17fe commit 2af4a26

File tree

4 files changed

+76
-42
lines changed

4 files changed

+76
-42
lines changed

JsonApiDotnetCore.sln

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
Microsoft Visual Studio Solution File, Format Version 12.00
2-
# Visual Studio 15
3-
VisualStudioVersion = 15.0.27130.2010
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.28606.126
45
MinimumVisualStudioVersion = 10.0.40219.1
56
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JsonApiDotNetCore", "src\JsonApiDotNetCore\JsonApiDotNetCore.csproj", "{C0EC9E70-EB2E-436F-9D94-FA16FA774123}"
67
EndProject
@@ -41,13 +42,13 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OperationsExample", "src\Ex
4142
EndProject
4243
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OperationsExampleTests", "test\OperationsExampleTests\OperationsExampleTests.csproj", "{9CD2C116-D133-4FE4-97DA-A9FEAFF045F1}"
4344
EndProject
44-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ResourceEntitySeparationExample", "src\Examples\ResourceEntitySeparationExample\ResourceEntitySeparationExample.csproj", "{F4097194-9415-418A-AB4E-315C5D5466AF}"
45+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ResourceEntitySeparationExample", "src\Examples\ResourceEntitySeparationExample\ResourceEntitySeparationExample.csproj", "{F4097194-9415-418A-AB4E-315C5D5466AF}"
4546
EndProject
46-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ResourceEntitySeparationExampleTests", "test\ResourceEntitySeparationExampleTests\ResourceEntitySeparationExampleTests.csproj", "{6DFA30D7-1679-4333-9779-6FB678E48EF5}"
47+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ResourceEntitySeparationExampleTests", "test\ResourceEntitySeparationExampleTests\ResourceEntitySeparationExampleTests.csproj", "{6DFA30D7-1679-4333-9779-6FB678E48EF5}"
4748
EndProject
48-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GettingStarted", "src\Examples\GettingStarted\GettingStarted.csproj", "{DF9BFD82-D937-4907-B0B4-64670417115F}"
49+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GettingStarted", "src\Examples\GettingStarted\GettingStarted.csproj", "{DF9BFD82-D937-4907-B0B4-64670417115F}"
4950
EndProject
50-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DiscoveryTests", "test\DiscoveryTests\DiscoveryTests.csproj", "{09C0C8D8-B721-4955-8889-55CB149C3B5C}"
51+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DiscoveryTests", "test\DiscoveryTests\DiscoveryTests.csproj", "{09C0C8D8-B721-4955-8889-55CB149C3B5C}"
5152
EndProject
5253
Global
5354
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -191,6 +192,18 @@ Global
191192
{6DFA30D7-1679-4333-9779-6FB678E48EF5}.Release|x64.Build.0 = Release|Any CPU
192193
{6DFA30D7-1679-4333-9779-6FB678E48EF5}.Release|x86.ActiveCfg = Release|Any CPU
193194
{6DFA30D7-1679-4333-9779-6FB678E48EF5}.Release|x86.Build.0 = Release|Any CPU
195+
{DF9BFD82-D937-4907-B0B4-64670417115F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
196+
{DF9BFD82-D937-4907-B0B4-64670417115F}.Debug|Any CPU.Build.0 = Debug|Any CPU
197+
{DF9BFD82-D937-4907-B0B4-64670417115F}.Debug|x64.ActiveCfg = Debug|Any CPU
198+
{DF9BFD82-D937-4907-B0B4-64670417115F}.Debug|x64.Build.0 = Debug|Any CPU
199+
{DF9BFD82-D937-4907-B0B4-64670417115F}.Debug|x86.ActiveCfg = Debug|Any CPU
200+
{DF9BFD82-D937-4907-B0B4-64670417115F}.Debug|x86.Build.0 = Debug|Any CPU
201+
{DF9BFD82-D937-4907-B0B4-64670417115F}.Release|Any CPU.ActiveCfg = Release|Any CPU
202+
{DF9BFD82-D937-4907-B0B4-64670417115F}.Release|Any CPU.Build.0 = Release|Any CPU
203+
{DF9BFD82-D937-4907-B0B4-64670417115F}.Release|x64.ActiveCfg = Release|Any CPU
204+
{DF9BFD82-D937-4907-B0B4-64670417115F}.Release|x64.Build.0 = Release|Any CPU
205+
{DF9BFD82-D937-4907-B0B4-64670417115F}.Release|x86.ActiveCfg = Release|Any CPU
206+
{DF9BFD82-D937-4907-B0B4-64670417115F}.Release|x86.Build.0 = Release|Any CPU
194207
{09C0C8D8-B721-4955-8889-55CB149C3B5C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
195208
{09C0C8D8-B721-4955-8889-55CB149C3B5C}.Debug|Any CPU.Build.0 = Debug|Any CPU
196209
{09C0C8D8-B721-4955-8889-55CB149C3B5C}.Debug|x64.ActiveCfg = Debug|Any CPU
@@ -203,8 +216,6 @@ Global
203216
{09C0C8D8-B721-4955-8889-55CB149C3B5C}.Release|x64.Build.0 = Release|Any CPU
204217
{09C0C8D8-B721-4955-8889-55CB149C3B5C}.Release|x86.ActiveCfg = Release|Any CPU
205218
{09C0C8D8-B721-4955-8889-55CB149C3B5C}.Release|x86.Build.0 = Release|Any CPU
206-
{DF9BFD82-D937-4907-B0B4-64670417115F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
207-
{DF9BFD82-D937-4907-B0B4-64670417115F}.Debug|Any CPU.Build.0 = Debug|Any CPU
208219
EndGlobalSection
209220
GlobalSection(SolutionProperties) = preSolution
210221
HideSolutionNode = FALSE

src/Examples/JsonApiDotNetCoreExample/Models/TodoItem.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using JsonApiDotNetCore.Models;
44

@@ -30,7 +30,11 @@ public TodoItem()
3030
[Attr("updated-date")]
3131
public DateTime? UpdatedDate { get; set; }
3232

33-
33+
[Attr("calculated-value")]
34+
public string CalculatedValue
35+
{
36+
get => "joe";
37+
}
3438

3539
public int? OwnerId { get; set; }
3640
public int? AssigneeId { get; set; }

src/Examples/JsonApiDotNetCoreExample/Startup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public virtual IServiceProvider ConfigureServices(IServiceCollection services)
4242
},
4343
mvcBuilder,
4444
discovery => discovery.AddCurrentAssembly());
45-
45+
4646
return services.BuildServiceProvider();
4747
}
4848

test/JsonApiDotNetCoreExampleTests/Acceptance/Spec/UpdatingDataTests.cs

Lines changed: 49 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Net;
@@ -40,10 +40,43 @@ public UpdatingDataTests(TestFixture<TestStartup> fixture)
4040
.RuleFor(p => p.LastName, f => f.Name.LastName());
4141
}
4242

43+
44+
[Fact]
45+
public async Task Response400IfUpdatingNotSettableAttribute()
46+
{
47+
// Arrange
48+
var builder = new WebHostBuilder().UseStartup<Startup>();
49+
var server = new TestServer(builder);
50+
var client = server.CreateClient();
51+
52+
var todoItem = _todoItemFaker.Generate();
53+
_context.TodoItems.Add(todoItem);
54+
_context.SaveChanges();
55+
56+
var content = new
57+
{
58+
datea = new
59+
{
60+
type = "todo-items",
61+
attributes = new
62+
{
63+
calculatedAttribute = "lol"
64+
}
65+
}
66+
};
67+
var request = PrepareRequest("PATCH", $"/api/v1/todo-items/{todoItem.Id}", content);
68+
69+
// Act
70+
var response = await client.SendAsync(request);
71+
72+
// Assert
73+
Assert.Equal(422, Convert.ToInt32(response.StatusCode));
74+
}
75+
4376
[Fact]
4477
public async Task Respond_404_If_EntityDoesNotExist()
4578
{
46-
// arrange
79+
// Arrange
4780
var maxPersonId = _context.TodoItems.LastOrDefault()?.Id ?? 0;
4881
var todoItem = _todoItemFaker.Generate();
4982
var builder = new WebHostBuilder()
@@ -65,13 +98,7 @@ public async Task Respond_404_If_EntityDoesNotExist()
6598
}
6699
}
67100
};
68-
69-
var httpMethod = new HttpMethod("PATCH");
70-
var route = $"/api/v1/todo-items/{maxPersonId + 100}";
71-
var request = new HttpRequestMessage(httpMethod, route);
72-
73-
request.Content = new StringContent(JsonConvert.SerializeObject(content));
74-
request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.api+json");
101+
var request = PrepareRequest("PATCH", $"/api/v1/todo-items/{maxPersonId + 100}", content);
75102

76103
// Act
77104
var response = await client.SendAsync(request);
@@ -109,13 +136,7 @@ public async Task Can_Patch_Entity()
109136
}
110137
}
111138
};
112-
113-
var httpMethod = new HttpMethod("PATCH");
114-
var route = $"/api/v1/todo-items/{todoItem.Id}";
115-
var request = new HttpRequestMessage(httpMethod, route);
116-
117-
request.Content = new StringContent(JsonConvert.SerializeObject(content));
118-
request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.api+json");
139+
var request = PrepareRequest("PATCH", $"/api/v1/todo-items/{todoItem.Id}", content);
119140

120141
// Act
121142
var response = await client.SendAsync(request);
@@ -172,13 +193,7 @@ public async Task Patch_Entity_With_HasMany_Does_Not_Included_Relationships()
172193
}
173194
}
174195
};
175-
176-
var httpMethod = new HttpMethod("PATCH");
177-
var route = $"/api/v1/people/{person.Id}";
178-
var request = new HttpRequestMessage(httpMethod, route);
179-
180-
request.Content = new StringContent(JsonConvert.SerializeObject(content));
181-
request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.api+json");
196+
var request = PrepareRequest("PATCH", $"/api/v1/people/{person.Id}", content);
182197

183198
// Act
184199
var response = await client.SendAsync(request);
@@ -237,13 +252,7 @@ public async Task Can_Patch_Entity_And_HasOne_Relationships()
237252
}
238253
}
239254
};
240-
241-
var httpMethod = new HttpMethod("PATCH");
242-
var route = $"/api/v1/todo-items/{todoItem.Id}";
243-
var request = new HttpRequestMessage(httpMethod, route);
244-
245-
request.Content = new StringContent(JsonConvert.SerializeObject(content));
246-
request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.api+json");
255+
var request = PrepareRequest("PATCH", $"/api/v1/todo-items/{todoItem.Id}", content);
247256

248257
// Act
249258
var response = await client.SendAsync(request);
@@ -255,5 +264,15 @@ public async Task Can_Patch_Entity_And_HasOne_Relationships()
255264
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
256265
Assert.Equal(person.Id, updatedTodoItem.OwnerId);
257266
}
267+
268+
private HttpRequestMessage PrepareRequest(string method, string route, object content)
269+
{
270+
var httpMethod = new HttpMethod(method);
271+
var request = new HttpRequestMessage(httpMethod, route);
272+
273+
request.Content = new StringContent(JsonConvert.SerializeObject(content));
274+
request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.api+json");
275+
return request;
276+
}
258277
}
259278
}

0 commit comments

Comments
 (0)