Skip to content

Commit b243f1c

Browse files
committed
tests
1 parent b0d13a7 commit b243f1c

File tree

2 files changed

+101
-1
lines changed

2 files changed

+101
-1
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
// Copyright 2024 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package com.google.firebase.firestore;
16+
17+
import com.google.firebase.firestore.index.DirectionalIndexByteEncoder;
18+
import com.google.firebase.firestore.index.FirestoreIndexValueWriter;
19+
import com.google.firebase.firestore.index.IndexByteEncoder;
20+
import com.google.firebase.firestore.model.DatabaseId;
21+
import com.google.firebase.firestore.model.FieldIndex;
22+
import com.google.firestore.v1.Value;
23+
import java.util.concurrent.ExecutionException;
24+
import org.junit.Assert;
25+
import org.junit.Test;
26+
import org.junit.runner.RunWith;
27+
import org.robolectric.RobolectricTestRunner;
28+
29+
@RunWith(RobolectricTestRunner.class)
30+
public class FirestoreIndexValueWriterTest {
31+
32+
@Test
33+
public void writeIndexValueSupportsVector() throws ExecutionException, InterruptedException {
34+
UserDataReader dataReader = new UserDataReader(DatabaseId.EMPTY);
35+
Value value = dataReader.parseQueryValue(FieldValue.vector(new double[] {1, 2, 3}));
36+
37+
IndexByteEncoder encoder = new IndexByteEncoder();
38+
FirestoreIndexValueWriter.INSTANCE.writeIndexValue(
39+
value, encoder.forKind(FieldIndex.Segment.Kind.ASCENDING));
40+
byte[] actualBytes = encoder.getEncodedBytes();
41+
42+
IndexByteEncoder expectedEncoder = new IndexByteEncoder();
43+
DirectionalIndexByteEncoder expectedDirectionalEncoder =
44+
expectedEncoder.forKind(FieldIndex.Segment.Kind.ASCENDING);
45+
expectedDirectionalEncoder.writeLong(
46+
FirestoreIndexValueWriter.INDEX_TYPE_VECTOR); // Vector type
47+
expectedDirectionalEncoder.writeLong(
48+
FirestoreIndexValueWriter.INDEX_TYPE_NUMBER); // Number type
49+
expectedDirectionalEncoder.writeLong(3); // vector length
50+
expectedDirectionalEncoder.writeLong(
51+
FirestoreIndexValueWriter.INDEX_TYPE_STRING); // String type
52+
expectedDirectionalEncoder.writeString("value"); // Vector value header
53+
expectedDirectionalEncoder.writeLong(FirestoreIndexValueWriter.INDEX_TYPE_ARRAY); // Array type
54+
expectedDirectionalEncoder.writeLong(
55+
FirestoreIndexValueWriter.INDEX_TYPE_NUMBER); // Number type
56+
expectedDirectionalEncoder.writeDouble(1); // position 0
57+
expectedDirectionalEncoder.writeLong(
58+
FirestoreIndexValueWriter.INDEX_TYPE_NUMBER); // Number type
59+
expectedDirectionalEncoder.writeDouble(2); // position 1
60+
expectedDirectionalEncoder.writeLong(
61+
FirestoreIndexValueWriter.INDEX_TYPE_NUMBER); // Number type
62+
expectedDirectionalEncoder.writeDouble(3); // position 2
63+
expectedDirectionalEncoder.writeLong(
64+
FirestoreIndexValueWriter.NOT_TRUNCATED); // Array not truncated
65+
expectedDirectionalEncoder.writeInfinity();
66+
byte[] expectedBytes = expectedEncoder.getEncodedBytes();
67+
68+
Assert.assertArrayEquals(actualBytes, expectedBytes);
69+
}
70+
71+
@Test
72+
public void writeIndexValueSupportsEmptyVector() throws ExecutionException, InterruptedException {
73+
UserDataReader dataReader = new UserDataReader(DatabaseId.EMPTY);
74+
Value value = dataReader.parseQueryValue(FieldValue.vector(new double[] {}));
75+
76+
IndexByteEncoder encoder = new IndexByteEncoder();
77+
FirestoreIndexValueWriter.INSTANCE.writeIndexValue(
78+
value, encoder.forKind(FieldIndex.Segment.Kind.ASCENDING));
79+
byte[] actualBytes = encoder.getEncodedBytes();
80+
81+
IndexByteEncoder expectedEncoder = new IndexByteEncoder();
82+
DirectionalIndexByteEncoder expectedDirectionalEncoder =
83+
expectedEncoder.forKind(FieldIndex.Segment.Kind.ASCENDING);
84+
expectedDirectionalEncoder.writeLong(
85+
FirestoreIndexValueWriter.INDEX_TYPE_VECTOR); // Vector type
86+
expectedDirectionalEncoder.writeLong(
87+
FirestoreIndexValueWriter.INDEX_TYPE_NUMBER); // Number type
88+
expectedDirectionalEncoder.writeLong(0); // vector length
89+
expectedDirectionalEncoder.writeLong(
90+
FirestoreIndexValueWriter.INDEX_TYPE_STRING); // String type
91+
expectedDirectionalEncoder.writeString("value"); // Vector value header
92+
expectedDirectionalEncoder.writeLong(FirestoreIndexValueWriter.INDEX_TYPE_ARRAY); // Array type
93+
expectedDirectionalEncoder.writeLong(
94+
FirestoreIndexValueWriter.NOT_TRUNCATED); // Array not truncated
95+
expectedDirectionalEncoder.writeInfinity();
96+
byte[] expectedBytes = expectedEncoder.getEncodedBytes();
97+
98+
Assert.assertArrayEquals(actualBytes, expectedBytes);
99+
}
100+
}

firebase-firestore/src/test/java/com/google/firebase/firestore/model/ValuesTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ public void testValueOrdering() {
201201
.addEqualityGroup(wrap(Arrays.asList("foo", "0")))
202202

203203
// vector
204-
// .addEqualityGroup(wrap(FieldValue.vector(new double[] {})))
204+
.addEqualityGroup(wrap(FieldValue.vector(new double[] {})))
205205
.addEqualityGroup(wrap(FieldValue.vector(new double[] {100})))
206206
.addEqualityGroup(wrap(FieldValue.vector(new double[] {1, 2, 3})))
207207
.addEqualityGroup(wrap(FieldValue.vector(new double[] {1, 3, 2})))

0 commit comments

Comments
 (0)