|
| 1 | +package scalikejdbc.bigquery |
| 2 | + |
| 3 | +import java.time.{ZonedDateTime, ZoneId} |
| 4 | + |
| 5 | +import com.google.cloud.bigquery.DatasetId |
| 6 | +import org.scalatest.FlatSpec |
| 7 | +import scalikejdbc._, BqExtension._ |
| 8 | + |
| 9 | +class QueryDSLIntegration extends FlatSpec with BigQueryFixture { |
| 10 | + |
| 11 | + //======================== |
| 12 | + // Suppose that tables and rows like following exist in DataSet:"scalikejdbc_bigquery_integration". |
| 13 | + // |
| 14 | + // create table post ( |
| 15 | + // id INT64, |
| 16 | + // body STRING, |
| 17 | + // posted_at TIMESTAMP, |
| 18 | + // ); |
| 19 | + // |
| 20 | + // create table tag ( |
| 21 | + // id INT64, |
| 22 | + // post_id INT64, |
| 23 | + // name STRING |
| 24 | + // ); |
| 25 | + // |
| 26 | + // insert into post(id, body, posted_at) values (1, 'first post about jvm languages', '2017-03-23T10:00:00.000000+0000') |
| 27 | + // insert into tag(id, post_id, name) values (1, 1, 'java'); |
| 28 | + // insert into tag(id, post_id, name) values (2, 1, 'scala'); |
| 29 | + // |
| 30 | + // insert into post(id, body, posted_at) values (2, 'second post', '2017-03-24T10:00:00.000000+0000') |
| 31 | + // |
| 32 | + // insert into post(id, body, posted_at) values (3, 'third post about lightweight languages', '2017-03-25T10:00:00.000000+0000') |
| 33 | + // insert into tag(id, post_id, name) values (3, 3, 'ruby'); |
| 34 | + // insert into tag(id, post_id, name) values (4, 3, 'python'); |
| 35 | + // insert into tag(id, post_id, name) values (5, 3, 'perl'); |
| 36 | + // |
| 37 | + //======================== |
| 38 | + it should "work correctly on standard SQL" in { |
| 39 | + val bigQuery = mkBigQuery() |
| 40 | + val queryConfig = QueryConfig() |
| 41 | + |
| 42 | + val dataset = DatasetId.of(projectId(), "scalikejdbc_bigquery_integration") |
| 43 | + |
| 44 | + val executor = new QueryExecutor(bigQuery, queryConfig) |
| 45 | + |
| 46 | + import Post.p, Tag.t, Post.postIdBinders |
| 47 | + val sub = SubQuery.syntax("sub").include(p) |
| 48 | + |
| 49 | + val response = bq { |
| 50 | + select(sub.result.*, t.result.*).from( |
| 51 | + select(p.result.*).from(standardTableReference(dataset, Post.tableName, Some(p.tableAliasName))) |
| 52 | + .where.in(p.id, Seq(PostId(1), PostId(2), PostId(3))) |
| 53 | + .limit(3) |
| 54 | + .offset(0) |
| 55 | + .as(sub) |
| 56 | + ) |
| 57 | + .leftJoin(standardTableReference(dataset, Tag.tableName, Some(t.tableAliasName))) |
| 58 | + .on(sub(p).id, t.postId) |
| 59 | + .orderBy(sub(p).id, t.id) |
| 60 | + } |
| 61 | + .one(Post(_, sub(p).resultName)) |
| 62 | + .toMany(Tag.opt) |
| 63 | + .map(PostWithTags) |
| 64 | + .list |
| 65 | + .run(executor) |
| 66 | + |
| 67 | + val result = response.result |
| 68 | + |
| 69 | + assert(result.size == 3) |
| 70 | + |
| 71 | + assert(result(0) == PostWithTags( |
| 72 | + Post(PostId(1), "first post about jvm languages", ZonedDateTime.of(2017, 3, 23, 10, 0, 0, 0, ZoneId.of("UTC"))), |
| 73 | + Seq( |
| 74 | + Tag(TagId(1), PostId(1), "java"), |
| 75 | + Tag(TagId(2), PostId(1), "scala") |
| 76 | + ) |
| 77 | + )) |
| 78 | + |
| 79 | + assert(result(1) == PostWithTags( |
| 80 | + Post(PostId(2), "second post", ZonedDateTime.of(2017, 3, 24, 10, 0, 0, 0, ZoneId.of("UTC"))), |
| 81 | + Nil |
| 82 | + )) |
| 83 | + |
| 84 | + assert(result(2) == PostWithTags( |
| 85 | + Post(PostId(3), "third post about lightweight languages", ZonedDateTime.of(2017, 3, 25, 10, 0, 0, 0, ZoneId.of("UTC"))), |
| 86 | + Seq( |
| 87 | + Tag(TagId(3), PostId(3), "ruby"), |
| 88 | + Tag(TagId(4), PostId(3), "python"), |
| 89 | + Tag(TagId(5), PostId(3), "perl") |
| 90 | + ) |
| 91 | + )) |
| 92 | + } |
| 93 | +} |
0 commit comments