Skip to content

Instantly share code, notes, and snippets.

@samhendley
Created January 25, 2011 19:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samhendley/795508 to your computer and use it in GitHub Desktop.
Save samhendley/795508 to your computer and use it in GitHub Desktop.
Test Suite showing squeryl bugs in 0.9.4-RC2
package reef.persistence.squeryl
import org.scalatest.{ FunSuite, BeforeAndAfterAll, BeforeAndAfterEach }
import org.scalatest.matchers.ShouldMatchers
import org.scalatest.junit.JUnitRunner
import org.junit.runner.RunWith
import org.squeryl.{ Schema, Table, KeyedEntity }
import org.squeryl.PrimitiveTypeMode._
class Foo(val value: String, val data : Long = 0) extends KeyedEntity[Long] {
val id: Long = 0
}
object FooSchema extends Schema {
val foos = table[Foo]
def reset() = {
drop // its protected for some reason
create
}
}
@RunWith(classOf[JUnitRunner])
class ExampleModelTest extends FunSuite with ShouldMatchers with BeforeAndAfterAll with BeforeAndAfterEach {
override def beforeAll() {
import reef.persistence.squeryl.{ DbConnector, DbInfo }
DbConnector.connect(DbInfo.loadInfo("test"))
}
override def beforeEach() {
transaction { FooSchema.reset }
}
test("FooSchema1") {
transaction {
FooSchema.foos.insert(new Foo("test"))
val foos = FooSchema.foos.where(f => f.value === "test")
foos.size should equal(1)
}
}
test("FooSchema2") {
transaction {
val foo1 = FooSchema.foos.insert(new Foo("test"))
val foo2 = FooSchema.foos.insert(new Foo("testa"))
val foos = FooSchema.foos.where(f => f.value === "test")
foos.size should equal(1)
}
}
// the following 3 tests prove the nested transaction bug with squeryl
// https://github.com/max-l/Squeryl/issues#issue/79
test("Excepting out of nested transaction") {
transaction {
FooSchema.reset
val foo1 = FooSchema.foos.insert(new Foo("test"))
FooSchema.foos.where(f => f.value === "test").size should equal(1)
intercept[Exception] {
doSomething(true)
}
intercept[Exception] {
// fails with "no session exception"
FooSchema.foos.where(f => f.value === "test").size should equal(1)
}
}
}
test("Returning out of nested transaction") {
transaction {
FooSchema.reset
val foo1 = FooSchema.foos.insert(new Foo("test"))
FooSchema.foos.where(f => f.value === "test").size should equal(1)
doSomething(false)
intercept[Exception] {
// fails with "no session exception"
FooSchema.foos.where(f => f.value === "test").size should equal(1)
}
}
}
test("Returning out of sibling transaction") {
transaction {
FooSchema.reset
val foo1 = FooSchema.foos.insert(new Foo("test"))
FooSchema.foos.where(f => f.value === "test").size should equal(1)
doSomething(false)
}
transaction {
FooSchema.foos.where(f => f.value === "test").size should equal(1)
}
}
test("Empty list inhibition with iterable"){
transaction {
FooSchema.reset
val foo1 = FooSchema.foos.insert(new Foo("test1"))
val map1 = Map("test1" -> "test1")
val result1 = FooSchema.foos.where(f => f.value in map1.keys).toList
result1 should equal(List(foo1))
//Select
// Foo1.data as Foo1_data,
// Foo1.id as Foo1_id,
// Foo1.value as Foo1_value
//From
// Foo Foo1
//Where
// (Foo1.value in ()) <-- EMPTY!
intercept[Exception]{
val map2 = Map.empty[String, String]
val result2 = FooSchema.foos.where(f => f.value in map2.keys).toList
result2 should equal(Nil)
}
}
}
test("Empty list inhibition inside join"){
transaction {
FooSchema.reset
val foo1 = FooSchema.foos.insert(new Foo("test1", 100))
val foo2 = FooSchema.foos.insert(new Foo("test1", 150))
val foo3 = FooSchema.foos.insert(new Foo("test2", 99))
val foo4 = FooSchema.foos.insert(new Foo("test2", 40))
def maxData(list : Iterable[String]) =
from(FooSchema.foos)(m =>
where((m.value in list))
groupBy (m.value)
compute (max(m.data)))
def byId(list : Iterable[String]) = join(FooSchema.foos, maxData(list))((m, mmt) =>
select(m)
on ((m.value === mmt.key) and (m.data === mmt.measures)))
val result1 = byId(List("test1", "test2")).toList
result1 should equal(List(foo2,foo3))
val result2 = byId(List("test2")).toList
result2 should equal(List(foo3))
val result3 = byId(Nil).toList
result3 should equal(Nil)
intercept[Exception] {
// throws an exception because of empty query
val map = Map.empty[String, String]
val result4 = byId(map.keys).toList
result4 should equal(Nil)
}
}
}
def doSomething(except: Boolean): Int = {
transaction {
if (except) throw new Exception()
return 1
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment