Search in sources :

Example 41 with MutableDocument

use of com.google.firebase.firestore.model.MutableDocument in project firebase-android-sdk by firebase.

the class QueryListenerTest method testWillRaiseInitialEventWhenGoingOffline.

@Test
public void testWillRaiseInitialEventWhenGoingOffline() {
    List<ViewSnapshot> events = new ArrayList<>();
    Query query = Query.atPath(path("rooms"));
    MutableDocument doc1 = doc("rooms/eros", 1, map("name", "eros"));
    MutableDocument doc2 = doc("rooms/hades", 2, map("name", "hades"));
    ListenOptions options = new ListenOptions();
    options.waitForSyncWhenOnline = true;
    QueryListener listener = queryListener(query, options, events);
    View view = new View(query, DocumentKey.emptyKeySet());
    ViewSnapshot snap1 = applyChanges(view, doc1);
    ViewSnapshot snap2 = applyChanges(view, doc2);
    // no event
    listener.onOnlineStateChanged(OnlineState.ONLINE);
    // no event
    listener.onViewSnapshot(snap1);
    // event
    listener.onOnlineStateChanged(OnlineState.OFFLINE);
    // event
    listener.onOnlineStateChanged(OnlineState.ONLINE);
    // no event
    listener.onOnlineStateChanged(OnlineState.OFFLINE);
    // event
    listener.onViewSnapshot(snap2);
    DocumentViewChange change1 = DocumentViewChange.create(Type.ADDED, doc1);
    DocumentViewChange change2 = DocumentViewChange.create(Type.ADDED, doc2);
    ViewSnapshot expectedSnapshot1 = new ViewSnapshot(snap1.getQuery(), snap1.getDocuments(), DocumentSet.emptySet(snap1.getQuery().comparator()), asList(change1), /* isFromCache= */
    true, snap1.getMutatedKeys(), /* didSyncStateChange= */
    true, /* excludesMetadataChanges= */
    true);
    ViewSnapshot expectedSnapshot2 = new ViewSnapshot(snap2.getQuery(), snap2.getDocuments(), snap1.getDocuments(), asList(change2), /* isFromCache= */
    true, snap2.getMutatedKeys(), /* didSyncStateChange= */
    false, /* excludesMetadataChanges= */
    true);
    assertEquals(asList(expectedSnapshot1, expectedSnapshot2), events);
}
Also used : ListenOptions(com.google.firebase.firestore.core.EventManager.ListenOptions) ArrayList(java.util.ArrayList) MutableDocument(com.google.firebase.firestore.model.MutableDocument) Test(org.junit.Test)

Example 42 with MutableDocument

use of com.google.firebase.firestore.model.MutableDocument in project firebase-android-sdk by firebase.

the class QueryListenerTest method testRaisesCollectionEvents.

@Test
public void testRaisesCollectionEvents() {
    final List<ViewSnapshot> accum = new ArrayList<>();
    final List<ViewSnapshot> otherAccum = new ArrayList<>();
    Query query = Query.atPath(path("rooms"));
    MutableDocument doc1 = doc("rooms/eros", 1, map("name", "eros"));
    MutableDocument doc2 = doc("rooms/hades", 2, map("name", "hades"));
    MutableDocument doc2prime = doc("rooms/hades", 3, map("name", "hades", "owner", "Jonny"));
    QueryListener listener = queryListener(query, accum);
    QueryListener otherListener = queryListener(query, otherAccum);
    View view = new View(query, DocumentKey.emptyKeySet());
    ViewSnapshot snap1 = applyChanges(view, doc1, doc2);
    ViewSnapshot snap2 = applyChanges(view, doc2prime);
    DocumentViewChange change1 = DocumentViewChange.create(Type.ADDED, doc1);
    DocumentViewChange change2 = DocumentViewChange.create(Type.ADDED, doc2);
    DocumentViewChange change3 = DocumentViewChange.create(Type.MODIFIED, doc2prime);
    // Second listener should receive doc2prime as added document not modified.
    DocumentViewChange change4 = DocumentViewChange.create(Type.ADDED, doc2prime);
    listener.onViewSnapshot(snap1);
    listener.onViewSnapshot(snap2);
    otherListener.onViewSnapshot(snap2);
    assertEquals(asList(snap1, snap2), accum);
    assertEquals(asList(change1, change2), accum.get(0).getChanges());
    assertEquals(asList(change3), accum.get(1).getChanges());
    ViewSnapshot snap2Prime = new ViewSnapshot(snap2.getQuery(), snap2.getDocuments(), DocumentSet.emptySet(snap2.getQuery().comparator()), asList(change1, change4), snap2.isFromCache(), snap2.getMutatedKeys(), /* didSyncStateChange= */
    true, /* excludesMetadataChanges= */
    false);
    assertEquals(asList(snap2Prime), otherAccum);
}
Also used : ArrayList(java.util.ArrayList) MutableDocument(com.google.firebase.firestore.model.MutableDocument) Test(org.junit.Test)

Example 43 with MutableDocument

use of com.google.firebase.firestore.model.MutableDocument in project firebase-android-sdk by firebase.

the class QueryListenerTest method testDoesNotRaiseEventsForMetadataChangesUnlessSpecified.

