Skip to content

Commit

Permalink
#135 - mysql bulk insert, minor other fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
grabdoc committed Jan 13, 2024
1 parent 1faeb8f commit d8fe0db
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 63 deletions.
31 changes: 2 additions & 29 deletions src/test/java/com/homihq/db2rest/MySQLBaseIntegrationTest.java
Original file line number Diff line number Diff line change
@@ -1,35 +1,8 @@
package com.homihq.db2rest;

import org.springframework.test.context.jdbc.Sql;
import org.springframework.context.annotation.Import;

import static org.assertj.core.api.Assertions.assertThat;

@Sql(scripts = {"classpath:mysql/mysql-sakila.sql", "classpath:mysql/mysql-sakila-insert-data.sql"})
@Import(MySQLContainerConfiguration.class)
public class MySQLBaseIntegrationTest extends BaseIntegrationTest{
/*
@ServiceConnection
private static final MySQLContainer mySQLContainer = new MySQLContainer("mysql:8.2");
static {
mySQLContainer
.withDatabaseName("sakila")
.withUsername("mysql")
.withPassword("mysql")
.withUrlParam("serverTimezone", "UTC");
}
@BeforeAll
static void beforeAll() {
mySQLContainer.start();
}
@AfterAll
static void afterAll() {
mySQLContainer.stop();
}

*/
}
Original file line number Diff line number Diff line change
@@ -1,37 +1,8 @@
package com.homihq.db2rest;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.jdbc.Sql;
import org.testcontainers.containers.MySQLContainer;
import org.testcontainers.containers.PostgreSQLContainer;

//@Sql(scripts = {"classpath:pg/postgres-sakila-schema.sql", "classpath:pg/postgres-sakila-insert-data.sql"})
@Import(PostgreSQLContainerConfiguration.class)
public class PostgreSQLBaseIntegrationTest extends BaseIntegrationTest{

/*
@ServiceConnection
private static final PostgreSQLContainer postgreSQLContainer = new PostgreSQLContainer("postgres:15-alpine");
static {
postgreSQLContainer.withUsername("postgres")
.withPassword("postgres")
.withDatabaseName("postgres").withReuse(true);
}
@BeforeAll
static void beforeAll() {
postgreSQLContainer.start();
}
@AfterAll
static void afterAll() {
postgreSQLContainer.stop();
}
*/
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package com.homihq.db2rest.rest;

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

import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

class MySQLBulkCreateControllerTest extends MySQLBaseIntegrationTest {

@Test
@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))
.andExpect(status().isCreated())
//.andExpect(jsonPath("$.rows", Matchers.equalTo(1)))
.andDo(print())
.andDo(document("pg-bulk-create-films"));

}


@Test
@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))
.andExpect(status().isBadRequest())
.andDo(print())
.andDo(document("pg-bulk-create-films-error"));

}


}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.homihq.db2rest.rest;

import com.homihq.db2rest.PostgreSQLBaseIntegrationTest;
import com.homihq.db2rest.MySQLBaseIntegrationTest;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand All @@ -12,7 +12,7 @@
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

class MySQLCreateControllerTest extends PostgreSQLBaseIntegrationTest {
class MySQLCreateControllerTest extends MySQLBaseIntegrationTest {

@Test
@DisplayName("Create a film.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

class MySQLReadControllerTest extends MySQLBaseIntegrationTest {
//@Test
@Test
@DisplayName("Get all fields.")
void findAllFilms() throws Exception {

mockMvc.perform(get("/films").header("Accept-Profile", "sakila")
mockMvc.perform(get("/film").header("Accept-Profile", "sakila")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isBadRequest())
.andExpect(status().isOk())
.andDo(print())
.andDo(document("mysql-get-all-films"));
}
Expand Down
30 changes: 30 additions & 0 deletions src/test/java/com/homihq/db2rest/rest/PgCreateControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,34 @@ void createError() throws Exception {
.andDo(document("pg-create-a-film-error"));

}

@Test
@DisplayName("Create a film - non existent table.")
void createNonExistentTable() throws Exception {

var json = """
{
"title" : "Dunki",
"description" : "Film about illegal immigration" ,
"release_year" : 2023,
"language_id" : 1,
"original_language_id" : null,
"rental_duration" : 6,
"rental_rate" : 0.99 ,
"length" : 150,
"replacement_cost" : 20.99 ,
"rating" : "PG-13" ,
"special_features" : "Commentaries"
}
""";

mockMvc.perform(post("/films").contentType(MediaType.APPLICATION_JSON).characterEncoding("utf-8")
.header("Content-Profile", "public")
.content(json).accept(MediaType.APPLICATION_JSON))
.andExpect(status().isBadRequest())
.andDo(print())
.andDo(document("pg-create-a-film-no-table"));

}
}

0 comments on commit d8fe0db

Please sign in to comment.