Skip to content

Commit

Permalink
Merge pull request #534 from michludw/timestampz-doobie-mapping
Browse files Browse the repository at this point in the history
fixing "TIMESTAMP WITH TIMEZONE" typo
  • Loading branch information
milessabin authored Dec 14, 2023
2 parents fd7f38a + 6aeb334 commit 939f0cf
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 3 deletions.
4 changes: 2 additions & 2 deletions modules/doobie-pg/src/main/scala/DoobieMapping.scala
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,9 @@ trait DoobieMappingLike[F[_]] extends Mapping[F] with SqlMappingLike[F] {
case SqlXml => Some("XML")
case Struct => Some("STRUCT")
case Time => Some("TIME")
case TimeWithTimezone => Some("TIME WITH TIMEZONE")
case TimeWithTimezone => Some("TIME WITH TIME ZONE")
case Timestamp => Some("TIMESTAMP")
case TimestampWithTimezone => Some("TIMESTAMP WITH TIMEZONE")
case TimestampWithTimezone => Some("TIMESTAMP WITH TIME ZONE")
case TinyInt => Some("TINYINT")
case VarBinary => Some("VARBINARY")
case VarChar => Some("VARCHAR")
Expand Down
12 changes: 12 additions & 0 deletions modules/sql/shared/src/test/resources/db/coalesce.sql
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ CREATE TABLE cb (
b BOOLEAN NOT NULL
);

CREATE TABLE cc (
id TEXT PRIMARY KEY,
rid TEXT NOT NULL,
c TIMESTAMP WITH TIME ZONE NOT NULL
);

COPY r (id) FROM STDIN WITH DELIMITER '|';
R1
R2
Expand All @@ -32,3 +38,9 @@ CB2a|R2|TRUE
CB2b|R2|FALSE
CB3|R3|TRUE
\.

COPY cc (id, rid, c) FROM STDIN WITH DELIMITER '|';
CC1|R1|2020-05-27T21:00:00+02
CC2|R2|2004-10-19T10:23:54+02
CC3|R3|2014-10-19T10:23:54+02
\.
28 changes: 27 additions & 1 deletion modules/sql/shared/src/test/scala/SqlCoalesceMapping.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package grackle.sql.test

import grackle.syntax._
import java.time.ZonedDateTime

trait SqlCoalesceMapping[F[_]] extends SqlTestMapping[F] {

Expand All @@ -35,15 +36,23 @@ trait SqlCoalesceMapping[F[_]] extends SqlTestMapping[F] {
val b = col("b", bool)
}

object cc extends TableDef("cc") {
val id = col("id", text)
val rid = col("rid", text)
val c = col("c", offsetDateTime)
}

val schema =
schema"""
type Query {
r: [R!]!
}
scalar DateTime
type R {
id: String!
ca: [CA!]!
cb: [CB!]!
cc: [CC!]!
}
type CA {
id: String!
Expand All @@ -53,12 +62,18 @@ trait SqlCoalesceMapping[F[_]] extends SqlTestMapping[F] {
id: String!
b: Boolean!
}
type CC {
id: String!
c: DateTime!
}
"""

val QueryType = schema.ref("Query")
val RType = schema.ref("R")
val CAType = schema.ref("CA")
val CBType = schema.ref("CB")
val CCType = schema.ref("CC")
val DateTimeType = schema.ref("DateTime")

val typeMappings =
List(
Expand All @@ -76,6 +91,7 @@ trait SqlCoalesceMapping[F[_]] extends SqlTestMapping[F] {
SqlField("id", r.id, key = true),
SqlObject("ca", Join(r.id, ca.rid)),
SqlObject("cb", Join(r.id, cb.rid)),
SqlObject("cc", Join(r.id, cc.rid)),
)
),
ObjectMapping(
Expand All @@ -95,6 +111,16 @@ trait SqlCoalesceMapping[F[_]] extends SqlTestMapping[F] {
SqlField("rid", cb.rid, hidden = true),
SqlField("b", cb.b)
)
)
),
ObjectMapping(
tpe = CCType,
fieldMappings =
List(
SqlField("id", cc.id, key = true),
SqlField("rid", cc.rid, hidden = true),
SqlField("c", cc.c)
)
),
LeafMapping[ZonedDateTime](DateTimeType)
)
}
81 changes: 81 additions & 0 deletions modules/sql/shared/src/test/scala/SqlCoalesceSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,85 @@ trait SqlCoalesceSuite extends CatsEffectSuite {
assert(numCells == 40)
}
}

test("zoned-date-time coalesced query") {
val query = """
query {
r {
id
ca {
id
a
}
cc {
id
c
}
}
}
"""

val expected = json"""
{
"data" : {
"r" : [
{
"id" : "R1",
"ca" : [
{
"id" : "CA1a",
"a" : 10
},
{
"id" : "CA1b",
"a" : 11
}
],
"cc" : [
{
"id" : "CC1",
"c" : "2020-05-27T19:00:00Z"
}
]
},
{
"id" : "R2",
"ca" : [
{
"id" : "CA2",
"a" : 20
}
],
"cc" : [
{
"id" : "CC2",
"c" : "2004-10-19T08:23:54Z"
}
]
},
{
"id" : "R3",
"ca" : [
{
"id" : "CA3",
"a" : 30
}
],
"cc" : [
{
"id" : "CC3",
"c" : "2014-10-19T08:23:54Z"
}
]
}
]
}
}
"""

for {
map <- mapping.map(_._1)
res <- map.compileAndRun(query)
} yield assertWeaklyEqual(res, expected)
}
}

0 comments on commit 939f0cf

Please sign in to comment.