Skip to content

Commit

Permalink
add test mariadb, fix test mysql
Browse files Browse the repository at this point in the history
  • Loading branch information
ntananh committed Nov 1, 2024
1 parent 584ea29 commit be5e926
Show file tree
Hide file tree
Showing 14 changed files with 201 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
package com.homihq.db2rest.rest.mariadb;

import com.homihq.db2rest.MariaDBBaseIntegrationTest;
import org.junit.jupiter.api.*;

import static com.homihq.db2rest.jdbc.rest.RdbmsRestApi.VERSION;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.core.AnyOf.anyOf;
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.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;

@TestClassOrder(ClassOrderer.OrderAnnotation.class)
@Order(309)
@DisplayName("Parameterized SQL template")
class MariaDBTemplateControllerTest extends MariaDBBaseIntegrationTest {

public final static int ID = 1;

@Test
@DisplayName("Test find all films with sql template")
void findAllFilms() throws Exception {
var tableName = "film";

mockMvc.perform(get(VERSION + "/mariadb/sql/select_all")
.contentType(APPLICATION_JSON).accept(APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.*").isArray())
.andExpect(jsonPath("$.*", anyOf(hasSize(4), hasSize(9), hasSize(8))))
.andExpect(jsonPath("$[0].*", hasSize(14)))
.andDo(document("mariadb-template-get-all-films-all-columns"));
}

@Test
@DisplayName("Find film by id with request param")
void findFilmByID() throws Exception {
mockMvc.perform(get(VERSION + "/mariadb/sql/select_by_id")
.param("film_id", String.valueOf(ID))
.contentType(APPLICATION_JSON).accept(APPLICATION_JSON))
.andExpect(status().isOk())
.andDo(print())
.andExpect(jsonPath("$.*").isArray())
.andExpect(jsonPath("$.*", anyOf(hasSize(1))))
.andDo(document("mariadb-template-get-film-by-id-with-params"));
}

@Test
@DisplayName("Find film by id with headers")
void findFilmByIDWithHeader() throws Exception {
mockMvc.perform(get(VERSION + "/mariadb/sql/select_by_id")
.header("film_id", String.valueOf(ID))
.contentType(APPLICATION_JSON).accept(APPLICATION_JSON))
.andExpect(status().isOk())
.andDo(print())
.andExpect(jsonPath("$.*").isArray())
.andExpect(jsonPath("$.*", anyOf(hasSize(1))))
.andDo(document("mariadb-template-get-film-by-id-with-headers"));
}

@Test
@DisplayName("Find film by id with custom path")
void findFilmByIDWithPath() throws Exception {
mockMvc.perform(get(VERSION + "/mariadb/sql/select_by_id/"+ ID)
.header("paths", "film_id")
.contentType(APPLICATION_JSON).accept(APPLICATION_JSON))
.andExpect(status().isOk())
.andDo(print())
.andExpect(jsonPath("$.*").isArray())
.andExpect(jsonPath("$.*", anyOf(hasSize(1))))
.andDo(document("mariadb-template-get-film-by-id-with-user-path"));
}

@Test
@DisplayName("Failed to find film by id without provided param")
void findFilmByIDWithRequiredConstraint() throws Exception {
mockMvc.perform(get(VERSION + "/mariadb/sql/select_by_id_is_required")
.contentType(APPLICATION_JSON).accept(APPLICATION_JSON))
.andExpect(status().is4xxClientError())
.andDo(document("mariadb-template-get-film-by-id-with-required-constraint"));
}

@Test
@DisplayName("Conditional render: select all when no request params")
void selectWithConditionalRenderNoRequestParams() throws Exception {
mockMvc.perform(get(VERSION + "/mariadb/sql/conditional_render_and_op")
.contentType(APPLICATION_JSON).accept(APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.*").isArray())
.andExpect(jsonPath("$.*", anyOf(hasSize(4), hasSize(9), hasSize(8))))
.andExpect(jsonPath("$[0].*", hasSize(14)))
.andDo(document("mariadb-template-conditional-render-no-request-params"));
}

@Test
@DisplayName("Conditional render: select render one condition")
void selectWithConditionalRenderWithID() throws Exception {
var id = 4;
mockMvc.perform(get(VERSION + "/mariadb/sql/conditional_render_and_op")
.param("film_id", String.valueOf(id))
.contentType(APPLICATION_JSON).accept(APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.*").isArray())
.andExpect(jsonPath("$.*", anyOf(hasSize(1))))
.andExpect(jsonPath("$[0].film_id").value(id))
.andDo(document("mariadb-template-conditional-render-with-id-condition"));
}

@Test
@DisplayName("Conditional render: render both and operations")
void selectWithConditionalRenderWithField() throws Exception {
var id = 4;
var rating = "G";
mockMvc.perform(get(VERSION + "/mariadb/sql/conditional_render_and_op")
.param("film_id", String.valueOf(id))
.param("rating", rating)
.contentType(APPLICATION_JSON).accept(APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.*").isArray())
.andExpect(jsonPath("$.*", anyOf(hasSize(1))))
.andExpect(jsonPath("$[0].film_id").value(id))
.andExpect(jsonPath("$[0].rating").value(rating))
.andDo(document("mariadb-template-conditional-render-render-both-and-operations"));
}


@Test
@DisplayName("Conditional render join: skip render join, select film by id")
void selectWithConditionalRenderJoinWithoutInput() throws Exception {
var film_id = 1;
mockMvc.perform(get(VERSION + "/mariadb/sql/conditional_render_join")
.param("film_id", String.valueOf(film_id))
.contentType(APPLICATION_JSON).accept(APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.*").isArray())
.andExpect(jsonPath("$.*", anyOf(hasSize(1))))
.andExpect(jsonPath("$[0].film_id").value(film_id))
.andDo(document("mariadb-template-conditional-render-join-skip-render"));
}

@Test
@DisplayName("Conditional render join: execute rendered join")
void selectWithConditionalRenderJoin() throws Exception {
var film_id = 1;
var language_id = 1;
mockMvc.perform(get(VERSION + "/mariadb/sql/conditional_render_join")
.param("film_id", String.valueOf(film_id))
.param("language_id", String.valueOf(language_id))
.contentType(APPLICATION_JSON).accept(APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.*").isArray())
.andExpect(jsonPath("$.*", anyOf(hasSize(1))))
.andExpect(jsonPath("$[0].film_id").value(film_id))
.andExpect(jsonPath("$[0].language_name").value("English"))
.andDo(document("mariadb-template-conditional-render-join"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ void findAllFilms() throws Exception {
var tableName = "film";

mockMvc.perform(get(VERSION + "/mysqldb/sql/select_all")
.param("table", tableName)
.contentType(APPLICATION_JSON).accept(APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.*").isArray())
Expand Down
1 change: 1 addition & 0 deletions api-rest/src/test/resources/application-it-mariadb.yaml
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
SQL_TEMPLATE_PATH: classpath:testdata/sql-mariadb

2 changes: 1 addition & 1 deletion api-rest/src/test/resources/application-it-mysql.yaml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
SQL_TEMPLATE_PATH: classpath:testdata/sql
SQL_TEMPLATE_PATH: classpath:testdata/sql-mysql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
SELECT *
FROM sakila.film f
WHERE 1 = 1
{% if params.film_id %}
AND f.film_id = {{ params.film_id }}
{% endif %}
{% if params.rating %}
AND f.rating = {{ params.rating }}
{% endif %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
SELECT
{% if params.film_id and params.language_id %}
f.film_id, f.title, l.name as language_name
{% else %}
*
{% endif %}
FROM sakila.film f

{% if params.film_id and params.language_id %}
JOIN
sakila.language l
ON f.language_id = l.language_id
{% endif %}

{% if params.film_id %}
WHERE f.film_id = {{ params.film_id | is_required }}
{% endif %}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SELECT * FROM sakila.film;
11 changes: 11 additions & 0 deletions api-rest/src/test/resources/testdata/sql-mysql/select_by_id.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
SELECT *
FROM sakila.film f
WHERE 1 = 1
AND f.film_id =
{% if params.film_id %}
{{ params.film_id }}
{% elif paths.film_id %}
{{ paths.film_id }}
{% elif headers.film_id %}
{{ headers.film_id }}
{% endif %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SELECT * FROM sakila.film f
WHERE f.film_id = {{ params.film_id | is_required }}

0 comments on commit be5e926

Please sign in to comment.