diff --git a/zio-quickstart-zquery/build.sbt b/zio-quickstart-zquery/build.sbt index 801cf7e..1479d8c 100644 --- a/zio-quickstart-zquery/build.sbt +++ b/zio-quickstart-zquery/build.sbt @@ -15,8 +15,7 @@ libraryDependencies ++= Seq( "org.testcontainers" % "database-commons" % "1.19.7" % Test, "org.testcontainers" % "postgresql" % "1.19.7" % Test, "org.testcontainers" % "jdbc" % "1.19.7" % Test, - "com.dimafeng" %% "testcontainers-scala-postgresql" % "0.41.3" % Test, - + "com.dimafeng" %% "testcontainers-scala-postgresql" % "0.41.3" % Test ) -resolvers ++= Resolver.sonatypeOssRepos("snapshots") \ No newline at end of file +resolvers ++= Resolver.sonatypeOssRepos("snapshots") diff --git a/zio-quickstart-zquery/src/test/scala/dev/zio/quickstart/Commons.scala b/zio-quickstart-zquery/src/test/scala/dev/zio/quickstart/Commons.scala index d9018f4..55abebc 100644 --- a/zio-quickstart-zquery/src/test/scala/dev/zio/quickstart/Commons.scala +++ b/zio-quickstart-zquery/src/test/scala/dev/zio/quickstart/Commons.scala @@ -9,32 +9,34 @@ import java.util.UUID object DatabaseQueriesActor { - object Customerr{ + object Customerr { case class Customer( - id: UUID, - firstName: String, - lastName: String, - verified: Boolean, - dob: LocalDate) + id: UUID, + firstName: String, + lastName: String, + verified: Boolean, + dob: LocalDate + ) - implicit val customerschema= DeriveSchema.gen[Customer] - val customers = defineTableSmart[Customer] + implicit val customerschema = DeriveSchema.gen[Customer] + val customers = defineTableSmart[Customer] val (customerid, firstName, lastName, verified, dob) = customers.columns - val all: List[Any] = customerid :: firstName :: lastName :: verified :: dob :: Nil + val all: List[Any] = + customerid :: firstName :: lastName :: verified :: dob :: Nil } - object Order{ + object Order { case class Order( - id: UUID, - customerId: UUID, - order_date: LocalDate, - ) - implicit val orderDetailsSchema = DeriveSchema.gen[Order] - val orderDetails = defineTableSmart[Order] + id: UUID, + customerId: UUID, + order_date: LocalDate + ) + implicit val orderDetailsSchema = DeriveSchema.gen[Order] + val orderDetails = defineTableSmart[Order] val (id, customerId, order_date) = orderDetails.columns } @@ -45,7 +47,7 @@ object DatabaseQueriesActor { select(customerid, firstName, lastName, verified, dob) .from(customers) .to { case (id, firstName, lastName, verified, dob) => - Customer(id, firstName, lastName, verified, dob) + Customer(id, firstName, lastName, verified, dob) } execute(query).runHead.map(_.get.id) @@ -57,18 +59,19 @@ object DatabaseQueriesActor { select(customerid, firstName, lastName, verified, dob) .from(customers) .to { case (id, firstName, lastName, verified, dob) => - Customer(id, firstName, lastName, verified, dob) + Customer(id, firstName, lastName, verified, dob) } - execute(query).runFold(List.empty[UUID]){ - case (list, customer) => list :+ customer.id + execute(query).runFold(List.empty[UUID]) { case (list, customer) => + list :+ customer.id } } - val id = UUID.fromString("60b01fc9-c902-4468-8d49-3c0f989def37") + val id = UUID.fromString("60b01fc9-c902-4468-8d49-3c0f989def37") - - def getSingleOrderbyCustomerIdDbQuery(customerId: UUID): ZIO[SqlDriver, Exception, Order.Order] = { + def getSingleOrderbyCustomerIdDbQuery( + customerId: UUID + ): ZIO[SqlDriver, Exception, Order.Order] = { val getSingleOrder = select(Order.id, Order.customerId, Order.order_date) .from(Order.orderDetails) @@ -91,5 +94,4 @@ object DatabaseQueriesActor { } - -} \ No newline at end of file +} diff --git a/zio-quickstart-zquery/src/test/scala/dev/zio/quickstart/JdbcRunnableSpec.scala b/zio-quickstart-zquery/src/test/scala/dev/zio/quickstart/JdbcRunnableSpec.scala index da790f0..7562266 100644 --- a/zio-quickstart-zquery/src/test/scala/dev/zio/quickstart/JdbcRunnableSpec.scala +++ b/zio-quickstart-zquery/src/test/scala/dev/zio/quickstart/JdbcRunnableSpec.scala @@ -1,16 +1,19 @@ package dev.zio.quickstart -import com.dimafeng.testcontainers.{JdbcDatabaseContainer, PostgreSQLContainer, SingleContainer} +import com.dimafeng.testcontainers.{ + JdbcDatabaseContainer, + PostgreSQLContainer, + SingleContainer +} import org.testcontainers.utility.DockerImageName import zio.sql.{ConnectionPool, ConnectionPoolConfig} import zio.{Scope, ZIO, ZLayer} import zio.sql.postgresql.PostgresJdbcModule import zio.test.{Spec, TestEnvironment, ZIOSpecDefault} - import java.util.Properties -trait JdbcRunnableSpec extends ZIOSpecDefault with PostgresJdbcModule { +trait JdbcRunnableSpec extends ZIOSpecDefault with PostgresJdbcModule { type JdbcEnvironment = TestEnvironment with SqlDriver @@ -30,9 +33,9 @@ trait JdbcRunnableSpec extends ZIOSpecDefault with PostgresJdbcModule { protected val autoCommit = false private[this] def connProperties( - user: String, - password: String - ): Properties = { + user: String, + password: String + ): Properties = { val props = new Properties props.setProperty("user", user) props.setProperty("password", password) @@ -59,16 +62,17 @@ trait JdbcRunnableSpec extends ZIOSpecDefault with PostgresJdbcModule { val connectionPool: ZLayer[Any, Throwable, ConnectionPool] = poolConfigLayer >>> ConnectionPool.live - private[this] val testContainer: ZIO[Any with Scope, Throwable, SingleContainer[_] with JdbcDatabaseContainer] = + private[this] val testContainer + : ZIO[Any with Scope, Throwable, SingleContainer[_] + with JdbcDatabaseContainer] = ZIO.acquireRelease { ZIO.attemptBlocking { val container = getContainer container.start() container } - }{ container => + } { container => ZIO.attemptBlocking(container.stop()).orDie } - } diff --git a/zio-quickstart-zquery/src/test/scala/dev/zio/quickstart/Pg.scala b/zio-quickstart-zquery/src/test/scala/dev/zio/quickstart/Pg.scala index 357f288..0b951bb 100644 --- a/zio-quickstart-zquery/src/test/scala/dev/zio/quickstart/Pg.scala +++ b/zio-quickstart-zquery/src/test/scala/dev/zio/quickstart/Pg.scala @@ -15,12 +15,12 @@ import java.sql.DriverManager object Pg extends JdbcRunnableSpec { def specLayered: Spec[Pg.JdbcEnvironment, Object] = - suite("Checking"){ - test("er"){ - for{ + suite("Checking") { + test("er") { + for { url <- ZIO.succeed(getContainer.username) - _ <- ZIO.succeed("") + _ <- ZIO.succeed("") } yield assertTrue(url == "ew") } - } + } } diff --git a/zio-quickstart-zquery/src/test/scala/dev/zio/quickstart/QuerySpec.scala b/zio-quickstart-zquery/src/test/scala/dev/zio/quickstart/QuerySpec.scala index fa838c2..3261acd 100644 --- a/zio-quickstart-zquery/src/test/scala/dev/zio/quickstart/QuerySpec.scala +++ b/zio-quickstart-zquery/src/test/scala/dev/zio/quickstart/QuerySpec.scala @@ -5,7 +5,6 @@ import zio.test._ import java.time.LocalDate - object QueryingSpec extends JdbcRunnableSpec { import zio._ @@ -15,40 +14,53 @@ object QueryingSpec extends JdbcRunnableSpec { case object GetAllUserIds extends Request[Throwable, List[UUID]] - - object CustomerIdDataSource extends DataSource.Batched[Any, GetAllUserIds.type] { + object CustomerIdDataSource + extends DataSource.Batched[Any, GetAllUserIds.type] { val identifier: String = "UserIdDataSource" - def run(requests: Chunk[GetAllUserIds.type])(implicit trace: Trace): ZIO[Any, Nothing, CompletedRequestMap] = - (ZIO.succeed { - val result = Queriess.getAllCustomerIds - result.foldZIO( - err => ZIO.succeed(CompletedRequestMap.empty.insert(GetAllUserIds, Exit.fail(err))), - ids => ZIO.succeed(CompletedRequestMap.empty.insert(GetAllUserIds, Exit.succeed(ids))) - ) - }.flatten).provideLayer(jdbcLayer) + def run( + requests: Chunk[GetAllUserIds.type] + )(implicit trace: Trace): ZIO[Any, Nothing, CompletedRequestMap] = + (ZIO + .succeed { + val result = Queriess.getAllCustomerIds + result.foldZIO( + err => + ZIO.succeed( + CompletedRequestMap.empty.insert(GetAllUserIds, Exit.fail(err)) + ), + ids => + ZIO.succeed( + CompletedRequestMap.empty + .insert(GetAllUserIds, Exit.succeed(ids)) + ) + ) + } + .flatten) + .provideLayer(jdbcLayer) object Queriess { def getAllCustomerIds: ZIO[Any, Throwable, List[UUID]] = - DatabaseQueriesActor.getAllCustomerIdsDbQuery.provideLayer(jdbcLayer) + DatabaseQueriesActor.getAllCustomerIdsDbQuery.provideLayer(jdbcLayer) def getUserNameById(id: UUID): ZIO[Any, Exception, Order.Order] = { - DatabaseQueriesActor.getSingleOrderbyCustomerIdDbQuery(id).provideLayer(jdbcLayer) + DatabaseQueriesActor + .getSingleOrderbyCustomerIdDbQuery(id) + .provideLayer(jdbcLayer) } - } + } } - object UserQueries { val getAllUserIds: ZQuery[Any, Throwable, List[UUID]] = ZQuery.fromRequest(GetAllUserIds)(CustomerIdDataSource) - val provisionalResults = List( - 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"), + val provisionalResults = List( + 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") ) } @@ -73,7 +85,9 @@ object QueryingSpec extends JdbcRunnableSpec { doneTime = java.lang.System.nanoTime() - currTime _ <- Console.printLine("with zquery => " + (doneTime / 1000000)) // _ <- Console.printLine(res) - } yield assertCompletes && assertTrue(res.size == 5) && assertTrue(res == UserQueries.provisionalResults) + } yield assertCompletes && assertTrue(res.size == 5) && assertTrue( + res == UserQueries.provisionalResults + ) } )