Permalink
Browse files

Fix specs

  • Loading branch information...
cchantep
cchantep committed Jun 24, 2013
1 parent 572c3a9 commit a758541af62a47f1a7d7216c694afe3faad69ac5
@@ -1,20 +1,22 @@
package cielago.controllers
import play.api.Play.current
-
-import play.api.cache.Cache
-
import play.api.mvc.PlainResult
-
import play.api.test.FakeApplication
+import acolyte.{ StatementHandler }
+
trait ControllerSpec {
- protected def fakeApp: FakeApplication =
+ protected def fakeApp(h: Option[StatementHandler] = None): FakeApplication =
FakeApplication(additionalConfiguration = Map(
"application.secret" -> "test",
- "db.default.driver" -> "org.apache.derby.jdbc.ClientDriver",
- "db.default.url" -> "jdbc:derby://localhost:1527/target/testdb;deregister=true",
- "evolutionplugin" -> "disabled"),
- classloader = Class.forName("org.apache.derby.jdbc.ClientDriver").getClassLoader)
+ "evolutionplugin" -> "disabled") ++ h.fold(Map[String, String]())(
+ handler {
+ val id = System.identityHashCode(this).toString
+ acolyte.Driver.register(id, handler)
+
+ Map("db.default.driver" -> "acolyte.Driver",
+ "db.default.url" -> "jdbc:acolyte:test?handler=%s".format(id))
+ }))
}
@@ -12,90 +12,93 @@ import scalaz.Scalaz._
import org.specs2.mutable.Specification
import play.api.Play.current
-
import play.api.cache.Cache
-
-import play.api.mvc.{ Cookie, Request, Results, SimpleResult }
-
+import play.api.mvc.{ AnyContent, Cookie, Request, Results, SimpleResult }
import play.api.test.{ FakeRequest, Helpers }
+import acolyte.Acolyte._
+import acolyte.RowLists.rowList2
+import acolyte.Rows.row2
+
import cielago.models.ListInfo
object MainSpec extends Specification with ControllerSpec {
- "Main controller specification".title
+ "Main controller".title
"Call to index action without cookie/session user digest" should {
- Helpers.running(fakeApp) {
- lazy val request = FakeRequest()
- lazy val plainResult: Future[SimpleResult] = Main.index(request)
+ beUnauthorized()
+ }
- "be unauthorized" in {
+ "Call to index action with invalid user digest as cookie" should {
+ beUnauthorized {
+ () FakeRequest().withCookies(Cookie("userDigest", "invalid"))
+ }
+ }
+
+ "Call to index action with valid user test1 digest as cookie" should {
+ lazy val handler = {
+ val noAccount = rowList2(
+ classOf[String] -> "list_id",
+ classOf[String] -> "account_name")
+
+ Some(handleStatement.
+ withQueryDetection("^SELECT").withQueryHandler({ e: acolyte.Execution
+ if (e.parameters(0).value == "31760a4f6e4e5edde51747a52f1a9628")
+ (noAccount :+ row2("listId", "test")).asResult
+ else noAccount.asResult
+ }))
+
+ }
+
+ Helpers.running(fakeApp(handler)) {
+ val digest = "userDigest%3A31760a4f6e4e5edde51747a52f1a9628"
+ val request = FakeRequest() withCookies {
+ Cookie("userDigest", "31760a4f6e4e5edde51747a52f1a9628")
+ }
+ val plainResult: Future[SimpleResult] = Main.index(request)
+
+ "be authorized" in {
plainResult.value aka "request" must beSome.which { r
r aka "result" must beLike {
case Success(res)
- implicit val app = fakeApp
-
- res.header aka "header" must_== Results.Unauthorized.header
+ (res.header.status.
+ aka("status") mustNotEqual Results.Unauthorized.header.status).
+ and(res.header.headers.get("Set-Cookie") must beSome.which(
+ _ aka "digest" must contain(digest)))
}
}
- }
- /*
- "and not have tracker authorization in cache" in {
- res.header.headers.get("Set-Cookie") must be like {
- case Some(digest) ⇒ digest must not contain "userDigest%3A"
- }
- }
- */
+ }
}
}
- "Call to index action with invalid user digest as cookie" should {
- Helpers.running(fakeApp) {
- lazy val request = FakeRequest() withCookies {
- Cookie("userDigest", "invalid")
- }
- lazy val plainResult: Future[SimpleResult] = Main.index(request)
+ def beUnauthorized(r: () Request[AnyContent] = () FakeRequest()) =
+ Helpers.running(fakeApp()) {
+ val request: Request[AnyContent] = r()
+ val plainResult: Future[SimpleResult] = Main.index(request)
"be unauthorized" in {
plainResult.value aka "request" must beSome.which { r
r aka "result" must beLike {
- case Success(res) res.header must_== Results.Unauthorized.header
+ case Success(res)
+ res aka "header" mustEqual Main.NoTrackerAvailable
}
}
}
- /*
- "and not have tracker authorization in cache" in {
- res.header.headers.get("Set-Cookie") must be like {
- case Some(digest) ⇒ digest must not contain "userDigest%3A"
- }
- }
- */
- }
- }
-
- "Call to index action with valid user test1 digest as cookie" should {
- Helpers.running(fakeApp) {
- implicit val app = current
- val digest = "userDigest%3A31760a4f6e4e5edde51747a52f1a9628"
- lazy val request = FakeRequest() withCookies {
- Cookie("userDigest", "31760a4f6e4e5edde51747a52f1a9628")
- }
- lazy val plainResult: Future[SimpleResult] = Main.index(request)
-
- "be authorized" in {
+ "… and not have tracker authorization in cache" in {
plainResult.value aka "request" must beSome.which { r
r aka "result" must beLike {
case Success(res)
- (res.header must not be Results.Unauthorized.header).
- and(res.header.headers.get("Set-Cookie") must beSome.which {
- d d aka "digest" must contain(digest)
- })
+ val cookie = res.header.headers.get("Set-Cookie")
+
+ (cookie aka "Set-Cookie" must beNone).
+ or(cookie aka "Set-Cookie" must beSome.which(
+ _ aka "digest" must not contain "userDigest%3A"))
+
}
}
-
}
}
- }
+
}
@@ -1,22 +1,117 @@
package cielago.models
-import scalaz.{ Failure, Success }
-
import org.specs2.mutable.Specification
-object DispatchReportSpec extends Specification {
- "Dispatch report specification" title
+object DispatchReportSpec extends Specification
+ with DispatchReportClauses with DispatchReportFixtures {
+
+ "Dispatch report" title
+
+ "Failure" should {
+ reportExample("occur if there is no row", TrackListSelector("list2")) {
+ (_, _, r) r must beLeft
+ }
+ }
+
+ "Report for period" should {
+ val start = new java.util.Date()
+ val end = new java.util.Date()
+ val sel = TrackPeriodSelector(start, end)
+ val expected = DispatchReport(2, 3)
+ lazy val clause = comparableSql(periodClause.
+ replace("{startTime}", "?").
+ replace("{endTime}", "?").
+ replace("{digest}", "?"))
+
+ reportExample("be exampled one", sel) { (sql, ps, r)
+ (comparableSql(sql) aka "SQL" must endWith(clause)).
+ and(ps.size aka "parameter count" mustEqual 3).
+ and(ps(0).value aka "start" mustEqual start.getTime).
+ and(ps(1).value aka "end" mustEqual end.getTime).
+ and(ps(2).value aka "digest" mustEqual "user").
+ and(r aka "result" must beRight(expected))
+ }
+ }
- "Dispatch report for list #1" should {
+ "Report for list #1" should {
val sel = TrackListSelector("list1")
- val expected = Success(DispatchReport(0, 0))
+ val expected = DispatchReport(1, 2)
+
+ reportExample("be expected one", sel) { (sql, ps, r)
+ (sql aka "SQL" must endWith(listClause.replace("{listId}", "?"))).
+ and(ps.size aka "parameter count" mustEqual 1).
+ and(ps(0).value aka "list" mustEqual "list1").
+ and(r aka "result" must beRight(expected))
+
+ }
+ }
+
+ "Report for list and period" should {
+ val start = new java.util.Date()
+ val end = new java.util.Date()
+ val sel = TrackPeriodListSelector(start, end, "list1")
+ val expected = DispatchReport(3, 4)
- lazy val report = Failure /*
- * TODO: inDerby { implicit con ⇒ DispatchReport.report(sel) }
- */
+ val clause = comparableSql(periodListClause.
+ replace("{startTime}", "?").
+ replace("{endTime}", "?").
+ replace("{listId}", "?"))
- "have expected message and recipient counts" in {
- report mustEqual expected
+ reportExample("be expected one", sel) { (sql, ps, r)
+ (comparableSql(sql) aka "SQL" must endWith(clause)).
+ and(ps.size aka "parameter count" mustEqual 3).
+ and(ps(0).value aka "start" mustEqual start.getTime).
+ and(ps(1).value aka "end" mustEqual end.getTime).
+ and(ps(2).value aka "list" mustEqual "list1").
+ and(r aka "result" must beRight(expected))
+
+ }
+ }
+}
+
+sealed trait DispatchReportFixtures { specs: Specification
+ import org.specs2.text.CodeMarkup
+ import org.specs2.specification.Example
+ import org.specs2.matcher.MatchResult
+ import acolyte.RowLists.rowList2
+ import acolyte.Rows.row2
+ import acolyte.Acolyte._
+ import acolyte.{ ExecutedParameter Param }
+
+ def comparableSql(s: String): String = "[ \t]{2,}".r.
+ replaceAllIn("[\r\n]+".r.replaceAllIn(s, " "), "")
+
+ val noReport = rowList2(
+ classOf[Int] -> "message_count",
+ classOf[Int] -> "recipient_count")
+
+ def reportExample(description: String, selector: TrackSelector)(f: (String, List[Param], Either[String, DispatchReport]) MatchResult[Any]): Example = {
+ var sql: String = null
+ var ps: List[Param] = Nil
+ val con = connection(handleStatement.withQueryDetection("^SELECT").
+ withQueryHandler({ e: acolyte.Execution
+ sql = e.sql; ps = e.parameters
+

This comment has been minimized.

Show comment
Hide comment
@cchantep

cchantep Apr 19, 2014

Owner

Describe cases to be handled using rules based on parameters.

@cchantep

cchantep Apr 19, 2014

Owner

Describe cases to be handled using rules based on parameters.

+ e.parameters match {
+ case Param(s, _) :: Param(e, _) :: Param("list1", _) :: Nil
+ (noReport :+ row2(3, 4)).asResult
+
+ case Param(s, _) :: Param(e, _) :: Param("user", _) :: Nil
+ (noReport :+ row2(2, 3)).asResult
+
+ case Param("list1", _) :: Nil
+ (noReport :+ row2(1, 2)).asResult
+
+ case _ noReport.asResult
+ }
+ }))
+
+ val res: Either[String, DispatchReport] = try {
+ Right(DispatchReport.report("user", selector)(con))
+ } catch {
+ case t: Throwable Left(t.getMessage)
}
+
+ exampleFactory.newExample(CodeMarkup(description), f(sql, ps, res))
}
}
Oops, something went wrong.

0 comments on commit a758541

Please sign in to comment.