Skip to content

Commit

Permalink
Merge branch 'master' into gen_random_uuid-for-postgresql
Browse files Browse the repository at this point in the history
  • Loading branch information
sviezypan authored May 26, 2022
2 parents eac3323 + a5f5c40 commit 55ba7f6
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
4 changes: 3 additions & 1 deletion mysql/src/main/scala/zio/sql/mysql/MysqlSqlModule.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package zio.sql.mysql

import java.time.{ LocalDate, LocalTime, Year, ZonedDateTime }
import java.sql.ResultSet
import java.time.{ LocalDate, Year }

import zio.sql.Sql

Expand Down Expand Up @@ -33,6 +33,8 @@ trait MysqlSqlModule extends Sql { self =>
val Degrees = FunctionDef[Double, Double](FunctionName("degrees"))
val Log2 = FunctionDef[Double, Double](FunctionName("log2"))
val Log10 = FunctionDef[Double, Double](FunctionName("log10"))
val MakeTime = FunctionDef[(Int, Int, Double), LocalTime](FunctionName("maketime"))
val Now = FunctionDef[Any, ZonedDateTime](FunctionName("now"))
val Pi = Expr.FunctionCall0[Double](FunctionDef[Any, Double](FunctionName("pi")))
}

Expand Down
33 changes: 33 additions & 0 deletions mysql/src/test/scala/zio/sql/mysql/FunctionDefSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import zio.test._
import zio.test.Assertion._
import java.time.LocalDate

import java.time.{ LocalTime, ZoneId }
import java.time.format.DateTimeFormatter

object FunctionDefSpec extends MysqlRunnableSpec with ShopSchema {

import Customers._
Expand Down Expand Up @@ -119,6 +122,23 @@ object FunctionDefSpec extends MysqlRunnableSpec with ShopSchema {

assertion.mapErrorCause(cause => Cause.stackless(cause.untraced))
},
test("now") {
val timestampFormatter =
DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss").withZone(ZoneId.of("UTC"))

val query = select(Now())

val testResult = execute(query)

val assertion =
for {
r <- testResult.runCollect
} yield assert(timestampFormatter.format(r.head))(
Assertion.matchesRegex("[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}")
)

assertion.mapErrorCause(cause => Cause.stackless(cause.untraced))
},
test("bit_length") {
val query = select(BitLength("hello"))

Expand All @@ -145,6 +165,19 @@ object FunctionDefSpec extends MysqlRunnableSpec with ShopSchema {

assertion.mapErrorCause(cause => Cause.stackless(cause.untraced))
},
test("maketime") {
val query = select(MakeTime(12, 15, 30.5)) from customers

val expected = LocalTime.parse("12:15:30.5")

val testResult = execute(query)

val assertion = for {
r <- testResult.runCollect
} yield assert(r.head)(equalTo(expected))

assertion.mapErrorCause(cause => Cause.stackless(cause.untraced))
},
test("pi") {
val query = select(Pi) from customers

Expand Down

0 comments on commit 55ba7f6

Please sign in to comment.