Skip to content

Commit

Permalink
FIX: sort strings in UTF-8 encoded byte order (#14312)
Browse files Browse the repository at this point in the history
  • Loading branch information
milaGGL authored Jan 15, 2025
1 parent 95f8ec8 commit a34d7ae
Showing 1 changed file with 136 additions and 0 deletions.
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" : @""},
@"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" : @[ @"" ]},
@"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" : @""}},
@"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" : @{@"" : @"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"},
@"" : @{@"value" : @"foo"},
@"" : @{@"value" : @"foo"},
@"🐵" : @{@"value" : @"foo"}

}];

FIRQuery *query = [collRef queryOrderedByFieldPath:[FIRFieldPath documentID]];
NSArray<NSString *> *expectedDocs =
@[ @"Sierpiński", @"Łukasiewicz", @"岩澤", @"", @"", @"🄟", @"🐵" ];
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

0 comments on commit a34d7ae

Please sign in to comment.