Search in sources :

Example 96 with Value

use of com.google.api.expr.v1alpha1.Value in project firebase-android-sdk by firebase.

the class Query method boundFromFields.

/**
 * Converts a list of field values to Bound.
 */
private Bound boundFromFields(String methodName, Object[] values, boolean inclusive) {
    // Use explicit order by's because it has to match the query the user made
    List<OrderBy> explicitOrderBy = query.getExplicitOrderBy();
    if (values.length > explicitOrderBy.size()) {
        throw new IllegalArgumentException("Too many arguments provided to " + methodName + "(). The number of arguments must be less " + "than or equal to the number of orderBy() clauses.");
    }
    List<Value> components = new ArrayList<>();
    for (int i = 0; i < values.length; i++) {
        Object rawValue = values[i];
        OrderBy orderBy = explicitOrderBy.get(i);
        if (orderBy.getField().equals(com.google.firebase.firestore.model.FieldPath.KEY_PATH)) {
            if (!(rawValue instanceof String)) {
                throw new IllegalArgumentException("Invalid query. Expected a string for document ID in " + methodName + "(), but got " + rawValue + ".");
            }
            String documentId = (String) rawValue;
            if (!query.isCollectionGroupQuery() && documentId.contains("/")) {
                throw new IllegalArgumentException("Invalid query. When querying a collection and ordering by FieldPath.documentId(), " + "the value passed to " + methodName + "() must be a plain document ID, but '" + documentId + "' contains a slash.");
            }
            ResourcePath path = query.getPath().append(ResourcePath.fromString(documentId));
            if (!DocumentKey.isDocumentKey(path)) {
                throw new IllegalArgumentException("Invalid query. When querying a collection group and ordering by " + "FieldPath.documentId(), the value passed to " + methodName + "() must result in a valid document path, but '" + path + "' is not because it contains an odd number of segments.");
            }
            DocumentKey key = DocumentKey.fromPath(path);
            components.add(Values.refValue(firestore.getDatabaseId(), key));
        } else {
            Value wrapped = firestore.getUserDataReader().parseQueryValue(rawValue);
            components.add(wrapped);
        }
    }
    return new Bound(components, inclusive);
}
Also used : OrderBy(com.google.firebase.firestore.core.OrderBy) ResourcePath(com.google.firebase.firestore.model.ResourcePath) Value(com.google.firestore.v1.Value) ArrayValue(com.google.firestore.v1.ArrayValue) ArrayList(java.util.ArrayList) DocumentKey(com.google.firebase.firestore.model.DocumentKey) Bound(com.google.firebase.firestore.core.Bound)

Example 97 with Value

use of com.google.api.expr.v1alpha1.Value in project firebase-android-sdk by firebase.

the class MutationTest method testNumericIncrementBaseValue.

@Test
public void testNumericIncrementBaseValue() {
    Map<String, Object> allValues = map("ignore", "foo", "double", 42.0, "long", 42, "string", "foo", "map", map());
    allValues.put("nested", new HashMap<>(allValues));
    MutableDocument baseDoc = doc("collection/key", 1, allValues);
    Map<String, Object> allTransforms = map("double", FieldValue.increment(1), "long", FieldValue.increment(1), "string", FieldValue.increment(1), "map", FieldValue.increment(1), "missing", FieldValue.increment(1));
    allTransforms.put("nested", new HashMap<>(allTransforms));
    Mutation mutation = patchMutation("collection/key", allTransforms);
    ObjectValue baseValue = mutation.extractTransformBaseValue(baseDoc);
    Value expected = wrap(map("double", 42.0, "long", 42, "string", 0, "map", 0, "missing", 0, "nested", map("double", 42.0, "long", 42, "string", 0, "map", 0, "missing", 0)));
    assertTrue(Values.equals(expected, baseValue.get(FieldPath.EMPTY_PATH)));
}
Also used : ObjectValue(com.google.firebase.firestore.model.ObjectValue) MutableDocument(com.google.firebase.firestore.model.MutableDocument) ObjectValue(com.google.firebase.firestore.model.ObjectValue) FieldValue(com.google.firebase.firestore.FieldValue) Value(com.google.firestore.v1.Value) TestUtil.wrapObject(com.google.firebase.firestore.testutil.TestUtil.wrapObject) TestUtil.deleteMutation(com.google.firebase.firestore.testutil.TestUtil.deleteMutation) TestUtil.setMutation(com.google.firebase.firestore.testutil.TestUtil.setMutation) TestUtil.patchMutation(com.google.firebase.firestore.testutil.TestUtil.patchMutation) TestUtil.mergeMutation(com.google.firebase.firestore.testutil.TestUtil.mergeMutation) Mutation.calculateOverlayMutation(com.google.firebase.firestore.model.mutation.Mutation.calculateOverlayMutation) Test(org.junit.Test)

