Skip to content

FIX: sort strings in UTF-8 encoded byte order #14312

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 15, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 136 additions & 0 deletions Firestore/Example/Tests/Integration/API/FIRQueryTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -896,6 +896,142 @@ - (void)testSdkOrdersQueryByDocumentIdTheSameWayOnlineAndOffline {
]];
}

- (void)testSnapshotListenerSortsUnicodeStringsInTheSameOrderAsServer {
FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
@"a" : @{@"value" : @"Łukasiewicz"},
@"b" : @{@"value" : @"Sierpiński"},
@"c" : @{@"value" : @"岩澤"},
@"d" : @{@"value" : @"🄟"},
@"e" : @{@"value" : @"P"},
@"f" : @{@"value" : @"︒"},
@"g" : @{@"value" : @"🐵"}

}];

FIRQuery *query = [collRef queryOrderedByField:@"value"];
NSArray<NSString *> *expectedDocs = @[ @"b", @"a", @"c", @"f", @"e", @"d", @"g" ];
FIRQuerySnapshot *getSnapshot = [self readDocumentSetForRef:query];
XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(getSnapshot), expectedDocs);

id<FIRListenerRegistration> registration =
[query addSnapshotListener:self.eventAccumulator.valueEventHandler];
FIRQuerySnapshot *watchSnapshot = [self.eventAccumulator awaitEventWithName:@"Snapshot"];
XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(watchSnapshot), expectedDocs);

[registration remove];

[self checkOnlineAndOfflineQuery:query matchesResult:expectedDocs];
}

- (void)testSnapshotListenerSortsUnicodeStringsInArrayInTheSameOrderAsServer {
FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
@"a" : @{@"value" : @[ @"Łukasiewicz" ]},
@"b" : @{@"value" : @[ @"Sierpiński" ]},
@"c" : @{@"value" : @[ @"岩澤" ]},
@"d" : @{@"value" : @[ @"🄟" ]},
@"e" : @{@"value" : @[ @"P" ]},
@"f" : @{@"value" : @[ @"︒" ]},
@"g" : @{@"value" : @[ @"🐵" ]}

}];

FIRQuery *query = [collRef queryOrderedByField:@"value"];
NSArray<NSString *> *expectedDocs = @[ @"b", @"a", @"c", @"f", @"e", @"d", @"g" ];
FIRQuerySnapshot *getSnapshot = [self readDocumentSetForRef:query];
XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(getSnapshot), expectedDocs);

id<FIRListenerRegistration> registration =
[query addSnapshotListener:self.eventAccumulator.valueEventHandler];
FIRQuerySnapshot *watchSnapshot = [self.eventAccumulator awaitEventWithName:@"Snapshot"];
XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(watchSnapshot), expectedDocs);

[registration remove];

[self checkOnlineAndOfflineQuery:query matchesResult:expectedDocs];
}

- (void)testSnapshotListenerSortsUnicodeStringsInMapInTheSameOrderAsServer {
FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
@"a" : @{@"value" : @{@"foo" : @"Łukasiewicz"}},
@"b" : @{@"value" : @{@"foo" : @"Sierpiński"}},
@"c" : @{@"value" : @{@"foo" : @"岩澤"}},
@"d" : @{@"value" : @{@"foo" : @"🄟"}},
@"e" : @{@"value" : @{@"foo" : @"P"}},
@"f" : @{@"value" : @{@"foo" : @"︒"}},
@"g" : @{@"value" : @{@"foo" : @"🐵"}}

}];

FIRQuery *query = [collRef queryOrderedByField:@"value"];
NSArray<NSString *> *expectedDocs = @[ @"b", @"a", @"c", @"f", @"e", @"d", @"g" ];
FIRQuerySnapshot *getSnapshot = [self readDocumentSetForRef:query];
XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(getSnapshot), expectedDocs);

id<FIRListenerRegistration> registration =
[query addSnapshotListener:self.eventAccumulator.valueEventHandler];
FIRQuerySnapshot *watchSnapshot = [self.eventAccumulator awaitEventWithName:@"Snapshot"];
XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(watchSnapshot), expectedDocs);

[registration remove];

[self checkOnlineAndOfflineQuery:query matchesResult:expectedDocs];
}

- (void)testSnapshotListenerSortsUnicodeStringsInMapKeyInTheSameOrderAsServer {
FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
@"a" : @{@"value" : @{@"Łukasiewicz" : @"foo"}},
@"b" : @{@"value" : @{@"Sierpiński" : @"foo"}},
@"c" : @{@"value" : @{@"岩澤" : @"foo"}},
@"d" : @{@"value" : @{@"🄟" : @"foo"}},
@"e" : @{@"value" : @{@"P" : @"foo"}},
@"f" : @{@"value" : @{@"︒" : @"foo"}},
@"g" : @{@"value" : @{@"🐵" : @"foo"}}

}];

FIRQuery *query = [collRef queryOrderedByField:@"value"];
NSArray<NSString *> *expectedDocs = @[ @"b", @"a", @"c", @"f", @"e", @"d", @"g" ];
FIRQuerySnapshot *getSnapshot = [self readDocumentSetForRef:query];
XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(getSnapshot), expectedDocs);

id<FIRListenerRegistration> registration =
[query addSnapshotListener:self.eventAccumulator.valueEventHandler];
FIRQuerySnapshot *watchSnapshot = [self.eventAccumulator awaitEventWithName:@"Snapshot"];
XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(watchSnapshot), expectedDocs);

[registration remove];

[self checkOnlineAndOfflineQuery:query matchesResult:expectedDocs];
}

- (void)testSnapshotListenerSortsUnicodeStringsInDocumentKeyInTheSameOrderAsServer {
FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
@"Łukasiewicz" : @{@"value" : @"foo"},
@"Sierpiński" : @{@"value" : @"foo"},
@"岩澤" : @{@"value" : @"foo"},
@"🄟" : @{@"value" : @"foo"},
@"P" : @{@"value" : @"foo"},
@"︒" : @{@"value" : @"foo"},
@"🐵" : @{@"value" : @"foo"}

}];

FIRQuery *query = [collRef queryOrderedByFieldPath:[FIRFieldPath documentID]];
NSArray<NSString *> *expectedDocs =
@[ @"Sierpiński", @"Łukasiewicz", @"岩澤", @"︒", @"P", @"🄟", @"🐵" ];
FIRQuerySnapshot *getSnapshot = [self readDocumentSetForRef:query];
XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(getSnapshot), expectedDocs);

id<FIRListenerRegistration> registration =
[query addSnapshotListener:self.eventAccumulator.valueEventHandler];
FIRQuerySnapshot *watchSnapshot = [self.eventAccumulator awaitEventWithName:@"Snapshot"];
XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(watchSnapshot), expectedDocs);

[registration remove];

[self checkOnlineAndOfflineQuery:query matchesResult:expectedDocs];
}

- (void)testCollectionGroupQueriesWithWhereFiltersOnArbitraryDocumentIDs {
// Use .document() to get a random collection group name to use but ensure it starts with 'b'
// for predictable ordering.
Expand Down
Loading