@@ -19,31 +19,32 @@ internal class SortTest {
19
19
20
20
companion object {
21
21
// :snippet-start: sort-data-model
22
- data class FoodOrder (
22
+ data class Order (
23
23
@BsonId val id : Int ,
24
- val letter : String ,
25
- val food : String
24
+ val date : String ,
25
+ val orderTotal : Double ,
26
+ val description : String ,
26
27
)
27
28
// :snippet-end:
28
29
29
30
val config = getConfig()
30
31
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 " )
33
34
34
35
@BeforeAll
35
36
@JvmStatic
36
37
fun beforeAll () {
37
38
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 " )
45
46
)
46
- collection.insertMany(foodOrders )
47
+ collection.insertMany(orders )
47
48
}
48
49
}
49
50
@@ -60,39 +61,39 @@ internal class SortTest {
60
61
@Test
61
62
fun basicSortTest () = runBlocking {
62
63
// :snippet-start: basic-sort
63
- collection.find().sort(Sorts .ascending(" _id " ))
64
+ val resultsFlow = collection.find().sort(Sorts .ascending(Order ::orderTotal.name ))
64
65
// :snippet-end:
65
66
// Junit test for the above code
66
67
val filter = Filters .empty()
67
68
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" )
75
75
)
76
- assertEquals(expected, collection.find(filter).sort((Sorts .ascending(" _id" ))).toList() )
76
+
77
+ assertEquals(expected, resultsFlow.toList() )
77
78
}
78
79
79
80
@Test
80
81
fun aggregationSortTest () = runBlocking {
81
82
// :snippet-start: aggregation-sort
82
83
val resultsFlow = collection.aggregate(listOf (
83
- Aggregates .sort(Sorts .ascending(" _id " ))
84
+ Aggregates .sort(Sorts .ascending(Order ::orderTotal.name ))
84
85
))
85
86
86
87
resultsFlow.collect { println (it) }
87
88
// :snippet-end:
88
89
// Junit test for the above code
89
90
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 " )
96
97
)
97
98
assertEquals(expected, resultsFlow.toList())
98
99
}
@@ -101,18 +102,18 @@ internal class SortTest {
101
102
fun ascendingSortTest () = runBlocking {
102
103
// :snippet-start: ascending-sort
103
104
val resultsFlow = collection.find()
104
- .sort(Sorts .ascending(" _id " ))
105
+ .sort(Sorts .ascending(Order ::orderTotal.name ))
105
106
106
107
resultsFlow.collect { println (it) }
107
108
// :snippet-end:
108
109
// Junit test for the above code
109
110
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 " )
116
117
)
117
118
assertEquals(expected, resultsFlow.toList())
118
119
}
@@ -121,49 +122,49 @@ internal class SortTest {
121
122
fun descendingSortTest () = runBlocking {
122
123
// :snippet-start: descending-sort
123
124
val resultsFlow = collection.find()
124
- .sort(Sorts .descending(" _id " ))
125
+ .sort(Sorts .descending(Order ::orderTotal.name ))
125
126
126
127
resultsFlow.collect { println (it) }
127
128
// :snippet-end:
128
129
// Junit test for the above code
129
130
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
+ )
137
138
assertEquals(expected, resultsFlow.toList())
138
139
}
139
140
140
141
@Test
141
142
fun handleTiesTest () = runBlocking {
142
143
// :snippet-start: handle-ties-1
143
- collection.find().sort(Sorts .ascending(FoodOrder ::letter .name))
144
+ collection.find().sort(Sorts .ascending(Order ::date .name))
144
145
// :snippet-end:
145
146
val results =
146
147
// :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 ))
148
149
// :snippet-end:
149
150
// Junit test for the above code
150
151
val filter = Filters .empty()
151
152
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
+ )
159
160
assertEquals(expected, results.toList() )
160
161
}
161
162
162
163
@Test
163
164
fun combineSortTest () = runBlocking {
164
165
// :snippet-start: combine-sort
165
166
val orderBySort = Sorts .orderBy(
166
- Sorts .descending(FoodOrder ::letter .name), Sorts .ascending(" _id " )
167
+ Sorts .descending(Order ::date .name), Sorts .ascending(Order ::orderTotal.name )
167
168
)
168
169
val results = collection.find().sort(orderBySort)
169
170
@@ -172,48 +173,47 @@ internal class SortTest {
172
173
// Junit test for the above code
173
174
val filter = Filters .empty()
174
175
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 " ),
181
182
)
182
183
assertEquals(expected, results.toList() )
183
184
}
184
185
185
186
@Test
186
187
fun textSearchTest () = runBlocking {
187
188
// :snippet-start: food-order-score
188
- data class FoodOrderScore (
189
+ data class OrderScore (
189
190
@BsonId val id : Int ,
190
- val letter : String ,
191
- val food : String ,
192
- val score : Double
191
+ val description : String ,
192
+ val score : Double
193
193
)
194
194
// :snippet-end:
195
195
// :snippet-start: text-search
196
- collection.createIndex(Indexes .text(FoodOrderScore ::food .name))
196
+ collection.createIndex(Indexes .text(Order ::description .name))
197
197
val metaTextScoreSort = Sorts .orderBy(
198
- Sorts .metaTextScore(FoodOrderScore ::score.name),
198
+ Sorts .metaTextScore(OrderScore ::score.name),
199
199
Sorts .descending(" _id" )
200
200
)
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 "
203
203
val searchQuery = Filters .text(searchTerm)
204
204
205
- val results = collection.find<FoodOrderScore >(searchQuery)
205
+ val results = collection.find<OrderScore >(searchQuery)
206
206
.projection(metaTextScoreProj)
207
207
.sort(metaTextScoreSort)
208
208
209
209
results.collect { println (it) }
210
210
// :snippet-end:
211
211
// Junit test for the above code
212
212
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
+ )
217
217
assertEquals(expected, results.toList())
218
218
}
219
219
}
0 commit comments