@Test
public void testDoesNotRaiseEventsForMetadataChangesUnlessSpecified() {
    List<ViewSnapshot> filteredAccum = new ArrayList<>();
    List<ViewSnapshot> fullAccum = new ArrayList<>();
    Query query = Query.atPath(path("rooms"));
    MutableDocument doc1 = doc("rooms/eros", 1, map("name", "eros"));
    MutableDocument doc2 = doc("rooms/hades", 2, map("name", "hades"));
    ListenOptions options1 = new ListenOptions();
    ListenOptions options2 = new ListenOptions();
    options2.includeQueryMetadataChanges = true;
    options2.includeDocumentMetadataChanges = true;
    QueryListener filteredListener = queryListener(query, options1, filteredAccum);
    QueryListener fullListener = queryListener(query, options2, fullAccum);
    View view = new View(query, DocumentKey.emptyKeySet());
    ViewSnapshot snap1 = applyChanges(view, doc1);
    TargetChange ackTarget = ackTarget(doc1);
    ViewSnapshot snap2 = view.applyChanges(view.computeDocChanges(docUpdates()), ackTarget).getSnapshot();
    ViewSnapshot snap3 = applyChanges(view, doc2);
    // local event
    filteredListener.onViewSnapshot(snap1);
    // no event
    filteredListener.onViewSnapshot(snap2);
    // doc2 update
    filteredListener.onViewSnapshot(snap3);
    // local event
    fullListener.onViewSnapshot(snap1);
    // no event
    fullListener.onViewSnapshot(snap2);
    // doc2 update
    fullListener.onViewSnapshot(snap3);
    assertEquals(asList(applyExpectedMetadata(snap1, MetadataChanges.EXCLUDE), applyExpectedMetadata(snap3, MetadataChanges.EXCLUDE)), filteredAccum);
    assertEquals(asList(snap1, snap2, snap3), fullAccum);
}
Also used : ListenOptions(com.google.firebase.firestore.core.EventManager.ListenOptions) TargetChange(com.google.firebase.firestore.remote.TargetChange) ArrayList(java.util.ArrayList) MutableDocument(com.google.firebase.firestore.model.MutableDocument) Test(org.junit.Test)

Example 44 with MutableDocument

use of com.google.firebase.firestore.model.MutableDocument in project firebase-android-sdk by firebase.

the class QueryTest method testNotInFiltersWithObjectValues.

@Test
public void testNotInFiltersWithObjectValues() {
    Query query = Query.atPath(ResourcePath.fromString("collection")).filter(filter("zip", "not-in", asList(map("a", asList(42)))));
    // Containing object in array.
    MutableDocument document = doc("collection/1", 0, map("zip", asList(map("a", asList(42)))));
    assertTrue(query.matches(document));
    // Containing object.
    document = doc("collection/1", 0, map("zip", map("a", asList(42))));
    assertFalse(query.matches(document));
}
Also used : MutableDocument(com.google.firebase.firestore.model.MutableDocument) Test(org.junit.Test)

Example 45 with MutableDocument

use of com.google.firebase.firestore.model.MutableDocument in project firebase-android-sdk by firebase.

the class QueryTest method testPrimitiveValueFilter.

@Test
public void testPrimitiveValueFilter() {
    Query query1 = Query.atPath(ResourcePath.fromString("collection")).filter(filter("sort", ">=", 2));
    Query query2 = Query.atPath(ResourcePath.fromString("collection")).filter(filter("sort", "<=", 2));
    MutableDocument doc1 = doc("collection/1", 0, map("sort", 1));
    MutableDocument doc2 = doc("collection/2", 0, map("sort", 2));
    MutableDocument doc3 = doc("collection/3", 0, map("sort", 3));
    MutableDocument doc4 = doc("collection/4", 0, map("sort", false));
    MutableDocument doc5 = doc("collection/5", 0, map("sort", "string"));
    assertFalse(query1.matches(doc1));
    assertTrue(query1.matches(doc2));
    assertTrue(query1.matches(doc3));
    assertFalse(query1.matches(doc4));
    assertFalse(query1.matches(doc5));
    assertTrue(query2.matches(doc1));
    assertTrue(query2.matches(doc2));
    assertFalse(query2.matches(doc3));
    assertFalse(query2.matches(doc4));
    assertFalse(query2.matches(doc5));
}
Also used : MutableDocument(com.google.firebase.firestore.model.MutableDocument) Test(org.junit.Test)

Aggregations

MutableDocument (com.google.firebase.firestore.model.MutableDocument)166 Test (org.junit.Test)125 DocumentKey (com.google.firebase.firestore.model.DocumentKey)43 Mutation.calculateOverlayMutation (com.google.firebase.firestore.model.mutation.Mutation.calculateOverlayMutation)30 TestUtil.deleteMutation (com.google.firebase.firestore.testutil.TestUtil.deleteMutation)30 TestUtil.mergeMutation (com.google.firebase.firestore.testutil.TestUtil.mergeMutation)30 TestUtil.patchMutation (com.google.firebase.firestore.testutil.TestUtil.patchMutation)30 TestUtil.setMutation (com.google.firebase.firestore.testutil.TestUtil.setMutation)30 HashMap (java.util.HashMap)22 TestUtil.wrapObject (com.google.firebase.firestore.testutil.TestUtil.wrapObject)18 ArrayList (java.util.ArrayList)18 TargetData (com.google.firebase.firestore.local.TargetData)15 WatchTargetChange (com.google.firebase.firestore.remote.WatchChange.WatchTargetChange)14 DocumentChange (com.google.firebase.firestore.remote.WatchChange.DocumentChange)13 ResourcePath (com.google.firebase.firestore.model.ResourcePath)12 Query (com.google.firebase.firestore.core.Query)10 Map (java.util.Map)10 SnapshotVersion (com.google.firebase.firestore.model.SnapshotVersion)8 Document (com.google.firebase.firestore.model.Document)7 Timestamp (com.google.firebase.Timestamp)6