Example 98 with Value

use of com.google.api.expr.v1alpha1.Value in project firebase-android-sdk by firebase.

the class MutationTest method testAppliesLocalServerTimestampTransformsToDocuments.

@Test
public void testAppliesLocalServerTimestampTransformsToDocuments() {
    Map<String, Object> data = map("foo", map("bar", "bar-value"), "baz", "baz-value");
    MutableDocument transformedDoc = doc("collection/key", 1, data);
    Timestamp timestamp = Timestamp.now();
    Mutation transform = patchMutation("collection/key", map("foo.bar", FieldValue.serverTimestamp()));
    transform.applyToLocalView(transformedDoc, /* previousMask= */
    null, timestamp);
    // Server timestamps aren't parsed, so we manually insert it.
    ObjectValue expectedData = wrapObject(map("foo", map("bar", "<server-timestamp>"), "baz", "baz-value"));
    Value fieldValue = ServerTimestamps.valueOf(timestamp, wrap("bar-value"));
    expectedData.set(field("foo.bar"), fieldValue);
    MutableDocument expectedDoc = doc("collection/key", 1, expectedData).setHasLocalMutations();
    assertEquals(expectedDoc, transformedDoc);
}
Also used : ObjectValue(com.google.firebase.firestore.model.ObjectValue) MutableDocument(com.google.firebase.firestore.model.MutableDocument) ObjectValue(com.google.firebase.firestore.model.ObjectValue) FieldValue(com.google.firebase.firestore.FieldValue) Value(com.google.firestore.v1.Value) TestUtil.wrapObject(com.google.firebase.firestore.testutil.TestUtil.wrapObject) TestUtil.deleteMutation(com.google.firebase.firestore.testutil.TestUtil.deleteMutation) TestUtil.setMutation(com.google.firebase.firestore.testutil.TestUtil.setMutation) TestUtil.patchMutation(com.google.firebase.firestore.testutil.TestUtil.patchMutation) TestUtil.mergeMutation(com.google.firebase.firestore.testutil.TestUtil.mergeMutation) Mutation.calculateOverlayMutation(com.google.firebase.firestore.model.mutation.Mutation.calculateOverlayMutation) Timestamp(com.google.firebase.Timestamp) Test(org.junit.Test)

Example 99 with Value

use of com.google.api.expr.v1alpha1.Value in project firebase-android-sdk by firebase.

the class RemoteSerializerTest method testEncodesDoubles.

@Test
public void testEncodesDoubles() {
    List<Double> tests = asList(Double.NEGATIVE_INFINITY, -Double.MAX_VALUE, Long.MAX_VALUE * -1.0 - 1.0, -2.0, -1.1, -1.0, -Double.MIN_VALUE, -Double.MIN_NORMAL, -0.0, 0.0, Double.MIN_NORMAL, Double.MIN_VALUE, 0.1, 1.1, Long.MAX_VALUE * 1.0, Double.MAX_VALUE, Double.POSITIVE_INFINITY);
    for (Double test : tests) {
        Value value = wrap(test);
        Value proto = Value.newBuilder().setDoubleValue(test).build();
        assertRoundTrip(value, proto, Value.ValueTypeCase.DOUBLE_VALUE);
    }
}
Also used : ObjectValue(com.google.firebase.firestore.model.ObjectValue) Values.refValue(com.google.firebase.firestore.model.Values.refValue) NullValue(com.google.protobuf.NullValue) ServerValue(com.google.firestore.v1.DocumentTransform.FieldTransform.ServerValue) Value(com.google.firestore.v1.Value) Int32Value(com.google.protobuf.Int32Value) MapValue(com.google.firestore.v1.MapValue) ArrayValue(com.google.firestore.v1.ArrayValue) Test(org.junit.Test)

Example 100 with Value

use of com.google.api.expr.v1alpha1.Value in project firebase-android-sdk by firebase.

the class RemoteSerializerTest method testEncodesDates.

@Test
public void testEncodesDates() {
    Calendar date1 = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
    date1.set(2016, 0, 2, 10, 20, 50);
    date1.set(Calendar.MILLISECOND, 500);
    Calendar date2 = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
    date2.set(2016, 5, 17, 10, 50, 15);
    date2.set(Calendar.MILLISECOND, 0);
    List<Date> tests = asList(date1.getTime(), date2.getTime());
    Timestamp ts1 = Timestamp.newBuilder().setNanos(500000000).setSeconds(1451730050).build();
    Timestamp ts2 = Timestamp.newBuilder().setNanos(0).setSeconds(1466160615).build();
    List<Value> expected = asList(Value.newBuilder().setTimestampValue(ts1).build(), Value.newBuilder().setTimestampValue(ts2).build());
    for (int i = 0; i < tests.size(); i++) {
        Value value = wrap(tests.get(i));
        assertRoundTrip(value, expected.get(i), Value.ValueTypeCase.TIMESTAMP_VALUE);
    }
}
Also used : Calendar(java.util.Calendar) ObjectValue(com.google.firebase.firestore.model.ObjectValue) Values.refValue(com.google.firebase.firestore.model.Values.refValue) NullValue(com.google.protobuf.NullValue) ServerValue(com.google.firestore.v1.DocumentTransform.FieldTransform.ServerValue) Value(com.google.firestore.v1.Value) Int32Value(com.google.protobuf.Int32Value) MapValue(com.google.firestore.v1.MapValue) ArrayValue(com.google.firestore.v1.ArrayValue) Timestamp(com.google.protobuf.Timestamp) Date(java.util.Date) GeoPoint(com.google.firebase.firestore.GeoPoint) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)126 Value (com.google.firestore.v1.Value)108 ArrayValue (com.google.firestore.v1.ArrayValue)73 LinkedHashSet (java.util.LinkedHashSet)71 ObjectValue (com.google.firebase.firestore.model.ObjectValue)53 NullValue (com.google.protobuf.NullValue)50 MapValue (com.google.firestore.v1.MapValue)47 ArrayList (java.util.ArrayList)33 HashMap (java.util.HashMap)25 Value (com.google.datastore.v1.Value)20 Map (java.util.Map)20 List (java.util.List)18 TableFieldSchema (com.google.api.services.bigquery.model.TableFieldSchema)17 Record (org.apache.avro.generic.GenericData.Record)16 SchemaAndRecord (org.apache.beam.sdk.io.gcp.bigquery.SchemaAndRecord)16 CoreMatchers.notNullValue (org.hamcrest.CoreMatchers.notNullValue)16 Set (java.util.Set)14 TestUtil.wrapObject (com.google.firebase.firestore.testutil.TestUtil.wrapObject)13 Nullable (androidx.annotation.Nullable)10 Value (com.google.privacy.dlp.v2.Value)9