1+
2+ import com.mongodb.client.model.Filters
3+ import com.mongodb.client.model.geojson.Point
4+ import com.mongodb.client.model.geojson.Polygon
5+ import com.mongodb.client.model.geojson.Position
6+ import com.mongodb.kotlin.client.coroutine.MongoClient
7+ import config.getConfig
8+ import kotlinx.coroutines.flow.firstOrNull
9+ import kotlinx.coroutines.flow.toList
10+ import kotlinx.coroutines.runBlocking
11+ import org.bson.codecs.pojo.annotations.BsonId
12+ import org.junit.jupiter.api.AfterAll
13+ import org.junit.jupiter.api.Assertions.assertEquals
14+ import org.junit.jupiter.api.BeforeAll
15+ import org.junit.jupiter.api.Test
16+
17+ class FiltersBuildersTest {
18+ // :snippet-start: paint-order-data-class
19+ data class PaintOrder (
20+ @BsonId val id : Int ,
21+ val qty : Int ,
22+ val color : String ,
23+ val vendors : List <String > = mutableListOf()
24+ )
25+ // :snippet-end:
26+
27+ companion object {
28+ val config = getConfig()
29+ val client = MongoClient .create(config.connectionUri)
30+ val database = client.getDatabase(" paints_r_us" )
31+ val collection = database.getCollection<PaintOrder >(" paints" )
32+
33+ @BeforeAll
34+ @JvmStatic
35+ fun beforeAll () {
36+ runBlocking {
37+ val paintOrders = listOf (
38+ PaintOrder (1 , 5 , " red" , listOf (" A" )),
39+ PaintOrder (2 , 10 , " purple" , listOf (" C" , " D" )),
40+ PaintOrder (3 , 8 , " blue" , listOf (" B" , " A" )),
41+ PaintOrder (4 , 6 , " white" , listOf (" D" )),
42+ PaintOrder (5 , 11 , " yellow" , listOf (" A" , " B" )),
43+ PaintOrder (6 , 5 , " pink" , listOf (" C" )),
44+ PaintOrder (7 , 8 , " green" , listOf (" B" , " C" )),
45+ PaintOrder (8 , 7 , " orange" , listOf (" A" , " D" ))
46+ )
47+ collection.insertMany(paintOrders)
48+ }
49+ }
50+
51+ @AfterAll
52+ @JvmStatic
53+ fun afterAll () {
54+ runBlocking {
55+ database.drop()
56+ client.close()
57+ }
58+ }
59+ }
60+
61+ @Test
62+ fun equalComparisonTest () = runBlocking {
63+ // :snippet-start: equalComparison
64+ val equalComparison = Filters .eq(PaintOrder ::qty.name, 5 )
65+ val resultsFlow = collection.find(equalComparison)
66+ resultsFlow.collect { println (it) }
67+ // :snippet-end:
68+ assertEquals(2 , resultsFlow.toList().size)
69+ }
70+
71+ @Test
72+ fun gteComparisonTest () = runBlocking {
73+ // :snippet-start: gteComparison
74+ val gteComparison = Filters .gte(PaintOrder ::qty.name, 10 )
75+ val resultsFlow = collection.find(gteComparison)
76+ resultsFlow.collect { println (it) }
77+ // :snippet-end:
78+ assertEquals(2 , resultsFlow.toList().size)
79+ }
80+
81+ @Test
82+ fun emptyComparisonTest () = runBlocking {
83+ // :snippet-start: emptyComparison
84+ val emptyComparison = Filters .empty()
85+ val resultsFlow = collection.find(emptyComparison)
86+ resultsFlow.collect { println (it) }
87+ // :snippet-end:
88+ assertEquals(8 , resultsFlow.toList().size)
89+ }
90+
91+ @Test
92+ fun orComparisonTest () = runBlocking {
93+ // :snippet-start: orComparison
94+ val orComparison = Filters .or (
95+ Filters .gt(PaintOrder ::qty.name, 8 ),
96+ Filters .eq(PaintOrder ::color.name, " pink" )
97+ )
98+ val resultsFlow = collection.find(orComparison)
99+ resultsFlow.collect { println (it) }
100+ // :snippet-end:
101+ assertEquals(3 , resultsFlow.toList().size)
102+ }
103+
104+ @Test
105+ fun allComparisonTest () = runBlocking {
106+ // :snippet-start: allComparison
107+ val search = listOf (" A" , " D" )
108+ val allComparison = Filters .all(PaintOrder ::vendors.name, search)
109+ val resultsFlow = collection.find(allComparison)
110+ resultsFlow.collect { println (it) }
111+ // :snippet-end:
112+ assertEquals(1 , resultsFlow.toList().size)
113+ }
114+
115+ @Test
116+ fun existsComparisonTest () = runBlocking {
117+ // :snippet-start: existsComparison
118+ val existsComparison = Filters .and (Filters .exists(PaintOrder ::qty.name), Filters .nin(" qty" , 5 , 8 ))
119+ val resultsFlow = collection.find(existsComparison)
120+ resultsFlow.collect { println (it) }
121+ // :snippet-end:
122+ assertEquals(4 , resultsFlow.toList().size)
123+ }
124+
125+ @Test
126+ fun regexComparisonTest () = runBlocking {
127+ // :snippet-start: regexComparison
128+ val regexComparison = Filters .regex(PaintOrder ::color.name, " ^p" )
129+ val resultsFlow = collection.find(regexComparison)
130+ resultsFlow.collect { println (it) }
131+ // :snippet-end:
132+ assertEquals(2 , resultsFlow.toList().size)
133+ }
134+
135+ @Test
136+ fun bitsComparisonTest () = runBlocking {
137+ // :snippet-start: bitsComparison
138+ data class BinaryNumber (
139+ @BsonId val id : Int ,
140+ val decimalValue : Int ,
141+ val binaryValue : String
142+ )
143+ val binaryCollection = database.getCollection<BinaryNumber >(" binary_numbers" )
144+ // :remove-start:
145+ val binaryNumbers = listOf (
146+ BinaryNumber (1 , 54 , " 00110110" ),
147+ BinaryNumber (2 , 20 , " 00010100" ),
148+ BinaryNumber (3 , 68 , " 1000100" ),
149+ BinaryNumber (4 , 102 , " 01100110" )
150+ )
151+ binaryCollection.insertMany(binaryNumbers)
152+ // :remove-end:
153+
154+ val bitmask = 34 .toLong() // 00100010 in binary
155+ val bitsComparison = Filters .bitsAllSet(BinaryNumber ::decimalValue.name, bitmask)
156+ val resultsFlow = binaryCollection.find(bitsComparison)
157+ resultsFlow.collect { println (it) }
158+ // :snippet-end:
159+ val expected = listOf (
160+ BinaryNumber (1 , 54 , " 00110110" ),
161+ BinaryNumber (4 , 102 , " 01100110" )
162+ )
163+ assertEquals(expected, resultsFlow.toList())
164+ }
165+
166+ @Test
167+ fun geoWithinComparisonTest () = runBlocking {
168+ // :snippet-start: geoWithinComparison
169+ data class Store (
170+ @BsonId val id : Int ,
171+ val name : String ,
172+ val coordinates : Point
173+ )
174+ val collection = database.getCollection<Store >(" stores" )
175+ // :remove-start:
176+ val stores = listOf (
177+ Store (
178+ 13 ,
179+ " Store 13" ,
180+ Point (Position (2.0 , 2.0 ))
181+ ),
182+ Store (
183+ 14 ,
184+ " Store 14" ,
185+ Point (Position (5.0 , 6.0 ))
186+ ),
187+ Store (
188+ 15 ,
189+ " Store 15" ,
190+ Point (Position (1.0 , 3.0 ))
191+ ),
192+ Store (
193+ 16 ,
194+ " Store 16" ,
195+ Point (Position (4.0 , 7.0 ))
196+ )
197+ )
198+ collection.insertMany(stores)
199+ // :remove-end:
200+
201+ val square = Polygon (listOf (
202+ Position (0.0 , 0.0 ),
203+ Position (4.0 , 0.0 ),
204+ Position (4.0 , 4.0 ),
205+ Position (0.0 , 4.0 ),
206+ Position (0.0 , 0.0 )))
207+ val geoWithinComparison = Filters .geoWithin(Store ::coordinates.name, square)
208+
209+ val resultsFlow = collection.find(geoWithinComparison)
210+ resultsFlow.collect { println (it) }
211+ // :snippet-end:
212+ assertEquals(2 , resultsFlow.toList().size)
213+ assertEquals(" Store 13" , resultsFlow.firstOrNull()?.name)
214+ }
215+ }
0 commit comments