Skip to content

Instantly share code, notes, and snippets.

@fmpwizard
Created December 17, 2010 03:53
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 fmpwizard/744461 to your computer and use it in GitHub Desktop.
Save fmpwizard/744461 to your computer and use it in GitHub Desktop.
Show a list of test names and their result status (PASS or FAIL)
package code {
package snippet {
import code.model.{AutomatedTests}
import _root_.scala.xml.{NodeSeq, Text}
import _root_.net.liftweb._
import util._
import common.Logger
import mapper.{OrderBy, Descending, SelectableField}
import http.SHtml._
import http.S._
import http.js.JsCmds.{SetHtml, SetValueAndFocus}
import Helpers._
class Overview extends Logger {
/**
* Generate the Test Result view section
*/
val showingVersion= http.S.param("v") openOr("ERROR")
def renderAgentResult( xhtml: NodeSeq ) = {
val testResultList = AutomatedTests.getAgentTestResultList( showingVersion )
// for this next line see
// http://stackoverflow.com/questions/4446949/scala-get-unique-values-from-list-with-a-twist
val testResultFiltered= testResultList.groupBy(_._1).map(pair => (pair._1, pair._2.reduceLeft((a,b) => if ("FAIL" == a._2 || "FAIL" == b._2) (a._1, "FAIL") else a))).map(_._2).toList.sorted
def bindTestResults(template: NodeSeq): NodeSeq = {
testResultFiltered.flatMap{ case (testName, testResult) => bind(
"test", template,
FuncAttrBindParam(
"classname", {
ns : NodeSeq => Text(if (testResult == "FAIL") "error" else "success" )
}, "class"
),
"name" -> testName,
"result" -> testResult
)}
}
bind("tests",xhtml, "version" -> showingVersion, "testResultListRow" -> bindTestResults _)
}
def renderServiceManagerResult( xhtml: NodeSeq ) = {
val testResultList = AutomatedTests.getServiceManagerTestResultList( showingVersion )
// for this next line see
// http://stackoverflow.com/questions/4446949/scala-get-unique-values-from-list-with-a-twist
val testResultFiltered= testResultList.groupBy(_._1).map(pair => (pair._1, pair._2.reduceLeft((a,b) => if ("FAIL" == a._2 || "FAIL" == b._2) (a._1, "FAIL") else a))).map(_._2).toList.sorted
def bindTestResults(template: NodeSeq): NodeSeq = {
testResultFiltered.flatMap{ case (testName, testResult) => bind(
"test", template,
FuncAttrBindParam(
"classname", {
ns : NodeSeq => Text(if (testResult == "FAIL") "error" else "success" )
}, "class"
),
"name" -> testName,
"result" -> testResult
)
}
}
bind("tests",xhtml, "version" -> showingVersion, "testResultListRow" -> bindTestResults _)
}
}
}
}
package code {
package snippet {
// First I create Mocks for the lift session
import javax.servlet.http._
import net.liftweb.http.{ S, Req, LiftSession }
import org.specs.mock.Mockito
import org.specs._
import org.specs.specification._
trait MockRequest extends Mockito { this: Specification =>
var request = mock[Req]
var httpRequest = mock[HttpServletRequest]
var session = mock[LiftSession]
def createMocks: Unit = {
request = mock[Req]
httpRequest = mock[HttpServletRequest]
session = mock[LiftSession]
request.request returns httpRequest
}
// this method can be used to executed any action inside a mocked session
def inSession(f: =>Any) { S.init(request, session) { f } }
def unsetParameter(name: String) { request.param(name) returns
None }
def setParameter(name: String, value: String) { request.param
(name) returns Some(value) }
}
// Context creation for the specification
//
// This "Specification context" specifies the User table must be cleaned up before each example.
// It also makes sure that the example expectations are executed in a mocked session
// see http://code.google.com/p/specs/wiki/DeclareSpecifications#Specification_context_(_from_1.6.1_) for more information
object DatabaseContext extends Specification with Contexts with
MockRequest {
val setup = new SpecContext {
beforeExample(inSession(Users.createQuery("deleteUser").executeUpdate)) //delete the User table before each example
aroundExpectations(inSession(_)) // execute each example inside a mocked session
}
}
// and finally the specification itself
class UserSpec extends SpecificationWithJUnit with MockRequest with
Contexts {
DatabaseContext.setup(this) // set the specification context on this
specification
"A Users repository" can {
"create a user" in {
val eric = User("etorreborre", "password", "Eric")
Users.mergeAndFlush(eric) // the Users object is a
//LocalEMFwith RequestVarEM so it needs a session
Users.find(classOf[User], "etorreborre") must_== Some(eric)
}
}
"A Users repository" should {
"throw an exception if the user name has a length < 5" in {
Users.merge(User("e", "password", "Eric")) must
throwAn[Exception]
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment