Skip to content

Commit 19a7e88

Browse files
author
Chris Santero
committed
add todo sample project
1 parent 5bd6d44 commit 19a7e88

30 files changed

+1532
-0
lines changed
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
using System.Linq;
2+
using System.Net;
3+
using System.Net.Http;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using FluentAssertions;
7+
using JSONAPI.Json;
8+
using JSONAPI.TodoMVC.API.Models;
9+
using Microsoft.Owin.Testing;
10+
using Microsoft.VisualStudio.TestTools.UnitTesting;
11+
12+
namespace JSONAPI.TodoMVC.API.Tests.Acceptance
13+
{
14+
[TestClass]
15+
public class AcceptanceTests
16+
{
17+
[TestMethod]
18+
[DeploymentItem(@"Data\Todo.csv", "Data")]
19+
public async Task Get()
20+
{
21+
using (var effortConnection = TestHelpers.GetEffortConnection("Data"))
22+
{
23+
using (var server = TestServer.Create(app =>
24+
{
25+
var startup = new Startup(context => new TodoMvcContext(effortConnection, false));
26+
startup.Configuration(app);
27+
}))
28+
{
29+
var response = await server.CreateRequest("http://localhost/todos").GetAsync();
30+
response.StatusCode.Should().Be(HttpStatusCode.OK);
31+
var responseContent = await response.Content.ReadAsStringAsync();
32+
33+
var expected =
34+
JsonHelpers.MinifyJson(
35+
TestHelpers.ReadEmbeddedFile(@"Acceptance\Fixtures\GetResponse.json"));
36+
responseContent.Should().Be(expected);
37+
}
38+
}
39+
}
40+
41+
[TestMethod]
42+
[DeploymentItem(@"Data\Todo.csv", "Data")]
43+
public async Task GetById()
44+
{
45+
using (var effortConnection = TestHelpers.GetEffortConnection("Data"))
46+
{
47+
using (var server = TestServer.Create(app =>
48+
{
49+
var startup = new Startup(context => new TodoMvcContext(effortConnection, false));
50+
startup.Configuration(app);
51+
}))
52+
{
53+
var response = await server.CreateRequest("http://localhost/todos/2").GetAsync();
54+
response.StatusCode.Should().Be(HttpStatusCode.OK);
55+
var responseContent = await response.Content.ReadAsStringAsync();
56+
57+
var expected =
58+
JsonHelpers.MinifyJson(
59+
TestHelpers.ReadEmbeddedFile(@"Acceptance\Fixtures\GetByIdResponse.json"));
60+
responseContent.Should().Be(expected);
61+
}
62+
}
63+
}
64+
65+
[TestMethod]
66+
[DeploymentItem(@"Data\Todo.csv", "Data")]
67+
public async Task Post()
68+
{
69+
using (var effortConnection = TestHelpers.GetEffortConnection("Data"))
70+
{
71+
using (var server = TestServer.Create(app =>
72+
{
73+
var startup = new Startup(context => new TodoMvcContext(effortConnection, false));
74+
startup.Configuration(app);
75+
}))
76+
{
77+
var requestContent =
78+
JsonHelpers.MinifyJson(
79+
TestHelpers.ReadEmbeddedFile(@"Acceptance\Fixtures\PostRequest.json"));
80+
var response = await server
81+
.CreateRequest("http://localhost/todos")
82+
.And(request =>
83+
{
84+
request.Content = new StringContent(requestContent, Encoding.UTF8, "application/vnd.api+json");
85+
})
86+
.PostAsync();
87+
response.StatusCode.Should().Be(HttpStatusCode.OK);
88+
var responseContent = await response.Content.ReadAsStringAsync();
89+
90+
var expected =
91+
JsonHelpers.MinifyJson(
92+
TestHelpers.ReadEmbeddedFile(@"Acceptance\Fixtures\PostResponse.json"));
93+
responseContent.Should().Be(expected);
94+
}
95+
96+
using (var dbContext = new TodoMvcContext(effortConnection, false))
97+
{
98+
var allTodos = dbContext.Todos.ToArray();
99+
allTodos.Length.Should().Be(4);
100+
var actualTodo = allTodos.First(t => t.Id == "4");
101+
actualTodo.ShouldBeEquivalentTo(new Todo
102+
{
103+
Id = "4",
104+
Text = "Go to the gym",
105+
IsCompleted = false
106+
});
107+
}
108+
}
109+
}
110+
111+
[TestMethod]
112+
[DeploymentItem(@"Data\Todo.csv", "Data")]
113+
public async Task Put()
114+
{
115+
using (var effortConnection = TestHelpers.GetEffortConnection("Data"))
116+
{
117+
using (var server = TestServer.Create(app =>
118+
{
119+
var startup = new Startup(context => new TodoMvcContext(effortConnection, false));
120+
startup.Configuration(app);
121+
}))
122+
{
123+
var requestContent =
124+
JsonHelpers.MinifyJson(
125+
TestHelpers.ReadEmbeddedFile(@"Acceptance\Fixtures\PutRequest.json"));
126+
var response = await server
127+
.CreateRequest("http://localhost/todos/3")
128+
.And(request =>
129+
{
130+
request.Content = new StringContent(requestContent, Encoding.UTF8,
131+
"application/vnd.api+json");
132+
}).SendAsync("PUT");
133+
response.StatusCode.Should().Be(HttpStatusCode.OK);
134+
var responseContent = await response.Content.ReadAsStringAsync();
135+
136+
var expected =
137+
JsonHelpers.MinifyJson(
138+
TestHelpers.ReadEmbeddedFile(@"Acceptance\Fixtures\PutResponse.json"));
139+
responseContent.Should().Be(expected);
140+
}
141+
142+
using (var dbContext = new TodoMvcContext(effortConnection, false))
143+
{
144+
var allTodos = dbContext.Todos.ToArray();
145+
allTodos.Length.Should().Be(3);
146+
var actualTodo = allTodos.First(t => t.Id == "3");
147+
actualTodo.ShouldBeEquivalentTo(new Todo
148+
{
149+
Id = "3",
150+
Text = "Walk the dog",
151+
IsCompleted = true
152+
});
153+
}
154+
}
155+
}
156+
157+
[TestMethod]
158+
[DeploymentItem(@"Data\Todo.csv", "Data")]
159+
public async Task Delete()
160+
{
161+
using (var effortConnection = TestHelpers.GetEffortConnection("Data"))
162+
{
163+
using (var server = TestServer.Create(app =>
164+
{
165+
var startup = new Startup(context => new TodoMvcContext(effortConnection, false));
166+
startup.Configuration(app);
167+
}))
168+
{
169+
var response = await server
170+
.CreateRequest("http://localhost/todos/1")
171+
.SendAsync("DELETE");
172+
response.StatusCode.Should().Be(HttpStatusCode.NoContent);
173+
}
174+
175+
using (var dbContext = new TodoMvcContext(effortConnection, false))
176+
{
177+
var allTodos = dbContext.Todos.ToArray();
178+
allTodos.Length.Should().Be(2);
179+
var actualTodo = allTodos.FirstOrDefault(t => t.Id == "1");
180+
actualTodo.Should().BeNull();
181+
}
182+
}
183+
}
184+
}
185+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"todos": [
3+
{
4+
"id": "2",
5+
"text": "Do the laundry",
6+
"isCompleted": true
7+
}
8+
]
9+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"todos": [
3+
{
4+
"id": "1",
5+
"text": "Do the dishes",
6+
"isCompleted": false
7+
}, {
8+
"id": "2",
9+
"text": "Do the laundry",
10+
"isCompleted": true
11+
}, {
12+
"id": "3",
13+
"text": "Walk the dog",
14+
"isCompleted": false
15+
}
16+
]
17+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"todos": [
3+
{
4+
"id": "4",
5+
"text": "Go to the gym",
6+
"isCompleted": false
7+
}
8+
]
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"todos": [
3+
{
4+
"id": "4",
5+
"text": "Go to the gym",
6+
"isCompleted": false
7+
}
8+
]
9+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"todos": [
3+
{
4+
"id": "3",
5+
"isCompleted": true
6+
}
7+
]
8+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"todos": [
3+
{
4+
"id": "3",
5+
"text": "Walk the dog",
6+
"isCompleted": true
7+
}
8+
]
9+
}

0 commit comments

Comments
 (0)