11package com .sfxcode .nosql .mongo .operation
22
3- import com .sfxcode .nosql .mongo .test .TestDatabase ._
43import com .sfxcode .nosql .mongo ._
54import com .sfxcode .nosql .mongo .dao .PersonSpecification
5+ import com .sfxcode .nosql .mongo .database .DatabaseProvider
66import com .sfxcode .nosql .mongo .model .CodecTest
7+ import com .sfxcode .nosql .mongo .test .TestDatabase ._
78import com .typesafe .scalalogging .LazyLogging
9+ import org .bson .conversions .Bson
810import org .mongodb .scala .bson .ObjectId
9- import org .mongodb .scala .model .changestream .ChangeStreamDocument
11+ import org .mongodb .scala .model .Filters .equal
12+ import org .mongodb .scala .result .DeleteResult
13+ import org .mongodb .scala .model .Updates ._
1014
1115class CrudSpec extends PersonSpecification with LazyLogging {
1216
@@ -15,32 +19,52 @@ class CrudSpec extends PersonSpecification with LazyLogging {
1519 override def beforeAll (): Unit = {
1620 super .beforeAll()
1721 CodecDao .drop().result()
18- // CodecDao.addChangeObserver(ChangeObserver(consumeCodecChanges))
19- val result = CodecDao .insertOne(CodecTest ()).result()
2022
21- val list : List [CodecTest ] = CodecDao .find().resultList()
22- list.size mustEqual 1
23+ }
2324
24- list.head._id must haveClass[ ObjectId ]
25+ " Crud Operations " should {
2526
26- def consumeCodecChanges (changeStreamDocument : ChangeStreamDocument [CodecTest ]): Unit =
27- logger.info(
28- " codec changed %s:%s with ID: %s" .format(
29- changeStreamDocument.getNamespace,
30- changeStreamDocument.getOperationType,
31- changeStreamDocument.getDocumentKey
32- )
33- )
27+ " create Document in" in {
28+ val result = CodecDao .insertOne(CodecTest ()).result()
3429
35- }
30+ val list : List [CodecTest ] = CodecDao .find().resultList()
31+ list.size mustEqual 1
3632
37- " Crud Operations" should {
33+ }
34+
35+ " update Document in" in {
36+ var list : List [CodecTest ] = CodecDao .find().resultList()
37+ var codec = list.head
38+ codec.id mustEqual 1
39+ CodecDao .updateOne(Map (" id" -> 1 ), set(" id" , 2 )).result()
40+ list = CodecDao .find().resultList()
41+ codec = list.head
42+ codec.id mustEqual 2
43+ }
44+
45+ " replace Document in" in {
46+ var list : List [CodecTest ] = CodecDao .find().resultList()
47+ var codec = list.head
48+ codec.id mustEqual 2
49+ CodecDao .replaceOne(codec.copy(id = 1 )).result()
50+ list = CodecDao .find().resultList()
51+ codec = list.head
52+ codec.id mustEqual 1
53+ }
54+
55+ " delete Document in" in {
56+ val hexString = CodecDao .find().result()._id.toHexString
57+ hexString must not beEmpty
58+
59+ val result : DeleteResult =
60+ CodecDao .deleteOne(equal(DatabaseProvider .ObjectIdKey , new ObjectId (hexString))).result()
61+
62+ result.wasAcknowledged() must beTrue
63+ result.getDeletedCount must beEqualTo(1 )
3864
39- " create Documents in " in {
65+ val list : List [ CodecTest ] = CodecDao .find().resultList()
4066
41- CodecDao .count().result() must be equalTo 1
42- val findOneResult : Option [CodecTest ] = CodecDao .find(" id" , 1 ).resultOption()
43- findOneResult must beSome[CodecTest ]
67+ list.size mustEqual 0
4468 }
4569
4670 }
0 commit comments