Skip to content

Commit 3501135

Browse files
committed
Lint and formatting
1 parent 86c9057 commit 3501135

File tree

3 files changed

+311
-53
lines changed

3 files changed

+311
-53
lines changed

packages/firestore/src/core/target.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -891,22 +891,25 @@ export function compositeFilterEquals(
891891
/** Returns a debug description for `filter`. */
892892
export function stringifyFilter(filter: Filter): string {
893893
debugAssert(
894-
(filter instanceof FieldFilter) || (filter instanceof CompositeFilter),
894+
filter instanceof FieldFilter || filter instanceof CompositeFilter,
895895
'stringifyFilter() only supports FieldFilters and CompositeFilters'
896896
);
897897
if (filter instanceof FieldFilter) {
898898
return stringifyFieldFilter(filter);
899-
}
900-
else if (filter instanceof CompositeFilter) {
899+
} else if (filter instanceof CompositeFilter) {
901900
return stringifyCompositeFilter(filter);
902-
}
903-
else {
901+
} else {
904902
return 'Filter';
905903
}
906904
}
907905

908906
export function stringifyCompositeFilter(filter: CompositeFilter): string {
909-
return filter.op.toString() + ` {` + filter.getFilters().map(stringifyFilter).join(' ,') + '}';
907+
return (
908+
filter.op.toString() +
909+
` {` +
910+
filter.getFilters().map(stringifyFilter).join(' ,') +
911+
'}'
912+
);
910913
}
911914

912915
export function stringifyFieldFilter(filter: FieldFilter): string {

packages/firestore/test/integration/api/query.test.ts

Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { Deferred } from '../../util/promise';
2222
import { EventsAccumulator } from '../util/events_accumulator';
2323
import {
2424
addDoc,
25+
and,
2526
Bytes,
2627
collection,
2728
collectionGroup,
@@ -36,10 +37,14 @@ import {
3637
endBefore,
3738
GeoPoint,
3839
getDocs,
40+
getDocsFromCache,
41+
getDocsFromServer,
3942
limit,
4043
limitToLast,
4144
onSnapshot,
45+
or,
4246
orderBy,
47+
Query,
4348
query,
4449
QuerySnapshot,
4550
setDoc,
@@ -54,6 +59,7 @@ import {
5459
apiDescribe,
5560
toChangesArray,
5661
toDataArray,
62+
toIds,
5763
withTestCollection,
5864
withTestDb
5965
} from '../util/helpers';
@@ -1288,6 +1294,189 @@ apiDescribe('Queries', (persistence: boolean) => {
12881294
expect(toDataArray(snapshot)).to.deep.equal([{ map: { nested: 'foo' } }]);
12891295
});
12901296
});
1297+
1298+
if (!persistence) {
1299+
return;
1300+
}
1301+
1302+
// TODO(orquery): Enable this test when prod supports OR queries.
1303+
// eslint-disable-next-line no-restricted-properties
1304+
it.skip('can use or queries', () => {
1305+
const testDocs = {
1306+
doc1: { a: 1, b: 0 },
1307+
doc2: { a: 2, b: 1 },
1308+
doc3: { a: 3, b: 2 },
1309+
doc4: { a: 1, b: 3 },
1310+
doc5: { a: 1, b: 1 }
1311+
};
1312+
1313+
return withTestCollection(persistence, testDocs, async coll => {
1314+
// Two equalities: a==1 || b==1.
1315+
await checkOnlineAndOfflineResultsMatch(
1316+
query(coll, or(where('a', '==', 1), where('b', '==', 1))),
1317+
'doc1',
1318+
'doc2',
1319+
'doc4',
1320+
'doc5'
1321+
);
1322+
1323+
// with one inequality: a>2 || b==1.
1324+
await checkOnlineAndOfflineResultsMatch(
1325+
query(coll, or(where('a', '>', 2), where('b', '==', 1))),
1326+
'doc5',
1327+
'doc2',
1328+
'doc3'
1329+
);
1330+
1331+
// (a==1 && b==0) || (a==3 && b==2)
1332+
await checkOnlineAndOfflineResultsMatch(
1333+
query(
1334+
coll,
1335+
or(
1336+
and(where('a', '==', 1), where('b', '==', 0)),
1337+
and(where('a', '==', 3), where('b', '==', 2))
1338+
)
1339+
),
1340+
'doc1',
1341+
'doc3'
1342+
);
1343+
1344+
// a==1 && (b==0 || b==3).
1345+
await checkOnlineAndOfflineResultsMatch(
1346+
query(
1347+
coll,
1348+
and(where('a', '==', 1), or(where('b', '==', 0), where('b', '==', 3)))
1349+
),
1350+
'doc1',
1351+
'doc4'
1352+
);
1353+
1354+
// (a==2 || b==2) && (a==3 || b==3)
1355+
await checkOnlineAndOfflineResultsMatch(
1356+
query(
1357+
coll,
1358+
and(
1359+
or(where('a', '==', 2), where('b', '==', 2)),
1360+
or(where('a', '==', 3), where('b', '==', 3))
1361+
)
1362+
),
1363+
'doc3'
1364+
);
1365+
1366+
// Test with limits (implicit order by ASC): (a==1) || (b > 0) LIMIT 2
1367+
await checkOnlineAndOfflineResultsMatch(
1368+
query(coll, or(where('a', '==', 1), where('b', '>', 0)), limit(2)),
1369+
'doc1',
1370+
'doc2'
1371+
);
1372+
1373+
// Test with limits (explicit order by): (a==1) || (b > 0) LIMIT_TO_LAST 2
1374+
// Note: The public query API does not allow implicit ordering when limitToLast is used.
1375+
await checkOnlineAndOfflineResultsMatch(
1376+
query(
1377+
coll,
1378+
or(where('a', '==', 1), where('b', '>', 0)),
1379+
limitToLast(2),
1380+
orderBy('b')
1381+
),
1382+
'doc3',
1383+
'doc4'
1384+
);
1385+
1386+
// Test with limits (explicit order by ASC): (a==2) || (b == 1) ORDER BY a LIMIT 1
1387+
await checkOnlineAndOfflineResultsMatch(
1388+
query(
1389+
coll,
1390+
or(where('a', '==', 2), where('b', '==', 1)),
1391+
limit(1),
1392+
orderBy('a')
1393+
),
1394+
'doc5'
1395+
);
1396+
1397+
// Test with limits (explicit order by DESC): (a==2) || (b == 1) ORDER BY a LIMIT_TO_LAST 1
1398+
await checkOnlineAndOfflineResultsMatch(
1399+
query(
1400+
coll,
1401+
or(where('a', '==', 2), where('b', '==', 1)),
1402+
limitToLast(1),
1403+
orderBy('a')
1404+
),
1405+
'doc2'
1406+
);
1407+
1408+
// Test with limits without orderBy (the __name__ ordering is the tie breaker).
1409+
await checkOnlineAndOfflineResultsMatch(
1410+
query(coll, or(where('a', '==', 2), where('b', '==', 1)), limit(1)),
1411+
'doc2'
1412+
);
1413+
});
1414+
});
1415+
1416+
// TODO(orquery): Enable this test when prod supports OR queries.
1417+
// eslint-disable-next-line no-restricted-properties
1418+
it.skip('can use or queries with in and not-in', () => {
1419+
const testDocs = {
1420+
doc1: { a: 1, b: 0 },
1421+
doc2: { b: 1 },
1422+
doc3: { a: 3, b: 2 },
1423+
doc4: { a: 1, b: 3 },
1424+
doc5: { a: 1 },
1425+
doc6: { a: 2 }
1426+
};
1427+
1428+
return withTestCollection(persistence, testDocs, async coll => {
1429+
// a==2 || b in [2,3]
1430+
await checkOnlineAndOfflineResultsMatch(
1431+
query(coll, or(where('a', '==', 2), where('b', 'in', [2, 3]))),
1432+
'doc3',
1433+
'doc4',
1434+
'doc6'
1435+
);
1436+
1437+
// a==2 || b not-in [2,3]
1438+
// Has implicit orderBy b.
1439+
await checkOnlineAndOfflineResultsMatch(
1440+
query(coll, or(where('a', '==', 2), where('b', 'not-in', [2, 3]))),
1441+
'doc1',
1442+
'doc2'
1443+
);
1444+
});
1445+
});
1446+
1447+
// TODO(orquery): Enable this test when prod supports OR queries.
1448+
// eslint-disable-next-line no-restricted-properties
1449+
it.skip('can use or queries with array membership', () => {
1450+
const testDocs = {
1451+
doc1: { a: 1, b: [0] },
1452+
doc2: { b: [1] },
1453+
doc3: { a: 3, b: [2, 7] },
1454+
doc4: { a: 1, b: [3, 7] },
1455+
doc5: { a: 1 },
1456+
doc6: { a: 2 }
1457+
};
1458+
1459+
return withTestCollection(persistence, testDocs, async coll => {
1460+
// a==2 || b array-contains 7
1461+
await checkOnlineAndOfflineResultsMatch(
1462+
query(coll, or(where('a', '==', 2), where('b', 'array-contains', 7))),
1463+
'doc3',
1464+
'doc4',
1465+
'doc6'
1466+
);
1467+
1468+
// a==2 || b array-contains-any [0, 3]
1469+
await checkOnlineAndOfflineResultsMatch(
1470+
query(
1471+
coll,
1472+
or(where('a', '==', 2), where('b', 'array-contains-any', [0, 3]))
1473+
),
1474+
'doc1',
1475+
'doc4',
1476+
'doc6'
1477+
);
1478+
});
1479+
});
12911480
});
12921481

12931482
function verifyDocumentChange<T>(
@@ -1302,3 +1491,25 @@ function verifyDocumentChange<T>(
13021491
expect(change.oldIndex).to.equal(oldIndex);
13031492
expect(change.newIndex).to.equal(newIndex);
13041493
}
1494+
1495+
/**
1496+
* Checks that running the query while online (against the backend/emulator) results in the same
1497+
* documents as running the query while offline. If `expectedDocs` is provided, it also checks
1498+
* that both online and offline query result is equal to the expected documents.
1499+
*
1500+
* @param query The query to check
1501+
* @param expectedDocs Ordered list of document keys that are expected to match the query
1502+
*/
1503+
async function checkOnlineAndOfflineResultsMatch(
1504+
query: Query,
1505+
...expectedDocs: string[]
1506+
): Promise<void> {
1507+
const docsFromServer = await getDocsFromServer(query);
1508+
1509+
if (expectedDocs.length !== 0) {
1510+
expect(expectedDocs).to.deep.equal(toIds(docsFromServer));
1511+
}
1512+
1513+
const docsFromCache = await getDocsFromCache(query);
1514+
expect(toIds(docsFromServer)).to.deep.equal(toIds(docsFromCache));
1515+
}

0 commit comments

Comments
 (0)