Skip to content

Commit

Permalink
Added a basic unit test.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gregg Hernandez committed Jun 22, 2015
1 parent 787f4f9 commit f4b2d13
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/main/scala/com/lucidchart/open/relate/SqlQuery.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,27 @@ private[relate] case class ExpandableQuery(
listParams: mutable.Map[String, ListParam] = mutable.Map[String, ListParam]()
) extends ParameterizableSql with Expandable {

val timeout: Option[Int] = None

val params = Nil
protected[relate] def queryParams = QueryParams(
query,
params,
listParams
)

protected def setTimeout(stmt: PreparedStatement): Unit = {
for {
seconds <- timeout
stmt <- Option(stmt)
} yield (stmt.setQueryTimeout(seconds))
}

def withTimeout(seconds: Int): ExpandableQuery = new ExpandableQuery(query, listParams) {
override val timeout: Option[Int] = Some(seconds)

override def applyParams(stmt: PreparedStatement) {
if (stmt != null) {
stmt.setQueryTimeout(seconds)
}
setTimeout(stmt)

super.applyParams(stmt)
}
Expand Down
24 changes: 24 additions & 0 deletions src/test/scala/SqlQuerySpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.lucidchart.open.relate

import java.sql.Connection
import java.sql.PreparedStatement
import org.specs2.mutable._
import org.specs2.mock.Mockito

class SqlQuerySpec extends Specification with Mockito {


"ExpandableQuery.withTimeout" should {
class TestEq extends ExpandableQuery("")

"set the timeout" in {
val eq = new TestEq().withTimeout(10)
eq.timeout must beSome(10)
}

"not set the timtout" in {
val eq = new TestEq()
eq.timeout must beNone
}
}
}

0 comments on commit f4b2d13

Please sign in to comment.