Skip to content

Commit

Permalink
Separate test data from the tests and refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
souravroy committed Feb 10, 2024
1 parent 3d1c1d7 commit 749f0b8
Show file tree
Hide file tree
Showing 16 changed files with 600 additions and 738 deletions.
11 changes: 11 additions & 0 deletions integration-test-guideline.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Let's improve writing tests for better readability and maintainability
1. Please define headers, query params together and make sure those are organised
2. Most of the test data are separated into ITestUtils.java, to reuse test data
3. Avoid redundant assertions, tests should be concise. For e.g. no need to have both these assertions:
.andExpect(jsonPath("$.rows").isArray())
.andExpect(jsonPath("$.rows", hasSize(2)))
If the rows has size > 1, it is implicit that it is an array
4. On the contrary, please provide with just enough assertions.
For e.g. don't just assert the error like .andExpect(status().isBadRequest()), assert the error details as well.
Please remember: Tests are another source of documentation to determine what the code does.
We must make the tests readable.
202 changes: 49 additions & 153 deletions src/test/java/com/homihq/db2rest/rest/MySQLBulkCreateControllerTest.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.homihq.db2rest.rest;

import com.homihq.db2rest.MySQLBaseIntegrationTest;
import com.homihq.db2rest.utils.ITestUtil;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.http.MediaType;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.hamcrest.Matchers.*;
import static org.springframework.http.MediaType.APPLICATION_JSON;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
Expand All @@ -18,52 +20,21 @@ class MySQLBulkCreateControllerTest extends MySQLBaseIntegrationTest {
@DisplayName("Create many films.")
void create() throws Exception {

var json = """
[
{
"title" : "Dunki",
"description" : "Film about illegal immigration" ,
"release_year" : 2023,
"language_id" : 6,
"original_language_id" : 6,
"rental_duration" : 6,
"rental_rate" : 0.99 ,
"length" : 150,
"replacement_cost" : 20.99 ,
"rating" : "PG-13" ,
"special_features" : "Commentaries"
},
{
"title" : "Jawan",
"description" : "Socio-econmic problems and corruption" ,
"release_year" : 2023,
"language_id" : 6,
"original_language_id" : 6,
"rental_duration" : 5,
"rental_rate" : 0.99 ,
"length" : 160,
"replacement_cost" : 20.99 ,
"rating" : "PG-13" ,
"special_features" : "Commentaries"
}
]
""";


mockMvc.perform(post("/film/bulk").contentType(MediaType.APPLICATION_JSON).characterEncoding("utf-8")
.header("Content-Profile", "public")
.content(json).accept(MediaType.APPLICATION_JSON))
mockMvc.perform(post("/film/bulk")
.characterEncoding(UTF_8)
.contentType(APPLICATION_JSON)
.accept(APPLICATION_JSON)
.header("Content-Profile", "sakila")
.content(ITestUtil.BULK_CREATE_FILM_REQUEST))
.andExpect(status().isCreated())
.andExpect(jsonPath("$.rows").isArray())
.andExpect(jsonPath("$.rows", hasSize(2)))
.andExpect(jsonPath("$.rows", hasItem(1)))
.andExpect(jsonPath("$.rows", hasItem(1)))
.andExpect(jsonPath("$.generated_keys").isArray())
.andExpect(jsonPath("$.generated_keys", hasSize(2)))
.andExpect(jsonPath("$.generated_keys", allOf(notNullValue())))
// .andDo(print())
//.andDo(print())
.andDo(document("mysql-bulk-create-films"));

}
Expand All @@ -72,25 +43,17 @@ void create() throws Exception {
@DisplayName("Create many films with CSV type.")
void createCSV() throws Exception {

String csv = """
title,description,release_year,language_id,original_language_id,rental_duration,rental_rate,length,replacement_cost,rating,special_features
Dunki2,Film about illegal immigration,2023,6,6,6,0.99,150,20.99,PG-13,Commentaries
Jawan2,Socio-econmic problems and corruption,2023,6,6,6,0.99,160,20.99,PG-13,Commentaries
""";

mockMvc.perform(post("/film/bulk").characterEncoding("utf-8")
.content(csv)
mockMvc.perform(post("/film/bulk")
.characterEncoding(UTF_8)
.contentType("text/csv")
.accept(MediaType.APPLICATION_JSON))
.accept(APPLICATION_JSON)
.content(ITestUtil.CREATE_FILM_REQUEST_CSV))
.andExpect(status().isCreated())
.andExpect(jsonPath("$.rows").isArray())
.andExpect(jsonPath("$.rows", hasSize(2)))
.andExpect(jsonPath("$.rows", hasItem(1)))
.andExpect(jsonPath("$.rows", hasItem(1)))
.andExpect(jsonPath("$.generated_keys").isArray())
.andExpect(jsonPath("$.generated_keys", hasSize(2)))
.andExpect(jsonPath("$.generated_keys", allOf(notNullValue())))
// .andDo(print())
//.andDo(print())
.andDo(document("mysql-bulk-create-films-csv"));

}
Expand All @@ -99,16 +62,11 @@ void createCSV() throws Exception {
@DisplayName("Create many films with CSV type resulting error.")
void createCSVWithError() throws Exception {

String csv = """
title,description,release_year,language_id,original_language_id,rental_duration,rental_rate,length,replacement_cost,rating,special_features,country
Dunki2,Film about illegal immigration,2023,6,6,6,0.99,150,20.99,PG-13,Commentaries,India
Jawan2,Socio-econmic problems and corruption,2023,6,6,6,0.99,160,20.99,PG-13,Commentaries,India
""";

mockMvc.perform(post("/film/bulk").characterEncoding("utf-8")
.content(csv)
mockMvc.perform(post("/film/bulk")
.characterEncoding(UTF_8)
.contentType("text/csv")
.accept(MediaType.APPLICATION_JSON))
.accept(APPLICATION_JSON)
.content(ITestUtil.CREATE_FILM_BAD_REQUEST_CSV))
.andExpect(status().isBadRequest())
//.andDo(print())
.andDo(document("mysql-bulk-create-films-csv-error"));
Expand All @@ -119,72 +77,30 @@ void createCSVWithError() throws Exception {
@DisplayName("Create many films with failure.")
void createError() throws Exception {

var json = """
[
{
"title" : "Dunki",
"description" : "Film about illegal immigration" ,
"release_year" : 2023,
"language_id" : 6,
"original_language_id" : 6,
"rental_duration" : 6,
"rental_rate" : 0.99 ,
"length" : 150,
"replacement_cost" : 20.99 ,
"rating" : "PG-13" ,
"special_features" : "Commentaries",
"country" : "INDIA"
},
{
"title" : "Jawan",
"description" : "Socio-econmic problems and corruption" ,
"release_year" : 2023,
"language_id" : 6,
"original_language_id" : 6,
"rental_duration" : 5,
"rental_rate" : 0.99 ,
"length" : 160,
"replacement_cost" : 20.99 ,
"rating" : "PG-13" ,
"special_features" : "Commentaries"
}
]
""";


mockMvc.perform(post("/film/bulk").contentType(MediaType.APPLICATION_JSON).characterEncoding("utf-8")
.header("Content-Profile", "public")
.content(json).accept(MediaType.APPLICATION_JSON))
mockMvc.perform(post("/film/bulk")
.characterEncoding(UTF_8)
.contentType(APPLICATION_JSON)
.accept(APPLICATION_JSON)
.header("Content-Profile", "sakila")
.content(ITestUtil.BULK_CREATE_FILM_BAD_REQUEST))
.andExpect(status().isBadRequest())
//.andDo(print())
// .andDo(print())
.andDo(document("mysql-bulk-create-films-error"));

}

@Test
@DisplayName("Create many directors.")
void createDirector() throws Exception {
var json = """
[
{
"first_name": "Anurag",
"last_name": "Kashyap"
},
{
"first_name": "Rajkumar",
"last_name": "Hirani"
}
]
""";

mockMvc.perform(post("/director/bulk").contentType(MediaType.APPLICATION_JSON).characterEncoding("utf-8")

mockMvc.perform(post("/director/bulk")
.characterEncoding(UTF_8)
.contentType(APPLICATION_JSON)
.accept(APPLICATION_JSON)
.header("Content-Profile", "sakila")
.param("tsid", "director_id")
.param("tsidType", "number")
.header("Content-Profile", "public")
.content(json).accept(MediaType.APPLICATION_JSON))
.content(ITestUtil.BULK_CREATE_DIRECTOR_REQUEST))
.andExpect(status().isCreated())
//.andDo(print())
.andDo(document("mysql-bulk-create-directors"));
Expand All @@ -194,52 +110,32 @@ void createDirector() throws Exception {
@Test
@DisplayName("Create many directors with wrong tsid type.")
void createDirectorWithWrongTsidType() throws Exception {
var json = """
[
{
"first_name": "Anurag",
"last_name": "Kashyap"
},
{
"first_name": "Rajkumar",
"last_name": "Hirani"
}
]
""";

mockMvc.perform(post("/director/bulk").contentType(MediaType.APPLICATION_JSON).characterEncoding("utf-8")

mockMvc.perform(post("/director/bulk")
.characterEncoding(UTF_8)
.contentType(APPLICATION_JSON)
.accept(APPLICATION_JSON)
.param("tsid", "director_id")
.param("tsidType", "string")
.header("Content-Profile", "public")
.content(json).accept(MediaType.APPLICATION_JSON))
.header("Content-Profile", "sakila")
.content(ITestUtil.BULK_CREATE_DIRECTOR_BAD_REQUEST))
.andExpect(status().isBadRequest())
// .andDo(print())
//.andDo(print())
.andDo(document("mysql-bulk-create-directors-with-wrong-tsid-type"));

}

@Test
@DisplayName("Create reviews with default tsid type.")
void createReviewWithDefaultTsidType() throws Exception {
var json = """
[
{
"message": "Very long film.",
"rating": 3,
"film_id" : 1
},
{
"message": "Very exciting film.",
"rating": 4,
"film_id" : 2
}
]
""";

mockMvc.perform(post("/review/bulk").contentType(MediaType.APPLICATION_JSON).characterEncoding("utf-8")

mockMvc.perform(post("/review/bulk")
.characterEncoding(UTF_8)
.contentType(APPLICATION_JSON)
.accept(APPLICATION_JSON)
.header("Content-Profile", "sakila")
.param("tsid", "review_id")
.header("Content-Profile", "public")
.content(json).accept(MediaType.APPLICATION_JSON))
.content(ITestUtil.BULK_CREATE_REVIEW_REQUEST))
.andExpect(status().isCreated())
//.andDo(print())
.andDo(document("mysql-bulk-create-reviews-with-default-tsid-type"));
Expand Down
Loading

0 comments on commit 749f0b8

Please sign in to comment.