Skip to content

Commit

Permalink
fixed linting and fmting issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Yummy-Yums committed Sep 27, 2024
1 parent 713c7bc commit d98d5f4
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 63 deletions.
5 changes: 2 additions & 3 deletions zio-quickstart-zquery/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
resolvers ++= Resolver.sonatypeOssRepos("snapshots")
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -91,5 +94,4 @@ object DatabaseQueriesActor {

}


}
}
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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)
Expand All @@ -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
}


}
10 changes: 5 additions & 5 deletions zio-quickstart-zquery/src/test/scala/dev/zio/quickstart/Pg.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import zio.test._

import java.time.LocalDate


object QueryingSpec extends JdbcRunnableSpec {

import zio._
Expand All @@ -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")
)
}
Expand All @@ -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
)
}
)

Expand Down

0 comments on commit d98d5f4

Please sign in to comment.