Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#4: QueryResultRow does not instantiate the row data #29

Merged
Merged
31 changes: 30 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,42 @@ jobs:

name: Scala ${{matrix.scala}}

services:
postgres:
image: postgres:15
env:
POSTGRES_PASSWORD: postgres
POSTGRES_DB: mag_db
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 5432:5432

steps:
- name: Checkout code
uses: actions/checkout@v4

- uses: coursier/cache-action@v5

- name: Setup Scala
uses: olafurpg/setup-scala@v10
with:
java-version: "adopt@1.8"
- name: Build and run tests

- name: Build and run unit tests
run: sbt ++${{matrix.scala}} test doc

- name: Setup database
run: |
psql postgresql://postgres:postgres@localhost:5432/mag_db -f balta/src/test/resources/db/postgres/02_users.ddl
psql postgresql://postgres:postgres@localhost:5432/mag_db -f balta/src/test/resources/db/postgres/03_schema_testing.ddl
psql postgresql://postgres:postgres@localhost:5432/mag_db -f balta/src/test/resources/db/postgres/04_testing.base_types.ddl
psql postgresql://postgres:postgres@localhost:5432/mag_db -f balta/src/test/resources/db/postgres/05_testing._base_types_data.sql
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
- name: Build and run integration tests
run: sbt ++${{matrix.scala}} testIT
41 changes: 41 additions & 0 deletions .github/workflows/test_filenames_check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#
# 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.
#

name: Test Filenames Check

on:
pull_request:
branches: [ master ]
types: [ opened, synchronize, reopened ]

jobs:
test_filenames_check:
name: Test Filenames Check
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Filename Inspector
id: scan-test-files
uses: AbsaOSS/filename-inspector@v0.1.0
with:
name-patterns: '*UnitTests.*,*IntegrationTests.*'
paths: '**/src/test/scala/**'
report-format: 'console'
excludes: 'balta/src/test/scala/za/co/absa/db/balta/testing/*'
verbose-logging: 'false'
fail-on-violation: 'true'
27 changes: 27 additions & 0 deletions .sbtrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#
# 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.
#

# Aliases in this file expected usage of test file naming conventions:
# - "UnitTests" suffix for test files and Suites which define unit tests
# - "IntegrationTests" suffix for test files and Suites which define integration tests

# CPS QA types aliases
# * Unit tests
alias test=; testOnly *UnitTests

# * Integration tests
alias testIT=; testOnly *IntegrationTests

17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,23 @@ It's a natural complement to the use of [Fa-Db library](https://github.com/AbsaO
Advantages of this approach is that the tests repeateble, they are isolated from each other and the database is always
in a known state before and after each test.

## How to Test
There are integration tests part of the package that can be run with the following command:

```bash
sbt testIT
```

The tests to finish successfully, a Postgres database must be running and populated.
* by default the database is expected to be running on `localhost:5432`
* if you wish to run against a different server modify the `src/test/resources/database.properties` file
* to populate the database run the scripts in the `src/test/resources/db/postgres` folder

## How to Release

Please see [this file](RELEASE.md) for more details.

miroslavpojer marked this conversation as resolved.
Show resolved Hide resolved
## Known Issues

### Postgres
* `TIMESTAMP WITH TIME ZONE[]`, `TIME WITH TIME ZONE[]`, generally arrays of time related types are not translated to appropriate time zone aware Scala/Java types
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ abstract class DBTestSuite extends AnyFunSuite {
}

// private functions
private def readConnectionInfoFromConfig = {
private def readConnectionInfoFromConfig: ConnectionInfo = {
val properties = new Properties()
properties.load(getClass.getResourceAsStream("/database.properties"))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,44 @@

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

import java.sql.ResultSet
import za.co.absa.db.balta.classes.QueryResultRow.{Extractors, FieldNames}

import java.sql.{ResultSet, ResultSetMetaData, SQLException}

/**
* This is an iterator over the result of a query.
*
* @param resultSet - the JDBC result of a query
*/
class QueryResult(resultSet: ResultSet) extends Iterator[QueryResultRow] {
private [this] var resultSetHasNext: Option[Boolean] = Some(resultSet.next())
val resultSetMetaData: ResultSetMetaData = resultSet.getMetaData
val columnCount: Int = resultSetMetaData.getColumnCount

private [this] var nextRow: Option[QueryResultRow] = None

private [this] implicit val fieldNames: FieldNames = QueryResultRow.fieldNamesFromMetadata(resultSetMetaData)
private [this] implicit val extractors: Extractors = QueryResultRow.createExtractors(resultSetMetaData)

override def hasNext: Boolean = {
resultSetHasNext.getOrElse {
val result = resultSet.next()
resultSetHasNext = Some(result)
result
if (nextRow.isEmpty) {
try {
if (resultSet.next()) {
nextRow = Some(QueryResultRow(resultSet))
}
} catch {
case _: SQLException => // Do nothing
}
}
nextRow.nonEmpty
}

override def next(): QueryResultRow = {
if (resultSetHasNext.isEmpty) {
resultSet.next()
new QueryResultRow(resultSet)
if (hasNext) {
val row = nextRow.get
nextRow = None
row
} else {
resultSetHasNext = None
new QueryResultRow(resultSet)

throw new NoSuchElementException("No more rows in the result set")
}
}
}
Loading
Loading