-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Increase IT coverage for queries (#40)
* Increase IT coverage for queries * Add IT for updates * Add test for $elemMatch * Fix $elemMatch documentation --------- Co-authored-by: denis_savitsky <[email protected]>
- Loading branch information
1 parent
53ce389
commit 07a46f9
Showing
5 changed files
with
226 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
oolong-mongo-it/src/test/scala/oolong/mongo/OolongMongoUpdateSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
package oolong.mongo | ||
|
||
import scala.concurrent.Await | ||
import scala.concurrent.ExecutionContext | ||
|
||
import com.dimafeng.testcontainers.ForAllTestContainer | ||
import com.dimafeng.testcontainers.MongoDBContainer | ||
import concurrent.duration.DurationInt | ||
import oolong.dsl.* | ||
import org.mongodb.scala.MongoClient | ||
import org.mongodb.scala.bson.BsonDocument | ||
import org.mongodb.scala.model.UpdateOptions | ||
import org.scalatest.BeforeAndAfterAll | ||
import org.scalatest.flatspec.AsyncFlatSpec | ||
|
||
class OolongMongoUpdateSpec extends AsyncFlatSpec with ForAllTestContainer with BeforeAndAfterAll { | ||
|
||
override val container: MongoDBContainer = MongoDBContainer() | ||
container.start() | ||
|
||
val client = MongoClient(container.replicaSetUrl) | ||
val collection = client.getDatabase("test").getCollection[BsonDocument]("testCollection") | ||
|
||
override def beforeAll(): Unit = { | ||
val documents = List( | ||
TestClass("0", 0, InnerClass("sdf", 2.0), List(1, 2), None, List.empty), | ||
TestClass("1", 1, InnerClass("qwe", 3), Nil, Some(2L), List.empty), | ||
TestClass("2", 2, InnerClass("asd", 0), Nil, None, List.empty), | ||
TestClass("3", 12, InnerClass("sdf", 1), Nil, None, List.empty), | ||
TestClass("12345", 12, InnerClass("sdf", 11), Nil, None, List.empty), | ||
) | ||
|
||
implicit val ec = ExecutionContext.global | ||
|
||
val f = for { | ||
_ <- client.getDatabase("test").createCollection("testCollection").head() | ||
_ <- collection.insertMany(documents.map(_.bson.asDocument())).head() | ||
} yield () | ||
|
||
Await.result(f, 30.seconds) | ||
} | ||
|
||
it should "update with $inc" in { | ||
for { | ||
res <- collection | ||
.updateOne( | ||
query[TestClass](_.field1 == "0"), | ||
update[TestClass](_.inc(_.field2, 1)) | ||
) | ||
.head() | ||
} yield assert(res.wasAcknowledged() && res.getMatchedCount == 1 && res.getModifiedCount == 1) | ||
} | ||
|
||
it should "update with $min" in { | ||
for { | ||
res <- collection | ||
.updateOne( | ||
query[TestClass](_.field1 == "3"), | ||
update[TestClass](_.min(_.field2, 1)) | ||
) | ||
.head() | ||
} yield assert(res.wasAcknowledged() && res.getMatchedCount == 1 && res.getModifiedCount == 1) | ||
} | ||
|
||
it should "update with $max" in { | ||
for { | ||
res <- collection | ||
.updateOne( | ||
query[TestClass](_.field1 == "0"), | ||
update[TestClass](_.max(_.field2, 10)) | ||
) | ||
.head() | ||
} yield assert(res.wasAcknowledged() && res.getMatchedCount == 1 && res.getModifiedCount == 1) | ||
} | ||
|
||
it should "update with $mul" in { | ||
for { | ||
res <- collection | ||
.updateOne( | ||
query[TestClass](_.field1 == "0"), | ||
update[TestClass](_.mul(_.field2, 10)) | ||
) | ||
.head() | ||
} yield assert(res.wasAcknowledged() && res.getMatchedCount == 1 && res.getModifiedCount == 1) | ||
} | ||
|
||
it should "update with $rename" in { | ||
for { | ||
res <- collection | ||
.updateOne( | ||
query[TestClass](_.field1 == "12345"), | ||
update[TestClass](_.rename(_.field2, "NewField2")) | ||
) | ||
.head() | ||
} yield assert(res.wasAcknowledged() && res.getMatchedCount == 1 && res.getModifiedCount == 1) | ||
} | ||
|
||
it should "update with $set" in { | ||
for { | ||
res <- collection | ||
.updateOne( | ||
query[TestClass](_.field1 == "0"), | ||
update[TestClass](_.set(_.field5.!!, 2L)) | ||
) | ||
.head() | ||
} yield assert(res.wasAcknowledged() && res.getMatchedCount == 1 && res.getModifiedCount == 1) | ||
} | ||
|
||
it should "not update existing document with $senOnInsert" in { | ||
for { | ||
res <- collection | ||
.updateOne( | ||
query[TestClass](_.field1 == "0"), | ||
update[TestClass](_.setOnInsert(_.field2, 2)), | ||
(new UpdateOptions).upsert(true) | ||
) | ||
.head() | ||
} yield assert(res.wasAcknowledged() && res.getMatchedCount == 1 && res.getModifiedCount == 0) | ||
} | ||
|
||
it should "update with $unset" in { | ||
for { | ||
res <- collection | ||
.updateOne( | ||
query[TestClass](_.field1 == "1"), | ||
update[TestClass](_.unset(_.field5)) | ||
) | ||
.head() | ||
} yield assert(res.wasAcknowledged() && res.getMatchedCount == 1 && res.getModifiedCount == 1) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters