Permalink
Browse files

Adds custom transactor for country and person tables

  • Loading branch information...
franciscodr committed Aug 8, 2016
1 parent 4e86b5b commit f14c2460aae546c4207e81b2e6c089e5d39cf271
@@ -1,6 +1,6 @@
package doobie
import doobie.DoobieUtils._
import doobie.DoobieUtils.CountryTable._
import doobie.imports._
import org.scalaexercises.definitions.Section
import org.scalatest._
@@ -11,9 +11,7 @@ import scalaz._, Scalaz._
object DoobieUtils {
// Transactor for single-use in-memory databases pre-populated with test data.
val xa = new Transactor[Task] {
class CustomTransactor[B](implicit beforeActions: ConnectionIO[B]) extends Transactor[Task] {
val driver = "org.h2.Driver"
def url = "jdbc:h2:mem:"
val user = "sa"
@@ -22,6 +20,11 @@ object DoobieUtils {
val connect: Task[Connection] =
Task.delay(Class.forName(driver)) *> FD.getConnection(url, user, pass).trans[Task]
override val before = super.before <* beforeActions
}
object CountryTable {
val createCountryTable: ConnectionIO[Int] =
sql"""
CREATE TABLE IF NOT EXISTS country (
@@ -33,11 +36,28 @@ object DoobieUtils {
""".update.run
def insertCountries(countries: List[Country]): ConnectionIO[Int] =
Update[Country](s"insert into country (code, name, population, gnp) values (?,?,?,?)").updateMany(countries)
Update[Country]("insert into country (code, name, population, gnp) values (?,?,?,?)")
.updateMany(countries)
override val before =
super.before <* createCountryTable <* insertCountries(countries)
implicit val beforeActions = createCountryTable <* insertCountries(countries)
// Transactor for single-use in-memory databases pre-populated with test data.
val xa = new CustomTransactor
}
object PersonTable {
val createPersonTable: ConnectionIO[Int] =
sql"""
CREATE TABLE IF NOT EXISTS person (
id IDENTITY,
name VARCHAR NOT NULL UNIQUE,
age INT
)
""".update.run
implicit val beforeActions = createPersonTable
val xa = new CustomTransactor
}
}
@@ -1,6 +1,6 @@
package doobie
import doobie.DoobieUtils._
import doobie.DoobieUtils.CountryTable._
import doobie.Model._
import doobie.imports._
import org.scalaexercises.definitions.Section
@@ -1,6 +1,6 @@
package doobie
import doobie.DoobieUtils._
import doobie.DoobieUtils.CountryTable._
import doobie.Model._
import doobie.ParameterizedQueryHelpers._
import doobie.imports._
@@ -1,6 +1,6 @@
package doobie
import doobie.DoobieUtils._
import doobie.DoobieUtils.CountryTable._
import doobie.imports._
import org.scalaexercises.definitions.Section
import org.scalatest.{FlatSpec, Matchers}
@@ -1,5 +1,6 @@
package doobie
import doobie.DoobieUtils.PersonTable._
import doobie.UpdatesSectionHelpers._
import doobie.imports._
import org.scalaexercises.definitions.Section
@@ -12,29 +12,6 @@ object UpdatesSectionHelpers {
case class Person(id: Long, name: String, age: Option[Short])
val xa = new Transactor[Task] {
val driver = "org.h2.Driver"
def url = "jdbc:h2:mem:"
val user = "sa"
val pass = ""
val connect: Task[Connection] =
Task.delay(Class.forName(driver)) *> FD.getConnection(url, user, pass).trans[Task]
val create: Update0 =
sql"""
CREATE TABLE person (
id IDENTITY,
name VARCHAR NOT NULL UNIQUE,
age INT)
""".update
override val before =
super.before <* create.run
}
def insert1(name: String, age: Option[Int]): Update0 =
sql"""
INSERT INTO person (name, age) VALUES ($name, $age)

0 comments on commit f14c246

Please sign in to comment.