@@ -1122,7 +1122,164 @@ class PipelineIntegrationTests: FSTIntegrationTestCase {
1122
1122
)
1123
1123
}
1124
1124
1125
- // TODO:
1125
+ func testCanAddFields( ) async throws {
1126
+ let collRef = collectionRef ( withDocuments: bookDocs)
1127
+ let db = collRef. firestore
1128
+
1129
+ let pipeline = db. pipeline ( )
1130
+ . collection ( collRef. path)
1131
+ . sort ( Field ( " author " ) . ascending ( ) )
1132
+ . limit ( 1 )
1133
+ . select ( " title " , " author " )
1134
+ . rawStage (
1135
+ name: " add_fields " ,
1136
+ params: [
1137
+ [
1138
+ " display " : Field ( " title " ) . strConcat (
1139
+ Constant ( " - " ) ,
1140
+ Field ( " author " )
1141
+ ) ,
1142
+ ] ,
1143
+ ]
1144
+ )
1145
+
1146
+ let snapshot = try await pipeline. execute ( )
1147
+
1148
+ TestHelper . compare (
1149
+ pipelineSnapshot: snapshot,
1150
+ expected: [
1151
+ [
1152
+ " title " : " The Hitchhiker's Guide to the Galaxy " ,
1153
+ " author " : " Douglas Adams " ,
1154
+ " display " : " The Hitchhiker's Guide to the Galaxy - Douglas Adams " ,
1155
+ ] ,
1156
+ ] ,
1157
+ enforceOrder: false
1158
+ )
1159
+ }
1160
+
1161
+ func testCanPerformDistinctQuery( ) async throws {
1162
+ let collRef = collectionRef ( withDocuments: bookDocs)
1163
+ let db = collRef. firestore
1164
+
1165
+ let pipeline = db. pipeline ( )
1166
+ . collection ( collRef. path)
1167
+ . select ( " title " , " author " , " rating " )
1168
+ . rawStage (
1169
+ name: " distinct " ,
1170
+ params: [
1171
+ [ " rating " : Field ( " rating " ) ] ,
1172
+ ]
1173
+ )
1174
+ . sort ( Field ( " rating " ) . descending ( ) )
1175
+
1176
+ let snapshot = try await pipeline. execute ( )
1177
+
1178
+ TestHelper . compare (
1179
+ pipelineSnapshot: snapshot,
1180
+ expected: [
1181
+ [ " rating " : 4.7 ] ,
1182
+ [ " rating " : 4.6 ] ,
1183
+ [ " rating " : 4.5 ] ,
1184
+ [ " rating " : 4.3 ] ,
1185
+ [ " rating " : 4.2 ] ,
1186
+ [ " rating " : 4.1 ] ,
1187
+ [ " rating " : 4.0 ] ,
1188
+ ] ,
1189
+ enforceOrder: true
1190
+ )
1191
+ }
1192
+
1193
+ func testCanPerformAggregateQuery( ) async throws {
1194
+ let collRef = collectionRef ( withDocuments: bookDocs)
1195
+ let db = collRef. firestore
1196
+
1197
+ let emptySendableDictionary : [ String : Sendable ? ] = [ : ]
1198
+
1199
+ let pipeline = db. pipeline ( )
1200
+ . collection ( collRef. path)
1201
+ . select ( " title " , " author " , " rating " )
1202
+ . rawStage (
1203
+ name: " aggregate " ,
1204
+ params: [
1205
+ [
1206
+ " averageRating " : Field ( " rating " ) . avg ( ) ,
1207
+ ] ,
1208
+ emptySendableDictionary,
1209
+ ]
1210
+ )
1211
+
1212
+ let snapshot = try await pipeline. execute ( )
1213
+
1214
+ TestHelper . compare (
1215
+ pipelineSnapshot: snapshot,
1216
+ expected: [
1217
+ [
1218
+ " averageRating " : 4.3100000000000005 ,
1219
+ ] ,
1220
+ ] ,
1221
+ enforceOrder: true
1222
+ )
1223
+ }
1224
+
1225
+ func testCanFilterWithWhere( ) async throws {
1226
+ let collRef = collectionRef ( withDocuments: bookDocs)
1227
+ let db = collRef. firestore
1228
+
1229
+ let pipeline = db. pipeline ( )
1230
+ . collection ( collRef. path)
1231
+ . select ( " title " , " author " )
1232
+ . rawStage (
1233
+ name: " where " ,
1234
+ params: [ Field ( " author " ) . eq ( " Douglas Adams " ) ]
1235
+ )
1236
+
1237
+ let snapshot = try await pipeline. execute ( )
1238
+
1239
+ TestHelper . compare (
1240
+ pipelineSnapshot: snapshot,
1241
+ expected: [
1242
+ [
1243
+ " title " : " The Hitchhiker's Guide to the Galaxy " ,
1244
+ " author " : " Douglas Adams " ,
1245
+ ] ,
1246
+ ] ,
1247
+ enforceOrder: false
1248
+ )
1249
+ }
1250
+
1251
+ func testCanLimitOffsetAndSort( ) async throws {
1252
+ let collRef = collectionRef ( withDocuments: bookDocs)
1253
+ let db = collRef. firestore
1254
+
1255
+ let pipeline = db. pipeline ( )
1256
+ . collection ( collRef. path)
1257
+ . select ( " title " , " author " )
1258
+ . rawStage (
1259
+ name: " sort " ,
1260
+ params: [
1261
+ [
1262
+ " direction " : " ascending " ,
1263
+ " expression " : Field ( " author " ) ,
1264
+ ] ,
1265
+ ]
1266
+ )
1267
+ . rawStage ( name: " offset " , params: [ 3 ] )
1268
+ . rawStage ( name: " limit " , params: [ 1 ] )
1269
+
1270
+ let snapshot = try await pipeline. execute ( )
1271
+
1272
+ TestHelper . compare (
1273
+ pipelineSnapshot: snapshot,
1274
+ expected: [
1275
+ [
1276
+ " author " : " Fyodor Dostoevsky " ,
1277
+ " title " : " Crime and Punishment " ,
1278
+ ] ,
1279
+ ] ,
1280
+ enforceOrder: false
1281
+ )
1282
+ }
1126
1283
1127
1284
// MARK: - Replace Stage Test
1128
1285
@@ -1153,6 +1310,31 @@ class PipelineIntegrationTests: FSTIntegrationTestCase {
1153
1310
)
1154
1311
}
1155
1312
1313
+ func testReplaceWithExprResult( ) async throws {
1314
+ let collRef = collectionRef ( withDocuments: bookDocs)
1315
+ let db = collRef. firestore
1316
+
1317
+ let pipeline = db. pipeline ( )
1318
+ . collection ( collRef. path)
1319
+ . where ( Field ( " title " ) . eq ( " The Hitchhiker's Guide to the Galaxy " ) )
1320
+ . replace ( with:
1321
+ MapExpression ( [
1322
+ " foo " : " bar " ,
1323
+ " baz " : MapExpression ( [
1324
+ " title " : Field ( " title " ) ,
1325
+ ] ) ,
1326
+ ] ) )
1327
+
1328
+ let snapshot = try await pipeline. execute ( )
1329
+
1330
+ let expectedResults : [ String : Sendable ? ] = [
1331
+ " foo " : " bar " ,
1332
+ " baz " : [ " title " : " The Hitchhiker's Guide to the Galaxy " ] ,
1333
+ ]
1334
+
1335
+ TestHelper . compare ( pipelineSnapshot: snapshot, expected: [ expectedResults] , enforceOrder: false )
1336
+ }
1337
+
1156
1338
// MARK: - Sample Stage Tests
1157
1339
1158
1340
func testSampleStageLimit3( ) async throws {
@@ -1212,7 +1394,7 @@ class PipelineIntegrationTests: FSTIntegrationTestCase {
1212
1394
let pipeline = db. pipeline ( )
1213
1395
. collection ( collRef. path)
1214
1396
. where ( Field ( " title " ) . eq ( " The Hitchhiker's Guide to the Galaxy " ) )
1215
- . unnest ( Field ( " tags " ) . as ( " tag " ) )
1397
+ . unnest ( Field ( " tags " ) . as ( " tag " ) , indexField : " tagsIndex " )
1216
1398
. select (
1217
1399
" title " ,
1218
1400
" author " ,
0 commit comments