Search in sources :

Example 6 with Timestamp

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

the class TypeTest method testCanUseTypedAccessors.

@Test
public void testCanUseTypedAccessors() {
    DocumentReference doc = testDoc();
    Map<String, Object> data = map("null", null, "boolean", true, "string", "string", "double", 0.0, "int", 1L, "geoPoint", new GeoPoint(1.24, 4.56), "blob", blob(0, 1, 2), "date", new Date(), "timestamp", new Timestamp(100, 123000000), "reference", doc);
    waitFor(doc.set(data));
    DocumentSnapshot snapshot = waitFor(doc.get());
    assertEquals(data.get("null"), snapshot.get("null"));
    assertEquals(data.get("null"), snapshot.get(FieldPath.fromDotSeparatedPath("null")));
    assertEquals(data.get("boolean"), snapshot.getBoolean("boolean"));
    assertEquals(data.get("string"), snapshot.getString("string"));
    assertEquals(data.get("double"), snapshot.getDouble("double"));
    assertEquals(Long.valueOf(0), snapshot.getLong("double"));
    assertEquals(data.get("int"), snapshot.getLong("int"));
    assertEquals(Double.valueOf(1.0), snapshot.getDouble("int"));
    assertEquals(data.get("geoPoint"), snapshot.getGeoPoint("geoPoint"));
    assertEquals(data.get("blob"), snapshot.getBlob("blob"));
    assertEquals(data.get("date"), snapshot.getDate("date"));
    assertEquals(new Timestamp((Date) data.get("date")), snapshot.getTimestamp("date"));
    assertEquals(data.get("timestamp"), snapshot.getTimestamp("timestamp"));
    Timestamp timestamp = (Timestamp) data.get("timestamp");
    assertEquals(timestamp.toDate(), snapshot.getDate("timestamp"));
    assertTrue(data.get("reference") instanceof DocumentReference);
    assertEquals(((DocumentReference) data.get("reference")).getPath(), doc.getPath());
}
Also used : Timestamp(com.google.firebase.Timestamp) Date(java.util.Date) Test(org.junit.Test)

Example 7 with Timestamp

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

the class TypeTest method testCanReadAndWriteDates.

@Test
public void testCanReadAndWriteDates() {
    Date date = new Date(1491847082123L);
    // Tests are set up to read back Timestamps, not Dates.
    verifySuccessfulWriteReadCycle(map("date", new Timestamp(date)), testDoc());
}
Also used : Timestamp(com.google.firebase.Timestamp) Date(java.util.Date) Test(org.junit.Test)

Example 8 with Timestamp

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

the class WriteBatchTest method testCanWriteTheSameDocumentMultipleTimes.

@Test
public void testCanWriteTheSameDocumentMultipleTimes() {
    DocumentReference doc = testDocument();
    EventAccumulator<DocumentSnapshot> accumulator = new EventAccumulator<>();
    doc.addSnapshotListener(MetadataChanges.INCLUDE, accumulator.listener());
    DocumentSnapshot initialSnap = accumulator.await();
    assertFalse(initialSnap.exists());
    waitFor(doc.getFirestore().batch().delete(doc).set(doc, map("a", 1, "b", 1, "when", "when")).update(doc, map("b", 2, "when", FieldValue.serverTimestamp())).commit());
    DocumentSnapshot localSnap = accumulator.await();
    assertTrue(localSnap.getMetadata().hasPendingWrites());
    assertEquals(map("a", 1L, "b", 2L, "when", null), localSnap.getData());
    DocumentSnapshot serverSnap = accumulator.await();
    assertFalse(serverSnap.getMetadata().hasPendingWrites());
    Timestamp when = serverSnap.getTimestamp("when");
    assertNotNull(when);
    assertEquals(map("a", 1L, "b", 2L, "when", when), serverSnap.getData());
}
Also used : EventAccumulator(com.google.firebase.firestore.testutil.EventAccumulator) Timestamp(com.google.firebase.Timestamp) Test(org.junit.Test)

Example 9 with Timestamp

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

the class FieldsTest method testTimestampsAreTruncated.

@Test
public void testTimestampsAreTruncated() {
    Timestamp originalTimestamp = new Timestamp(100, 123456789);
    // Timestamps are currently truncated to microseconds after being written to the database.
    Timestamp truncatedTimestamp = new Timestamp(originalTimestamp.getSeconds(), originalTimestamp.getNanoseconds() / 1000 * 1000);
    DocumentReference docRef = testCollection().document();
    waitFor(docRef.set(objectWithTimestamp(originalTimestamp)));
    DocumentSnapshot snapshot = waitFor(docRef.get());
    Map<String, Object> data = snapshot.getData();
    Timestamp readTimestamp = (Timestamp) snapshot.get("timestamp");
    assertThat(readTimestamp).isEqualTo(truncatedTimestamp);
    assertThat(readTimestamp).isEqualTo(data.get("timestamp"));
    Timestamp readNestedTimestamp = (Timestamp) snapshot.get("nested.timestamp2");
    assertThat(readNestedTimestamp).isEqualTo(truncatedTimestamp);
    @SuppressWarnings("unchecked") Map<String, Object> nestedObject = (Map<String, Object>) data.get("nested");
    assertThat(nestedObject.get("timestamp2")).isEqualTo(readNestedTimestamp);
}
Also used : Timestamp(com.google.firebase.Timestamp) Map(java.util.Map) Test(org.junit.Test)

Example 10 with Timestamp

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

the class ValuesTest method testCanonicalIds.

@Test
public void testCanonicalIds() {
    assertCanonicalId(TestUtil.wrap(null), "null");
    assertCanonicalId(TestUtil.wrap(true), "true");
    assertCanonicalId(TestUtil.wrap(false), "false");
    assertCanonicalId(TestUtil.wrap(1), "1");
    assertCanonicalId(TestUtil.wrap(1.0), "1.0");
    assertCanonicalId(TestUtil.wrap(new Timestamp(30, 1000)), "time(30,1000)");
    assertCanonicalId(TestUtil.wrap("a"), "a");
    assertCanonicalId(TestUtil.wrap(blob(1, 2, 3)), "010203");
    assertCanonicalId(wrapRef(dbId("p1", "d1"), key("c1/doc1")), "c1/doc1");
    assertCanonicalId(TestUtil.wrap(new GeoPoint(30, 60)), "geo(30.0,60.0)");
    assertCanonicalId(TestUtil.wrap(Arrays.asList(1, 2, 3)), "[1,2,3]");
    assertCanonicalId(TestUtil.wrap(map("a", 1, "b", 2, "c", "3")), "{a:1,b:2,c:3}");
    assertCanonicalId(TestUtil.wrap(map("a", Arrays.asList("b", map("c", new GeoPoint(30, 60))))), "{a:[b,{c:geo(30.0,60.0)}]}");
}
Also used : GeoPoint(com.google.firebase.firestore.GeoPoint) Timestamp(com.google.firebase.Timestamp) Test(org.junit.Test)

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