Skip to content

Commit 743edd1

Browse files
committed
test(sparse-fields): test that fields can be restricted using query
1 parent b3c3f16 commit 743edd1

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

test/JsonApiDotNetCoreExampleTests/Acceptance/Spec/SparseFieldSetTests.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88
using JsonApiDotNetCore.Extensions;
99
using JsonApiDotNetCoreExample.Models;
1010
using System.Linq;
11+
using Microsoft.AspNetCore.Hosting;
12+
using System.Net.Http;
13+
using Microsoft.AspNetCore.TestHost;
14+
using Newtonsoft.Json;
15+
using JsonApiDotNetCore.Models;
16+
using System.Diagnostics;
1117

1218
namespace JsonApiDotNetCoreExampleTests.Acceptance.Spec
1319
{
@@ -46,5 +52,36 @@ public async Task Can_Select_Sparse_Fieldsets()
4652
Assert.Equal(0, result.Ordinal);
4753
Assert.Equal(todoItem.Description, result.Description);
4854
}
55+
56+
[Fact]
57+
public async Task Fields_Query_Selects_Sparse_Field_Sets()
58+
{
59+
// arrange
60+
var todoItem = new TodoItem {
61+
Description = "description",
62+
Ordinal = 1
63+
};
64+
_dbContext.TodoItems.Add(todoItem);
65+
await _dbContext.SaveChangesAsync();
66+
67+
var builder = new WebHostBuilder()
68+
.UseStartup<Startup>();
69+
var httpMethod = new HttpMethod("GET");
70+
var server = new TestServer(builder);
71+
var client = server.CreateClient();
72+
73+
var route = $"/api/v1/todo-items/{todoItem.Id}?fields[todo-items]=description";
74+
var request = new HttpRequestMessage(httpMethod, route);
75+
76+
// act
77+
var response = await client.SendAsync(request);
78+
var body = await response.Content.ReadAsStringAsync();
79+
var deserializeBody = JsonConvert.DeserializeObject<Document>(body);
80+
81+
// assert
82+
Assert.Equal(todoItem.StringId, deserializeBody.Data.Id);
83+
Assert.Equal(1, deserializeBody.Data.Attributes.Count);
84+
Assert.Equal(todoItem.Description, deserializeBody.Data.Attributes["description"]);
85+
}
4986
}
5087
}

0 commit comments

Comments
 (0)