Skip to content

Commit 3475835

Browse files
committed
chore: add e2e test for vector value
1 parent c582b0a commit 3475835

File tree

3 files changed

+140
-35
lines changed

3 files changed

+140
-35
lines changed

packages/firestore/__tests__/firestore.test.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1431,4 +1431,41 @@ describe('Firestore', function () {
14311431
});
14321432
});
14331433
});
1434+
1435+
describe('VectorValue (unit serializer)', function () {
1436+
it('constructs and validates values', function () {
1437+
const { default: FirestoreVectorValue } = require('../lib/FirestoreVectorValue');
1438+
const v = new FirestoreVectorValue([0, 1.5, -2]);
1439+
expect(v.values).toEqual([0, 1.5, -2]);
1440+
expect(v.isEqual(new FirestoreVectorValue([0, 1.5, -2]))).toBe(true);
1441+
expect(v.isEqual(new FirestoreVectorValue([0, 1.5]))).toBe(false);
1442+
});
1443+
1444+
it('serializes to type map and parses back', function () {
1445+
const { default: FirestoreVectorValue } = require('../lib/FirestoreVectorValue');
1446+
const serialize = require('../lib/utils/serialize');
1447+
const { getTypeMapName } = require('../lib/utils/typemap');
1448+
1449+
const v = new FirestoreVectorValue([0.1, 0.2, 0.3]);
1450+
const typed = serialize.generateNativeData(v, false);
1451+
expect(Array.isArray(typed)).toBe(true);
1452+
expect(getTypeMapName(typed[0])).toBe('vector');
1453+
const parsed = serialize.parseNativeData(null, typed);
1454+
expect(parsed instanceof FirestoreVectorValue).toBe(true);
1455+
expect(parsed.values).toEqual([0.1, 0.2, 0.3]);
1456+
});
1457+
1458+
it('serializes inside objects and arrays', function () {
1459+
const { default: FirestoreVectorValue } = require('../lib/FirestoreVectorValue');
1460+
const serialize = require('../lib/utils/serialize');
1461+
const { getTypeMapName } = require('../lib/utils/typemap');
1462+
1463+
const v = new FirestoreVectorValue([1, 2, 3]);
1464+
const map = serialize.buildNativeMap({ a: v }, false);
1465+
expect(getTypeMapName(map.a[0])).toBe('vector');
1466+
1467+
const arr = serialize.buildNativeArray([v], false);
1468+
expect(getTypeMapName(arr[0][0])).toBe('vector');
1469+
});
1470+
});
14341471
});

packages/firestore/__tests__/vector.test.ts

Lines changed: 0 additions & 35 deletions
This file was deleted.
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* Copyright (c) 2016-present Invertase Limited & Contributors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this library except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
describe('firestore.VectorValue', function () {
19+
describe('v8 compatibility', function () {
20+
beforeEach(async function beforeEachTest() {
21+
// @ts-ignore
22+
globalThis.RNFB_SILENCE_MODULAR_DEPRECATION_WARNINGS = true;
23+
});
24+
25+
afterEach(async function afterEachTest() {
26+
// @ts-ignore
27+
globalThis.RNFB_SILENCE_MODULAR_DEPRECATION_WARNINGS = false;
28+
});
29+
30+
function ref(id) {
31+
return firebase.firestore().doc(`e2e_vector/${id}`);
32+
}
33+
34+
it('writes and reads a vector', async function () {
35+
const r = ref('basic');
36+
await r.set({ embedding: firebase.firestore.vector([0.12, 0.34, 0.56]) });
37+
38+
const snap = await r.get();
39+
const v = snap.get('embedding');
40+
should.exist(v);
41+
v.values.should.eql([0.12, 0.34, 0.56]);
42+
});
43+
44+
it('supports vectors in nested structures', async function () {
45+
const r = ref('nested');
46+
await r.set({
47+
a: { b: firebase.firestore.vector([1, 2, 3]) },
48+
arr: [firebase.firestore.vector([4, 5])],
49+
});
50+
51+
const snap = await r.get();
52+
snap.get('a').b.values.should.eql([1, 2, 3]);
53+
snap.get('arr')[0].values.should.eql([4, 5]);
54+
});
55+
56+
it('updates a vector field', async function () {
57+
const r = ref('update');
58+
await r.set({ x: 1 });
59+
await r.update({ embedding: firebase.firestore.vector([9, 8, 7]) });
60+
61+
const snap = await r.get();
62+
snap.get('embedding').values.should.eql([9, 8, 7]);
63+
});
64+
65+
it('batch writes a vector', async function () {
66+
const r = ref('batch');
67+
const batch = firebase.firestore().batch();
68+
batch.set(r, { embedding: firebase.firestore.vector([0.1, 0.2]) });
69+
await batch.commit();
70+
71+
const snap = await r.get();
72+
snap.get('embedding').values.should.eql([0.1, 0.2]);
73+
});
74+
75+
it('transaction writes a vector', async function () {
76+
const r = ref('transaction');
77+
await firebase.firestore().runTransaction(async tx => {
78+
tx.set(r, { embedding: firebase.firestore.vector([3.14, 2.72]) });
79+
});
80+
81+
const snap = await r.get();
82+
snap.get('embedding').values.should.eql([3.14, 2.72]);
83+
});
84+
});
85+
86+
describe('modular', function () {
87+
function ref(id) {
88+
// @ts-ignore test env provides firestoreModular
89+
return firestoreModular.doc(firestoreModular.getFirestore(), `e2e_vector/${id}`);
90+
}
91+
92+
it('writes and reads using modular vector()', async function () {
93+
// @ts-ignore test env provides firestoreModular
94+
const { setDoc, getDoc, vector } = firestoreModular;
95+
const r = ref('modular-basic');
96+
await setDoc(r, { embedding: vector([0.5, 0.25]) });
97+
const snap = await getDoc(r);
98+
const v = snap.get('embedding');
99+
should.exist(v);
100+
v.values.should.eql([0.5, 0.25]);
101+
});
102+
});
103+
});

0 commit comments

Comments
 (0)