public
Created

UnitTests.scala in Play 1.x vs. Play 2.x

  • Download Gist
UnitTests1.scala
Scala
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
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)
}
}
UnitTests2.scala
Scala
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
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)
}
}
}
}

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.

Thanks for the tip Eric!

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.