-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#35: Simplify working with the query results in the DB tests
* 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
Showing
4 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
balta/src/main/scala/za/co/absa/db/balta/implicits/OptionImplicits.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
balta/src/test/scala/za/co/absa/db/balta/implicits/OptionImplicitsUnitTests.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
|
||
} |