Skip to content

Commit

Permalink
#35: Simplify working with the query results in the DB tests
Browse files Browse the repository at this point in the history
* method `@=` for Option types as a synonym for `contains`
* method 'noMore` as an alias for `hasMore` in `QueryResult` class
* enhanced tests for `QueryResultRowIntegration`
  • Loading branch information
benedeki committed Sep 25, 2024
1 parent 5b334af commit 752ca59
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,9 @@ class QueryResult(resultSet: ResultSet) extends Iterator[QueryResultRow] {
throw new NoSuchElementException("No more rows in the result set")
}
}

/**
* A naturally easily understandable opposite of `hasNext`
*/
def noMore: Boolean = !hasNext
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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.implicits

object OptionImplicits {
implicit class OptionEnhancements[T](val option: Option[T]) extends AnyVal {
/**
* Gets the `option` value or throws the provided exception
*
* @param exception the exception to throw in case the `option` is None
* @return
*/
def getOrThrow(exception: => Throwable): T = {
option.getOrElse(throw exception)
}

/**
* The function is an alias for `contains` method, but shorter and suitable for inflix usage
*
* @param value the value to check if present in the `option`
* @return true if the `option` is defined and contains the provided value, false otherwise
*/
def @=(value: T): Boolean = option.contains(value)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ class QueryResultRowIntegrationTests extends AnyFunSuiteLike with DBTestingConne
assert(result == expecedResult)
}

test("columnCount") {
assert(tableRows.head.columnCount == 14)
}

test("rowNumber") {
assert(tableRows.head.rowNumber == 1)
assert(tableRows.tail.head.rowNumber == 2)
}

test("getLong") {
//first row
assert(tableRows.head.getLong(1).contains(1))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* 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.implicits

import org.scalatest.funsuite.AnyFunSuiteLike
import za.co.absa.db.balta.implicits.OptionImplicits.OptionEnhancements


class OptionImplicitsUnitTests extends AnyFunSuiteLike {
test("getOrThrow returns the value if it is defined") {
val opt = Some(true)
assert(opt.getOrThrow(new Exception("Foo")))
}

test("getOrThrow throws an exception if the value is not defined") {
val opt = None
assertThrows[Exception](opt.getOrThrow(new Exception("Foo")))
}

test("@= returns true if the value is defined and equals the provided value") {
val opt = Some(42)
assert(opt @= 42)
}

test("@= returns false if the value is defined but does not equal the provided value") {
val opt = Some(42)
assert(!(opt @= 43))
}

test("@= returns false if the value is not defined") {
val opt = None
assert(!(opt @= 42))
}

}

0 comments on commit 752ca59

Please sign in to comment.