Skip to content

Commit 24fddb0

Browse files
committed
add more tests
1 parent 22b89e8 commit 24fddb0

File tree

3 files changed

+157
-28
lines changed

3 files changed

+157
-28
lines changed

dev/src/order.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ function compareObjects(left: ApiMapValue, right: ApiMapValue): number {
214214
leftKeys.sort();
215215
rightKeys.sort();
216216
for (let i = 0; i < leftKeys.length && i < rightKeys.length; i++) {
217-
const keyComparison = primitiveComparator(leftKeys[i], rightKeys[i]);
217+
const keyComparison = compareUtf8Strings(leftKeys[i], rightKeys[i]);
218218
if (keyComparison !== 0) {
219219
return keyComparison;
220220
}
@@ -252,10 +252,10 @@ function stringToUTF8Bytes(str: string): Uint8Array {
252252
return new TextEncoder().encode(str);
253253
}
254254

255-
function compareUTF8bytes(left: string, right: string): number {
255+
export function compareUtf8Strings(left: string, right: string): number {
256256
const leftBytes = stringToUTF8Bytes(left);
257257
const rightBytes = stringToUTF8Bytes(right);
258-
return Buffer.compare(Buffer.from(leftBytes), Buffer.from(rightBytes));
258+
return compareBlobs(Buffer.from(leftBytes), Buffer.from(rightBytes));
259259
}
260260

261261
/*!
@@ -279,7 +279,7 @@ export function compare(left: api.IValue, right: api.IValue): number {
279279
case TypeOrder.BOOLEAN:
280280
return primitiveComparator(left.booleanValue!, right.booleanValue!);
281281
case TypeOrder.STRING:
282-
return compareUTF8bytes(left.stringValue!, right.stringValue!);
282+
return compareUtf8Strings(left.stringValue!, right.stringValue!);
283283
case TypeOrder.NUMBER:
284284
return compareNumberProtos(left, right);
285285
case TypeOrder.TIMESTAMP:

dev/src/path.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import * as firestore from '@google-cloud/firestore';
1818

1919
import {google} from '../protos/firestore_v1_proto_api';
20+
import {compareUtf8Strings} from './order';
2021

2122
import {isObject} from './util';
2223
import {
@@ -191,7 +192,7 @@ abstract class Path<T> {
191192
);
192193
} else {
193194
// both non-numeric
194-
return this.compareStrings(lhs, rhs);
195+
return compareUtf8Strings(lhs, rhs);
195196
}
196197
}
197198

dev/system-test/firestore.ts

Lines changed: 151 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4085,34 +4085,162 @@ describe('Query class', () => {
40854085
});
40864086
});
40874087

4088-
it('snapshot listener sorts query by unicode strings same way as server', async () => {
4089-
const collection = await testCollectionWithDocs({
4090-
a: {value: 'Łukasiewicz'},
4091-
b: {value: 'Sierpiński'},
4092-
c: {value: '岩澤'},
4093-
d: {value: '🄟'},
4094-
e: {value: 'P'},
4095-
f: {value: '︒'},
4096-
g: {value: '🐵'},
4097-
});
4088+
describe('sort unicode strings', () => {
4089+
it('snapshot listener sorts query by unicode strings same as server', async () => {
4090+
const collection = await testCollectionWithDocs({
4091+
a: {value: 'Łukasiewicz'},
4092+
b: {value: 'Sierpiński'},
4093+
c: {value: '岩澤'},
4094+
d: {value: '🄟'},
4095+
e: {value: 'P'},
4096+
f: {value: '︒'},
4097+
g: {value: '🐵'},
4098+
});
40984099

4099-
const query = collection.orderBy("value");
4100-
const expectedDocs = ['b', 'a', 'c', 'f', 'e', 'd', 'g'];
4100+
const query = collection.orderBy('value');
4101+
const expectedDocs = ['b', 'a', 'c', 'f', 'e', 'd', 'g'];
41014102

4102-
const getSnapshot = await query.get();
4103-
expect(getSnapshot.docs.map(d => d.id)).to.deep.equal(expectedDocs);
4103+
const getSnapshot = await query.get();
4104+
expect(getSnapshot.docs.map(d => d.id)).to.deep.equal(expectedDocs);
41044105

4105-
const unsubscribe = query.onSnapshot(snapshot =>
4106-
currentDeferred.resolve(snapshot)
4107-
);
4106+
const unsubscribe = query.onSnapshot(snapshot =>
4107+
currentDeferred.resolve(snapshot)
4108+
);
41084109

4109-
const watchSnapshot = await waitForSnapshot();
4110-
// Compare the snapshot (including sort order) of a snapshot
4111-
snapshotsEqual(watchSnapshot, {
4112-
docs: getSnapshot.docs,
4113-
docChanges: getSnapshot.docChanges(),
4110+
const watchSnapshot = await waitForSnapshot();
4111+
// Compare the snapshot (including sort order) of a snapshot
4112+
snapshotsEqual(watchSnapshot, {
4113+
docs: getSnapshot.docs,
4114+
docChanges: getSnapshot.docChanges(),
4115+
});
4116+
unsubscribe();
4117+
});
4118+
4119+
it('snapshot listener sorts query by unicode strings in array same as server', async () => {
4120+
const collection = await testCollectionWithDocs({
4121+
a: {value: ['Łukasiewicz']},
4122+
b: {value: ['Sierpiński']},
4123+
c: {value: ['岩澤']},
4124+
d: {value: ['🄟']},
4125+
e: {value: ['P']},
4126+
f: {value: ['︒']},
4127+
g: {value: ['🐵']},
4128+
});
4129+
4130+
const query = collection.orderBy('value');
4131+
const expectedDocs = ['b', 'a', 'c', 'f', 'e', 'd', 'g'];
4132+
4133+
const getSnapshot = await query.get();
4134+
expect(getSnapshot.docs.map(d => d.id)).to.deep.equal(expectedDocs);
4135+
4136+
const unsubscribe = query.onSnapshot(snapshot =>
4137+
currentDeferred.resolve(snapshot)
4138+
);
4139+
4140+
const watchSnapshot = await waitForSnapshot();
4141+
snapshotsEqual(watchSnapshot, {
4142+
docs: getSnapshot.docs,
4143+
docChanges: getSnapshot.docChanges(),
4144+
});
4145+
unsubscribe();
4146+
});
4147+
4148+
it('snapshot listener sorts query by unicode strings in map same as server', async () => {
4149+
const collection = await testCollectionWithDocs({
4150+
a: {value: {foo: 'Łukasiewicz'}},
4151+
b: {value: {foo: 'Sierpiński'}},
4152+
c: {value: {foo: '岩澤'}},
4153+
d: {value: {foo: '🄟'}},
4154+
e: {value: {foo: 'P'}},
4155+
f: {value: {foo: '︒'}},
4156+
g: {value: {foo: '🐵'}},
4157+
});
4158+
4159+
const query = collection.orderBy('value');
4160+
const expectedDocs = ['b', 'a', 'c', 'f', 'e', 'd', 'g'];
4161+
4162+
const getSnapshot = await query.get();
4163+
expect(getSnapshot.docs.map(d => d.id)).to.deep.equal(expectedDocs);
4164+
4165+
const unsubscribe = query.onSnapshot(snapshot =>
4166+
currentDeferred.resolve(snapshot)
4167+
);
4168+
4169+
const watchSnapshot = await waitForSnapshot();
4170+
// Compare the snapshot (including sort order) of a snapshot
4171+
snapshotsEqual(watchSnapshot, {
4172+
docs: getSnapshot.docs,
4173+
docChanges: getSnapshot.docChanges(),
4174+
});
4175+
unsubscribe();
4176+
});
4177+
it('snapshot listener sorts query by unicode strings in map key same as server', async () => {
4178+
const collection = await testCollectionWithDocs({
4179+
a: {value: {Łukasiewicz: true}},
4180+
b: {value: {Sierpiński: true}},
4181+
c: {value: {岩澤: true}},
4182+
d: {value: {'🄟': true}},
4183+
e: {value: {: true}},
4184+
f: {value: {'︒': true}},
4185+
g: {value: {'🐵': true}},
4186+
});
4187+
4188+
const query = collection.orderBy('value');
4189+
const expectedDocs = ['b', 'a', 'c', 'f', 'e', 'd', 'g'];
4190+
4191+
const getSnapshot = await query.get();
4192+
expect(getSnapshot.docs.map(d => d.id)).to.deep.equal(expectedDocs);
4193+
4194+
const unsubscribe = query.onSnapshot(snapshot =>
4195+
currentDeferred.resolve(snapshot)
4196+
);
4197+
4198+
const watchSnapshot = await waitForSnapshot();
4199+
// Compare the snapshot (including sort order) of a snapshot
4200+
snapshotsEqual(watchSnapshot, {
4201+
docs: getSnapshot.docs,
4202+
docChanges: getSnapshot.docChanges(),
4203+
});
4204+
unsubscribe();
4205+
});
4206+
4207+
it('snapshot listener sorts query by unicode strings in document key same as server', async () => {
4208+
const collection = await testCollectionWithDocs({
4209+
Łukasiewicz: {value: true},
4210+
Sierpiński: {value: true},
4211+
岩澤: {value: true},
4212+
'🄟': {value: true},
4213+
: {value: true},
4214+
'︒': {value: true},
4215+
'🐵': {value: true},
4216+
});
4217+
4218+
const query = collection.orderBy('value');
4219+
const expectedDocs = [
4220+
'Sierpiński',
4221+
'Łukasiewicz',
4222+
'岩澤',
4223+
'︒',
4224+
'P',
4225+
'🄟',
4226+
'🐵',
4227+
];
4228+
4229+
const getSnapshot = await query.get();
4230+
expect(getSnapshot.docs.map(d => d.id)).to.deep.equal(expectedDocs);
4231+
4232+
const unsubscribe = query.onSnapshot(snapshot =>
4233+
currentDeferred.resolve(snapshot)
4234+
);
4235+
4236+
const watchSnapshot = await waitForSnapshot();
4237+
// Compare the snapshot (including sort order) of a snapshot
4238+
snapshotsEqual(watchSnapshot, {
4239+
docs: getSnapshot.docs,
4240+
docChanges: getSnapshot.docChanges(),
4241+
});
4242+
unsubscribe();
41144243
});
4115-
unsubscribe();
41164244
});
41174245
});
41184246

0 commit comments

Comments
 (0)