Search in sources :

Example 11 with Timestamp

use of com.google.firebase.Timestamp in project firebase-android-sdk by firebase.

the class ValuesTest method testValueEquality.

@Test
public void testValueEquality() {
    GeoPoint geoPoint1 = new GeoPoint(1, 0);
    GeoPoint geoPoint2 = new GeoPoint(0, 2);
    Timestamp timestamp1 = new Timestamp(date1);
    Timestamp timestamp2 = new Timestamp(date2);
    new EqualsTester().addEqualityGroup(wrap(true), wrap(true)).addEqualityGroup(wrap(false), wrap(false)).addEqualityGroup(wrap((Object) null), wrap(Values.NULL_VALUE)).addEqualityGroup(wrap(0.0 / 0.0), wrap(Double.longBitsToDouble(0x7ff8000000000000L)), wrap(Values.NAN_VALUE)).addEqualityGroup(wrap(-0.0)).addEqualityGroup(wrap(0.0)).addEqualityGroup(wrap(1), wrap(1)).addEqualityGroup(wrap(1.0), wrap(1.0)).addEqualityGroup(wrap(1.1), wrap(1.1)).addEqualityGroup(wrap(blob(0, 1, 2)), wrap(blob(0, 1, 2))).addEqualityGroup(wrap(blob(0, 1))).addEqualityGroup(wrap("string"), wrap("string")).addEqualityGroup(wrap("strin")).addEqualityGroup(wrap("e\u0301b")).addEqualityGroup(wrap("\u00e9a")).addEqualityGroup(wrap(date1), wrap(timestamp1)).addEqualityGroup(wrap(timestamp2)).addEqualityGroup(wrap(ServerTimestamps.valueOf(new Timestamp(date1), null)), wrap(ServerTimestamps.valueOf(new Timestamp(date1), null))).addEqualityGroup(wrap(ServerTimestamps.valueOf(new Timestamp(date2), null))).addEqualityGroup(wrap(geoPoint1), wrap(new GeoPoint(1, 0))).addEqualityGroup(wrap(geoPoint2)).addEqualityGroup(wrap(ref("coll/doc1")), wrap(wrapRef(dbId("project"), key("coll/doc1")))).addEqualityGroup(wrap(wrapRef(dbId("project", "bar"), key("coll/doc2")))).addEqualityGroup(wrap(wrapRef(dbId("project", "baz"), key("coll/doc2")))).addEqualityGroup(wrap(Arrays.asList("foo", "bar")), wrap(Arrays.asList("foo", "bar"))).addEqualityGroup(wrap(Arrays.asList("foo", "bar", "baz"))).addEqualityGroup(wrap(Arrays.asList("foo"))).addEqualityGroup(wrap(map("bar", 1, "foo", 2)), wrap(map("foo", 2, "bar", 1))).addEqualityGroup(wrap(map("bar", 2, "foo", 1))).addEqualityGroup(wrap(map("bar", 1))).addEqualityGroup(wrap(map("foo", 1))).testEquals();
}
Also used : GeoPoint(com.google.firebase.firestore.GeoPoint) EqualsTester(com.google.common.testing.EqualsTester) Timestamp(com.google.firebase.Timestamp) Test(org.junit.Test)

Example 12 with Timestamp

use of com.google.firebase.Timestamp 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 13 with Timestamp

use of com.google.firebase.Timestamp in project firebase-android-sdk by firebase.

the class SQLiteTargetCacheTest method testMetadataPersistedAcrossRestarts.

@Test
public void testMetadataPersistedAcrossRestarts() {
    String name = "test-targetCache-restarts";
    SQLitePersistence db1 = PersistenceTestHelpers.createSQLitePersistence(name);
    TargetCache targetCache1 = db1.getTargetCache();
    assertEquals(0, targetCache1.getHighestListenSequenceNumber());
    long originalSequenceNumber = 1234;
    int targetId = 5;
    SnapshotVersion snapshotVersion = new SnapshotVersion(new Timestamp(1, 2));
    Query query = query("rooms");
    TargetData targetData = new TargetData(query.toTarget(), targetId, originalSequenceNumber, QueryPurpose.LISTEN);
    db1.runTransaction("add query data", () -> {
        targetCache1.addTargetData(targetData);
        targetCache1.setLastRemoteSnapshotVersion(snapshotVersion);
    });
    db1.shutdown();
    SQLitePersistence db2 = PersistenceTestHelpers.createSQLitePersistence(name);
    db2.runTransaction("verify sequence number", () -> {
        long newSequenceNumber = db2.getReferenceDelegate().getCurrentSequenceNumber();
        assertTrue(newSequenceNumber > originalSequenceNumber);
    });
    TargetCache targetCache2 = db2.getTargetCache();
    assertEquals(targetId, targetCache2.getHighestTargetId());
    assertEquals(snapshotVersion, targetCache2.getLastRemoteSnapshotVersion());
    assertEquals(1, targetCache2.getTargetCount());
    db2.shutdown();
}
Also used : Query(com.google.firebase.firestore.core.Query) SnapshotVersion(com.google.firebase.firestore.model.SnapshotVersion) Timestamp(com.google.firebase.Timestamp) Test(org.junit.Test)

