-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathQueryDSLIntegration.scala
164 lines (124 loc) · 4.48 KB
/
QueryDSLIntegration.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package scalikejdbc.bigquery
import java.time.{ZonedDateTime, ZoneId}
import com.google.cloud.bigquery.DatasetId
import org.scalatest.flatspec.AnyFlatSpec
import scalikejdbc._
class QueryDSLIntegration extends AnyFlatSpec with BigQueryFixture {
/* ========================
Suppose that tables and rows like following exist in DataSet:"scalikejdbc_bigquery_integration".
create table scalikejdbc_bigquery_integration.post (
id INT64,
body STRING,
posted_at TIMESTAMP
);
create table scalikejdbc_bigquery_integration.tag (
id INT64,
post_id INT64,
name STRING
);
insert into scalikejdbc_bigquery_integration.post (id, body, posted_at) values
(1, 'first post about jvm languages', '2017-03-23T10:00:00.000000'),
(2, 'second post', '2017-03-24T10:00:00.000000'),
(3, 'third post about lightweight languages', '2017-03-25T10:00:00.000000');
insert into scalikejdbc_bigquery_integration.tag(id, post_id, name) values
(1, 1, 'java'),
(2, 1, 'scala'),
(3, 3, 'ruby'),
(4, 3, 'python'),
(5, 3, 'perl');
======================== */
it should "work correctly on standard SQL" in {
val bigQuery = mkBigQuery()
val queryConfig = QueryConfig()
val dataset = DatasetId.of(projectId(), "scalikejdbc_bigquery_integration")
val executor = new QueryExecutor(bigQuery, queryConfig)
import Post.p, Tag.t, Post.postIdBinders
val sub = SubQuery.syntax("sub").include(p)
val response = bq {
select(sub.result.*, t.result.*).from(
select(p.result.*).from(Post in dataset as p)
.where.in(p.id, Seq(PostId(1), PostId(2), PostId(3)))
.limit(3)
.offset(0)
.as(sub)
)
.leftJoin(Tag in dataset as t)
.on(sub(p).id, t.postId)
.orderBy(sub(p).id, t.id)
}
.one(Post(_, sub(p).resultName))
.toMany(Tag.opt)
.map(PostWithTags)
.list
.run(executor)
// handle environment-dependent TimeZone difference
val result = response.result.map(p => p.copy(post = p.post.copy(postedAt = p.post.postedAt.withZoneSameInstant(ZoneId.of("UTC")))))
assert(result.size == 3)
assert(result(0) == PostWithTags(
Post(PostId(1), "first post about jvm languages", ZonedDateTime.of(2017, 3, 23, 10, 0, 0, 0, ZoneId.of("UTC"))),
Seq(
Tag(TagId(1), PostId(1), "java"),
Tag(TagId(2), PostId(1), "scala")
)
))
assert(result(1) == PostWithTags(
Post(PostId(2), "second post", ZonedDateTime.of(2017, 3, 24, 10, 0, 0, 0, ZoneId.of("UTC"))),
Nil
))
assert(result(2) == PostWithTags(
Post(PostId(3), "third post about lightweight languages", ZonedDateTime.of(2017, 3, 25, 10, 0, 0, 0, ZoneId.of("UTC"))),
Seq(
Tag(TagId(3), PostId(3), "ruby"),
Tag(TagId(4), PostId(3), "python"),
Tag(TagId(5), PostId(3), "perl")
)
))
}
it should "work correctly on standard SQL single result" in {
val bigQuery = mkBigQuery()
val queryConfig = QueryConfig()
val dataset = DatasetId.of(projectId(), "scalikejdbc_bigquery_integration")
val executor = new QueryExecutor(bigQuery, queryConfig)
import Post.p, Post.postIdBinders
val response = bq {
select(sqls.count).from(Post in dataset as p)
.where.in(p.id, Seq(PostId(1), PostId(2)))
}
.map(_.long(0))
.single
.run(executor)
val result = response.result.get
assert(result == 2)
}
it should "correctly bind timeZone parameter" in {
val bigQuery = mkBigQuery()
val queryConfig = QueryConfig()
val dataset = DatasetId.of(projectId(), "scalikejdbc_bigquery_integration")
val executor = new QueryExecutor(bigQuery, queryConfig)
import Post.p
val response = bq {
select(p.result.id).from(Post in dataset as p)
.where.eq(p.postedAt, ZonedDateTime.of(2017, 3, 23, 10, 0, 0, 0, ZoneId.of("UTC")))
}
.map(_.long(p.resultName.id))
.single
.run(executor)
val result = response.result.get
assert(result == 1L)
}
it should "return empty result" in {
val bigQuery = mkBigQuery()
val queryConfig = QueryConfig()
val dataset = DatasetId.of(projectId(), "scalikejdbc_bigquery_integration")
val executor = new QueryExecutor(bigQuery, queryConfig)
import Post.p
val response = bq {
select(p.result.id).from(Post in dataset as p)
.where.isNull(p.id)
}
.map(_.long(p.resultName.id))
.single
.run(executor)
assert(response.result.isEmpty)
}
}