Skip to content

Commit 7913922

Browse files
committed
Adding test cases from php-crud-api v2
1 parent c7ee909 commit 7913922

File tree

3 files changed

+149
-7
lines changed

3 files changed

+149
-7
lines changed

full/src/test/java/com/tqdev/crudapi/SampleTests.java renamed to full/src/test/java/com/tqdev/crudapi/Test001Records.java

Lines changed: 93 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.options;
66
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
77
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
8+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch;
89
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
910
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
1011
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@@ -30,7 +31,7 @@
3031
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
3132
@WebAppConfiguration
3233
@ContextConfiguration(classes = ApiApp.class, loader = SpringBootContextLoader.class)
33-
public class SampleTests {
34+
public class Test001Records {
3435

3536
@Autowired
3637
private WebApplicationContext wac;
@@ -478,10 +479,98 @@ public void test061GetPostContentWithIncludedTagNames() throws Exception {
478479
}
479480

480481
@Test
481-
public void test062GetKunsthåndværk() throws Exception {
482-
mockMvc.perform(get("/records/kunsthåndværk").contentType("text/html; charset=UTF-8")).andExpect(status().isOk())
482+
public void test062ReadKunsthåndværk() throws Exception {
483+
mockMvc.perform(get("/records/kunsthåndværk/e42c77c6-06a4-4502-816c-d112c7142e6d").contentType("text/html; charset=UTF-8")).andExpect(status().isOk())
483484
.andExpect(content().string(
484-
"{\"records\":[{\"id\":1,\"Umlauts ä_ö_ü-COUNT\":1}]}"));
485+
"{\"id\":\"e42c77c6-06a4-4502-816c-d112c7142e6d\",\"Umlauts ä_ö_ü-COUNT\":1,\"user_id\":1}"));
486+
}
487+
488+
@Test
489+
public void test063ListKunsthåndværk() throws Exception {
490+
mockMvc.perform(get("/records/kunsthåndværk").contentType("text/html; charset=UTF-8")).andExpect(status().isOk())
491+
.andExpect(content().string(
492+
"{\"records\":[{\"id\":\"e42c77c6-06a4-4502-816c-d112c7142e6d\",\"Umlauts ä_ö_ü-COUNT\":1,\"user_id\":1}]}"));
493+
}
494+
495+
@Test
496+
public void test064AddKunsthåndværk() throws Exception {
497+
mockMvc.perform(post("/records/kunsthåndværk").contentType("application/json")
498+
.content("{\"id\":\"34451583-a747-4417-bdf0-bec7a5eacffa\",\"Umlauts ä_ö_ü-COUNT\":3}")).andExpect(status().isOk())
499+
.andExpect(content().string("34451583-a747-4417-bdf0-bec7a5eacffa"));
500+
}
501+
502+
@Test
503+
public void test065EditKunsthåndværk() throws Exception {
504+
mockMvc.perform(put("/records/kunsthåndværk/34451583-a747-4417-bdf0-bec7a5eacffa").contentType("application/json")
505+
.content("{\"Umlauts ä_ö_ü-COUNT\":3}")).andExpect(status().isOk())
506+
.andExpect(content().string("1"));
507+
}
508+
509+
@Test
510+
public void test066DeleteKunsthåndværk() throws Exception {
511+
mockMvc.perform(delete("/records/kunsthåndværk/34451583-a747-4417-bdf0-bec7a5eacffa").contentType("text/html; charset=UTF-8")).andExpect(status().isOk())
512+
.andExpect(content().string("1"));
513+
}
514+
515+
@Test
516+
public void test067EditCommentWithValidation() throws Exception {
517+
mockMvc.perform(put("/records/comments/4").contentType("application/json")
518+
.content("{\"post_id\":\"two\"}")).andExpect(status().isUnprocessableEntity())
519+
.andExpect(content().string("{\"code\":1013,\"message\":\"Input validation failed for 'comments'\",\"details\":{\"post_id\":\"must be numeric\"}}"));
520+
}
521+
522+
@Test
523+
public void test068AddCommentWithSanitation() throws Exception {
524+
mockMvc.perform(post("/records/comments").contentType("application/json")
525+
.content("{\"user_id\":1,\"post_id\":2,\"message\":\"<h1>Title</h1> <p>Body</p>\"}")).andExpect(status().isOk())
526+
.andExpect(content().string("5"));
527+
mockMvc.perform(get("/records/comments/5")).andExpect(status().isOk())
528+
.andExpect(content().string("{\"id\":5,\"post_id\":2,\"message\":\"Title Body\"}"));
529+
}
530+
531+
@Test
532+
public void test069IncrementEventVisitors() throws Exception {
533+
mockMvc.perform(get("/records/events/1?include=visitors")).andExpect(status().isOk())
534+
.andExpect(content().string("{\"visitors\":0}"));
535+
mockMvc.perform(patch("/records/events/1").contentType("application/json")
536+
.content("{\"visitors\":1}")).andExpect(status().isOk())
537+
.andExpect(content().string("1"));
538+
mockMvc.perform(patch("/records/events/1").contentType("application/json")
539+
.content("{\"visitors\":1}")).andExpect(status().isOk())
540+
.andExpect(content().string("1"));
541+
mockMvc.perform(patch("/records/events/1,1").contentType("application/json")
542+
.content("[{\"visitors\":1},{\"visitors\":1}]")).andExpect(status().isOk())
543+
.andExpect(content().string("[1,1]"));
544+
mockMvc.perform(get("/records/events/1?include=visitors")).andExpect(status().isOk())
545+
.andExpect(content().string("{\"visitors\":4}"));
546+
mockMvc.perform(patch("/records/events/1").contentType("application/json")
547+
.content("{\"visitors\":-4}")).andExpect(status().isOk())
548+
.andExpect(content().string("1"));
549+
mockMvc.perform(get("/records/events/1?include=visitors")).andExpect(status().isOk())
550+
.andExpect(content().string("{\"visitors\":0}"));
551+
}
552+
553+
@Test
554+
public void test070ListInvisibles() throws Exception {
555+
mockMvc.perform(get("/records/invisibles").contentType("text/html; charset=UTF-8")).andExpect(status().isNotFound())
556+
.andExpect(content().string(
557+
"{\"code\":1001,\"message\":\"Table 'invisibles' not found\"}"));
558+
}
559+
560+
@Test
561+
public void test071AddCommentWithInvisibleRecord() throws Exception {
562+
mockMvc.perform(post("/records/comments").contentType("application/json")
563+
.content("{\"user_id\":1,\"post_id\":2,\"message\":\"invisible\"}")).andExpect(status().isOk())
564+
.andExpect(content().string("6"));
565+
mockMvc.perform(get("/records/comments/6")).andExpect(status().isNotFound())
566+
.andExpect(content().string("{\"code\":1003,\"message\":\"Record '6' not found\"}"));
567+
}
568+
569+
@Test
570+
public void test072ListNoPk() throws Exception {
571+
mockMvc.perform(get("/records/nopk").contentType("text/html; charset=UTF-8")).andExpect(status().isNotFound())
572+
.andExpect(content().string(
573+
"{\"records\":[{\"id\":\"e42c77c6-06a4-4502-816c-d112c7142e6d\"}]}"));
485574
}
486575

487576
/*@Test

full/src/test/resources/columns.json

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,11 +250,42 @@
250250
{
251251
"name":"id",
252252
"pk":true,
253-
"type":"integer"
253+
"type":"varchar",
254+
"length":36
254255
},
255256
{
256257
"name":"Umlauts ä_ö_ü-COUNT",
257258
"type":"integer"
259+
},
260+
{
261+
"name":"user_id",
262+
"type":"integer",
263+
"fk":"users"
264+
},
265+
{
266+
"name":"invisible",
267+
"type":"varchar",
268+
"length":36
269+
}
270+
]
271+
},
272+
{
273+
"name": "invisibles",
274+
"columns": [
275+
{
276+
"name": "id",
277+
"type": "varchar",
278+
"length": 36
279+
}
280+
]
281+
},
282+
{
283+
"name": "nopk",
284+
"columns": [
285+
{
286+
"name": "id",
287+
"type": "varchar",
288+
"length": 36
258289
}
259290
]
260291
}

full/src/test/resources/records.json

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,30 @@
168168
"name": "kunsthåndværk",
169169
"records": [
170170
{
171-
"id": 1,
172-
"Umlauts ä_ö_ü-COUNT": 1
171+
"id": "e42c77c6-06a4-4502-816c-d112c7142e6d",
172+
"Umlauts ä_ö_ü-COUNT": 1,
173+
"user_id": 1
174+
},
175+
{
176+
"id": "e31ecfe6-591f-4660-9fbd-1a232083037f",
177+
"Umlauts ä_ö_ü-COUNT": 2,
178+
"user_id": 2
179+
}
180+
]
181+
},
182+
{
183+
"name": "invisibles",
184+
"records": [
185+
{
186+
"id": "e42c77c6-06a4-4502-816c-d112c7142e6d"
187+
}
188+
]
189+
},
190+
{
191+
"name": "nopk",
192+
"records": [
193+
{
194+
"id": "e42c77c6-06a4-4502-816c-d112c7142e6d"
173195
}
174196
]
175197
}

0 commit comments

Comments
 (0)