Skip to content

Commit

Permalink
zio#654: Add now function to MysqlSqlModule
Browse files Browse the repository at this point in the history
  • Loading branch information
baldram committed May 26, 2022
1 parent bf3b198 commit dff2615
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
4 changes: 2 additions & 2 deletions mysql/src/main/scala/zio/sql/mysql/MysqlSqlModule.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package zio.sql.mysql

import java.sql.ResultSet
import java.time.Year

import java.time.{ Year, ZonedDateTime }
import zio.sql.Sql

trait MysqlSqlModule extends Sql { self =>
Expand Down Expand Up @@ -31,6 +30,7 @@ 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 Now = FunctionDef[Any, ZonedDateTime](FunctionName("now"))
val Pi = Expr.FunctionCall0[Double](FunctionDef[Any, Double](FunctionName("pi")))
val BitLength = FunctionDef[String, Int](FunctionName("bit_length"))
}
Expand Down
19 changes: 19 additions & 0 deletions mysql/src/test/scala/zio/sql/mysql/FunctionDefSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@ import zio.Cause
import zio.test._
import zio.test.Assertion._

import java.time.ZoneId
import java.time.format.DateTimeFormatter

object FunctionDefSpec extends MysqlRunnableSpec with ShopSchema {

import Customers._
import FunctionDef._
import MysqlFunctionDef._

private val timestampFormatter = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.SSSS").withZone(ZoneId.of("UTC"))

override def specLayered = suite("MySQL FunctionDef")(
test("lower") {
val query = select(Lower(fName)) from customers limit (1)
Expand Down Expand Up @@ -118,6 +123,20 @@ object FunctionDefSpec extends MysqlRunnableSpec with ShopSchema {

assertion.mapErrorCause(cause => Cause.stackless(cause.untraced))
},
test("now") {
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}.[0-9]{4}")
)

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

Expand Down

0 comments on commit dff2615

Please sign in to comment.