|
8 | 8 | using JsonApiDotNetCore.Extensions;
|
9 | 9 | using JsonApiDotNetCoreExample.Models;
|
10 | 10 | 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; |
11 | 17 |
|
12 | 18 | namespace JsonApiDotNetCoreExampleTests.Acceptance.Spec
|
13 | 19 | {
|
@@ -46,5 +52,36 @@ public async Task Can_Select_Sparse_Fieldsets()
|
46 | 52 | Assert.Equal(0, result.Ordinal);
|
47 | 53 | Assert.Equal(todoItem.Description, result.Description);
|
48 | 54 | }
|
| 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 | + } |
49 | 86 | }
|
50 | 87 | }
|
0 commit comments