public void WithDocumentRemoved() { var doc1 = CreateSnapshot("col1/doc1"); var doc2 = CreateSnapshot("col1/doc2"); var doc3 = CreateSnapshot("col1/doc3"); // First create a set with 2 documents (doc1, doc2 and doc3) var set3 = DocumentSet.Empty(PathComparer.Instance) .WithDocumentAdded(doc1) .WithDocumentAdded(doc2) .WithDocumentAdded(doc3); // Now remove doc2 var set2 = set3.WithDocumentRemoved(doc2.Reference); // This shouldn't have modified the existing set Assert.Equal(3, set3.Count); Assert.Equal(new[] { doc1, doc2, doc3 }, set3.ToList()); // But the new set should only have two documents, still in order Assert.Equal(2, set2.Count); Assert.Equal(new[] { doc1, doc3 }, set2.ToList()); Assert.Equal(0, set2.IndexOf(doc1.Reference)); Assert.Equal(1, set2.IndexOf(doc3.Reference)); Assert.True(set2.TryGetDocument(doc1.Reference, out var result)); Assert.Same(doc1, result); Assert.False(set2.TryGetDocument(doc2.Reference, out result)); Assert.Null(result); Assert.True(set2.TryGetDocument(doc3.Reference, out result)); Assert.Same(doc3, result); // Trying to remove doc2 again should give the same reference back Assert.Same(set2, set2.WithDocumentRemoved(doc2.Reference)); }
public void Empty() { var empty = DocumentSet.Empty(PathComparer.Instance); Assert.Empty(empty); Assert.Equal(0, empty.Count); var docRef = CreateSnapshot("col1/doc1").Reference; Assert.False(empty.Contains(docRef)); Assert.Equal(-1, empty.IndexOf(docRef)); Assert.False(empty.TryGetDocument(docRef, out var snapshot)); Assert.Null(snapshot); }
public void WithDocumentAdded() { var doc1 = CreateSnapshot("col1/doc1"); var doc2 = CreateSnapshot("col1/doc2"); var doc3 = CreateSnapshot("col1/doc3"); // First create a set with 2 documents (doc1 and doc3) var set2 = DocumentSet.Empty(PathComparer.Instance) .WithDocumentAdded(doc3) .WithDocumentAdded(doc1); // Examine the set in various ways Assert.Equal(2, set2.Count); Assert.Equal(new[] { doc1, doc3 }, set2.ToList()); Assert.Equal(0, set2.IndexOf(doc1.Reference)); Assert.Equal(1, set2.IndexOf(doc3.Reference)); Assert.True(set2.TryGetDocument(doc1.Reference, out var result)); Assert.Same(doc1, result); Assert.False(set2.TryGetDocument(doc2.Reference, out result)); Assert.Null(result); Assert.True(set2.TryGetDocument(doc3.Reference, out result)); Assert.Same(doc3, result); // Now add doc2, which should go between doc1 and doc3 var set3 = set2.WithDocumentAdded(doc2); // This shouldn't have modified the existing set Assert.Equal(2, set2.Count); Assert.Equal(new[] { doc1, doc3 }, set2.ToList()); // But the new set should have all three documents Assert.Equal(3, set3.Count); Assert.Equal(new[] { doc1, doc2, doc3 }, set3.ToList()); Assert.Equal(0, set3.IndexOf(doc1.Reference)); Assert.Equal(1, set3.IndexOf(doc2.Reference)); Assert.Equal(2, set3.IndexOf(doc3.Reference)); Assert.True(set3.TryGetDocument(doc1.Reference, out result)); Assert.Same(doc1, result); Assert.True(set3.TryGetDocument(doc2.Reference, out result)); Assert.Same(doc2, result); Assert.True(set3.TryGetDocument(doc3.Reference, out result)); Assert.Same(doc3, result); // Adding an existing element shouldn't have any effect var set3Again = set3.WithDocumentAdded(doc1); Assert.Equal(3, set3Again.Count); }
public void ValueBasedComparer() { var doc1 = CreateSnapshot("col1/doc1", 5); var doc2 = CreateSnapshot("col1/doc2", 3); var doc3 = CreateSnapshot("col1/doc3", 1); var doc4 = CreateSnapshot("col1/doc4", 6); // Just validate the documents are in the right order after adding in a different // order - that the field value is being used rather than just the path. // (In practice, the comparer we use will always use the path as part of the comparison.) var set = DocumentSet.Empty(FieldValueComparer.Instance) .WithDocumentAdded(doc1) .WithDocumentAdded(doc2) .WithDocumentAdded(doc3) .WithDocumentAdded(doc4); Assert.Equal(4, set.Count); Assert.Equal(new[] { doc3, doc2, doc1, doc4 }, set.ToList()); }
public void SnapshotFromChanges() { var query = s_db.Collection("col1"); var readTime = new Timestamp(10, 2); var doc1 = GetSampleSnapshot(s_db, "doc1"); var doc2 = GetSampleSnapshot(s_db, "doc2"); var documentSet = DocumentSet.Empty(query.CreateDocumentSnapshotComparer()) .WithDocumentAdded(doc1) .WithDocumentAdded(doc2); var changes = new[] { new DocumentChange(doc2, DocumentChange.Type.Added, null, 1) }; // It's fine for the list to be a different length; the document set is *all* the // documents, whereas changes is just the delta from the previous snapshot. var snapshot = QuerySnapshot.ForChanges(query, documentSet, changes, readTime); Assert.Equal(changes, snapshot.Changes); Assert.Equal(new[] { doc1, doc2 }, snapshot.Documents); Assert.Equal(new[] { doc1, doc2 }, snapshot.ToList()); }