|
| 1 | +/* |
| 2 | + * Copyright 2022 ABSA Group Limited |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package za.co.absa.fadb.slick |
| 18 | + |
| 19 | +import za.co.absa.fadb.naming.implementations.SnakeCaseNaming.Implicits._ |
| 20 | +import za.co.absa.fadb.slick.FaDbPostgresProfile.api._ |
| 21 | +import org.scalatest.funsuite.AnyFunSuite |
| 22 | +import slick.jdbc.{GetResult, SQLActionBuilder} |
| 23 | +import za.co.absa.fadb.DBFunction.DBSingleResultFunction |
| 24 | +import za.co.absa.fadb.DBSchema |
| 25 | +import com.github.tminglei.slickpg.{InetString, LTree, MacAddrString, Range} |
| 26 | + |
| 27 | +import java.time.{Duration, LocalDate, LocalDateTime, LocalTime, OffsetDateTime, ZonedDateTime} |
| 28 | +import java.util.UUID |
| 29 | +import java.util.concurrent.TimeUnit |
| 30 | +import scala.concurrent.Await |
| 31 | +import scala.concurrent.duration.{FiniteDuration, Duration => ScalaDuration} |
| 32 | +import scala.language.implicitConversions |
| 33 | +import scala.concurrent.ExecutionContext.Implicits.global |
| 34 | + |
| 35 | +class FaDbPostgresProfileSuite extends AnyFunSuite { |
| 36 | + |
| 37 | + implicit def javaDurationToScalaDuration(duration: Duration): ScalaDuration = { |
| 38 | + FiniteDuration(duration.toNanos, TimeUnit.NANOSECONDS) |
| 39 | + } |
| 40 | + private val database = Database.forConfig("postgrestestdb") |
| 41 | + private val testDBEngine: SlickPgEngine = new SlickPgEngine(database) |
| 42 | + |
| 43 | + |
| 44 | + test("Test query types support with actual values") { |
| 45 | + |
| 46 | + case class InputOutput( |
| 47 | + uuid1: UUID, //uuid |
| 48 | + dateTime1: LocalDate, //date |
| 49 | + dateTime2: LocalTime, //time |
| 50 | + dateTime3: LocalDateTime, //timestamp |
| 51 | + dateTime4: Duration, //interval |
| 52 | + dateTime5: ZonedDateTime, //timestamptz |
| 53 | + dateTime6: OffsetDateTime, //timestamptz |
| 54 | + range1: Range[Int], //range |
| 55 | + lTree1: LTree, //ltree |
| 56 | + map1: Map[String, String],//hstore |
| 57 | + inet1: InetString, //inet |
| 58 | + macaddr1: MacAddrString //macaddr |
| 59 | + ) |
| 60 | + |
| 61 | + class TestFunction(implicit override val schema: DBSchema, override val dbEngine: SlickPgEngine) |
| 62 | + extends DBSingleResultFunction[InputOutput, InputOutput, SlickPgEngine] |
| 63 | + with SlickFunction[InputOutput, InputOutput] { |
| 64 | + |
| 65 | + override protected def sql(values: InputOutput): SQLActionBuilder = { |
| 66 | + sql"""SELECT #$selectEntry |
| 67 | + FROM #$functionName( |
| 68 | + ${values.uuid1}, |
| 69 | + ${values.dateTime1}, |
| 70 | + ${values.dateTime2}, |
| 71 | + ${values.dateTime3}, |
| 72 | + ${values.dateTime4}, |
| 73 | + ${values.dateTime5}, |
| 74 | + ${values.dateTime6}, |
| 75 | + ${values.range1}, |
| 76 | + ${values.lTree1}, |
| 77 | + ${values.map1}, |
| 78 | + ${values.inet1}, |
| 79 | + ${values.macaddr1} |
| 80 | + ) #$alias;""" |
| 81 | + } |
| 82 | + |
| 83 | + override protected def slickConverter: GetResult[InputOutput] = GetResult{r => InputOutput( |
| 84 | + r.<<, |
| 85 | + r.<<, |
| 86 | + r.<<, |
| 87 | + r.<<, |
| 88 | + r.<<, |
| 89 | + r.<<, |
| 90 | + r.<<, |
| 91 | + r.<<, |
| 92 | + r.<<, |
| 93 | + r.<<, |
| 94 | + r.<<, |
| 95 | + r.<< |
| 96 | + )} |
| 97 | + } |
| 98 | + |
| 99 | + class TestSchema (implicit dBEngine: SlickPgEngine) extends DBSchema("public"){ |
| 100 | + |
| 101 | + val testFunction = new TestFunction |
| 102 | + } |
| 103 | + |
| 104 | + |
| 105 | + val input = InputOutput( |
| 106 | + UUID.randomUUID(), |
| 107 | + LocalDate.now(), |
| 108 | + LocalTime.now(), |
| 109 | + LocalDateTime.now(), |
| 110 | + Duration.ofMinutes(42), |
| 111 | + ZonedDateTime.now(), |
| 112 | + OffsetDateTime.now(), |
| 113 | + range1 = Range(7, 13), |
| 114 | + LTree(List("This", "is", "an", "LTree")), |
| 115 | + Map("a" -> "Hello", "bb" -> "beautiful", "ccc" -> "world"), |
| 116 | + InetString("168.0.0.1"), |
| 117 | + MacAddrString("12:34:56:78:90:ab") |
| 118 | + ) |
| 119 | + // because postgres does not fully support time zone as Java, so we need to clear it for later successful assert |
| 120 | + val expected = input.copy(dateTime5 = input.dateTime5.toOffsetDateTime.toZonedDateTime) |
| 121 | + |
| 122 | + |
| 123 | + val timeout = Duration.ofMinutes(1) |
| 124 | + val result = Await.result(new TestSchema()(testDBEngine).testFunction(input), timeout) |
| 125 | + assert(result == expected) |
| 126 | + } |
| 127 | + |
| 128 | + test("Test query types support with NULL values") { |
| 129 | + |
| 130 | + case class InputOutput( |
| 131 | + uuid1: Option[UUID], //uuid |
| 132 | + dateTime1: Option[LocalDate], //date |
| 133 | + dateTime2: Option[LocalTime], //time |
| 134 | + dateTime3: Option[LocalDateTime], //timestamp |
| 135 | + dateTime4: Option[Duration], //interval |
| 136 | + dateTime5: Option[ZonedDateTime], //timestamptz |
| 137 | + dateTime6: Option[OffsetDateTime], //timestamptz |
| 138 | + range1: Option[Range[Int]], //range |
| 139 | + lTree1: Option[LTree], //ltree |
| 140 | + map1: Option[Map[String, String]], //hstore |
| 141 | + inet1: Option[InetString], //inet |
| 142 | + macaddr1: Option[MacAddrString] //macaddr |
| 143 | + ) |
| 144 | + |
| 145 | + class TestFunction(implicit override val schema: DBSchema, override val dbEngine: SlickPgEngine) |
| 146 | + extends DBSingleResultFunction[InputOutput, InputOutput, SlickPgEngine] |
| 147 | + with SlickFunction[InputOutput, InputOutput] { |
| 148 | + |
| 149 | + override protected def sql(values: InputOutput): SQLActionBuilder = { |
| 150 | + sql"""SELECT #$selectEntry |
| 151 | + FROM #$functionName( |
| 152 | + ${values.uuid1}, |
| 153 | + ${values.dateTime1}, |
| 154 | + ${values.dateTime2}, |
| 155 | + ${values.dateTime3}, |
| 156 | + ${values.dateTime4}, |
| 157 | + ${values.dateTime5}, |
| 158 | + ${values.dateTime6}, |
| 159 | + ${values.range1}, |
| 160 | + ${values.lTree1}, |
| 161 | + ${values.map1}, |
| 162 | + ${values.inet1}, |
| 163 | + ${values.macaddr1} |
| 164 | + ) #$alias;""" |
| 165 | + } |
| 166 | + |
| 167 | + override protected def slickConverter: GetResult[InputOutput] = GetResult{r => InputOutput( |
| 168 | + r.<<, |
| 169 | + r.<<, |
| 170 | + r.<<, |
| 171 | + r.<<, |
| 172 | + r.<<, |
| 173 | + r.<<, |
| 174 | + r.<<, |
| 175 | + r.<<, |
| 176 | + r.<<, |
| 177 | + r.nextHStoreOption(), |
| 178 | + r.<<, |
| 179 | + r.nextMacAddrOption() |
| 180 | + )} |
| 181 | + } |
| 182 | + |
| 183 | + class TestSchema (implicit dBEngine: SlickPgEngine) extends DBSchema("public"){ |
| 184 | + |
| 185 | + val testFunction = new TestFunction |
| 186 | + } |
| 187 | + |
| 188 | + |
| 189 | + val inputOutput = InputOutput( |
| 190 | + None, |
| 191 | + None, |
| 192 | + None, |
| 193 | + None, |
| 194 | + None, |
| 195 | + None, |
| 196 | + None, |
| 197 | + None, |
| 198 | + None, |
| 199 | + None, |
| 200 | + None, |
| 201 | + None |
| 202 | + ) |
| 203 | + |
| 204 | + val timeout = Duration.ofMinutes(1) |
| 205 | + val result = Await.result(new TestSchema()(testDBEngine).testFunction(inputOutput), timeout) |
| 206 | + assert(result == inputOutput) |
| 207 | + } |
| 208 | + |
| 209 | +} |
| 210 | + |
0 commit comments