Permalink
Browse files

[reactivemongo] Pattern matching for request body with multiple document

  • Loading branch information...
cchantep
cchantep committed Sep 26, 2014
1 parent f5e58c0 commit c94cd5883bbef746273c5245885d5e7d22e2e8a3
View
@@ -275,6 +275,13 @@ val queryHandler = QueryHandler { queryRequest =>
case Request("col1", SimpleBody(("$in", ValueList(bsonA, bsonB)) :: Nil)) =>
// Matching BSONArray using with $in operator
resultM
+
+ case Request(_, RequestBody(List(("sel", BSONString("hector"))) ::
+ List(("updated", BSONString("property"))) :: Nil))
+ // Matches a request with multiple document in body
+ // (e.g. update with selector)
+ resultN
+
}
}
```
@@ -69,8 +69,6 @@ private[reactivemongo] class Actor(
case msg @ CheckedWriteRequestExResp(
r @ CheckedWriteRequest(op, doc, GetLastError(_, _, _, _)))
- println(s"buf = ${new String(doc.merged.array)}")
-
val req = Request(op.fullCollectionName, doc.merged)
val exp = new ExpectingResponse(msg)
val cid = r()._1.channelIdHint getOrElse 1
@@ -14,14 +14,10 @@ import reactivemongo.bson.buffer.{
*/
trait Request {
- /**
- * Fully qualified name of collection
- */
+ /** Fully qualified name of collection */
def collection: String
- /**
- * Request body (BSON statement)
- */
+ /** Request body (BSON statement) */
def body: List[BSONDocument]
override lazy val toString = s"Request($collection, $body)"
@@ -170,19 +170,21 @@ object DriverSpec extends org.specs2.mutable.Specification
} aka "work with collection" must beEqualTo(7).await(5)
}
}
+ }
+ "Driver" should {
"return expected query result" >> {
"when is successful #1" in {
awaitRes(AcolyteDSL.withFlatCollection(chandler1, query1.collection) {
- col col.find(query1.body).cursor[BSONDocument].toList()
+ col col.find(query1.body.head).cursor[BSONDocument].toList()
}) aka "query result" must beSuccessfulTry[List[BSONDocument]].like {
case ValueDocument(("b", BSONInteger(3)) :: Nil) :: Nil ok
}
}
"when is successful #2" in {
awaitRes(AcolyteDSL.withFlatDB(chandler1) { db
- db(query2.collection).find(query2.body).
+ db(query2.collection).find(query2.body.head).
cursor[BSONDocument].toList()
}) aka ("query result") must beSuccessfulTry[List[BSONDocument]].like {
case ValueDocument(("d", BSONDouble(4.56d)) :: Nil) ::
@@ -196,7 +198,8 @@ object DriverSpec extends org.specs2.mutable.Specification
BSONDocument("res" -> "ult", "n" -> 3)) { driver
AcolyteDSL.withFlatConnection(driver) { con
val db = con("anyDb")
- db("anyCol").find(query1.body).cursor[BSONDocument].toList()
+ db("anyCol").find(query1.body.head).
+ cursor[BSONDocument].toList()
}
}) aka "query result" must beSuccessfulTry[List[BSONDocument]].
like {
@@ -209,7 +212,7 @@ object DriverSpec extends org.specs2.mutable.Specification
awaitRes(AcolyteDSL.withFlatQueryResult(
List(BSONDocument("doc" -> 1), BSONDocument("doc" -> 2.3d))) { d
AcolyteDSL.withFlatCollection(d, "anyCol") {
- _.find(query1.body).cursor[BSONDocument].toList()
+ _.find(query1.body.head).cursor[BSONDocument].toList()
}
}) aka "query result" must beSuccessfulTry[List[BSONDocument]].
like {
@@ -221,7 +224,7 @@ object DriverSpec extends org.specs2.mutable.Specification
"for an explicit error" in {
awaitRes(AcolyteDSL.withFlatQueryResult("Error" -> 7) { driver
AcolyteDSL.withFlatCollection(driver, query1.collection) {
- _.find(query1.body).cursor[BSONDocument].toList()
+ _.find(query1.body.head).cursor[BSONDocument].toList()
}
}) aka "query result" must beFailedTry.
withThrowable[DetailedDatabaseException](".*Error.*code = 7.*")
@@ -230,7 +233,7 @@ object DriverSpec extends org.specs2.mutable.Specification
"when undefined" in {
awaitRes(AcolyteDSL.withFlatQueryResult(None) { driver
AcolyteDSL.withFlatCollection(driver, query1.collection) {
- _.find(query1.body).cursor[BSONDocument].toList()
+ _.find(query1.body.head).cursor[BSONDocument].toList()
}
}) aka "query result" must beFailedTry.
withThrowable[DetailedDatabaseException](".*No response:.*")
@@ -241,7 +244,7 @@ object DriverSpec extends org.specs2.mutable.Specification
awaitRes(AcolyteDSL.withFlatQueryHandler(
{ _: Request QueryResponse.empty }) { d
AcolyteDSL.withFlatCollection(d, query3.collection) {
- _.find(query3.body).cursor[BSONDocument].toList()
+ _.find(query3.body.head).cursor[BSONDocument].toList()
}
}) aka "query result" must beSuccessfulTry.like {
case res if res.isEmpty ok
@@ -251,7 +254,7 @@ object DriverSpec extends org.specs2.mutable.Specification
"as error when connection handler is empty" in {
awaitRes(AcolyteDSL.withFlatCollection(AcolyteDSL.handle,
query3.collection) {
- _.find(query3.body).cursor[BSONDocument].toList()
+ _.find(query3.body.head).cursor[BSONDocument].toList()
}) aka "query result" must beFailedTry.
withThrowable[DetailedDatabaseException](".*No response: .*")
}
@@ -263,7 +266,7 @@ object DriverSpec extends org.specs2.mutable.Specification
awaitRes(AcolyteDSL.withFlatDriver(handler) { driver
AcolyteDSL.withFlatConnection(driver) { con
val db = con("anyDb")
- db(query3.collection).find(query3.body).
+ db(query3.collection).find(query3.body.head).
cursor[BSONDocument].toList()
}
}) aka "query result" must beFailedTry.
@@ -275,14 +278,14 @@ object DriverSpec extends org.specs2.mutable.Specification
"return expected write result" >> {
"when error is raised without code" in {
awaitRes(AcolyteDSL.withFlatCollection(
- chandler1, write1._2.collection) { _.remove(write1._2.body) }).
+ chandler1, write1._2.collection) { _.remove(write1._2.body.head) }).
aka("write result") must beFailedTry.
withThrowable[LastError](".*Error #2.*code = -1.*")
}
"when successful" in {
awaitRes(AcolyteDSL.withFlatDB(chandler1) {
- _(write2._2.collection).insert(write2._2.body)
+ _(write2._2.collection).insert(write2._2.body.head)
}) aka "result" must beSuccessfulTry.like {
case lastError
lastError.elements.toList aka "body" must beLike {
@@ -302,7 +305,7 @@ object DriverSpec extends org.specs2.mutable.Specification
val db = con("anyDb")
val col = db(write3._2.collection)
- col.update(BSONDocument("name" -> "x"), write3._2.body)
+ col.update(BSONDocument("name" -> "x"), write3._2.body.head)
}) aka "result" must beFailedTry.
withThrowable[LastError](".*No response: .*")
}
@@ -312,7 +315,7 @@ object DriverSpec extends org.specs2.mutable.Specification
WriteResponse.undefined
}) { d
AcolyteDSL.withFlatCollection(d, query3.collection) {
- _.update(BSONDocument("name" -> "x"), write3._2.body)
+ _.update(BSONDocument("name" -> "x"), write3._2.body.head)
}
}) aka "result" must beFailedTry.
withThrowable[LastError](".*No response: .*")
@@ -322,7 +325,7 @@ object DriverSpec extends org.specs2.mutable.Specification
awaitRes(AcolyteDSL.withFlatQueryResult(BSONDocument("prop" -> "A")) {
driver
AcolyteDSL.withFlatCollection(driver, write3._2.collection) {
- _.update(BSONDocument("name" -> "x"), write3._2.body)
+ _.update(BSONDocument("name" -> "x"), write3._2.body.head)
}
}) aka "result" must beFailedTry.
withThrowable[LastError](".*No response: .*")
@@ -334,7 +337,7 @@ object DriverSpec extends org.specs2.mutable.Specification
AcolyteDSL.withFlatConnection(driver) { con
val db = con("anyDb")
val col = db(write1._2.collection)
- col.remove(write1._2.body)
+ col.remove(write1._2.body.head)
}
}) aka "write result" must beSuccessfulTry.like {
case lastError
@@ -347,7 +350,7 @@ object DriverSpec extends org.specs2.mutable.Specification
"for explicit error" in {
awaitRes(AcolyteDSL.withFlatWriteResult("Write err" -> 9) { driver
AcolyteDSL.withFlatCollection(driver, write2._2.collection) {
- _.insert(write2._2.body)
+ _.insert(write2._2.body.head)
}
}) aka "write result" must beFailedTry.
withThrowable[LastError](".*Write err.*code = 9.*")
@@ -356,12 +359,25 @@ object DriverSpec extends org.specs2.mutable.Specification
"when undefined" in {
awaitRes(AcolyteDSL.withFlatWriteResult(None) { driver
AcolyteDSL.withFlatCollection(driver, write3._2.collection) {
- _.update(BSONDocument(), write3._2.body)
+ _.update(BSONDocument(), write3._2.body.head)
}
}) aka "write result" must beFailedTry.
withThrowable[LastError](".*No response.*")
}
}
+
+ "using withWriteHandler" in {
+ awaitRes(AcolyteDSL.withFlatWriteHandler({
+ case (UpdateOp, Request(_, RequestBody(
+ List(("sel", BSONString("hector"))) ::
+ List(("filter", BSONString("valC"))) :: Nil)))
+ WriteResponse(1)
+ }) { driver
+ AcolyteDSL.withFlatCollection(driver, "col") {
+ _.update(BSONDocument("sel" -> "hector"), write3._2.body.head)
+ }
+ }) aka "write result" must beSuccessfulTry[LastError]
+ }
}
}
@@ -338,6 +338,15 @@ object RequestSpec extends org.specs2.mutable.Specification
}
}
}
+
+ "Multiple document body" should {
+ "be extracted" in {
+ update1 aka "update request" must beLike {
+ case Request(_, RequestBody(List(("sel", BSONString("hector"))) ::
+ List(("updated", BSONString("property"))) :: Nil)) ok
+ }
+ }
+ }
}
sealed trait RequestFixtures {
@@ -362,4 +371,10 @@ sealed trait RequestFixtures {
val body = List(BSONDocument("count" -> "col3",
"query" -> BSONDocument("fil" -> "ter")))
}
+
+ val update1 = new Request {
+ val collection = "db1.col4"
+ val body = List(BSONDocument("sel" -> "hector"),
+ BSONDocument("updated" -> "property"))
+ }
}

0 comments on commit c94cd58

Please sign in to comment.