public void FromProto() { var direct = new Timestamp(1, 100); var fromProto = Timestamp.FromProto(CreateProtoTimestamp(1, 100)); Assert.Equal(direct, fromProto); }
public void FromProto() { var direct = new Timestamp(1, 100); var fromProto = Timestamp.FromProto(new wkt::Timestamp { Seconds = 1, Nanos = 100 }); Assert.Equal(direct, fromProto); }
public void CollectionGroup_InvalidCursor() { var db = FirestoreDb.Create("proj", "db", new FakeFirestoreClient()); var collection = s_db.Collection("col1"); var document = new Document { CreateTime = CreateProtoTimestamp(0, 0), UpdateTime = CreateProtoTimestamp(0, 0), Name = collection.Document("doc").Path, Fields = { { "field", CreateArray(CreateValue(1), CreateValue(2)) } } }; var snapshot = DocumentSnapshot.ForDocument(s_db, document, Timestamp.FromProto(document.CreateTime)); // Collection group query for a different collection var query = db.CollectionGroup("col2"); Assert.Throws <ArgumentException>(() => query.StartAt(snapshot)); }
public void ArrayContainsIsEquality() { var collection = s_db.Collection("col"); var document = new Document { CreateTime = CreateProtoTimestamp(0, 0), UpdateTime = CreateProtoTimestamp(0, 0), Name = collection.Document("doc").Path, Fields = { { "field", CreateArray(CreateValue(1), CreateValue(2)) } } }; var snapshot = DocumentSnapshot.ForDocument(s_db, document, Timestamp.FromProto(document.CreateTime)); // An inequality filter would create an implicit ordering here, but "array contains" // is effectively an equality filter, so we should end up with document ID ordering instead. var query = s_db.Collection("col").WhereArrayContains("field", 1).StartAt(snapshot); var structured = query.ToStructuredQuery(); var documentIdOrder = new Order { Direction = Direction.Ascending, Field = FieldPath.DocumentId.ToFieldReference() }; Assert.Equal(new[] { documentIdOrder }, structured.OrderBy); }
protected override object DeserializeTimestamp(FirestoreDb db, wkt::Timestamp value) => Timestamp.FromProto(value);
protected override object DeserializeTimestamp(DeserializationContext context, wkt::Timestamp value) => Timestamp.FromProto(value);
public async Task Listen(SerializableTest wrapper) { ListenTest test = wrapper.Test.Listen; var db = FirestoreDb.Create(ProjectId, DatabaseId, new FakeFirestoreClient()); var query = db.Collection("C").OrderBy("a"); Func <Task> action = async() => { List <QuerySnapshot> snapshots = new List <QuerySnapshot>(); var watchState = new WatchState(query, (snapshot, token) => { snapshots.Add(snapshot); return(Task.FromResult(1)); }); watchState.OnStreamInitialization(StreamInitializationCause.WatchStarting); foreach (var response in test.Responses) { // Fix up the test response to use our watch target ID. ReplaceWatchTargetId(response.TargetChange?.TargetIds); ReplaceWatchTargetId(response.DocumentChange?.TargetIds); ReplaceWatchTargetId(response.DocumentChange?.RemovedTargetIds); ReplaceWatchTargetId(response.DocumentDelete?.RemovedTargetIds); ReplaceWatchTargetId(response.DocumentRemove?.RemovedTargetIds); var result = await watchState.HandleResponseAsync(response, default); if (result == WatchResponseResult.ResetStream) { watchState.OnStreamInitialization(StreamInitializationCause.ResetRequested); } } var expectedSnapshots = test.Snapshots.Select(snapshot => ConvertSnapshot(snapshot)).ToList(); Assert.Equal(expectedSnapshots, snapshots); }; if (test.IsError) { // TODO: Should we actually check that it's only the last response that causes the exception? var exception = await Assert.ThrowsAnyAsync <Exception>(action); Assert.True(exception is ArgumentException || exception is InvalidOperationException, $"Exception type: {exception.GetType()}"); } else { await action(); } // Different clients use different watch target IDs. The test data always uses 1 // to mean "the target ID that the client uses". void ReplaceWatchTargetId(RepeatedField <int> ids) { if (ids == null) { return; } for (int i = 0; i < ids.Count; i++) { if (ids[i] == 1) { ids[i] = WatchStream.WatchTargetId; } } } // Converts from a test proto snapshot to a QuerySnapshot QuerySnapshot ConvertSnapshot(Snapshot snapshot) { var readTime = Timestamp.FromProto(snapshot.ReadTime); var changes = snapshot.Changes.Select(change => ConvertChange(change, readTime)).ToList(); var docs = snapshot.Docs.Select(doc => DocumentSnapshot.ForDocument(db, doc, readTime)).ToList(); return(QuerySnapshot.ForChanges(query, docs, changes, readTime)); } DocumentChange ConvertChange(DocChange change, Timestamp readTime) { var snapshot = DocumentSnapshot.ForDocument(db, change.Doc, readTime); return(new DocumentChange(snapshot, (DocumentChange.Type)change.Kind, change.OldIndex == -1 ? default(int?) : change.OldIndex, change.NewIndex == -1 ? default(int?) : change.NewIndex)); } }