Skip to content

Instantly share code, notes, and snippets.

@darkfrog26
Created June 3, 2015 21:35
Show Gist options
  • Save darkfrog26/006567e4518cf2c6dcd7 to your computer and use it in GitHub Desktop.
Save darkfrog26/006567e4518cf2c6dcd7 to your computer and use it in GitHub Desktop.
import org.scalatest.{Matchers, WordSpec}
/**
* @author Matt Hicks <matt@outr.com>
*/
class BasicsSpec extends WordSpec with Matchers {
import Schema._
"Table" should {
"create successfully" in {
transaction {
suppliers.create()
coffees.create()
}
}
"import suppliers" in {
transaction {
import suppliers._
// Clean and type-safe insert using
insert(id(101), name("Acme, Inc."), street("99 Market Street"), city("Groundsville"), state("CA"), zip("95199"))
insert(id(49), name("Superior Coffee"), street("1 Party Place"), city("Mendocino"), state("CA"), zip("95460"))
// Short-hand when using values in order
insertInto(suppliers, 150, "The High Ground", "100 Coffee Lane", "Meadows", "CA", "93966")
}
}
"import coffees" in {
transaction {
import coffees._
// Batch insert
coffees ++= Seq(
("Colombian", 101, 7.99, 0, 0), // Slick-like
(name("French_Roast"), supID(49), price(8.99), sales(0), total(0)), // Cleaner
("Espresso", 150, 9.99, 0, 0),
("Colombian_Decaf", 101, 8.99, 0, 0),
("French_Roast_Decaf", 49, 9.99, 0, 0)
)
}
}
"simple coffee query" in {
import coffees._
query.foreach {
case row => println(s" ${row(id)}\t${row(supID)}\t${row(price)}\t${row(sales)}\t${row(total)}")
}
}
"simple join query" in {
(coffees join suppliers on(suppliers.id === coffees.supID) columns(coffees.name, suppliers.name)).foreach {
case row => println(s"Coffee: ${row(coffees.name)}, Supplier: ${row(suppliers.name)}")
}
}
"simple join query with filter" in {
(coffees join suppliers on(suppliers.id === coffees.supID) columns(coffees.name, suppliers.name) filter(coffees.price < 9.0)).foreach {
case row => println(s"Coffee: ${row(coffees.name)}, Supplier: ${row(suppliers.name)}")
}
}
}
}
object Schema {
object suppliers extends Table {
val id = column[Int]("SUP_ID", PrimaryKey, AutoIncrement)
val name = column[String]("SUP_NAME")
val street = column[String]
val city = column[String]
val state = column[String]
val zip = column[String]
}
object coffees extends Table {
val name = column[String]("COF_NAME", PrimaryKey)
val supID = column[Int]("SUP_ID", ForeignKey(suppliers))
val price = column[Double]
val sales = column[Int]
val total = column[Int]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment