Skip to content

Commit 7cff6d0

Browse files
committed
test(no-ef): additional tests / examples
1 parent 3d95c01 commit 7cff6d0

File tree

2 files changed

+75
-3
lines changed

2 files changed

+75
-3
lines changed

src/NoEntityFrameworkExample/Services/TodoItemService.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,14 @@ public Task<object> GetRelationshipsAsync(int id, string relationshipName)
6464
throw new NotImplementedException();
6565
}
6666

67-
public Task<TodoItem> CreateAsync(TodoItem entity)
67+
public async Task<TodoItem> CreateAsync(TodoItem entity)
6868
{
69-
throw new NotImplementedException();
69+
return (await QueryAsync<TodoItem>(async connection =>
70+
{
71+
var query = "insert into \"TodoItems\" (\"Description\", \"Ordinal\") values (@description, @ordinal) returning \"Id\",\"Description\",\"Ordinal\"";
72+
var result = await connection.QueryAsync<TodoItem>(query, new { description = entity.Description, ordinal = entity.Ordinal });
73+
return result;
74+
})).SingleOrDefault();
7075
}
7176

7277
public Task<bool> DeleteAsync(int id)

test/NoEntityFrameworkTests/Acceptance/Extensibility/NoEntityFrameworkTests.cs

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
using System.Net;
1010
using JsonApiDotNetCoreExample.Models;
1111
using JsonApiDotNetCoreExample.Data;
12+
using System;
13+
using Newtonsoft.Json;
14+
using System.Net.Http.Headers;
1215

1316
namespace NoEntityFrameworkTests.Acceptance.Extensibility
1417
{
@@ -27,7 +30,7 @@ public NoEntityFrameworkTests()
2730
}
2831

2932
[Fact]
30-
public async Task Can_Implement_Custom_IResourceService_Without_EFAsync()
33+
public async Task Can_Get_TodoItems()
3134
{
3235
// arrange
3336
_context.TodoItems.Add(new TodoItem());
@@ -51,5 +54,69 @@ public async Task Can_Implement_Custom_IResourceService_Without_EFAsync()
5154
Assert.NotNull(deserializedBody);
5255
Assert.NotEmpty(deserializedBody);
5356
}
57+
58+
[Fact]
59+
public async Task Can_Get_TodoItems_By_Id()
60+
{
61+
// arrange
62+
var todoItem = new TodoItem();
63+
_context.TodoItems.Add(todoItem);
64+
_context.SaveChanges();
65+
66+
var client = _server.CreateClient();
67+
68+
var httpMethod = new HttpMethod("GET");
69+
var route = $"/api/v1/custom-todo-items/{todoItem.Id}";
70+
71+
var request = new HttpRequestMessage(httpMethod, route);
72+
73+
// act
74+
var response = await client.SendAsync(request);
75+
var responseBody = await response.Content.ReadAsStringAsync();
76+
var deserializedBody = (TodoItem)_server.GetService<IJsonApiDeSerializer>()
77+
.Deserialize(responseBody);
78+
79+
// assert
80+
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
81+
Assert.NotNull(deserializedBody);
82+
Assert.Equal(todoItem.Id, deserializedBody.Id);
83+
}
84+
85+
[Fact]
86+
public async Task Can_Create_TodoItems()
87+
{
88+
// arrange
89+
var description = Guid.NewGuid().ToString();
90+
var client = _server.CreateClient();
91+
var httpMethod = new HttpMethod("POST");
92+
var route = $"/api/v1/custom-todo-items/";
93+
var content = new
94+
{
95+
data = new
96+
{
97+
type = "custom-todo-items",
98+
attributes = new
99+
{
100+
description = description,
101+
ordinal = 1
102+
}
103+
}
104+
};
105+
106+
var request = new HttpRequestMessage(httpMethod, route);
107+
request.Content = new StringContent(JsonConvert.SerializeObject(content));
108+
request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.api+json");
109+
110+
// act
111+
var response = await client.SendAsync(request);
112+
var responseBody = await response.Content.ReadAsStringAsync();
113+
var deserializedBody = (TodoItem)_server.GetService<IJsonApiDeSerializer>()
114+
.Deserialize(responseBody);
115+
116+
// assert
117+
Assert.Equal(HttpStatusCode.Created, response.StatusCode);
118+
Assert.NotNull(deserializedBody);
119+
Assert.Equal(description, deserializedBody.Description);
120+
}
54121
}
55122
}

0 commit comments

Comments
 (0)