Skip to content

Commit

Permalink
Merge pull request zio#321 from andrewhj/full-stack-mysql
Browse files Browse the repository at this point in the history
Full stack mysql test environment
  • Loading branch information
jczuchnowski authored Nov 28, 2020
2 parents d940b36 + 5685224 commit b60d869
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 48 deletions.
37 changes: 19 additions & 18 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ addCommandAlias("fmtOnce", "all scalafmtSbt scalafmt test:scalafmt")
addCommandAlias("fmt", "fmtOnce;fmtOnce")
addCommandAlias("check", "all scalafmtSbtCheck scalafmtCheck test:scalafmtCheck")

val zioVersion = "1.0.3"
val testcontainersVersion = "1.15.0"
val zioVersion = "1.0.3"
val testcontainersVersion = "1.15.0"
val testcontainersScalaVersion = "1.0.0-alpha1"

lazy val startPostgres = taskKey[Unit]("Start up Postgres")
startPostgres := startService(Database.Postgres, streams.value)
Expand Down Expand Up @@ -153,14 +154,14 @@ lazy val mysql = project
.settings(
libraryDependencies ++= Seq(
"dev.zio" %% "zio" % zioVersion,
"dev.zio" %% "zio-test" % zioVersion % "test",
"dev.zio" %% "zio-test-sbt" % zioVersion % "test",
"org.testcontainers" % "testcontainers" % testcontainersVersion % Test,
"org.testcontainers" % "database-commons" % testcontainersVersion % Test,
"org.testcontainers" % "mysql" % testcontainersVersion % Test,
"org.testcontainers" % "jdbc" % testcontainersVersion % Test,
"mysql" % "mysql-connector-java" % "8.0.22" % Test,
"com.dimafeng" %% "testcontainers-scala-mysql" % "0.38.6" % Test
"dev.zio" %% "zio-test" % zioVersion % "test",
"dev.zio" %% "zio-test-sbt" % zioVersion % "test",
"org.testcontainers" % "testcontainers" % testcontainersVersion % Test,
"org.testcontainers" % "database-commons" % testcontainersVersion % Test,
"org.testcontainers" % "jdbc" % testcontainersVersion % Test,
"org.testcontainers" % "mysql" % testcontainersVersion % Test,
"mysql" % "mysql-connector-java" % "8.0.22" % Test,
"com.dimafeng" %% "testcontainers-scala-mysql" % testcontainersScalaVersion % Test
)
)
.settings(testFrameworks += new TestFramework("zio.test.sbt.ZTestFramework"))
Expand Down Expand Up @@ -195,14 +196,14 @@ lazy val postgres = project
.settings(
libraryDependencies ++= Seq(
"dev.zio" %% "zio" % zioVersion,
"dev.zio" %% "zio-test" % zioVersion % Test,
"dev.zio" %% "zio-test-sbt" % zioVersion % Test,
"org.testcontainers" % "testcontainers" % testcontainersVersion % Test,
"org.testcontainers" % "database-commons" % testcontainersVersion % Test,
"org.testcontainers" % "postgresql" % testcontainersVersion % Test,
"org.testcontainers" % "jdbc" % testcontainersVersion % Test,
"org.postgresql" % "postgresql" % "42.2.18" % Test,
"com.dimafeng" %% "testcontainers-scala-postgresql" % "1.0.0-alpha1" % Test
"dev.zio" %% "zio-test" % zioVersion % Test,
"dev.zio" %% "zio-test-sbt" % zioVersion % Test,
"org.testcontainers" % "testcontainers" % testcontainersVersion % Test,
"org.testcontainers" % "database-commons" % testcontainersVersion % Test,
"org.testcontainers" % "postgresql" % testcontainersVersion % Test,
"org.testcontainers" % "jdbc" % testcontainersVersion % Test,
"org.postgresql" % "postgresql" % "42.2.18" % Test,
"com.dimafeng" %% "testcontainers-scala-postgresql" % testcontainersScalaVersion % Test
)
)
.settings(testFrameworks += new TestFramework("zio.test.sbt.ZTestFramework"))
Expand Down
10 changes: 6 additions & 4 deletions mysql/src/test/resources/shop_schema.sql
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
-- TODO: currently id fields are storing a UUID. varchar is a rather slow and inefficient way to store a UUID
-- (though easier to bootstrap a test). Consider a more efficient method of storage as referenced in
-- https://mysqlserverteam.com/storing-uuid-values-in-mysql-tables/
create table customers
(
id varchar(36) not null primary key,
first_name varchar(100) not null,
last_name varchar(100) not null,
first_name varchar(255) not null,
last_name varchar(255) not null,
verified boolean not null,
dob date not null
);
Expand Down Expand Up @@ -37,7 +40,6 @@ create table order_details
unit_price decimal(15,2) not null
);


