Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

lift-couchdb

  • Loading branch information...
commit 026252a18fbafbc2fe6e01cca9835f9260b116eb 1 parent d8a6120
@nafg nafg authored
View
26 persistence/couchdb/src/test/scala/net/liftweb/couchdb/CouchDatabaseSpec.scala
@@ -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 {
@@ -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
@@ -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_== ()
}
}
}
View
26 persistence/couchdb/src/test/scala/net/liftweb/couchdb/CouchDocumentSpec.scala
@@ -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._
@@ -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_!
}
@@ -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 {
@@ -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
@@ -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 {
View
27 persistence/couchdb/src/test/scala/net/liftweb/couchdb/CouchQuerySpec.scala
@@ -21,42 +21,45 @@ 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")) ::
@@ -64,7 +67,7 @@ object CouchQuerySpec extends Specification("CouchQuery Specification") {
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]) = {
View
86 persistence/couchdb/src/test/scala/net/liftweb/couchdb/CouchRecordSpec.scala
@@ -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._
@@ -30,30 +30,33 @@ import DocumentHelpers.stripIdAndRev
package couchtestrecords {
class Person private () extends CouchRecord[Person] {
def meta = Person
-
+
object name extends StringField(this, 200)
object age extends IntField(this)
}
-
+
object Person extends Person with CouchMetaRecord[Person]
-
+
class Company private () extends CouchRecord[Company] {
def meta = Company
-
+
object name extends StringField(this, 200)
- }
-
+ }
+
object Company extends Company with CouchMetaRecord[Company]
}
/**
* Systems under specification for CouchRecord.
*/
-object CouchRecordSpec extends Specification("CouchRecord Specification") {
+object CouchRecordSpec extends Specification {
+ "CouchRecord Specification".title
+ sequential
+
import CouchDB.defaultDatabase
import couchtestrecords._
-
- val design: JObject =
+
+ val design: JObject =
("language" -> "javascript") ~
("views" -> (("people_by_age" -> ("map" -> "function(doc) { if (doc.type == 'Person') { emit(doc.age, doc); } }")) ~
("oldest" -> (("map" -> "function(doc) { if (doc.type == 'Person') { emit(doc.name, doc.age); } }") ~
@@ -61,7 +64,7 @@ object CouchRecordSpec extends Specification("CouchRecord Specification") {
def setup = {
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.design("test") put design)
defaultDatabase = database
@@ -80,7 +83,7 @@ object CouchRecordSpec extends Specification("CouchRecord Specification") {
assertEqualPerson(found, expected)
}
}
-
+
"A couch record" should {
def testRec1: Person = Person.createRecord.name("Alice").age(25)
val testDoc1: JObject = ("age" -> 25) ~ ("name" -> "Alice") ~ ("type" -> "Person")
@@ -90,26 +93,25 @@ object CouchRecordSpec extends Specification("CouchRecord Specification") {
"give emtpy box on get when nonexistant" in {
setup must_== ()
-
- Person.fetch("testdoc") must verify (!_.isDefined)
+
+ Person.fetch("testdoc").isDefined must_== false
}
"be insertable" in {
setup
-
+
val newRec = testRec1
newRec save
assertEqualPerson(newRec, testRec1)
newRec.saved_? must_== true
- newRec.id.valueBox must verify (_.isDefined)
- newRec.rev.valueBox must verify (_.isDefined)
+ newRec.id.valueBox.isDefined must_== true
+ newRec.rev.valueBox.isDefined must_== true
val Full(foundRec) = Person.fetch(newRec.id.valueBox.open_!)
assertEqualPerson(foundRec, testRec1)
foundRec.id.valueBox must_== newRec.id.valueBox
foundRec.rev.valueBox must_== newRec.rev.valueBox
-
}
"generate the right JSON" in {
@@ -126,17 +128,17 @@ object CouchRecordSpec extends Specification("CouchRecord Specification") {
val newRec = testRec1
newRec.save
- newRec.id.valueBox must verify(_.isDefined)
+ newRec.id.valueBox.isDefined must_== true
val id = newRec.id.valueBox.open_!
- Person.fetch(id) must verify(_.isDefined)
- newRec.delete_! must verify(_.isDefined)
- Person.fetch(id) must not(verify(_.isDefined))
- newRec.delete_! must not(verify(_.isDefined))
+ Person.fetch(id).isDefined must_== true
+ newRec.delete_!.isDefined must_== true
+ Person.fetch(id).isDefined must_== false
+ newRec.delete_!.isDefined must_== false
newRec.save
Http(defaultDatabase(newRec.id.valueBox.open_!) @@ newRec.rev.valueBox.open_! delete)
- newRec.delete_! must not(verify(_.isDefined))
+ newRec.delete_!.isDefined must_== false
}
"be fetchable in bulk" in {
@@ -154,7 +156,7 @@ object CouchRecordSpec extends Specification("CouchRecord Specification") {
val expectedRows = newRec1::newRec3::Nil
Person.fetchMany(newRec1.id.valueBox.open_!, newRec3.id.valueBox.open_!).map(_.toList) must beLike {
- case Full(foundRows) => assertEqualRows(foundRows, expectedRows); true
+ case Full(foundRows) => assertEqualRows(foundRows, expectedRows); 1 must_== 1
}
}
@@ -174,7 +176,7 @@ object CouchRecordSpec extends Specification("CouchRecord Specification") {
val expectedRows = newRec2::Nil
Person.queryView("test", "people_by_age", _.key(JInt(30))) must beLike {
- case Full(foundRows) => assertEqualRows(foundRows, expectedRows); true
+ case Full(foundRows) => assertEqualRows(foundRows, expectedRows); 1 must_== 1
}
}
@@ -192,7 +194,7 @@ object CouchRecordSpec extends Specification("CouchRecord Specification") {
val expectedRows = newRec1::newRec2::Nil
Person.queryViewDocs("test", "oldest", _.dontReduce) must beLike {
- case Full(foundRows) => assertEqualRows(foundRows, expectedRows); true
+ case Full(foundRows) => assertEqualRows(foundRows, expectedRows); 1 must_== 1
}
}
@@ -210,14 +212,14 @@ object CouchRecordSpec extends Specification("CouchRecord Specification") {
val expectedRows = newRec1::newRec2::Nil
Person.queryViewDocs("test", "people_by_age", identity) must beLike {
- case Full(foundRows) => assertEqualRows(foundRows, expectedRows); true
+ case Full(foundRows) => assertEqualRows(foundRows, expectedRows); 1 must_== 1
}
}
"support multiple databases for fetching" in {
setup
val database2 = new Database("test2")
- (try { Http(database2 delete) } catch { case StatusCode(_, _) => () }) must not(throwAnException[ConnectException]).orSkipExample
+ (try { Http(database2 delete) } catch { case StatusCode(_, _) => () }) must not(throwA[ConnectException]).orSkip
Http(database2 create)
val newRec = testRec1
@@ -227,19 +229,19 @@ object CouchRecordSpec extends Specification("CouchRecord Specification") {
newRec.saved_? must_== true
val foundRecBox = Person.fetchFrom(database2, newRec.id.valueBox.open_!)
- foundRecBox must verify(_.isDefined)
+ foundRecBox.isDefined must_== true
val Full(foundRec) = foundRecBox
assertEqualPerson(foundRec, testRec1)
foundRec.id.valueBox must_== newRec.id.valueBox
foundRec.rev.valueBox must_== newRec.rev.valueBox
-
- Person.fetch(newRec.id.valueBox.open_!) must not(verify(_.isDefined))
+
+ Person.fetch(newRec.id.valueBox.open_!).isDefined must_== false
}
"support multiple databases for fetching in bulk" in {
setup
val database2 = new Database("test2")
- (try { Http(database2 delete) } catch { case StatusCode(_, _) => () }) must not(throwAnException[ConnectException]).orSkipExample
+ (try { Http(database2 delete) } catch { case StatusCode(_, _) => () }) must not(throwA[ConnectException]).orSkip
Http(database2 create)
val newRec1, newRec2, newRec3 = testRec1
@@ -257,16 +259,18 @@ object CouchRecordSpec extends Specification("CouchRecord Specification") {
val expectedRows = newRec1::newRec3::Nil
Person.fetchManyFrom(database2, newRec1.id.valueBox.open_!, newRec3.id.valueBox.open_!).map(_.toList) must beLike {
- case Full(foundRows) => assertEqualRows(foundRows, expectedRows); true
+ case Full(foundRows) => assertEqualRows(foundRows, expectedRows); 1 must_== 1
}
- Person.fetchMany(newRec1.id.valueBox.open_!, newRec3.id.valueBox.open_!) must beLike { case Full(seq) if seq.isEmpty => true }
+ Person.fetchMany(newRec1.id.valueBox.open_!, newRec3.id.valueBox.open_!) must beLike {
+ case Full(seq) => seq.isEmpty must_== true
+ }
}
"support multiple databases for queries" in {
setup
val database2 = new Database("test2")
- (try { Http(database2 delete) } catch { case StatusCode(_, _) => () }) must not(throwAnException[ConnectException]).orSkipExample
+ (try { Http(database2 delete) } catch { case StatusCode(_, _) => () }) must not(throwA[ConnectException]).orSkip
Http(database2 create)
Http(database2.design("test") put design)
@@ -286,16 +290,16 @@ object CouchRecordSpec extends Specification("CouchRecord Specification") {
val expectedRows = newRec2::Nil
Person.queryViewFrom(database2, "test", "people_by_age", _.key(JInt(30))) must beLike {
- case Full(foundRows) => assertEqualRows(foundRows, expectedRows); true
+ case Full(foundRows) => assertEqualRows(foundRows, expectedRows); 1 must_== 1
}
- Person.queryView("test", "people_by_age", _.key(JInt(30))) must beLike { case Full(seq) if seq.isEmpty => true }
+ Person.queryView("test", "people_by_age", _.key(JInt(30))) must beLike { case Full(seq) => seq.isEmpty must_== true }
}
"support multiple databases for queries returning documents" in {
setup
val database2 = new Database("test2")
- (try { Http(database2 delete) } catch { case StatusCode(_, _) => () }) must not(throwAnException[ConnectException]).orSkipExample
+ (try { Http(database2 delete) } catch { case StatusCode(_, _) => () }) must not(throwA[ConnectException]).orSkip
Http(database2 create)
Http(database2.design("test") put design)
@@ -312,10 +316,10 @@ object CouchRecordSpec extends Specification("CouchRecord Specification") {
val expectedRows = newRec1::newRec2::Nil
Person.queryViewDocsFrom(database2, "test", "oldest", _.dontReduce) must beLike {
- case Full(foundRows) => assertEqualRows(foundRows, expectedRows); true
+ case Full(foundRows) => assertEqualRows(foundRows, expectedRows); 1 must_== 1
}
- Person.queryViewDocs("test", "oldest", _.dontReduce) must beLike { case Full(seq) if seq.isEmpty => true }
+ Person.queryViewDocs("test", "oldest", _.dontReduce) must beLike { case Full(seq) => seq.isEmpty must_== true }
}
}
}
View
38 persistence/couchdb/src/test/scala/net/liftweb/couchdb/JsonRecordSpec.scala
@@ -17,7 +17,8 @@
package net.liftweb
package couchdb
-import org.specs.Specification
+import org.specs2.mutable.Specification
+import org.specs2.execute.Result
import common._
import json._
@@ -37,7 +38,6 @@ package jsontestrecords {
object favoriteColor extends OptionalStringField(this, 200)
object address extends JSONSubRecordField(this, Address, Empty) {
override def optional_? = true
-
}
}
@@ -61,10 +61,13 @@ package jsontestrecords {
/**
* Systems under specification for JsonRecord.
*/
-object JsonRecordSpec extends Specification("JsonRecord Specification") {
+object JsonRecordSpec extends Specification {
+ "JsonRecord Specification".title
+ sequential
+
import jsontestrecords._
- def assertEqualPerson(a: Person, b: Person) = {
+ def assertEqualPerson(a: Person, b: Person): Result = {
a.name.valueBox must_== b.name.valueBox
a.age.valueBox must_== b.age.valueBox
a.favoriteColor.valueBox must_== b.favoriteColor.valueBox
@@ -74,7 +77,8 @@ object JsonRecordSpec extends Specification("JsonRecord Specification") {
aa.postalCode.valueBox must_== aa.postalCode.valueBox
aa.city.valueBox must_== aa.city.valueBox
aa.street.valueBox must_== aa.street.valueBox
- }
+ }
+ success
}
"A JSON record" should {
@@ -94,19 +98,19 @@ object JsonRecordSpec extends Specification("JsonRecord Specification") {
"encode record with subrecord correctly" in {
compact(render(testRec4.asJValue)) must_== compact(render(testDoc4))
}
-
+
"decode basic records correctly" in {
val recBox = Person.fromJValue(testDoc1)
- recBox must verify (_.isDefined)
+ recBox.isDefined must_== true
val Full(rec) = recBox
assertEqualPerson(rec, testRec1)
}
"preserve extra fields from JSON" in {
val recBox = Person.fromJValue(testDoc2)
- recBox must verify (_.isDefined)
+ recBox.isDefined must_== true
val Full(rec) = recBox
- rec.additionalJFields must_== List(JField("extra1", JString("value1")),
+ rec.additionalJFields must_== List(JField("extra1", JString("value1")),
JField("extra2", JString("value2")))
rec.age.set(1)
@@ -115,15 +119,15 @@ object JsonRecordSpec extends Specification("JsonRecord Specification") {
"support unset optional fields" in {
val recBox = Person.fromJValue(testDoc1)
- recBox must verify (_.isDefined)
+ recBox.isDefined must_== true
val Full(rec) = recBox
- rec.favoriteColor.value must not (verify (_.isDefined))
+ rec.favoriteColor.value.isDefined must_== false
}
"support set optional fields" in {
val recBox = Person.fromJValue(testDoc2)
- recBox must verify (_.isDefined)
+ recBox.isDefined must_== true
val Full(rec) = recBox
rec.favoriteColor.value must_== Some("blue")
@@ -131,7 +135,7 @@ object JsonRecordSpec extends Specification("JsonRecord Specification") {
"support set subRecord field" in {
val recBox = Person.fromJValue(testDoc4)
- recBox must verify (_.isDefined)
+ recBox.isDefined must_== true
val Full(rec) = recBox
rec.address.valueBox.flatMap(_.street.valueBox) must_== Full("my street")
@@ -146,22 +150,22 @@ object JsonRecordSpec extends Specification("JsonRecord Specification") {
"honor overrideIgnoreExtraJSONFields == true" in {
val recBox = JSONMetaRecord.overrideIgnoreExtraJSONFields.doWith(true) { Person.fromJValue(testDoc2) }
- recBox must verify (_.isDefined)
+ recBox.isDefined must_== true
}
"honor overrideIgnoreExtraJSONFields == false" in {
val recBox = JSONMetaRecord.overrideIgnoreExtraJSONFields.doWith(false) { Person.fromJValue(testDoc2) }
- recBox must not (verify (_.isDefined))
+ recBox.isDefined must_== false
}
"honor overrideNeedAllJSONFields == true" in {
val recBox = JSONMetaRecord.overrideNeedAllJSONFields.doWith(true) { Person.fromJValue(testDoc3) }
- recBox must not (verify (_.isDefined))
+ recBox.isDefined must_== false
}
"honor overrideNeedAllJSONFields == false" in {
val recBox = JSONMetaRecord.overrideNeedAllJSONFields.doWith(false) { Person.fromJValue(testDoc3) }
- recBox must verify (_.isDefined)
+ recBox.isDefined must_== true
}
}
}

1 comment on commit 026252a

@fmpwizard
Owner

+1

Please sign in to comment.
Something went wrong with that request. Please try again.