Search in sources :

Example 1 with EventAccumulator

use of com.google.firebase.firestore.testutil.EventAccumulator 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 2 with EventAccumulator

use of com.google.firebase.firestore.testutil.EventAccumulator in project firebase-android-sdk by firebase.

the class BundleTest method testLoadWithDocumentsThatAreAlreadyPulledFromBackend.

@Test
public void testLoadWithDocumentsThatAreAlreadyPulledFromBackend() throws Exception {
    Task<Void> docA = db.document("coll-1/a").set(map("bar", "newValueA"));
    Tasks.await(docA);
    Task<Void> docB = db.document("coll-1/b").set(map("bar", "newValueB"));
    Tasks.await(docB);
    EventAccumulator<QuerySnapshot> accumulator = new EventAccumulator<>();
    ListenerRegistration listenerRegistration = null;
    try {
        listenerRegistration = db.collection("coll-1").addSnapshotListener(accumulator.listener());
        accumulator.awaitRemoteEvent();
        ByteBuffer bundle = ByteBuffer.wrap(createBundle());
        // Test the ByteBuffer overload
        LoadBundleTask bundleTask = db.loadBundle(bundle);
        LoadBundleTaskProgress result = Tasks.await(bundleTask);
        verifySuccessProgress(result);
        // The test bundle is holding ancient documents, so no events are generated as a result.
        // The case where a bundle has newer doc than cache can only be tested in spec tests.
        accumulator.assertNoAdditionalEvents();
        CollectionReference collectionQuery = db.collection("coll-1");
        QuerySnapshot collectionSnapshot = Tasks.await(collectionQuery.get(Source.CACHE));
        assertEquals(asList(map("bar", "newValueA"), map("bar", "newValueB")), querySnapshotToValues(collectionSnapshot));
        Query limitQuery = Tasks.await(db.getNamedQuery("limit"));
        QuerySnapshot limitSnapshot = Tasks.await(limitQuery.get(Source.CACHE));
        assertEquals(1, limitSnapshot.size());
        Query limitToLastQuery = Tasks.await(db.getNamedQuery("limit-to-last"));
        QuerySnapshot limitToLastSnapshot = Tasks.await(limitToLastQuery.get(Source.CACHE));
        assertEquals(1, limitToLastSnapshot.size());
    } finally {
        if (listenerRegistration != null) {
            listenerRegistration.remove();
        }
    }
}
Also used : EventAccumulator(com.google.firebase.firestore.testutil.EventAccumulator) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 3 with EventAccumulator

use of com.google.firebase.firestore.testutil.EventAccumulator in project firebase-android-sdk by firebase.

the class WriteBatchTest method testWriteTheSameServerTimestampAcrossWrites.

@Test
public void testWriteTheSameServerTimestampAcrossWrites() {
    CollectionReference collection = testCollection();
    DocumentReference docA = collection.document("a");
    DocumentReference docB = collection.document("b");
    EventAccumulator<QuerySnapshot> accumulator = new EventAccumulator<>();
    collection.addSnapshotListener(MetadataChanges.INCLUDE, accumulator.listener());
    QuerySnapshot initialSnap = accumulator.await();
    assertEquals(0, initialSnap.size());
    // Atomically write two documents with server timestamps.
    waitFor(collection.getFirestore().batch().set(docA, map("when", FieldValue.serverTimestamp())).set(docB, map("when", FieldValue.serverTimestamp())).commit());
    QuerySnapshot localSnap = accumulator.await();
    assertTrue(localSnap.getMetadata().hasPendingWrites());
    assertEquals(asList(map("when", null), map("when", null)), querySnapshotToValues(localSnap));
    QuerySnapshot serverSnap = accumulator.awaitRemoteEvent();
    assertFalse(serverSnap.getMetadata().hasPendingWrites());
    assertEquals(2, serverSnap.size());
    Timestamp when = serverSnap.getDocuments().get(0).getTimestamp("when");
    assertNotNull(when);
    assertEquals(asList(map("when", when), map("when", when)), querySnapshotToValues(serverSnap));
}
Also used : EventAccumulator(com.google.firebase.firestore.testutil.EventAccumulator) Timestamp(com.google.firebase.Timestamp) Test(org.junit.Test)

Example 4 with EventAccumulator

use of com.google.firebase.firestore.testutil.EventAccumulator in project firebase-android-sdk by firebase.

the class WriteBatchTest method testBatchesFailAtomicallyRaisingCorrectEvents.

@Test
public void testBatchesFailAtomicallyRaisingCorrectEvents() {
    CollectionReference collection = testCollection();
    DocumentReference docA = collection.document("a");
    DocumentReference docB = collection.document("b");
    EventAccumulator<QuerySnapshot> accumulator = new EventAccumulator<>();
    collection.addSnapshotListener(MetadataChanges.INCLUDE, accumulator.listener());
    QuerySnapshot initialSnap = accumulator.await();
    assertEquals(0, initialSnap.size());
    // Atomically write 1 document and update a nonexistent document.
    Exception err = waitForException(collection.getFirestore().batch().set(docA, map("a", 1)).update(docB, map("b", 2)).commit());
    // Local event with the set document.
    QuerySnapshot localSnap = accumulator.await();
    assertTrue(localSnap.getMetadata().hasPendingWrites());
    assertEquals(asList(map("a", 1L)), querySnapshotToValues(localSnap));
    // Server event with the set reverted
    QuerySnapshot serverSnap = accumulator.await();
    assertFalse(serverSnap.getMetadata().hasPendingWrites());
    assertEquals(0, serverSnap.size());
    assertNotNull(err);
    assertTrue(err instanceof FirebaseFirestoreException);
    assertEquals(Code.NOT_FOUND, ((FirebaseFirestoreException) err).getCode());
}
Also used : EventAccumulator(com.google.firebase.firestore.testutil.EventAccumulator) IntegrationTestUtil.waitForException(com.google.firebase.firestore.testutil.IntegrationTestUtil.waitForException) Test(org.junit.Test)

Aggregations

EventAccumulator (com.google.firebase.firestore.testutil.EventAccumulator)4 Test (org.junit.Test)4 Timestamp (com.google.firebase.Timestamp)2 IntegrationTestUtil.waitForException (com.google.firebase.firestore.testutil.IntegrationTestUtil.waitForException)1 ByteBuffer (java.nio.ByteBuffer)1