diff --git a/balta/src/main/scala/za/co/absa/db/balta/classes/QueryResult.scala b/balta/src/main/scala/za/co/absa/db/balta/classes/QueryResult.scala index aa3a987..f286292 100644 --- a/balta/src/main/scala/za/co/absa/db/balta/classes/QueryResult.scala +++ b/balta/src/main/scala/za/co/absa/db/balta/classes/QueryResult.scala @@ -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 } diff --git a/balta/src/main/scala/za/co/absa/db/balta/implicits/OptionImplicits.scala b/balta/src/main/scala/za/co/absa/db/balta/implicits/OptionImplicits.scala new file mode 100644 index 0000000..9d91e49 --- /dev/null +++ b/balta/src/main/scala/za/co/absa/db/balta/implicits/OptionImplicits.scala @@ -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) + } + +} diff --git a/balta/src/test/scala/za/co/absa/db/balta/classes/QueryResultRowIntegrationTests.scala b/balta/src/test/scala/za/co/absa/db/balta/classes/QueryResultRowIntegrationTests.scala index bace35c..bd82773 100644 --- a/balta/src/test/scala/za/co/absa/db/balta/classes/QueryResultRowIntegrationTests.scala +++ b/balta/src/test/scala/za/co/absa/db/balta/classes/QueryResultRowIntegrationTests.scala @@ -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)) diff --git a/balta/src/test/scala/za/co/absa/db/balta/implicits/OptionImplicitsUnitTests.scala b/balta/src/test/scala/za/co/absa/db/balta/implicits/OptionImplicitsUnitTests.scala new file mode 100644 index 0000000..4ef9065 --- /dev/null +++ b/balta/src/test/scala/za/co/absa/db/balta/implicits/OptionImplicitsUnitTests.scala @@ -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)) + } + +}