Skip to content

Commit

Permalink
Merge branch 'master' into feaature/43-dbtestsuite-parametrize-persis…
Browse files Browse the repository at this point in the history
…t_data-into-constructor
  • Loading branch information
benedeki authored Oct 22, 2024
2 parents e492ac6 + 9d216c2 commit 08c38e5
Show file tree
Hide file tree
Showing 15 changed files with 402 additions and 9 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ jobs:
psql postgresql://postgres:postgres@localhost:5432/mag_db -f balta/src/test/resources/db/postgres/06_testing.pg_types.ddl
psql postgresql://postgres:postgres@localhost:5432/mag_db -f balta/src/test/resources/db/postgres/07_testing_pg_types_data.sql
psql postgresql://postgres:postgres@localhost:5432/mag_db -f balta/src/test/resources/db/postgres/08_testing.simple_function.sql
psql postgresql://postgres:postgres@localhost:5432/mag_db -f balta/src/test/resources/db/postgres/09_testing.table_lifecycle.ddl
- name: Build and run integration tests
run: sbt ++${{matrix.scala}} testIT
1 change: 1 addition & 0 deletions .github/workflows/jacoco_report.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ jobs:
psql postgresql://postgres:postgres@localhost:5432/mag_db -f balta/src/test/resources/db/postgres/06_testing.pg_types.ddl
psql postgresql://postgres:postgres@localhost:5432/mag_db -f balta/src/test/resources/db/postgres/07_testing_pg_types_data.sql
psql postgresql://postgres:postgres@localhost:5432/mag_db -f balta/src/test/resources/db/postgres/08_testing.simple_function.sql
psql postgresql://postgres:postgres@localhost:5432/mag_db -f balta/src/test/resources/db/postgres/09_testing.table_lifecycle.ddl
- name: Build and run tests
continue-on-error: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ trait DBQuerySupport {

setters.foldLeft(1) { case (parameterIndex, fnc) =>
fnc(preparedStatement, parameterIndex)
parameterIndex + 1
if (fnc.setsToNull) parameterIndex //null values is entered as constant, not as parameter
else parameterIndex + 1
}

val result = preparedStatement.executeQuery()
Expand Down
39 changes: 36 additions & 3 deletions balta/src/main/scala/za/co/absa/db/balta/classes/DBTable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,25 @@ case class DBTable(tableName: String) extends DBQuerySupport{
composeSelectAndRun(None, strToOption(orderBy))(verify)
}

def deleteWithCheck[R](verify: QueryResult => R)(implicit connection: DBConnection): R = {
composeDeleteAndRun(None)(verify)
}

def deleteWithCheck[R](whereParams: NamedParams)(verify: QueryResult => R)(implicit connection: DBConnection): R = {
composeDeleteAndRun(strToOption(paramsToWhereCondition(whereParams)), whereParams.setters)(verify)
}

def deleteWithCheck[R](whereCondition: String)(verify: QueryResult => R)(implicit connection: DBConnection): R = {
composeDeleteAndRun(strToOption(whereCondition))(verify)
}

def delete(whereParams: NamedParams)(implicit connection: DBConnection): Unit = {
composeDeleteAndRun(strToOption(paramsToWhereCondition(whereParams)), whereParams.setters)(_ => ())
}

def delete(whereCondition: String = "")(implicit connection: DBConnection): Unit = {
composeDeleteAndRun(strToOption(whereCondition))(_ => ())
}
/**
* Counts the rows in the table.
* @param connection - a database connection used for the SELECT operation.
Expand Down Expand Up @@ -201,8 +220,16 @@ case class DBTable(tableName: String) extends DBQuerySupport{
runQuery(sql, setters)(verify)
}

private def composeCountAndRun(whereCondition: Option[String], setters: List[SetterFnc] = List.empty)
(implicit connection: DBConnection): Long = {
private def composeDeleteAndRun[R](whereCondition: Option[String], setters: List[SetterFnc] = List.empty)
(verify: QueryResult => R)
(implicit connection: DBConnection): R = {
val where = whereCondition.map("WHERE " + _).getOrElse("")
val sql = s"DELETE FROM $tableName $where RETURNING *;"
runQuery(sql, setters)(verify)
}

private def composeCountAndRun[R](whereCondition: Option[String], setters: List[SetterFnc] = List.empty)
(implicit connection: DBConnection): Long = {
val where = whereCondition.map("WHERE " + _).getOrElse("")
val sql = s"SELECT count(1) AS cnt FROM $tableName $where;"
runQuery(sql, setters) {resultSet =>
Expand All @@ -220,7 +247,13 @@ case class DBTable(tableName: String) extends DBQuerySupport{

private def paramsToWhereCondition(params: NamedParams): String = {
params.pairs.foldRight(List.empty[String]) {case ((fieldName, setterFnc), acc) =>
s"$fieldName = ${setterFnc.sqlEntry}" :: acc // TODO https://github.com/AbsaOSS/balta/issues/2
// TODO https://github.com/AbsaOSS/balta/issues/2
val condition = if (setterFnc.setsToNull) {
s"$fieldName IS ${setterFnc.sqlEntry}"
} else {
s"$fieldName = ${setterFnc.sqlEntry}"
}
condition :: acc
}.mkString(" AND ")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import java.time.{Instant, LocalDate, LocalTime, OffsetDateTime, ZoneOffset}
*/
abstract class SetterFnc extends ((PreparedStatement, Int) => Unit) {
def sqlEntry: String = "?"
def setsToNull: Boolean = false
}

object SetterFnc {
Expand Down Expand Up @@ -60,7 +61,12 @@ object SetterFnc {
}
}

val nullSetterFnc: SetterFnc = simple((prep: PreparedStatement, position: Int) => prep.setNull(position, SqlTypes.NULL))
val nullSetterFnc: SetterFnc = new SetterFnc {
override def apply(v1: PreparedStatement, v2: Int): Unit = {}
override def sqlEntry: String = "NULL"
override def setsToNull: Boolean = true
}
//simple((prep: PreparedStatement, position: Int) => prep.setNull(position, SqlTypes.NULL))

private [this] def simple(body: (PreparedStatement, Int) => Unit): SetterFnc = {
new SetterFnc {
Expand Down
18 changes: 17 additions & 1 deletion balta/src/test/resources/db/postgres/01_db.ddl
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
CREATE DATABASE mag_db
/*
* Copyright 2023 ABSA Group Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

CREATE DATABASE mag_db
WITH
OWNER = postgres
ENCODING = 'UTF8'
Expand Down
16 changes: 16 additions & 0 deletions balta/src/test/resources/db/postgres/02_users.ddl
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright 2023 ABSA Group Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

CREATE ROLE mag_owner WITH
LOGIN
NOSUPERUSER
Expand Down
18 changes: 17 additions & 1 deletion balta/src/test/resources/db/postgres/03_schema_testing.ddl
Original file line number Diff line number Diff line change
@@ -1,2 +1,18 @@
CREATE SCHEMA IF NOT EXISTS testing
/*
* Copyright 2023 ABSA Group Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

CREATE SCHEMA IF NOT EXISTS testing
AUTHORIZATION mag_owner;
16 changes: 16 additions & 0 deletions balta/src/test/resources/db/postgres/04_testing.base_types.ddl
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright 2023 ABSA Group Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

DROP TABLE IF EXISTS testing.base_types;

CREATE TABLE IF NOT EXISTS testing.base_types
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright 2023 ABSA Group Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

TRUNCATE testing.base_types;

INSERT INTO testing.base_types(
Expand Down
18 changes: 18 additions & 0 deletions balta/src/test/resources/db/postgres/06_testing.pg_types.ddl
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
/*
* Copyright 2023 ABSA Group Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

DROP TABLE IF EXISTS testing.pg_types;

CREATE TABLE testing.pg_types
(
id bigint NOT NULL,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
TRUNCATE testing.pg_types;
/*
* Copyright 2023 ABSA Group Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

TRUNCATE testing.pg_types;

INSERT INTO testing.pg_types(
id, json_type, jsonb_type, array_of_json_type)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2023 ABSA Group Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

DROP TABLE IF EXISTS testing.table_lifecycle;

CREATE TABLE testing.table_lifecycle
(
id_field bigint NOT NULL,
text_field text,
boolean_field boolean,
created_at timestamp with time zone NOT NULL DEFAULT now(),
PRIMARY KEY (id_field)
);

ALTER TABLE IF EXISTS testing.table_lifecycle
OWNER to mag_owner;
Loading

0 comments on commit 08c38e5

Please sign in to comment.