Example 14 with Timestamp

use of com.google.firebase.Timestamp in project firebase-android-sdk by firebase.

the class DocumentSnapshot method getDate.

/**
 * Returns the value of the field as a Date.
 *
 * @param field The path to the field.
 * @param serverTimestampBehavior Configures the behavior for server timestamps that have not yet
 *     been set to their final value.
 * @throws RuntimeException if the value is not a Date.
 * @return The value of the field
 */
@Nullable
public Date getDate(@NonNull String field, @NonNull ServerTimestampBehavior serverTimestampBehavior) {
    checkNotNull(field, "Provided field path must not be null.");
    checkNotNull(serverTimestampBehavior, "Provided serverTimestampBehavior value must not be null.");
    @Nullable Timestamp timestamp = getTimestamp(field, serverTimestampBehavior);
    return timestamp != null ? timestamp.toDate() : null;
}
Also used : Timestamp(com.google.firebase.Timestamp) Nullable(androidx.annotation.Nullable) Nullable(androidx.annotation.Nullable)

Example 15 with Timestamp

use of com.google.firebase.Timestamp in project firebase-android-sdk by firebase.

the class BundleSerializer method decodeTimestamp.

private Timestamp decodeTimestamp(String timestamp) {
    try {
        int dayOffset = timestamp.indexOf('T');
        if (dayOffset == -1) {
            throw new IllegalArgumentException("Invalid timestamp: " + timestamp);
        }
        int timezoneOffsetPosition = timestamp.indexOf('Z', dayOffset);
        if (timezoneOffsetPosition == -1) {
            timezoneOffsetPosition = timestamp.indexOf('+', dayOffset);
        }
        if (timezoneOffsetPosition == -1) {
            timezoneOffsetPosition = timestamp.indexOf('-', dayOffset);
        }
        if (timezoneOffsetPosition == -1) {
            throw new IllegalArgumentException("Invalid timestamp: Missing valid timezone offset: " + timestamp);
        }
        // Parse seconds and nanos.
        String timeValue = timestamp.substring(0, timezoneOffsetPosition);
        String secondValue = timeValue;
        String nanoValue = "";
        int pointPosition = timeValue.indexOf('.');
        if (pointPosition != -1) {
            secondValue = timeValue.substring(0, pointPosition);
            nanoValue = timeValue.substring(pointPosition + 1);
        }
        Date date = timestampFormat.parse(secondValue);
        long seconds = date.getTime() / MILLIS_PER_SECOND;
        int nanos = nanoValue.isEmpty() ? 0 : parseNanos(nanoValue);
        // Parse timezone offsets.
        if (timestamp.charAt(timezoneOffsetPosition) == 'Z') {
            if (timestamp.length() != timezoneOffsetPosition + 1) {
                throw new IllegalArgumentException("Invalid timestamp: Invalid trailing data \"" + timestamp.substring(timezoneOffsetPosition) + "\"");
            }
        } else {
            String offsetValue = timestamp.substring(timezoneOffsetPosition + 1);
            long offset = decodeTimezoneOffset(offsetValue);
            if (timestamp.charAt(timezoneOffsetPosition) == '+') {
                seconds -= offset;
            } else {
                seconds += offset;
            }
        }
        return new Timestamp(seconds, nanos);
    } catch (ParseException e) {
        throw new IllegalArgumentException("Failed to parse timestamp", e);
    }
}
Also used : ByteString(com.google.protobuf.ByteString) ParseException(java.text.ParseException) Timestamp(com.google.firebase.Timestamp) Date(java.util.Date)

Aggregations

Timestamp (com.google.firebase.Timestamp)36 Test (org.junit.Test)18 MutableDocument (com.google.firebase.firestore.model.MutableDocument)7 Date (java.util.Date)6 DocumentKey (com.google.firebase.firestore.model.DocumentKey)5 SnapshotVersion (com.google.firebase.firestore.model.SnapshotVersion)5 ArrayList (java.util.ArrayList)5 GeoPoint (com.google.firebase.firestore.GeoPoint)4 ObjectValue (com.google.firebase.firestore.model.ObjectValue)4 TestUtil.wrapObject (com.google.firebase.firestore.testutil.TestUtil.wrapObject)4 Mutation (com.google.firebase.firestore.model.mutation.Mutation)3 Mutation.calculateOverlayMutation (com.google.firebase.firestore.model.mutation.Mutation.calculateOverlayMutation)3 TestUtil.deleteMutation (com.google.firebase.firestore.testutil.TestUtil.deleteMutation)3 TestUtil.mergeMutation (com.google.firebase.firestore.testutil.TestUtil.mergeMutation)3 TestUtil.patchMutation (com.google.firebase.firestore.testutil.TestUtil.patchMutation)3 TestUtil.setMutation (com.google.firebase.firestore.testutil.TestUtil.setMutation)3 Value (com.google.firestore.v1.Value)3 HashMap (java.util.HashMap)3 FieldValue (com.google.firebase.firestore.FieldValue)2 Query (com.google.firebase.firestore.core.Query)2