Skip to content

Commit

Permalink
Merge pull request zio#696 from ujali/673-Add-Union-UnionAll-example
Browse files Browse the repository at this point in the history
673 Added examples and tests for union and unionAll functions
  • Loading branch information
sviezypan authored May 27, 2022
2 parents 894f6b1 + 5a46b46 commit 7d2f448
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
12 changes: 12 additions & 0 deletions examples/src/main/scala/zio/sql/Examples.scala
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,16 @@ object Examples extends App with ShopSchema with PostgresJdbcModule {

val insertedSchemaRows = execute(insertSchema)
println(s"$insertedSchemaRows rows are inserted!")

/* SELECT "users"."user_id" FROM "users"
UNION
SELECT "orders"."usr_id" FROM "orders"
*/
val selectWithUnion = select(userId).from(users).union(select(fkUserId).from(orders))

/* SELECT "users"."user_id" FROM "users"
UNION ALL
SELECT "orders"."usr_id" FROM "orders"
*/
val selectWithUnionAll = select(userId).from(users).unionAll(select(fkUserId).from(orders))
}
28 changes: 28 additions & 0 deletions mysql/src/test/scala/zio/sql/mysql/MysqlModuleSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,34 @@ object MysqlModuleSpec extends MysqlRunnableSpec with ShopSchema {

assertion.mapErrorCause(cause => Cause.stackless(cause.untraced))
},
test("Execute union on select queries") {
val query = select(customerId).from(customers).union(select(fkCustomerId).from(orders))

val expected =
Seq(
UUID.fromString("60b01fc9-c902-4468-8d49-3c0f989def37"),
UUID.fromString("f76c9ace-be07-4bf3-bd4c-4a9c62882e64"),
UUID.fromString("784426a5-b90a-4759-afbb-571b7a0ba35e"),
UUID.fromString("df8215a2-d5fd-4c6c-9984-801a1b3a2a0b"),
UUID.fromString("636ae137-5b1a-4c8c-b11f-c47c624d9cdc")
)

assertZIO(execute(query).runCollect)(hasSameElements(expected))
},
test("Execute union all on select queries") {
val query = select(customerId).from(customers).unionAll(select(fkCustomerId).from(orders))

val expected =
Chunk(
UUID.fromString("60b01fc9-c902-4468-8d49-3c0f989def37"),
UUID.fromString("f76c9ace-be07-4bf3-bd4c-4a9c62882e64"),
UUID.fromString("784426a5-b90a-4759-afbb-571b7a0ba35e"),
UUID.fromString("df8215a2-d5fd-4c6c-9984-801a1b3a2a0b"),
UUID.fromString("636ae137-5b1a-4c8c-b11f-c47c624d9cdc")
)

assertZIO(execute(query).runCollect)(hasSameElementsDistinct(expected))
},
/*
* This is a failing test for aggregation function.
* Uncomment it when aggregation function handling is fixed.
Expand Down

0 comments on commit 7d2f448

Please sign in to comment.