Skip to content

Commit

Permalink
lift-couchdb
Browse files Browse the repository at this point in the history
  • Loading branch information
Naftoli Gugenheim committed Jul 31, 2012
1 parent d8a6120 commit 026252a
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,39 @@ import java.net.ConnectException

import dispatch.{Http, StatusCode}

import org.specs.Specification
import org.specs2.mutable.Specification


/**
* Systems under specification for CouchDatabase.
*/
object CouchDatabaseSpec extends Specification("CouchDatabase Specification") {
object CouchDatabaseSpec extends Specification {
"CouchDatabase Specification".title
sequential

def setup = {
val http = new Http
val database = new Database("test")
(try { http(database delete) } catch { case StatusCode(_, _) => () }) must not(throwAnException[ConnectException]).orSkipExample
(try { http(database delete) } catch { case StatusCode(_, _) => () }) must not(throwA[ConnectException]).orSkip

(http, database)
}

def hasCode(i: Int): PartialFunction[Throwable, org.specs2.matcher.MatchResult[Any]] =
{ case StatusCode(c, _) => c must_== i }

"A database" should {
"give 404 when info called and nonexistant" in {
setup
val (http, database) = setup

http(database info) must throwAnException[StatusCode].like { case StatusCode(404, _) => true }
http(database info) must throwA[StatusCode].like(hasCode(404))
}

"give 404 when deleted but nonexistant" in {
val (http, database) = setup

http(database delete) must throwAnException[StatusCode].like { case StatusCode(404, _) => true }
http(database delete) must throwA[StatusCode].like(hasCode(404))
}

"succeed being created" in {
Expand All @@ -57,9 +63,9 @@ object CouchDatabaseSpec extends Specification("CouchDatabase Specification") {
val (http, database) = setup

http(database create) must_== ()
http(database create) must throwAnException[StatusCode].like { case StatusCode(412, _) => true }
http(database create) must throwA[StatusCode].like(hasCode(412))
}

"have info when created" in {
val (http, database) = setup

Expand All @@ -70,16 +76,16 @@ object CouchDatabaseSpec extends Specification("CouchDatabase Specification") {
"succeed in being deleted" in {
val (http, database) = setup

http(database create) must_== ()
http(database create) must_== ()
http(database delete) must_== ()
}

"succeed being recreated" in {
val (http, database) = setup

http(database create) must_== ()
http(database create) must_== ()
http(database delete) must_== ()
http(database create) must_== ()
http(database create) must_== ()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import java.net.ConnectException

import dispatch.{Http, StatusCode}

import org.specs.Specification
import org.specs2.mutable.Specification

import common._
import json._
Expand All @@ -29,18 +29,24 @@ import DocumentHelpers._
/**
* Systems under specification for CouchDocument.
*/
object CouchDocumentSpec extends Specification("CouchDocument Specification") {
object CouchDocumentSpec extends Specification {
"CouchDocument Specification".title
sequential

def hasCode(i: Int): PartialFunction[Throwable, org.specs2.matcher.MatchResult[Any]] =
{ case StatusCode(c, _) => c must_== i }

def setup = {
val http = new Http
val database = new Database("test")
(try { http(database delete) } catch { case StatusCode(_, _) => () }) must not(throwAnException[ConnectException]).orSkipExample
(try { http(database delete) } catch { case StatusCode(_, _) => () }) must not(throwA[ConnectException]).orSkip
http(database create)

(http, database)
}

private final def verifyAndOpen[A](b: Box[A]): A = {
b must verify(_.isDefined)
b.isDefined must_== true
b.open_!
}

Expand All @@ -51,7 +57,7 @@ object CouchDocumentSpec extends Specification("CouchDocument Specification") {
"give 404 on get when nonexistant" in {
val (http, database) = setup

http(database("testdoc") fetch) must throwAnException[StatusCode].like { case StatusCode(404, _) => true }
http(database("testdoc") fetch) must throwA[StatusCode].like(hasCode(404))
}

"be insertable" in {
Expand All @@ -69,7 +75,7 @@ object CouchDocumentSpec extends Specification("CouchDocument Specification") {
val (http, database) = setup

val firstDocBox = http(database post testDoc1)
firstDocBox must verify(_.isDefined)
firstDocBox.isDefined must_== true
val Full(firstDoc) = firstDocBox
val Full(id) = firstDoc._id
val Full(rev) = firstDoc._rev
Expand All @@ -87,16 +93,16 @@ object CouchDocumentSpec extends Specification("CouchDocument Specification") {
val (http, database) = setup

val newDoc = verifyAndOpen(http(database store testDoc1))
http(database(newDoc) @@ newDoc delete) must be ()
http(database(newDoc) fetch) must throwAnException[StatusCode].like { case StatusCode(404, _) => true }
http(database(newDoc) @@ newDoc delete) must_== ()
http(database(newDoc) fetch) must throwA[StatusCode].like(hasCode(404))
}

"give 404 on delete when nonexistant" in {
val (http, database) = setup

val newDoc = verifyAndOpen(http(database store testDoc1))
http(database(newDoc) @@ newDoc delete) must be ()
http(database(newDoc) @@ newDoc delete) must throwAnException[StatusCode].like { case StatusCode(404, _) => true }
http(database(newDoc) @@ newDoc delete) must_== ()
http(database(newDoc) @@ newDoc delete) must throwA[StatusCode].like(hasCode(404))
}

"be force storable" in {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,50 +21,53 @@ import dispatch.{Http, StatusCode}
import common._
import json._
import JsonDSL._
import org.specs.Specification
import org.specs2.mutable.Specification
import DocumentHelpers.jobjectToJObjectExtension


/**
* Systems under specification for CouchQuery.
*/
object CouchQuerySpec extends Specification("CouchQuery Specification") {
object CouchQuerySpec extends Specification {
"CouchQuery Specification".title
sequential

def setup = {
val http = new Http
val database = new Database("test")
(try { http(database delete) } catch { case StatusCode(_, _) => () }) must not(throwAnException[ConnectException]).orSkipExample
(try { http(database delete) } catch { case StatusCode(_, _) => () }) must not(throwA[ConnectException]).orSkip
http(database create)

(http, database)
}

private def verifyAndOpen[A](b: Box[A]): A = {
b must verify(_.isDefined)
b.isDefined must_== true
b.open_!
}

"Queries" should {
val design: JObject =
val design: JObject =
("language" -> "javascript") ~
("views" -> (("all_students" -> ("map" -> "function(doc) { if (doc.type == 'student') { emit(null, doc); } }")) ~
("students_by_age" -> ("map" -> "function(doc) { if (doc.type == 'student') { emit(doc.age, doc); } }")) ~
("students_by_age_and_name" -> ("map" -> "function(doc) { if (doc.type == 'student') { emit([doc.age, doc.name], doc); } }"))))

val docs: List[JObject] =
(("type" -> "student") ~ ("name" -> "Alice") ~ ("age" -> 10)) ::
(("type" -> "student") ~ ("name" -> "Bob") ~ ("age" -> 11)) ::
(("type" -> "student") ~ ("name" -> "Charlie") ~ ("age" -> 11)) ::
(("type" -> "student") ~ ("name" -> "Donna") ~ ("age" -> 12)) ::
(("type" -> "student") ~ ("name" -> "Eric") ~ ("age" -> 12)) ::
(("type" -> "student") ~ ("name" -> "Fran") ~ ("age" -> 13)) ::
(("type" -> "student") ~ ("name" -> "Alice") ~ ("age" -> 10)) ::
(("type" -> "student") ~ ("name" -> "Bob") ~ ("age" -> 11)) ::
(("type" -> "student") ~ ("name" -> "Charlie") ~ ("age" -> 11)) ::
(("type" -> "student") ~ ("name" -> "Donna") ~ ("age" -> 12)) ::
(("type" -> "student") ~ ("name" -> "Eric") ~ ("age" -> 12)) ::
(("type" -> "student") ~ ("name" -> "Fran") ~ ("age" -> 13)) ::
(("type" -> "class") ~ ("name" -> "Astronomy")) ::
(("type" -> "class") ~ ("name" -> "Baking")) ::
(("type" -> "class") ~ ("name" -> "Chemistry")) ::
Nil

def findStudents(docs: List[JObject]): List[JObject] = docs.filter(_.isA("student"))

def compareName(a: JObject, b: JObject): Boolean =
def compareName(a: JObject, b: JObject): Boolean =
(a.getString("name") openOr "design") < (b.getString("name") openOr "design")

def prep(http: Http, database: Database): (JObject, List[JObject]) = {
Expand Down
Loading

1 comment on commit 026252a

@fmpwizard
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

Please sign in to comment.