insert into customers
(id, first_name, last_name, verified, dob)
values
Expand Down Expand Up @@ -189,4 +191,4 @@ values
('852E2DC9-4EC3-4225-A6F7-4F42F8FF728E', 'D5137D3A-894A-4109-9986-E982541B434F', 1, 45.45),
('D6D8DDDC-4B0B-4D74-8EDC-A54E9B7F35F7', 'D5137D3A-894A-4109-9986-E982541B434F', 2, 50.00),
('2C3FC180-D0DF-4D7B-A271-E6CCD2440393', 'D5137D3A-894A-4109-9986-E982541B434F', 2, 50.00),
('5883CB62-D792-4EE3-ACBC-FE85B6BAA998', 'D5137D3A-894A-4109-9986-E982541B434F', 1, 55.00);
('5883CB62-D792-4EE3-ACBC-FE85B6BAA998', 'D5137D3A-894A-4109-9986-E982541B434F', 1, 55.00);
13 changes: 13 additions & 0 deletions mysql/src/test/scala/zio/sql/mysql/FunctionDefSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,19 @@ object FunctionDefSpec extends MysqlRunnableSpec with ShopSchema {
import FunctionDef._

val spec = suite("Mysql FunctionDef")(
testM("lower") {
val query = select(Lower("first_name")) from customers limit (1)

val expected = "ronald"

val testResult = execute(query).to[String, String](identity)

val assertion = for {
r <- testResult.runCollect
} yield assert(r.head)(equalTo(expected))

assertion.mapErrorCause(cause => Cause.stackless(cause.untraced))
},
testM("sin") {
val query = select(Sin(1.0)) from customers

Expand Down
44 changes: 23 additions & 21 deletions mysql/src/test/scala/zio/sql/mysql/MysqlModuleTest.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package zio.sql.mysql

import java.time.LocalDate
import java.util.UUID

import zio.Cause
import zio.test._
Expand All @@ -14,47 +15,48 @@ object MysqlModuleTest extends MysqlRunnableSpec with ShopSchema {

val spec = suite("Mysql module")(
testM("Can select from single table") {
case class Customer(id: String, fname: String, lname: String, dateOfBirth: LocalDate)
case class Customer(id: UUID, fname: String, lname: String, dateOfBirth: LocalDate)

val query = select(customerId ++ fName ++ lName ++ dob) from customers

println(renderRead(query))

val expected =
Seq(
Customer(
"60b01fc9-c902-4468-8d49-3c0f989def37",
UUID.fromString("60b01fc9-c902-4468-8d49-3c0f989def37"),
"Ronald",
"Russell",
LocalDate.parse("1983-01-05")
),
Customer(
"636ae137-5b1a-4c8c-b11f-c47c624d9cdc",
"Jose",
"Wiggins",
LocalDate.parse("1987-03-23")
UUID.fromString("f76c9ace-be07-4bf3-bd4c-4a9c62882e64"),
"Terrence",
"Noel",
LocalDate.parse("1999-11-02")
),
Customer(
"784426a5-b90a-4759-afbb-571b7a0ba35e",
UUID.fromString("784426a5-b90a-4759-afbb-571b7a0ba35e"),
"Mila",
"Paterso",
LocalDate.parse("1990-11-16")
),
Customer(
"df8215a2-d5fd-4c6c-9984-801a1b3a2a0b",
UUID.fromString("df8215a2-d5fd-4c6c-9984-801a1b3a2a0b"),
"Alana",
"Murray",
LocalDate.parse("1995-11-12")
),
Customer(
"f76c9ace-be07-4bf3-bd4c-4a9c62882e64",
"Terrence",
"Noel",
LocalDate.parse("1999-11-02")
UUID.fromString("636ae137-5b1a-4c8c-b11f-c47c624d9cdc"),
"Jose",
"Wiggins",
LocalDate.parse("1987-03-23")
)
)

val testResult = execute(query)
.to[String, String, String, LocalDate, Customer] { case row =>
.to[UUID, String, String, LocalDate, Customer] { case row =>
Customer(row._1, row._2, row._3, row._4)
}

Expand All @@ -65,7 +67,7 @@ object MysqlModuleTest extends MysqlRunnableSpec with ShopSchema {
assertion.mapErrorCause(cause => Cause.stackless(cause.untraced))
},
testM("Can select with property operator") {
case class Customer(id: String, fname: String, lname: String, verified: Boolean, dateOfBirth: LocalDate)
case class Customer(id: UUID, fname: String, lname: String, verified: Boolean, dateOfBirth: LocalDate)

val query = select(customerId ++ fName ++ lName ++ verified ++ dob) from customers where (verified isNotTrue)

Expand All @@ -74,7 +76,7 @@ object MysqlModuleTest extends MysqlRunnableSpec with ShopSchema {
val expected =
Seq(
Customer(
"636ae137-5b1a-4c8c-b11f-c47c624d9cdc",
UUID.fromString("636ae137-5b1a-4c8c-b11f-c47c624d9cdc"),
"Jose",
"Wiggins",
false,
Expand All @@ -83,18 +85,18 @@ object MysqlModuleTest extends MysqlRunnableSpec with ShopSchema {
)

val testResult = execute(query)
.to[String, String, String, Boolean, LocalDate, Customer] { case row =>
.to[UUID, String, String, Boolean, LocalDate, Customer] { case row =>
Customer(row._1, row._2, row._3, row._4, row._5)
}

val assertion = for {
r <- testResult.runCollect
} yield assert(r)(hasSameElementsDistinct(expected))

assertion.mapErrorCause(cause => Cause.stackless(cause.untraced))
},
testM("Can select from single table with limit, offset and order by") {
case class Customer(id: String, fname: String, lname: String, dateOfBirth: LocalDate)
case class Customer(id: UUID, fname: String, lname: String, dateOfBirth: LocalDate)

val query = (select(customerId ++ fName ++ lName ++ dob) from customers).limit(1).offset(1).orderBy(fName)

Expand All @@ -103,15 +105,15 @@ object MysqlModuleTest extends MysqlRunnableSpec with ShopSchema {
val expected =
Seq(
Customer(
"636ae137-5b1a-4c8c-b11f-c47c624d9cdc",
UUID.fromString("636ae137-5b1a-4c8c-b11f-c47c624d9cdc"),
"Jose",
"Wiggins",
LocalDate.parse("1987-03-23")
)
)

val testResult = execute(query)
.to[String, String, String, LocalDate, Customer] { case row =>
.to[UUID, String, String, LocalDate, Customer] { case row =>
Customer(row._1, row._2, row._3, row._4)
}

Expand Down
6 changes: 3 additions & 3 deletions mysql/src/test/scala/zio/sql/mysql/MysqlRunnableSpec.scala
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package zio.sql.mysql

import java.util.Properties

import zio.{ Has, ZEnv, ZLayer }
import zio.blocking.Blocking
import zio.sql.JdbcRunnableSpec
import zio.sql.TestContainer
import zio.test.environment.TestEnvironment

import java.util.Properties
import zio.sql.JdbcRunnableSpec

trait MysqlRunnableSpec extends JdbcRunnableSpec with MysqlModule {

private def connProperties(user: String, password: String): Properties = {
Expand Down
4 changes: 2 additions & 2 deletions mysql/src/test/scala/zio/sql/mysql/ShopSchema.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ trait ShopSchema extends Jdbc { self =>

object Customers {
val customers =
(string("id") ++ localDate("dob") ++ string("first_name") ++ string("last_name") ++ boolean("verified"))
(uuid("id") ++ localDate("dob") ++ string("first_name") ++ string("last_name") ++ boolean("verified"))
.table("customers")

val customerId :*: dob :*: fName :*: lName :*: verified :*: _ = customers.columns
}
object Orders {
val orders = (string("id") ++ uuid("customer_id") ++ localDate("order_date")).table("orders")
val orders = (uuid("id") ++ uuid("customer_id") ++ localDate("order_date")).table("orders")

val orderId :*: fkCustomerId :*: orderDate :*: _ = orders.columns
}
Expand Down

0 comments on commit b60d869

Please sign in to comment.