Skip to content

Instantly share code, notes, and snippets.

@mslinn
Last active December 31, 2015 21:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mslinn/8046542 to your computer and use it in GitHub Desktop.
Save mslinn/8046542 to your computer and use it in GitHub Desktop.
-Dconfig.resource=test.conf -Dlogger.file=test/resources/logback-test.xml
#!/bin/bash
set -e
export JPDA_PORT=9999
source commonJavaOpts
export JAVA_OPTS="$JAVA_OPTS -Dconfig.file=`pwd`/conf/dev.conf"
export JAVA_OPTS="$JAVA_OPTS -Dscala.repl.maxprintstring=9999"
export _JAVA_OPTIONS="$JAVA_OPTS"
CMD="$PLAY_HOME/play"
if [ "$OSTYPE" == cygwin ]; then CMD="$PLAY_HOME/play.bat"; fi
if [ "$#" == 0 ]; then
"$CMD" "run 9000"
elif [ "$1" == test ]; then
"$CMD"
else
"$CMD" "$@"
fi
package controllers
import model.training._
import org.specs2.mutable._
import play.api.db.DB
import play.api.Play.current
import play.api.test._
import play.api.test.Helpers._
import scala.slick.driver.PostgresDriver.simple._
import org.slf4j.LoggerFactory
import com.typesafe.config.{Config,ConfigFactory}
class SectionTest extends Specification {
val DatabaseId = "test"
val Logger = LoggerFactory.getLogger("test")
val postgresDB = postgresDatabase(DatabaseId)
def postgresDatabase(name: String = "default", options: Map[String, String] = Map.empty): Map[String, String] = {
val config: Config = ConfigFactory.load("test.conf")
val dbConfig: Config = config.getConfig(s"db.$name")
Map(
"db.default.driver" -> dbConfig.getString("driver"),
"db.default.user" -> dbConfig.getString("user"),
"db.default.password" -> dbConfig.getString("password"),
"db.default.url" -> dbConfig.getString("url")
)
}
def fakeApp[T](block: => T): T = {
val fakeApplication = FakeApplication(additionalConfiguration =
postgresDB ++ Map("evolutionplugin" -> "disabled"))
val database = Database.forDataSource(DB.getDataSource("default")(fakeApplication))
running(fakeApplication) {
println("About to run a test using fakeApplication")
database.withSession { implicit s: Session => block }
}
}
object x {
val groupId = -1234L
val courseId = -1234L
val sectionId = -1234L
val course: Course = Courses.upsert(Course(Some(groupId), "course_testCourse123", id=Some(-1234L)))
val lectures = List(
Lectures.upsert(Lecture(Some(sectionId), "Test lecture description 1", "lecture_testSku1", "Transcript 1", "Lecture Title 1", id=Some(-1234L))),
Lectures.upsert(Lecture(Some(sectionId), "Test lecture description 2", "lecture_testSku2", "Transcript 2", "Lecture Title 2", id=Some(-1235L))),
Lectures.upsert(Lecture(Some(sectionId), "Test lecture description 3", "lecture_testSku3", "Transcript 3", "Lecture Title 3", id=Some(-1236L))),
Lectures.upsert(Lecture(Some(sectionId), "Test lecture description 4", "lecture_testSku4", "Transcript 4", "Lecture Title 4", id=Some(-1237L)))
)
var lectureIdStr: String = lectures.map(_.id).mkString(",")
var section: Section = Section(Some(courseId), "section_testName1", "Test Section Title 1", Some(lectureIdStr), id=Some(sectionId))
}
"LectureIdList" should {
"be in the proper format" in fakeApp {
println("First test start")
x.lectureIdStr.split(",").size must equalTo(4)
}
}
"Lectures" should {
"associate with their section" in fakeApp {
println("Second test start")
val sectionName1 = x.lectures(1).sectionName
sectionName1 must equalTo("testName")
}
}
}
Listening for transport dt_socket at address: 9999
[info] Loading global plugins from /home/mslinn/.sbt/0.13/plugins
[info] Loading project definition from /home/mslinn/work/training/cadenza/project
[info] Set current project to cadenza (in build file:/home/mslinn/work/training/cadenza/)
[info] Compiling 1 Scala source to /home/mslinn/work/training/cadenza/target/scala-2.10/test-classes...
[info] controllers.SectionTest
[info] SectionTest
[info] LectureIdList should
[info] ! be in the proper format
[error] RuntimeException: There is no started application (Play.scala:68)
[error] play.api.Play$$anonfun$current$1.apply(Play.scala:68)
[error] play.api.Play$$anonfun$current$1.apply(Play.scala:68)
[error] play.api.Play$.current(Play.scala:68)
[error] controllers.SectionTest.fakeApp(SectionTest.scala:32)
[error] controllers.SectionTest$$anonfun$2$$anonfun$apply$4.apply(SectionTest.scala:58)
[error] controllers.SectionTest$$anonfun$2$$anonfun$apply$4.apply(SectionTest.scala:58)
[info] Lectures should
[info] ! associate with their section
[error] RuntimeException: There is no started application (Play.scala:68)
[error] play.api.Play$$anonfun$current$1.apply(Play.scala:68)
[error] play.api.Play$$anonfun$current$1.apply(Play.scala:68)
[error] play.api.Play$.current(Play.scala:68)
[error] controllers.SectionTest.fakeApp(SectionTest.scala:32)
[error] controllers.SectionTest$$anonfun$3$$anonfun$apply$7.apply(SectionTest.scala:65)
[error] controllers.SectionTest$$anonfun$3$$anonfun$apply$7.apply(SectionTest.scala:65)
[info] Total for specification SectionTest
[info] Finished in 325 ms
[info] 2 examples, 0 failure, 2 errors
[info] ! There is no started application
[info] ! There is no started application
[info]
[info]
[info] Total for test controllers.SectionTest
[info] Finished in 3.506 seconds
[info] 2 tests, 0 failures, 2 errors
[info] ScalaTest
[info] Run completed in 3 seconds, 628 milliseconds.
[info] Total number of tests run: 0
[info] Suites: completed 0, aborted 0
[info] Tests: succeeded 0, failed 0, canceled 0, ignored 0, pending 0
[info] All tests passed.
[error] Error: Total 2, Failed 0, Errors 2, Passed 0
[error] Error during tests:
# This is the unit test configuration file.
db.test.driver=org.postgresql.Driver
db.test.user=postgres
db.test.password=asdfasdf
db.test.url="jdbc:postgresql://localhost/asdfasdf"
db.test.url=${?DATABASE_URL}
include "../../conf/application.conf"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment