Skip to content

Instantly share code, notes, and snippets.

@doanphihai
Last active December 14, 2015 13:59
Show Gist options
  • Save doanphihai/eec40115ead6d035bf4d to your computer and use it in GitHub Desktop.
Save doanphihai/eec40115ead6d035bf4d to your computer and use it in GitHub Desktop.
package test
import org.specs2.mutable._
import play.api.test._
import play.api.test.Helpers._
class ApplicationSpec extends Specification {
import models._
// -- Date helpers
def dateIs(date: java.util.Date, str: String) = new java.text.SimpleDateFormat("yyyy-MM-dd").format(date) == str
// --
"Application" should {
"redirect to the computer list on /" in {
val result = controllers.Application.index(FakeRequest())
status(result) must equalTo(SEE_OTHER)
redirectLocation(result) must beSome.which(_ == "/computers")
}
"list computers on the the first page" in {
running(FakeApplication(additionalConfiguration = inMemoryDatabase())) {
val result = controllers.Application.list(0, 2, "")(FakeRequest())
status(result) must equalTo(OK)
contentAsString(result) must contain("565 computers found")
}
}
"filter computer by name" in {
running(FakeApplication(additionalConfiguration = inMemoryDatabase())) {
val result = controllers.Application.list(0, 2, "Apple")(FakeRequest())
status(result) must equalTo(OK)
contentAsString(result) must contain("13 computers found")
}
}
"create new computer" in {
running(FakeApplication(additionalConfiguration = inMemoryDatabase())) {
val apple_company = Company.get("Apple Inc.")
val badResult = controllers.Application.save(FakeRequest())
status(badResult) must equalTo(BAD_REQUEST)
val badDateFormat = controllers.Application.save(
FakeRequest().withFormUrlEncodedBody("name" -> "FooBar", "introduced" -> "badbadbad", "company" -> apple_company.id)
)
status(badDateFormat) must equalTo(BAD_REQUEST)
contentAsString(badDateFormat) must contain(s"""<option value="${apple_company.id}" selected>Apple Inc.</option>""")
contentAsString(badDateFormat) must contain("""<input type="text" id="introduced" name="introduced" value="badbadbad" >""")
contentAsString(badDateFormat) must contain("""<input type="text" id="name" name="name" value="FooBar" >""")
val result = controllers.Application.save(
FakeRequest().withFormUrlEncodedBody("name" -> "FooBar", "introduced" -> "2011-12-24", "company" -> apple_company.id)
)
status(result) must equalTo(SEE_OTHER)
redirectLocation(result) must beSome.which(_ == "/computers")
flash(result).get("success") must beSome.which(_ == "Computer FooBar has been created")
val list = controllers.Application.list(0, 2, "FooBar")(FakeRequest())
status(list) must equalTo(OK)
contentAsString(list) must contain("One computer found")
}
}
}
}
import sbt._
import Keys._
import play.Project._
object ApplicationBuild extends Build {
val appName = "computer-database"
val appVersion = "1.0"
val appDependencies = Nil
val customResolvers = Seq(
"fwbrasil.net" at "http://fwbrasil.net/maven/",
"Local Maven Repository" at "file://"+Path.userHome+"/.m2/repository"
)
val activateCore = "net.fwbrasil" %% "activate-core" % "1.2-SNAPSHOT"
val activatePlay = "net.fwbrasil" %% "activate-play" % "1.2-SNAPSHOT"
val activateJdbc = "net.fwbrasil" %% "activate-jdbc" % "1.2-SNAPSHOT"
// val activateCore = "net.fwbrasil" %% "activate-core" % "1.2-RC4"
// val activatePlay = "net.fwbrasil" %% "activate-play" % "1.2-RC4"
// val activateJdbc = "net.fwbrasil" %% "activate-jdbc" % "1.2-RC4"
val postgre = "postgresql" % "postgresql" % "9.1-901.jdbc4"
val main = play.Project(appName, appVersion, appDependencies).settings(
libraryDependencies ++= Seq(activateCore, activatePlay, activateJdbc, postgre),
resolvers ++= customResolvers
)
}
package models
import net.fwbrasil.activate.play.EntityForm
import java.util.Date
import postgresqlContext._
class Company(
var name: String)
extends Entity
class Computer(
var name: String,
var introduced: Option[Date],
var discontinued: Option[Date],
var company: Option[Company])
extends Entity
/**
* Helper for pagination.
*/
case class Page[A](items: Seq[A], page: Int, offset: Long, total: Long) {
lazy val prev = Option(page - 1).filter(_ >= 0)
lazy val next = Option(page + 1).filter(_ => (offset + items.size) < total)
}
object Computer {
def list(page: Int = 0, pageSize: Int = 10, orderBy: Int = 2, filter: String = "*"): Page[(Computer, Option[Company])] = transactional {
val unordered =
paginatedQuery {
(c: Computer) =>
where(c.name like filter) select (c)
}
val ordered =
orderBy.abs match {
case 2 =>
unordered.orderBy(_.name)
case 3 =>
unordered.orderBy(_.introduced)
case 4 =>
unordered.orderBy(_.discontinued)
case 5 =>
unordered.orderBy(_.company.map(_.name))
case _ =>
unordered.orderBy(_.name)
}
val directionOrdered =
if (orderBy < 0)
ordered.reverse
else
ordered
val navigator =
directionOrdered.navigator(pageSize)
if (navigator.numberOfResults > 0)
Page(navigator.page(page).map(c => (c, c.company)), page, page * pageSize, navigator.numberOfResults)
else
Page(Nil, 0, 0, 0)
}
}
object Company {
/**
* Construct the Map[String,String] needed to fill a select options set.
*/
def options: Seq[(String, String)] = transactional {
query {
(company: Company) =>
where(company isNotNull) select (company.id, company.name) orderBy (company.name)
}
}
def get(name: String): Company = transactional {
val list = (select[Company] where(_.name :== name))
list.head
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment