test db date

pull/83/head
Thibault Duplessis 2013-05-05 07:58:52 -03:00
parent 7c2bfe9d10
commit cb3fa88811
4 changed files with 51 additions and 42 deletions

View File

@ -7,8 +7,8 @@ abstract class WithDb extends WithApplication(WithDb.fakeApp)
object WithDb {
val fakeApp = FakeApplication(
lazy val fakeApp = FakeApplication(
additionalPlugins = Seq("play.modules.reactivemongo.ReactiveMongoPlugin"),
additionalConfiguration = Map("mongodb.db" -> "test")
additionalConfiguration = Map("mongodb.db" -> "lila-test")
)
}

View File

@ -3,37 +3,25 @@ package test
import Types._
import play.api.Play
import play.api.test._
import reactivemongo.api._
import scala.concurrent._
import scala.concurrent.duration._
import org.specs2.execute.{ Result, AsResult }
abstract class WithTestColl(a: FakeApplication = FakeApplication())
extends WithApplication(a) {
trait WithColl {
implicit lazy val conn = new MongoDriver connection List("localhost:27017")
def withColl[A](f: Coll A): A = {
implicit lazy val coll: Coll = {
val db = conn("test")
implicit val ec = ExecutionContext.Implicits.global
val timeout = 1 seconds
val conn = new MongoDriver connection List("localhost:27017")
val db = conn("lila-test") ~ { _.drop }
val coll = db("test")
coll.drop().await
coll
}
override def around[T: AsResult](t: T): Result = {
running(app)(AsResult(t))
}
private def running[T](fakeApp: FakeApplication)(t: T): T = synchronized {
try {
Play.start(fakeApp)
t
}
finally {
conn.close()
Play.stop()
play.api.libs.ws.WS.resetClient()
}
val res = f(coll)
conn.close
res
}
}

View File

@ -1,7 +1,7 @@
package lila.db
import test.WithTestColl
import Implicits._
import api._
import org.specs2.specification._
import org.specs2.mutable._
@ -13,10 +13,18 @@ import reactivemongo.api._
import reactivemongo.bson._
import org.joda.time.DateTime
class DateTest extends Specification {
class DateTest extends Specification with test.WithColl {
case class TestEvent(id: String, on: DateTime)
import Tube.Helpers._
implicit val eTube: Tube[TestEvent] = Tube[TestEvent](
reader = (__.json update readDate('on)) andThen Json.reads[TestEvent],
writer = Json.writes[TestEvent],
writeTransformer = (__.json update writeDate('on)).some
)
val date = DateTime.now
import api.Free._
import play.modules.reactivemongo.json.ImplicitBSONHandlers._
"date conversion" should {
@ -24,23 +32,36 @@ class DateTest extends Specification {
val doc = JsObjectWriter.write(Json.obj(
"ca" -> $gt($date(date))
))
doc.getAsTry[BSONDocument]("ca") flatMap { gt =>
doc.getAsTry[BSONDocument]("ca") flatMap { gt
gt.getAsTry[BSONDateTime]("$gt")
} must_== scala.util.Success(BSONDateTime(date.getMillis))
}
}
// "save and retrieve" should {
"save and retrieve" should {
// val trans = __.json update Tube.Helpers.readDate('ca)
sequential
// "JsObject" in new WithTestColl {
// (coll.insert(Json.obj("ca" -> $date(date))) >>
// coll.find(Json.obj()).one[JsObject]
// ).await flatMap (x (x transform trans).asOpt) must beSome.like {
// case o o \ "ca" must_== date
// }
// }
val trans = __.json update Tube.Helpers.readDate('ca)
// "raw reactive" in {
// withColl { coll
// (coll.insert(Json.obj("ca" -> $date(date))) >>
// coll.find(Json.obj()).one[JsObject]
// ).await flatMap (x (x transform trans).asOpt) must beSome.like {
// case o o \ "ca" must_== date
// }
// }
// }
"tube" in {
val event = TestEvent("test", date)
withColl { coll
implicit val tube = eTube inColl coll
($insert(event) >> $find.one(Json.obj())).await must beSome.like {
case TestEvent("test", d) d must_== date
}
}
}
// "BSONDocument" in new WithTestColl {
// val doc = BSONDocument(
// "ca" -> BSONDateTime(date.getMillis)
@ -57,5 +78,5 @@ class DateTest extends Specification {
// $set("foo" -> date) must_== Json.obj(
// "$set" -> Json.obj("foo" -> Json.obj("$date" -> date.getMillis)))
// }
// }
}
}

View File

@ -11,7 +11,7 @@ import org.joda.time.DateTime
class DbApiTest extends Specification {
import DbApi._
import api._
val date = DateTime.now