Skip to content

Instantly share code, notes, and snippets.

@mraible
Created June 6, 2012 01:11
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mraible/2879222 to your computer and use it in GitHub Desktop.
Save mraible/2879222 to your computer and use it in GitHub Desktop.
UnitTests.scala in Play 1.x vs. Play 2.x
import java.util.Date
import play.test._
import org.scalatest._
import org.scalatest.matchers._
class UnitTests extends UnitFlatSpec with ShouldMatchers with BeforeAndAfterEach {
import models._
import play.db.anorm._
override def beforeEach() {
Fixtures.deleteDatabase()
}
it should "create and retrieve a Athlete" in {
var user = Athlete(NotAssigned, "jim@gmail.com", "secret", "Jim", "Smith")
Athlete.create(user)
val jim = Athlete.find(
"email={email}").on("email" -> "jim@gmail.com"
).first()
jim should not be (None)
jim.get.firstName should be("Jim")
}
it should "connect a Athlete" in {
Athlete.create(Athlete(NotAssigned, "bob@gmail.com", "secret", "Bob", "Johnson"))
Athlete.connect("bob@gmail.com", "secret") should not be (None)
Athlete.connect("bob@gmail.com", "badpassword") should be(None)
Athlete.connect("tom@gmail.com", "secret") should be(None)
}
it should "create a Workout" in {
Athlete.create(Athlete(Id(1), "bob@gmail.com", "secret", "Bob", "Johnson"))
Workout.create(Workout(NotAssigned, "My first run", "With a hangover", 4, 38, new Date, 1))
Workout.count().single() should be (1)
val workouts = Workout.find("athleteId={id}").on("id" -> 1).as(Workout*)
workouts.length should be (1)
val firstWorkout = workouts.headOption
firstWorkout should not be (None)
firstWorkout.get.athleteId should be (1)
firstWorkout.get.title should be ("My first run")
firstWorkout.get.description should be ("With a hangover")
}
it should "retrieve Workouts by athlete" in {
Athlete.create(Athlete(Id(1), "bob@gmail.com", "secret", "Bob", "Johnson"))
Workout.create(Workout(NotAssigned, "My 1st workout", "Yee Haw!", 3.20, 70, new Date, 1))
val workouts = Workout.allWithAthlete
workouts.length should be (1)
val (workout,user) = workouts.head
workout.title should be ("My 1st workout")
user.firstName should be ("Bob")
}
it should "support Comments" in {
Athlete.create(Athlete(Id(1), "bob@gmail.com", "secret", "Bob", "Johnson"))
Workout.create(Workout(Id(1), "My first workout", "Yee Haw!", 2, 10, new Date, 1))
Comment.create(Comment(NotAssigned, "Jim", "Nice workout!", new Date, 1))
Comment.create(Comment(NotAssigned, "Bryan", "Great weather today!", new Date, 1))
Athlete.count().single() should be (1)
Workout.count().single() should be (1)
Comment.count().single() should be (2)
val Some( (post,user,comments) ) = Workout.byIdWithAthleteAndComments(1)
post.title should be ("My first workout")
user.firstName should be ("Bob")
comments.length should be (2)
comments(0).author should be ("Jim")
comments(1).author should be ("Bryan")
}
it should "load a complex graph from Yaml" in {
Yaml[List[Any]]("data.yml").foreach {
_ match {
case a:Athlete => Athlete.create(a)
case w:Workout => Workout.create(w)
case c:Comment => Comment.create(c)
}
}
Athlete.count().single() should be (2)
Workout.count().single() should be (3)
Comment.count().single() should be (3)
Athlete.connect("mraible@gmail.com", "beer") should not be (None)
Athlete.connect("trishmcginity@gmail.com", "whiskey") should not be (None)
Athlete.connect("trishmcginity@gmail.com", "badpassword") should be (None)
Athlete.connect("fred@gmail.com", "secret") should be (None)
val allWorkoutsWithAthleteAndComments = Workout.allWithAthleteAndComments
allWorkoutsWithAthleteAndComments.length should be (3)
val (workout,athlete,comments) = allWorkoutsWithAthleteAndComments(1)
workout.title should be ("Monarch Lake Trail")
athlete.firstName should be ("Matt")
comments.length should be (2)
// We have a referential integrity error
evaluating {
Athlete.delete("email={email}")
.on("email" -> "mraible@gmail.com").executeUpdate()
} should produce[java.sql.SQLException]
Workout.delete("athleteId={id}")
.on("id" -> 1).executeUpdate() should be (2)
Athlete.delete("email={email}")
.on("email" -> "mraible@gmail.com").executeUpdate() should be (1)
Athlete.count().single() should be (1)
Workout.count().single() should be (1)
Comment.count().single() should be (0)
}
}
import java.util.Date
import org.specs2.mutable._
import play.api.test._
import play.api.test.Helpers._
class UnitTests extends Specification {
import models._
import anorm._
"Athlete model" should {
"create and find itself" in {
running(FakeApplication(additionalConfiguration = inMemoryDatabase())) {
var user = Athlete(NotAssigned, "jim@gmail.com", "secret", "Jim", "Smith")
Athlete.create(user)
val jim = Athlete.find("email", "jim@gmail.com").head
jim must not be equalTo(None)
jim.firstName must_== ("Jim")
}
}
"login" in {
running(FakeApplication(additionalConfiguration = inMemoryDatabase())) {
Athlete.create(Athlete(NotAssigned, "bob@gmail.com", "secret", "Bob", "Johnson"))
Athlete.connect("bob@gmail.com", "secret") must_!= (None)
Athlete.connect("bob@gmail.com", "badpassword") must_== (None)
Athlete.connect("tom@gmail.com", "secret") must be_==(None)
}
}
}
"Workout model" should {
"create a Workout" in {
running(FakeApplication(additionalConfiguration = inMemoryDatabase())) {
Athlete.create(Athlete(Id(1), "bob@gmail.com", "secret", "Bob", "Johnson"))
Workout.create(Workout(NotAssigned, "My first run", "With a hangover", 4, 38, new Date, 1))
Workout.count() must be_==(1)
val workouts = Workout.find("athleteId", "1")
workouts.length must be_==(1)
val firstWorkout = workouts.headOption
firstWorkout must not be_== (None)
firstWorkout.get.athleteId must be_==(1)
firstWorkout.get.title must be_==("My first run")
firstWorkout.get.description must be_==("With a hangover")
}
}
"retrieve Workouts by athlete" in {
running(FakeApplication(additionalConfiguration = inMemoryDatabase())) {
Athlete.create(Athlete(Id(1), "bob@gmail.com", "secret", "Bob", "Johnson"))
Workout.create(Workout(NotAssigned, "My 1st workout", "Yee Haw!", 3.20, 70, new Date, 1))
val workouts = Workout.allWithAthlete
workouts.length must be_==(1)
val (workout, user) = workouts.head
workout.title must be_==("My 1st workout")
user.firstName must be_==("Bob")
}
}
"support Comments" in {
running(FakeApplication(additionalConfiguration = inMemoryDatabase())) {
Athlete.create(Athlete(Id(1), "bob@gmail.com", "secret", "Bob", "Johnson"))
Workout.create(Workout(Id(1), "My first workout", "Yee Haw!", 2, 10, new Date, 1))
Comment.create(Comment(NotAssigned, "Jim", "Nice workout!", new Date, 1))
Comment.create(Comment(NotAssigned, "Bryan", "Great weather today!", new Date, 1))
Athlete.count() must be_==(1)
Workout.count() must be_==(1)
Comment.count() must be_==(2)
val list = Workout.allWithAthleteAndComments
list.size must beEqualTo(1)
val comments = list(0)._3
comments.size must beEqualTo(2)
val Some((workout, athlete, comments2)) = Workout.byIdWithAthleteAndComments(1)
workout.title must be_==("My first workout")
athlete.firstName must be_==("Bob")
comments2.length must beEqualTo(2)
comments2(0).author must be_==("Jim")
comments2(1).author must be_==("Bryan")
}
}
}
"Global" should {
"load seed data into DB" in {
running(FakeApplication()) {
Workout.allWithAthlete.foreach { o =>
Workout.delete(o._1.id.get)
}
Athlete.findAll().foreach { o =>
Athlete.delete(o.id.get)
}
InitialData.insert()
Athlete.count() must be_==(2)
Workout.count() must be_==(3)
Comment.count() must be_==(3)
Athlete.connect("mraible@gmail.com", "beer") should not be (None)
Athlete.connect("trishmcginity@gmail.com", "whiskey") should not be (None)
Athlete.connect("trishmcginity@gmail.com", "badpassword") must be_==(None)
Athlete.connect("fred@gmail.com", "secret") must be_==(None)
val allWorkoutsWithAthleteAndComments = Workout.allWithAthleteAndComments
allWorkoutsWithAthleteAndComments.size must be_==(3)
val (workout, athlete, comments) = allWorkoutsWithAthleteAndComments(2)
workout.title must be_==("Monarch Lake Trail")
athlete.firstName must be_==("Matt")
comments.length must be_==(2)
// We have a referential integrity error
Athlete.delete(athlete.id.get) must throwA[java.sql.SQLException]
Workout.deleteByAthlete(1) must be_==(2)
Athlete.delete(1) must be_==(1)
Athlete.count() must be_==(1)
Workout.count() must be_==(1)
Comment.count() must be_==(0)
}
}
}
}
@etorreborre
Copy link

Hi Matt,

I think that you can remove some of the duplication by using an Around implicit context:

import org.specs2.execute.Result

class UnitTests extends Specification {
  // this can be reused in other specifications by being added to a reusable trait or object
  def inMemoryContext = new Around { 
    def around[R <% Result](r: =>R) = running(FakeApplication(additionalConfiguration = inMemoryDatabase()))(r) 
  }

  import models._
  import anorm._

  "Athlete model" should {
    // I'm scoping the use of this implicit context locally here because in your original specification you have both
    // in-memory DB testing and real DB testing. If that's not the case you can put this line at the beginning of your specification
    implicit val context = inMemoryContext 

    "create and find itself" in {
      var user = Athlete(NotAssigned, "jim@gmail.com", "secret", "Jim", "Smith")
      Athlete.create(user)

      val jim = Athlete.find("email", "jim@gmail.com").head

       jim must not be equalTo(None)
       jim.firstName must_== ("Jim")
    }

    "login" in {
      Athlete.create(Athlete(NotAssigned, "bob@gmail.com", "secret", "Bob", "Johnson"))

      Athlete.connect("bob@gmail.com", "secret") must_!= (None)
      Athlete.connect("bob@gmail.com", "badpassword") must_== (None)
      Athlete.connect("tom@gmail.com", "secret") must be_==(None)
    }
  }
  ...
}

If that doesn't work or if that's not really what you want, thanks for asking on the specs2-users mailing-list.

@mraible
Copy link
Author

mraible commented Jun 6, 2012

Thanks for the tip Eric!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment