@@ -19,31 +19,32 @@ internal class SortTest {
1919
2020 companion object {
2121 // :snippet-start: sort-data-model
22- data class FoodOrder (
22+ data class Order (
2323 @BsonId val id : Int ,
24- val letter : String ,
25- val food : String
24+ val date : String ,
25+ val orderTotal : Double ,
26+ val description : String ,
2627 )
2728 // :snippet-end:
2829
2930 val config = getConfig()
3031 val client = MongoClient .create(config.connectionUri)
31- val database = client.getDatabase(" cafe " )
32- val collection = database.getCollection<FoodOrder >(" food_order " )
32+ val database = client.getDatabase(" testDB " )
33+ val collection = database.getCollection<Order >(" orders " )
3334
3435 @BeforeAll
3536 @JvmStatic
3637 fun beforeAll () {
3738 runBlocking {
38- val foodOrders = listOf (
39- FoodOrder (1 , " c " , " coffee with milk " ),
40- FoodOrder ( 3 , " a " , " maple syrup " ),
41- FoodOrder ( 4 , " b " , " coffee with sugar " ),
42- FoodOrder ( 5 , " a " , " milk and cookies " ),
43- FoodOrder ( 2 , " a " , " donuts and coffee " ),
44- FoodOrder (6 , " c " , " maple donut " )
39+ val orders = listOf (
40+ Order (1 , " 2022-01-03 " , 17.86 , " 1/2 lb cream cheese and 1 dozen bagels " ),
41+ Order ( 2 , " 2022-01-11 " , 83.87 , " two medium vanilla birthday cakes " ),
42+ Order ( 3 , " 2022-01-11 " , 19.49 , " 1 dozen vanilla cupcakes " ),
43+ Order ( 4 , " 2022-01-15 " , 43.62 , " 2 chicken lunches and a diet coke " ),
44+ Order ( 5 , " 2022-01-23 " , 60.31 , " one large vanilla and chocolate cake " ),
45+ Order (6 , " 2022-01-23 " , 10.99 , " 1 bagel, 1 orange juice, 1 muffin " )
4546 )
46- collection.insertMany(foodOrders )
47+ collection.insertMany(orders )
4748 }
4849 }
4950
@@ -60,39 +61,39 @@ internal class SortTest {
6061 @Test
6162 fun basicSortTest () = runBlocking {
6263 // :snippet-start: basic-sort
63- collection.find().sort(Sorts .ascending(" _id " ))
64+ val resultsFlow = collection.find().sort(Sorts .ascending(Order ::orderTotal.name ))
6465 // :snippet-end:
6566 // Junit test for the above code
6667 val filter = Filters .empty()
6768 val expected = listOf (
68- FoodOrder (1 , " c" , " coffee with milk" ),
69- FoodOrder (2 , " a" , " donuts and coffee" ),
70- FoodOrder (3 , " a" , " maple syrup" ),
71- FoodOrder (4 , " b" , " coffee with sugar" ),
72- FoodOrder (5 , " a" , " milk and cookies" ),
73- FoodOrder (6 , " c" , " maple donut" )
74-
69+ Order (6 , " 2022-01-23" , 10.99 , " 1 bagel, 1 orange juice, 1 muffin" ),
70+ Order (1 , " 2022-01-03" , 17.86 , " 1/2 lb cream cheese and 1 dozen bagels" ),
71+ Order (3 , " 2022-01-11" , 19.49 , " 1 dozen vanilla cupcakes" ),
72+ Order (4 , " 2022-01-15" , 43.62 , " 2 chicken lunches and a diet coke" ),
73+ Order (5 , " 2022-01-23" , 60.31 , " one large vanilla and chocolate cake" ),
74+ Order (2 , " 2022-01-11" , 83.87 , " two medium vanilla birthday cakes" )
7575 )
76- assertEquals(expected, collection.find(filter).sort((Sorts .ascending(" _id" ))).toList() )
76+
77+ assertEquals(expected, resultsFlow.toList() )
7778 }
7879
7980 @Test
8081 fun aggregationSortTest () = runBlocking {
8182 // :snippet-start: aggregation-sort
8283 val resultsFlow = collection.aggregate(listOf (
83- Aggregates .sort(Sorts .ascending(" _id " ))
84+ Aggregates .sort(Sorts .ascending(Order ::orderTotal.name ))
8485 ))
8586
8687 resultsFlow.collect { println (it) }
8788 // :snippet-end:
8889 // Junit test for the above code
8990 val expected = listOf (
90- FoodOrder ( 1 , " c " , " coffee with milk " ),
91- FoodOrder ( 2 , " a " , " donuts and coffee " ),
92- FoodOrder (3 , " a " , " maple syrup " ),
93- FoodOrder (4 , " b " , " coffee with sugar " ),
94- FoodOrder (5 , " a " , " milk and cookies " ),
95- FoodOrder ( 6 , " c " , " maple donut " )
91+ Order ( 6 , " 2022-01-23 " , 10.99 , " 1 bagel, 1 orange juice, 1 muffin " ),
92+ Order ( 1 , " 2022-01-03 " , 17.86 , " 1/2 lb cream cheese and 1 dozen bagels " ),
93+ Order (3 , " 2022-01-11 " , 19.49 , " 1 dozen vanilla cupcakes " ),
94+ Order (4 , " 2022-01-15 " , 43.62 , " 2 chicken lunches and a diet coke " ),
95+ Order (5 , " 2022-01-23 " , 60.31 , " one large vanilla and chocolate cake " ),
96+ Order ( 2 , " 2022-01-11 " , 83.87 , " two medium vanilla birthday cakes " )
9697 )
9798 assertEquals(expected, resultsFlow.toList())
9899 }
@@ -101,18 +102,18 @@ internal class SortTest {
101102 fun ascendingSortTest () = runBlocking {
102103 // :snippet-start: ascending-sort
103104 val resultsFlow = collection.find()
104- .sort(Sorts .ascending(" _id " ))
105+ .sort(Sorts .ascending(Order ::orderTotal.name ))
105106
106107 resultsFlow.collect { println (it) }
107108 // :snippet-end:
108109 // Junit test for the above code
109110 val expected = listOf (
110- FoodOrder ( 1 , " c " , " coffee with milk " ),
111- FoodOrder ( 2 , " a " , " donuts and coffee " ),
112- FoodOrder (3 , " a " , " maple syrup " ),
113- FoodOrder (4 , " b " , " coffee with sugar " ),
114- FoodOrder (5 , " a " , " milk and cookies " ),
115- FoodOrder ( 6 , " c " , " maple donut " )
111+ Order ( 6 , " 2022-01-23 " , 10.99 , " 1 bagel, 1 orange juice, 1 muffin " ),
112+ Order ( 1 , " 2022-01-03 " , 17.86 , " 1/2 lb cream cheese and 1 dozen bagels " ),
113+ Order (3 , " 2022-01-11 " , 19.49 , " 1 dozen vanilla cupcakes " ),
114+ Order (4 , " 2022-01-15 " , 43.62 , " 2 chicken lunches and a diet coke " ),
115+ Order (5 , " 2022-01-23 " , 60.31 , " one large vanilla and chocolate cake " ),
116+ Order ( 2 , " 2022-01-11 " , 83.87 , " two medium vanilla birthday cakes " )
116117 )
117118 assertEquals(expected, resultsFlow.toList())
118119 }
@@ -121,49 +122,49 @@ internal class SortTest {
121122 fun descendingSortTest () = runBlocking {
122123 // :snippet-start: descending-sort
123124 val resultsFlow = collection.find()
124- .sort(Sorts .descending(" _id " ))
125+ .sort(Sorts .descending(Order ::orderTotal.name ))
125126
126127 resultsFlow.collect { println (it) }
127128 // :snippet-end:
128129 // Junit test for the above code
129130 val expected = listOf (
130- FoodOrder ( 6 , " c " , " maple donut " ),
131- FoodOrder (5 , " a " , " milk and cookies " ),
132- FoodOrder (4 , " b " , " coffee with sugar " ),
133- FoodOrder (3 , " a " , " maple syrup " ),
134- FoodOrder ( 2 , " a " , " donuts and coffee " ),
135- FoodOrder ( 1 , " c " , " coffee with milk " )
136- )
131+ Order ( 2 , " 2022-01-11 " , 83.87 , " two medium vanilla birthday cakes " ),
132+ Order (5 , " 2022-01-23 " , 60.31 , " one large vanilla and chocolate cake " ),
133+ Order (4 , " 2022-01-15 " , 43.62 , " 2 chicken lunches and a diet coke " ),
134+ Order (3 , " 2022-01-11 " , 19.49 , " 1 dozen vanilla cupcakes " ),
135+ Order ( 1 , " 2022-01-03 " , 17.86 , " 1/2 lb cream cheese and 1 dozen bagels " ),
136+ Order ( 6 , " 2022-01-23 " , 10.99 , " 1 bagel, 1 orange juice, 1 muffin " )
137+ )
137138 assertEquals(expected, resultsFlow.toList())
138139 }
139140
140141 @Test
141142 fun handleTiesTest () = runBlocking {
142143 // :snippet-start: handle-ties-1
143- collection.find().sort(Sorts .ascending(FoodOrder ::letter .name))
144+ collection.find().sort(Sorts .ascending(Order ::date .name))
144145 // :snippet-end:
145146 val results =
146147 // :snippet-start: handle-ties-2
147- collection.find().sort(Sorts .ascending(FoodOrder ::letter .name, " _id " ))
148+ collection.find().sort(Sorts .ascending(Order ::date .name, Order ::orderTotal.name ))
148149 // :snippet-end:
149150 // Junit test for the above code
150151 val filter = Filters .empty()
151152 val expected = listOf (
152- FoodOrder ( 2 , " a " , " donuts and coffee " ),
153- FoodOrder (3 , " a " , " maple syrup " ),
154- FoodOrder ( 5 , " a " , " milk and cookies " ),
155- FoodOrder (4 , " b " , " coffee with sugar " ),
156- FoodOrder ( 1 , " c " , " coffee with milk " ),
157- FoodOrder ( 6 , " c " , " maple donut " )
158- )
153+ Order ( 1 , " 2022-01-03 " , 17.86 , " 1/2 lb cream cheese and 1 dozen bagels " ),
154+ Order (3 , " 2022-01-11 " , 19.49 , " 1 dozen vanilla cupcakes " ),
155+ Order ( 2 , " 2022-01-11 " , 83.87 , " two medium vanilla birthday cakes " ),
156+ Order (4 , " 2022-01-15 " , 43.62 , " 2 chicken lunches and a diet coke " ),
157+ Order ( 6 , " 2022-01-23 " , 10.99 , " 1 bagel, 1 orange juice, 1 muffin " ),
158+ Order ( 5 , " 2022-01-23 " , 60.31 , " one large vanilla and chocolate cake " )
159+ )
159160 assertEquals(expected, results.toList() )
160161 }
161162
162163 @Test
163164 fun combineSortTest () = runBlocking {
164165 // :snippet-start: combine-sort
165166 val orderBySort = Sorts .orderBy(
166- Sorts .descending(FoodOrder ::letter .name), Sorts .ascending(" _id " )
167+ Sorts .descending(Order ::date .name), Sorts .ascending(Order ::orderTotal.name )
167168 )
168169 val results = collection.find().sort(orderBySort)
169170
@@ -172,48 +173,47 @@ internal class SortTest {
172173 // Junit test for the above code
173174 val filter = Filters .empty()
174175 val expected = listOf (
175- FoodOrder ( 1 , " c " , " coffee with milk " ),
176- FoodOrder ( 6 , " c " , " maple donut " ),
177- FoodOrder (4 , " b " , " coffee with sugar " ),
178- FoodOrder ( 2 , " a " , " donuts and coffee " ),
179- FoodOrder ( 3 , " a " , " maple syrup " ),
180- FoodOrder ( 5 , " a " , " milk and cookies " )
176+ Order ( 6 , " 2022-01-23 " , 10.99 , " 1 bagel, 1 orange juice, 1 muffin " ),
177+ Order ( 5 , " 2022-01-23 " , 60.31 , " one large vanilla and chocolate cake " ),
178+ Order (4 , " 2022-01-15 " , 43.62 , " 2 chicken lunches and a diet coke " ),
179+ Order ( 3 , " 2022-01-11 " , 19.49 , " 1 dozen vanilla cupcakes " ),
180+ Order ( 2 , " 2022-01-11 " , 83.87 , " two medium vanilla birthday cakes " ),
181+ Order ( 1 , " 2022-01-03 " , 17.86 , " 1/2 lb cream cheese and 1 dozen bagels " ),
181182 )
182183 assertEquals(expected, results.toList() )
183184 }
184185
185186 @Test
186187 fun textSearchTest () = runBlocking {
187188 // :snippet-start: food-order-score
188- data class FoodOrderScore (
189+ data class OrderScore (
189190 @BsonId val id : Int ,
190- val letter : String ,
191- val food : String ,
192- val score : Double
191+ val description : String ,
192+ val score : Double
193193 )
194194 // :snippet-end:
195195 // :snippet-start: text-search
196- collection.createIndex(Indexes .text(FoodOrderScore ::food .name))
196+ collection.createIndex(Indexes .text(Order ::description .name))
197197 val metaTextScoreSort = Sorts .orderBy(
198- Sorts .metaTextScore(FoodOrderScore ::score.name),
198+ Sorts .metaTextScore(OrderScore ::score.name),
199199 Sorts .descending(" _id" )
200200 )
201- val metaTextScoreProj = Projections .metaTextScore(FoodOrderScore ::score.name)
202- val searchTerm = " maple donut "
201+ val metaTextScoreProj = Projections .metaTextScore(OrderScore ::score.name)
202+ val searchTerm = " vanilla "
203203 val searchQuery = Filters .text(searchTerm)
204204
205- val results = collection.find<FoodOrderScore >(searchQuery)
205+ val results = collection.find<OrderScore >(searchQuery)
206206 .projection(metaTextScoreProj)
207207 .sort(metaTextScoreSort)
208208
209209 results.collect { println (it) }
210210 // :snippet-end:
211211 // Junit test for the above code
212212 val expected = listOf (
213- FoodOrderScore ( 6 , " c " , " maple donut " , 1.5 ),
214- FoodOrderScore ( 3 , " a " , " maple syrup " , 0.75 ),
215- FoodOrderScore (2 , " a " , " donuts and coffee " , 0.75 ),
216- )
213+ OrderScore ( 3 , " 1 dozen vanilla cupcakes " , 0.625 ),
214+ OrderScore ( 5 , " one large vanilla and chocolate cake " , 0.6 ),
215+ OrderScore (2 , " two medium vanilla birthday cakes " , 0.6 )
216+ )
217217 assertEquals(expected, results.toList())
218218 }
219219}
0 commit comments