Skip to content

Commit

Permalink
Deleting the data that was inserted for tests to bring the db back to…
Browse files Browse the repository at this point in the history
… original state
  • Loading branch information
souravroy committed Feb 5, 2024
1 parent 7c673c0 commit 7f3987c
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ public class MySQLContainerConfiguration {

private static final MySQLContainer mySQLContainer = (MySQLContainer) new MySQLContainer("mysql:8.2")
.withDatabaseName("sakila")
.withUsername("root");
.withUsername("root")
//.withPassword("mysql")
//.withReuse(true);
.withReuse(true);

static {
mySQLContainer.start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public class PostgreSQLContainerConfiguration {
private static final PostgreSQLContainer testPostgres = (PostgreSQLContainer) new PostgreSQLContainer("postgres:15.2-alpine")
.withDatabaseName("postgres")
.withUsername("postgres")
.withPassword("postgres");
//.withReuse(true);
.withPassword("postgres")
.withReuse(true);

static {
testPostgres.start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@

import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.springframework.http.MediaType.APPLICATION_JSON;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.get;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.post;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
Expand Down Expand Up @@ -197,7 +197,7 @@ void create_a_film_with_columns_specified_in_query_param() throws Exception {
.andExpect(jsonPath("$.row", equalTo(1)))
.andExpect(jsonPath("$.generated_key").exists())
.andDo(print())
.andDo(document("pg-create-a-film"))
.andDo(document("mysql-create-a-film"))
.andReturn();

var primary_key = JsonPath.read(result.getResponse().getContentAsString(), "$.generated_key");
Expand All @@ -210,6 +210,14 @@ void create_a_film_with_columns_specified_in_query_param() throws Exception {
.andExpect(jsonPath("$[0].title", equalTo("Dunki")))
.andExpect(jsonPath("$[0].release_year").doesNotExist())
.andDo(print());

mockMvc.perform(delete("/film")
.param("filter", String.format("film_id==%s", primary_key))
.accept(APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.rows", Matchers.equalTo(1)))
.andDo(print())
.andDo(document("mysql-delete-a-film"));
}

@Test
Expand Down Expand Up @@ -239,7 +247,7 @@ void should_ignore_when_columns_query_param_is_empty() throws Exception {
.andExpect(jsonPath("$.row", equalTo(1)))
.andExpect(jsonPath("$.generated_key").exists())
.andDo(print())
.andDo(document("pg-create-a-film"))
.andDo(document("mysql-create-a-film"))
.andReturn();

var primary_key = JsonPath.read(result.getResponse().getContentAsString(), "$.generated_key");
Expand All @@ -252,6 +260,14 @@ void should_ignore_when_columns_query_param_is_empty() throws Exception {
.andExpect(jsonPath("$[0].title", equalTo("Dunki")))
.andExpect(jsonPath("$[0].release_year", equalTo("2023-01-01")))
.andDo(print());

mockMvc.perform(delete("/film")
.param("filter", String.format("film_id==%s", primary_key))
.accept(APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.rows", Matchers.equalTo(1)))
.andDo(print())
.andDo(document("mysql-delete-a-film"));
}

@Test
Expand Down Expand Up @@ -280,7 +296,7 @@ void column_is_present_in_columns_query_param_but_not_in_payload() throws Except
.andExpect(jsonPath("$.row", equalTo(1)))
.andExpect(jsonPath("$.generated_key").exists())
.andDo(print())
.andDo(document("pg-create-a-film"))
.andDo(document("mysql-create-a-film"))
.andReturn();

var primary_key = JsonPath.read(result.getResponse().getContentAsString(), "$.generated_key");
Expand All @@ -293,6 +309,14 @@ void column_is_present_in_columns_query_param_but_not_in_payload() throws Except
.andExpect(jsonPath("$[0].title", equalTo("Dunki")))
.andExpect(jsonPath("$[0].description").doesNotExist())
.andDo(print());

mockMvc.perform(delete("/film")
.param("filter", String.format("film_id==%s", primary_key))
.accept(APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.rows", Matchers.equalTo(1)))
.andDo(print())
.andDo(document("mysql-delete-a-film"));
}

@Test
Expand Down Expand Up @@ -322,6 +346,6 @@ void column_violates_not_null_constraint() throws Exception {
.andExpect(jsonPath("$.detail",
containsString("Field 'language_id' doesn't have a default value")))
.andDo(print())
.andDo(document("pg-create-a-film"));
.andDo(document("mysql-create-a-film"));
}
}
34 changes: 30 additions & 4 deletions src/test/java/com/homihq/db2rest/rest/PgCreateControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@

import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.springframework.http.MediaType.APPLICATION_JSON;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.get;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.post;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
Expand Down Expand Up @@ -191,7 +191,8 @@ void create_a_film_with_columns_specified_in_query_param() throws Exception {
}
""";

var result = mockMvc.perform(post("/film").contentType(MediaType.APPLICATION_JSON).characterEncoding("utf-8")
var result = mockMvc.perform(post("/film")
.contentType(MediaType.APPLICATION_JSON).characterEncoding("utf-8")
.header("Content-Profile", "public")
.queryParam("columns", "title,description,language_id")
.content(json).accept(MediaType.APPLICATION_JSON))
Expand All @@ -212,6 +213,14 @@ void create_a_film_with_columns_specified_in_query_param() throws Exception {
.andExpect(jsonPath("$[0].title", equalTo("Dunki")))
.andExpect(jsonPath("$[0].release_year").doesNotExist())
.andDo(print());

mockMvc.perform(delete("/film")
.param("filter", String.format("film_id==%s", primary_key))
.accept(APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.rows", Matchers.equalTo(1)))
.andDo(print())
.andDo(document("pg-delete-a-film"));
}

@Test
Expand All @@ -233,7 +242,8 @@ void should_ignore_when_columns_query_param_is_empty() throws Exception {
}
""";

var result = mockMvc.perform(post("/film").contentType(MediaType.APPLICATION_JSON).characterEncoding("utf-8")
var result = mockMvc.perform(post("/film")
.contentType(MediaType.APPLICATION_JSON).characterEncoding("utf-8")
.header("Content-Profile", "public")
.queryParam("columns", "")
.content(json).accept(MediaType.APPLICATION_JSON))
Expand All @@ -254,6 +264,14 @@ void should_ignore_when_columns_query_param_is_empty() throws Exception {
.andExpect(jsonPath("$[0].title", equalTo("Dunki")))
.andExpect(jsonPath("$[0].release_year", equalTo(2023)))
.andDo(print());

mockMvc.perform(delete("/film")
.param("filter", String.format("film_id==%s", primary_key))
.accept(APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.rows", Matchers.equalTo(1)))
.andDo(print())
.andDo(document("pg-delete-a-film"));
}

@Test
Expand Down Expand Up @@ -295,6 +313,14 @@ void column_is_present_in_columns_query_param_but_not_in_payload() throws Except
.andExpect(jsonPath("$[0].title", equalTo("Dunki")))
.andExpect(jsonPath("$[0].description").doesNotExist())
.andDo(print());

mockMvc.perform(delete("/film")
.param("filter", String.format("film_id==%s", primary_key))
.accept(APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.rows", Matchers.equalTo(1)))
.andDo(print())
.andDo(document("pg-delete-a-film"));
}

@Test
Expand Down

0 comments on commit 7f3987c

Please sign in to comment.