Skip to content

Commit

Permalink
#26: Move code into sub package db (#28)
Browse files Browse the repository at this point in the history
* Fixed badge link in README.md
* Update README.md

---------

Co-authored-by: Ladislav Sulak <laco.sulak@gmail.com>
Co-authored-by: miroslavpojer <miroslav.pojer@absa.africa>
  • Loading branch information
3 people authored Aug 1, 2024
1 parent 4acf802 commit 7971646
Show file tree
Hide file tree
Showing 18 changed files with 124 additions and 86 deletions.
15 changes: 12 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
# Balta

Scala library to write Postgres DB code tests with
Balta makes testing DB entities from Scala easier. It's primarily focused on testing the behavior of [Postgres functions](https://www.postgresql.org/docs/current/xfunc.html).

---

### Build Status

[![Build](https://github.com/AbsaOSS/balta/workflows/Build/badge.svg)](https://github.com/AbsaOSS/balta/actions)

[![Maven Central](https://maven-badges.herokuapp.com/maven-central/za.co.absa.db/balta_2.12/badge.svg)](https://search.maven.org/search?q=g:za.co.absa.db.balta)

---

Balta is a Scala library to help creating database tests, particularly testing Database functions. It is based on the
popular [ScalaTest](http://www.scalatest.org/) library and uses [PostgreSQL](https://www.postgresql.org/) as the
database engine.
popular [ScalaTest](http://www.scalatest.org/) library and uses [PostgreSQL](https://www.postgresql.org/) as the database engine.

It's a natural complement to the use of [Fa-Db library](https://github.com/AbsaOSS/fa-db) in applications.

Expand Down
42 changes: 0 additions & 42 deletions balta/src/main/scala/za/co/absa/balta/classes/package.scala

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@
* limitations under the License.
*/

package za.co.absa.balta
package za.co.absa.db.balta

import org.scalactic.source
import org.scalatest.Tag
import org.scalatest.funsuite.AnyFunSuite
import za.co.absa.balta.classes.DBFunction.DBFunctionWithPositionedParamsOnly
import za.co.absa.balta.classes.setter.{AllowedParamTypes, Params}
import za.co.absa.balta.classes.setter.Params.{NamedParams, OrderedParams}
import za.co.absa.balta.classes.{ConnectionInfo, DBConnection, DBFunction, DBTable, QueryResult}
import za.co.absa.db.balta.classes.DBFunction.DBFunctionWithPositionedParamsOnly
import classes.setter.{AllowedParamTypes, Params}
import za.co.absa.db.balta.classes.setter.Params.{NamedParams, OrderedParams}
import classes.{DBConnection, DBFunction, DBTable, QueryResult}
import za.co.absa.db.balta.classes.simple.ConnectionInfo

import java.sql.DriverManager
import java.time.OffsetDateTime
import java.util.Properties

Expand All @@ -39,13 +39,7 @@ abstract class DBTestSuite extends AnyFunSuite {

/* the DB connection is ``lazy`, so it actually can be created only when needed and therefore the credentials
overridden in the successor */
protected lazy implicit val dbConnection: DBConnection = {
createConnection(
connectionInfo.dbUrl,
connectionInfo.username,
connectionInfo.password
)
}
protected lazy implicit val dbConnection: DBConnection = DBConnection(connectionInfo)

/**
* This is the connection info for the DB. It can be overridden in the derived classes to provide specific credentials
Expand Down Expand Up @@ -152,12 +146,6 @@ abstract class DBTestSuite extends AnyFunSuite {
}

// private functions
private def createConnection(url: String, username: String, password: String): DBConnection = {
val conn = DriverManager.getConnection(url, username, password)
conn.setAutoCommit(false)
new DBConnection(conn)
}

private def readConnectionInfoFromConfig = {
val properties = new Properties()
properties.load(getClass.getResourceAsStream("/database.properties"))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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.
*/

package za.co.absa.db.balta.classes

import za.co.absa.db.balta.classes.simple.ConnectionInfo

import java.sql.{Connection, DriverManager}

class DBConnection(val connection: Connection) extends AnyVal

object DBConnection {
def apply(connectionInfo: =>ConnectionInfo): DBConnection = {
val connection = DriverManager.getConnection(connectionInfo.dbUrl, connectionInfo.username, connectionInfo.password)
connection.setAutoCommit(false)
new DBConnection(connection)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
* limitations under the License.
*/

package za.co.absa.balta.classes
package za.co.absa.db.balta.classes

import za.co.absa.balta.classes.DBFunction.{DBFunctionWithNamedParamsToo, DBFunctionWithPositionedParamsOnly, ParamsMap}
import za.co.absa.balta.classes.setter.{AllowedParamTypes, SetterFnc}
import DBFunction.{DBFunctionWithNamedParamsToo, DBFunctionWithPositionedParamsOnly, ParamsMap}
import za.co.absa.db.balta.classes.setter.{AllowedParamTypes, SetterFnc}

import scala.collection.immutable.ListMap

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
* limitations under the License.
*/

package za.co.absa.balta.classes
package za.co.absa.db.balta.classes

import za.co.absa.balta.classes.setter.SetterFnc
import za.co.absa.db.balta.classes.setter.SetterFnc

/**
* This is a based trait providing the ability to run an SQL query and verify the result via a provided function.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
* limitations under the License.
*/

package za.co.absa.balta.classes
package za.co.absa.db.balta.classes

import za.co.absa.balta.classes.setter.{AllowedParamTypes, Params, SetterFnc}
import za.co.absa.balta.classes.setter.Params.NamedParams
import za.co.absa.db.balta.classes.setter.{AllowedParamTypes, Params, SetterFnc}
import za.co.absa.db.balta.classes.setter.Params.NamedParams

/**
* This class represents a database table. It allows to perform INSERT, SELECT and COUNT operations on the table easily.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package za.co.absa.balta.classes
package za.co.absa.db.balta.classes

import java.sql.ResultSet

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
* limitations under the License.
*/

package za.co.absa.balta.classes
package za.co.absa.db.balta.classes

import org.postgresql.util.PGobject
import za.co.absa.db.balta.classes.simple.JsonBString

import java.sql.{Date, ResultSet, Time}
import java.time.{Instant, OffsetDateTime}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
* limitations under the License.
*/

package za.co.absa.balta.classes.setter
package za.co.absa.db.balta.classes.setter

import za.co.absa.balta.classes.JsonBString
import za.co.absa.db.balta.classes.simple.JsonBString

import java.time.{Instant, OffsetDateTime, LocalTime, LocalDate}
import java.time.{Instant, LocalDate, LocalTime, OffsetDateTime}
import java.util.UUID

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package za.co.absa.balta.classes.setter
package za.co.absa.db.balta.classes.setter

/**
* This is a case class representing a custom DB type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package za.co.absa.balta.classes.setter
package za.co.absa.db.balta.classes.setter

import scala.collection.immutable.ListMap

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@
* limitations under the License.
*/

package za.co.absa.balta.classes.setter

import za.co.absa.balta.classes.JsonBString
package za.co.absa.db.balta.classes.setter

import java.sql.{Date, PreparedStatement, Time, Timestamp, Types => SqlTypes}
import java.util.UUID
import org.postgresql.util.PGobject
import za.co.absa.db.balta.classes.simple.JsonBString

import java.time.{Instant, LocalDate, LocalTime, OffsetDateTime, ZoneId, ZoneOffset}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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.
*/

package za.co.absa.db.balta.classes.simple

/**
* This is a function that sets a parameter of a prepared statement.
*
* @param dbUrl - the JDBC URL of the database
* @param username - the username to use when connecting to the database
* @param password - the password to use when connecting to the database
* @param persistData - whether to persist the data to the database (usually false for tests, set to true for
* debugging purposes)
*/
case class ConnectionInfo(
dbUrl: String,
username: String,
password: String,
persistData: Boolean
)
Original file line number Diff line number Diff line change
@@ -0,0 +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.
*/

package za.co.absa.db.balta.classes.simple

case class JsonBString(value: String) extends AnyVal
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
* limitations under the License.
*/

package za.co.absa.balta
package za.co.absa.db.balta

import za.co.absa.balta.classes.DBConnection
import za.co.absa.db.balta.classes.DBConnection

import java.sql.Connection
import scala.language.implicitConversions
Expand Down
2 changes: 0 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

import Dependencies._

ThisBuild / organization := "za.co.absa"

lazy val scala211 = "2.11.12"
lazy val scala212 = "2.12.18"
lazy val scala213 = "2.13.11"
Expand Down
2 changes: 2 additions & 0 deletions publish.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ ThisBuild / developers := List(
)
)

ThisBuild / organization := "za.co.absa.db"

ThisBuild / organizationName := "ABSA Group Limited"
ThisBuild / organizationHomepage := Some(url("https://www.absa.africa"))

Expand Down

0 comments on commit 7971646

Please sign in to comment.