Skip to content

Commit e923dfd

Browse files
committed
Add tests for evaluating expressions in arrays and maps
1 parent 7d6aec1 commit e923dfd

File tree

1 file changed

+156
-15
lines changed

1 file changed

+156
-15
lines changed

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

Lines changed: 156 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { expect, use } from 'chai';
1919
import chaiAsPromised from 'chai-as-promised';
2020

2121
import { Bytes, vector } from '../../../src/api';
22+
import { array, map } from '../../../src/lite-api/expressions';
2223
import { GeoPoint } from '../../../src/lite-api/geo_point';
2324
import { Timestamp } from '../../../src/lite-api/timestamp';
2425
import { addEqualityMatcher } from '../../util/equality_matcher';
@@ -79,7 +80,8 @@ import {
7980
and,
8081
documentId,
8182
addDoc,
82-
getDoc
83+
getDoc,
84+
multiply
8385
} from '../util/firebase_export';
8486
import {
8587
apiDescribe,
@@ -283,6 +285,142 @@ apiDescribe.only('Pipelines', persistence => {
283285
expect(result.length).to.equal(10);
284286
});
285287

288+
it('evaluates expression in map', async () => {
289+
const result = await firestore
290+
.pipeline()
291+
.collection(randomCol.path)
292+
.sort(Field.of('rating').descending())
293+
.limit(1)
294+
.addFields(
295+
map({
296+
genre: Field.of('genre'),
297+
rating: Field.of('rating').multiply(10)
298+
}).as('metadata')
299+
)
300+
.execute();
301+
302+
expect(result.length).to.equal(1);
303+
expectResults(result, {
304+
title: 'The Lord of the Rings',
305+
author: 'J.R.R. Tolkien',
306+
genre: 'Fantasy',
307+
published: 1954,
308+
rating: 4.7,
309+
tags: ['adventure', 'magic', 'epic'],
310+
awards: { hugo: false, nebula: false },
311+
metadata: {
312+
genre: 'Fantasy',
313+
rating: 47
314+
}
315+
});
316+
});
317+
318+
it('evaluates expression in array', async () => {
319+
const result = await firestore
320+
.pipeline()
321+
.collection(randomCol.path)
322+
.sort(Field.of('rating').descending())
323+
.limit(1)
324+
.addFields(
325+
array([1, 2, Field.of('genre'), multiply('rating', 10)]).as(
326+
'metadata'
327+
)
328+
)
329+
.execute();
330+
expect(result.length).to.equal(1);
331+
expectResults(result, {
332+
title: 'The Lord of the Rings',
333+
author: 'J.R.R. Tolkien',
334+
genre: 'Fantasy',
335+
published: 1954,
336+
rating: 4.7,
337+
tags: ['adventure', 'magic', 'epic'],
338+
awards: { hugo: false, nebula: false },
339+
metadata: [1, 2, 'Fantasy', 47]
340+
});
341+
});
342+
343+
it('converts arrays and plain objects to functionValues if the customer intent is unspecified', async () => {
344+
const result = await firestore
345+
.pipeline()
346+
.collection(randomCol.path)
347+
.sort(Field.of('rating').descending())
348+
.limit(1)
349+
.addFields(
350+
array([
351+
1,
352+
2,
353+
Field.of('genre'),
354+
multiply('rating', 10),
355+
[Field.of('title')],
356+
{
357+
published: Field.of('published')
358+
}
359+
]).as('metadataArray'),
360+
map({
361+
genre: Field.of('genre'),
362+
rating: multiply('rating', 10),
363+
nestedArray: [Field.of('title')],
364+
nestedMap: {
365+
published: Field.of('published')
366+
}
367+
}).as('metadata')
368+
)
369+
.where(
370+
andFunction(
371+
eq('metadataArray', [
372+
1,
373+
2,
374+
Field.of('genre'),
375+
multiply('rating', 10),
376+
[Field.of('title')],
377+
{
378+
published: Field.of('published')
379+
}
380+
]),
381+
eq('metadata', {
382+
genre: Field.of('genre'),
383+
rating: multiply('rating', 10),
384+
nestedArray: [Field.of('title')],
385+
nestedMap: {
386+
published: Field.of('published')
387+
}
388+
})
389+
)
390+
)
391+
.execute();
392+
393+
expect(result.length).to.equal(1);
394+
395+
expectResults(result, {
396+
title: 'The Lord of the Rings',
397+
author: 'J.R.R. Tolkien',
398+
genre: 'Fantasy',
399+
published: 1954,
400+
rating: 4.7,
401+
tags: ['adventure', 'magic', 'epic'],
402+
awards: { hugo: false, nebula: false },
403+
metadataArray: [
404+
1,
405+
2,
406+
'Fantasy',
407+
47,
408+
['The Lord of the Rings'],
409+
{
410+
published: 1954
411+
}
412+
],
413+
metadata: {
414+
genre: 'Fantasy',
415+
rating: 47,
416+
nestedArray: ['The Lord of the Rings'],
417+
nestedMap: {
418+
published: 1954
419+
}
420+
}
421+
});
422+
});
423+
286424
it('returns aggregate results as expected', async () => {
287425
let result = await firestore
288426
.pipeline()
@@ -681,18 +819,20 @@ apiDescribe.only('Pipelines', persistence => {
681819
});
682820

683821
// skip: arrayConcat not supported
684-
// it.skip('arrayConcat works', async () => {
685-
// const results = await randomCol
686-
// .pipeline()
687-
// .select(
688-
// Field.of('tags').arrayConcat(['newTag1', 'newTag2']).as('modifiedTags')
689-
// )
690-
// .limit(1)
691-
// .execute();
692-
// expectResults(results, {
693-
// modifiedTags: ['comedy', 'space', 'adventure', 'newTag1', 'newTag2']
694-
// });
695-
// });
822+
it('arrayConcat works', async () => {
823+
const results = await randomCol
824+
.pipeline()
825+
.select(
826+
Field.of('tags')
827+
.arrayConcat(['newTag1', 'newTag2'])
828+
.as('modifiedTags')
829+
)
830+
.limit(1)
831+
.execute();
832+
expectResults(results, {
833+
modifiedTags: ['comedy', 'space', 'adventure', 'newTag1', 'newTag2']
834+
});
835+
});
696836

697837
it('testStrConcat', async () => {
698838
const results = await randomCol
@@ -917,6 +1057,7 @@ apiDescribe.only('Pipelines', persistence => {
9171057
it('testMapGet', async () => {
9181058
const results = await randomCol
9191059
.pipeline()
1060+
.sort(Field.of('published').descending())
9201061
.select(
9211062
Field.of('awards').mapGet('hugo').as('hugoAward'),
9221063
Field.of('awards').mapGet('others').as('others'),
@@ -931,7 +1072,7 @@ apiDescribe.only('Pipelines', persistence => {
9311072
title: "The Hitchhiker's Guide to the Galaxy",
9321073
others: { unknown: { year: 1980 } }
9331074
},
934-
{ hugoAward: true, title: 'Dune', others: null }
1075+
{ hugoAward: true, hugoAward2: true, title: 'Dune', others: null }
9351076
);
9361077
});
9371078

@@ -1108,7 +1249,7 @@ apiDescribe.only('Pipelines', persistence => {
11081249
orFunction(
11091250
andFunction(
11101251
Field.of('rating').eq(lastDoc.get('rating')),
1111-
Field.of('__path__').gt(lastDoc.ref?.path)
1252+
Field.of('__path__').gt(lastDoc.ref?.id)
11121253
),
11131254
Field.of('rating').lt(lastDoc.get('rating'))
11141255
)

0 commit comments

Comments
 (0)