Skip to content

Commit e063c84

Browse files
committed
add page test
1 parent fdf69ee commit e063c84

File tree

3 files changed

+146
-23
lines changed

3 files changed

+146
-23
lines changed

Firestore/Swift/Source/ExprImpl.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,12 @@ public extension Expr {
105105
return FunctionExpr("array_length", [self])
106106
}
107107

108-
func arrayOffset(_ offset: Int) -> FunctionExpr {
109-
return FunctionExpr("array_offset", [self, Helper.sendableToExpr(offset)])
108+
func arrayGet(_ offset: Int) -> FunctionExpr {
109+
return FunctionExpr("array_get", [self, Helper.sendableToExpr(offset)])
110110
}
111111

112-
func arrayOffset(_ offsetExpr: Expr) -> FunctionExpr {
113-
return FunctionExpr("array_offset", [self, offsetExpr])
112+
func arrayGet(_ offsetExpr: Expr) -> FunctionExpr {
113+
return FunctionExpr("array_get", [self, offsetExpr])
114114
}
115115

116116
func gt(_ other: Expr) -> BooleanExpr {

Firestore/Swift/Source/SwiftAPI/Pipeline/Expr.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -304,14 +304,14 @@ public protocol Expr: Sendable {
304304
///
305305
/// ```swift
306306
/// // Return the value in the 'tags' field array at index 1.
307-
/// Field("tags").arrayOffset(1)
307+
/// Field("tags").arrayGet(1)
308308
/// // Return the last element in the 'tags' field array.
309-
/// Field("tags").arrayOffset(-1)
309+
/// Field("tags").arrayGet(-1)
310310
/// ```
311311
///
312312
/// - Parameter offset: The literal `Int` offset of the element to return.
313-
/// - Returns: A new `FunctionExpr` representing the 'arrayOffset' operation.
314-
func arrayOffset(_ offset: Int) -> FunctionExpr
313+
/// - Returns: A new `FunctionExpr` representing the 'arrayGet' operation.
314+
func arrayGet(_ offset: Int) -> FunctionExpr
315315

316316
/// Creates an expression that accesses an element in an array (from `self`) at the offset
317317
/// specified by an expression.
@@ -321,13 +321,13 @@ public protocol Expr: Sendable {
321321
///
322322
/// ```swift
323323
/// // Return the value in the tags field array at index specified by field 'favoriteTagIndex'.
324-
/// Field("tags").arrayOffset(Field("favoriteTagIndex"))
324+
/// Field("tags").arrayGet(Field("favoriteTagIndex"))
325325
/// ```
326326
///
327327
/// - Parameter offsetExpr: An `Expr` (evaluating to an Int) representing the offset of the
328328
/// element to return.
329-
/// - Returns: A new `FunctionExpr` representing the 'arrayOffset' operation.
330-
func arrayOffset(_ offsetExpr: Expr) -> FunctionExpr
329+
/// - Returns: A new `FunctionExpr` representing the 'arrayGet' operation.
330+
func arrayGet(_ offsetExpr: Expr) -> FunctionExpr
331331

332332
// MARK: Equality with Sendable
333333

@@ -424,7 +424,7 @@ public protocol Expr: Sendable {
424424
///
425425
/// ```swift
426426
/// // Check if accessing a non-existent array index causes an error
427-
/// Field("myArray").arrayOffset(100).isError()
427+
/// Field("myArray").arrayGet(100).isError()
428428
/// ```
429429
///
430430
/// - Returns: A new `BooleanExpr` representing the 'isError' check.
@@ -1520,7 +1520,7 @@ public protocol Expr: Sendable {
15201520
///
15211521
/// ```swift
15221522
/// // Get first item in 'title' array, or return "Default Title" if error (e.g., empty array)
1523-
/// Field("title").arrayOffset(0).ifError("Default Title")
1523+
/// Field("title").arrayGet(0).ifError("Default Title")
15241524
/// ```
15251525
///
15261526
/// - Parameter catchValue: The literal `Sendable` value to return if this expression errors.

Firestore/Swift/Tests/Integration/PipelineTests.swift

Lines changed: 133 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2008,8 +2008,8 @@ class PipelineIntegrationTests: FSTIntegrationTestCase {
20082008
.select(
20092009
Field("rating").isNull().as("ratingIsNull"),
20102010
Field("rating").isNan().as("ratingIsNaN"),
2011-
Field("title").arrayOffset(0).isError().as("isError"),
2012-
Field("title").arrayOffset(0).ifError(Constant("was error")).as("ifError"),
2011+
Field("title").arrayGet(0).isError().as("isError"),
2012+
Field("title").arrayGet(0).ifError(Constant("was error")).as("ifError"),
20132013
Field("foo").isAbsent().as("isAbsent"),
20142014
Field("title").isNotNull().as("titleIsNotNull"),
20152015
Field("cost").isNotNan().as("costIsNotNan"),
@@ -2045,8 +2045,8 @@ class PipelineIntegrationTests: FSTIntegrationTestCase {
20452045
.select(
20462046
Field("rating").isNull().as("ratingIsNull"),
20472047
Field("rating").isNan().as("ratingIsNaN"),
2048-
Field("title").arrayOffset(0).isError().as("isError"),
2049-
Field("title").arrayOffset(0).ifError(Constant("was error")).as("ifError"),
2048+
Field("title").arrayGet(0).isError().as("isError"),
2049+
Field("title").arrayGet(0).ifError(Constant("was error")).as("ifError"),
20502050
Field("foo").isAbsent().as("isAbsent"),
20512051
Field("title").isNotNull().as("titleIsNotNull"),
20522052
Field("cost").isNotNan().as("costIsNotNan")
@@ -2452,18 +2452,16 @@ class PipelineIntegrationTests: FSTIntegrationTestCase {
24522452
let collRef = collectionRef(withDocuments: bookDocs)
24532453

24542454
let expectedResultsPart1: [[String: Sendable?]] = [
2455-
["firstTag": "adventure"], // book4 (rating 4.7)
2456-
["firstTag": "politics"], // book10 (rating 4.6)
2457-
["firstTag": "classic"], // book2 (rating 4.5)
2455+
["firstTag": "adventure"],
2456+
["firstTag": "politics"],
2457+
["firstTag": "classic"],
24582458
]
24592459

2460-
// Part 1: Using arrayOffset as FunctionExpr("array_offset", ...)
2461-
// (Assuming direct top-level ArrayOffset() isn't available, as per Expr.swift structure)
24622460
let pipeline1 = db.pipeline()
24632461
.collection(collRef.path)
24642462
.sort(Field("rating").descending())
24652463
.limit(3)
2466-
.select(Field("tags").arrayOffset(0).as("firstTag"))
2464+
.select(Field("tags").arrayGet(0).as("firstTag"))
24672465

24682466
let snapshot1 = try await pipeline1.execute()
24692467
XCTAssertEqual(snapshot1.results.count, 3, "Part 1: Should retrieve three documents")
@@ -3039,4 +3037,129 @@ class PipelineIntegrationTests: FSTIntegrationTestCase {
30393037
enforceOrder: false
30403038
)
30413039
}
3040+
3041+
private func addBooks(to collectionReference: CollectionReference) async throws {
3042+
try await collectionReference.document("book11").setData([
3043+
"title": "Jonathan Strange & Mr Norrell",
3044+
"author": "Susanna Clarke",
3045+
"genre": "Fantasy",
3046+
"published": 2004,
3047+
"rating": 4.6,
3048+
"tags": ["historical fantasy", "magic", "alternate history", "england"],
3049+
"awards": ["hugo": false, "nebula": false],
3050+
])
3051+
try await collectionReference.document("book12").setData([
3052+
"title": "The Master and Margarita",
3053+
"author": "Mikhail Bulgakov",
3054+
"genre": "Satire",
3055+
"published": 1967,
3056+
"rating": 4.6,
3057+
"tags": ["russian literature", "supernatural", "philosophy", "dark comedy"],
3058+
"awards": [:],
3059+
])
3060+
try await collectionReference.document("book13").setData([
3061+
"title": "A Long Way to a Small, Angry Planet",
3062+
"author": "Becky Chambers",
3063+
"genre": "Science Fiction",
3064+
"published": 2014,
3065+
"rating": 4.6,
3066+
"tags": ["space opera", "found family", "character-driven", "optimistic"],
3067+
"awards": ["hugo": false, "nebula": false, "kitschies": true],
3068+
])
3069+
}
3070+
3071+
func testSupportsPaginationWithOffsetsUsingName() async throws {
3072+
try XCTSkipIf(true, "Skip this test since backend has not yet supported.")
3073+
3074+
let collRef = collectionRef(withDocuments: bookDocs)
3075+
let db = collRef.firestore
3076+
try await addBooks(to: collRef)
3077+
3078+
let pageSize = 2
3079+
3080+
let pipeline = db.pipeline()
3081+
.collection(collRef.path)
3082+
.select("title", "rating", "__name__")
3083+
.sort(
3084+
Field("rating").descending(),
3085+
Field("__name__").ascending()
3086+
)
3087+
3088+
var snapshot = try await pipeline.limit(Int32(pageSize)).execute()
3089+
3090+
TestHelper.compare(
3091+
pipelineSnapshot: snapshot,
3092+
expected: [
3093+
["title": "The Lord of the Rings", "rating": 4.7],
3094+
["title": "Jonathan Strange & Mr Norrell", "rating": 4.6],
3095+
],
3096+
enforceOrder: true
3097+
)
3098+
3099+
let lastDoc = snapshot.results.last!
3100+
3101+
snapshot = try await pipeline.where(
3102+
(Field("rating").eq(lastDoc.get("rating")!)
3103+
&& Field("rating").lt(lastDoc.get("rating")!))
3104+
|| Field("rating").lt(lastDoc.get("rating")!)
3105+
).limit(Int32(pageSize)).execute()
3106+
3107+
TestHelper.compare(
3108+
pipelineSnapshot: snapshot,
3109+
expected: [
3110+
["title": "Pride and Prejudice", "rating": 4.5],
3111+
["title": "Crime and Punishment", "rating": 4.3],
3112+
],
3113+
enforceOrder: false
3114+
)
3115+
}
3116+
3117+
func testSupportsPaginationWithOffsetsUsingPath() async throws {
3118+
try XCTSkipIf(true, "Skip this test since backend has not yet supported.")
3119+
3120+
let collRef = collectionRef(withDocuments: bookDocs)
3121+
let db = collRef.firestore
3122+
try await addBooks(to: collRef)
3123+
3124+
let pageSize = 2
3125+
var currPage = 0
3126+
3127+
let pipeline = db.pipeline()
3128+
.collection(collRef.path)
3129+
.select("title", "rating", "__path__")
3130+
.sort(
3131+
Field("rating").descending(),
3132+
Field("__path__").ascending()
3133+
)
3134+
3135+
var snapshot = try await pipeline.offset(Int32(currPage) * Int32(pageSize)).limit(
3136+
Int32(pageSize)
3137+
).execute()
3138+
3139+
currPage += 1
3140+
3141+
TestHelper.compare(
3142+
pipelineSnapshot: snapshot,
3143+
expected: [
3144+
["title": "The Lord of the Rings", "rating": 4.7],
3145+
["title": "Dune", "rating": 4.6],
3146+
],
3147+
enforceOrder: true
3148+
)
3149+
3150+
snapshot = try await pipeline.offset(Int32(currPage) * Int32(pageSize)).limit(
3151+
Int32(pageSize)
3152+
).execute()
3153+
3154+
currPage += 1
3155+
3156+
TestHelper.compare(
3157+
pipelineSnapshot: snapshot,
3158+
expected: [
3159+
["title": "A Long Way to a Small, Angry Planet", "rating": 4.6],
3160+
["title": "Pride and Prejudice", "rating": 4.5],
3161+
],
3162+
enforceOrder: true
3163+
)
3164+
}
30423165
}

0 commit comments

Comments
 (0)