示例#1
0
        public async Task updates_existing_document()
        {
            var sut      = CrossFirebaseFirestore.Current;
            var pokemon  = PokemonFactory.CreateSquirtle();
            var path     = $"testing/{pokemon.Id}";
            var document = sut.GetDocument(path);

            await document.SetDataAsync(pokemon);

            Assert.Equal(pokemon, (await document.GetDocumentSnapshotAsync <Pokemon>()).Data);

            var update = new Dictionary <object, object> {
                { "name", "Cool Squirtle" },
                { "moves", FieldValue.ArrayUnion("Bubble-Blast") },
                { "first_sighting_location.latitude", 13.37 }
            };

            await document.UpdateDataAsync(update);

            var snapshot = await document.GetDocumentSnapshotAsync <Pokemon>();

            Assert.Equal("Cool Squirtle", snapshot.Data.Name);
            Assert.True(snapshot.Data.Moves.Contains("Bubble-Blast"));
            Assert.Equal(13.37, snapshot.Data.FirstSightingLocation.Latitude);
        }
示例#2
0
        public async Task runs_transaction()
        {
            var sut                = CrossFirebaseFirestore.Current;
            var bulbasur           = PokemonFactory.CreateBulbasur();
            var charmander         = PokemonFactory.CreateCharmander();
            var squirtle           = PokemonFactory.CreateSquirtle();
            var documentBulbasur   = sut.GetDocument("testing/1");
            var documentCharmander = sut.GetDocument("testing/4");
            var documentSquirtle   = sut.GetDocument("testing/7");
            await documentBulbasur.SetDataAsync(bulbasur);

            await documentCharmander.SetDataAsync(charmander);

            var charmanderSightingCount = await sut.RunTransactionAsync(transaction => {
                var snapshotCharmander = transaction.GetDocument <Pokemon>(documentCharmander);
                var newSightingCount   = snapshotCharmander.Data.SightingCount + 1;
                transaction.SetData(documentSquirtle, squirtle);
                transaction.UpdateData(documentCharmander, ("sighting_count", newSightingCount));
                transaction.DeleteDocument(documentBulbasur);
                return(newSightingCount);
            });

            Assert.Equal(squirtle, (await documentSquirtle.GetDocumentSnapshotAsync <Pokemon>()).Data);
            Assert.Equal(charmander.SightingCount + 1, charmanderSightingCount);
            Assert.Null((await documentBulbasur.GetDocumentSnapshotAsync <Pokemon>()).Data);
        }
示例#3
0
        public async Task gets_real_time_updates_on_multiple_documents()
        {
            var sut        = CrossFirebaseFirestore.Current;
            var collection = sut.GetCollection("testing");

            var changes    = new List <IEnumerable <DocumentChangeType> >();
            var disposable = collection
                             .WhereEqualsTo("poke_type", PokeType.Fire)
                             .AddSnapshotListener <Pokemon>(x => {
                changes.Add(x.DocumentChanges.Select(y => y.ChangeType));
            });

            await collection.GetDocument("4").SetDataAsync(PokemonFactory.CreateCharmander());

            await Task.Delay(TimeSpan.FromMilliseconds(500));

            await collection.GetDocument("5").SetDataAsync(PokemonFactory.CreateCharmeleon());

            await Task.Delay(TimeSpan.FromMilliseconds(500));

            await collection.GetDocument("6").SetDataAsync(PokemonFactory.CreateCharizard());

            await Task.Delay(TimeSpan.FromMilliseconds(500));

            await collection.GetDocument("4").UpdateDataAsync(("sighting_count", 1337));

            await Task.Delay(TimeSpan.FromMilliseconds(500));

            await collection.GetDocument("5").DeleteDocumentAsync();

            await Task.Delay(TimeSpan.FromMilliseconds(500));

            var expectedChangesOnAndroid = new[] {
                DocumentChangeType.Added,
                DocumentChangeType.Added,
                DocumentChangeType.Added,
                DocumentChangeType.Modified,
                DocumentChangeType.Removed
            };

            var expectedChangesOniOS = new[] {
                DocumentChangeType.Added,
                DocumentChangeType.Modified,
                DocumentChangeType.Added,
                DocumentChangeType.Modified,
                DocumentChangeType.Added,
                DocumentChangeType.Modified,
                DocumentChangeType.Modified,
                DocumentChangeType.Removed
            };

            Assert.Equal(DeviceInfo.Platform == DevicePlatform.Android ? expectedChangesOnAndroid : expectedChangesOniOS, changes.SelectMany(x => x));
            disposable.Dispose();
        }
示例#4
0
        public async Task deletes_document()
        {
            var sut      = CrossFirebaseFirestore.Current;
            var pokemon  = PokemonFactory.CreateCharmander();
            var path     = $"testing/{pokemon.Id}";
            var document = sut.GetDocument(path);

            await document.SetDataAsync(pokemon);

            Assert.NotNull((await sut.GetDocument(path).GetDocumentSnapshotAsync <Pokemon>()).Data);

            await document.DeleteDocumentAsync();

            Assert.Null((await sut.GetDocument(path).GetDocumentSnapshotAsync <Pokemon>()).Data);
        }
示例#5
0
        public async Task adds_document_to_collection()
        {
            var sut      = CrossFirebaseFirestore.Current;
            var pokemon  = PokemonFactory.CreateBulbasur();
            var path     = $"testing/{pokemon.Id}";
            var document = sut.GetDocument(path);

            await document.SetDataAsync(pokemon);

            var snapshot = await document.GetDocumentSnapshotAsync <Pokemon>();

            Assert.False(snapshot.Metadata.HasPendingWrites);
            Assert.Equal(pokemon.Id, snapshot.Reference.Id);
            Assert.Equal(path, snapshot.Reference.Path);
            Assert.Equal(pokemon, snapshot.Data);
        }
示例#6
0
        public async Task deletes_fields_of_document()
        {
            var sut      = CrossFirebaseFirestore.Current;
            var pokemon  = PokemonFactory.CreateCharmander();
            var path     = $"testing/{pokemon.Id}";
            var document = sut.GetDocument(path);
            await document.SetDataAsync(pokemon);

            await document.UpdateDataAsync(
                ("moves", FieldValue.Delete()),
                ("evolutions", FieldValue.Delete()),
                ("first_sighting_location", FieldValue.Delete()),
                ("poke_type", FieldValue.Delete()));

            var snapshot = await document.GetDocumentSnapshotAsync <Pokemon>();

            Assert.Null(snapshot.Data.Moves);
            Assert.Null(snapshot.Data.FirstSightingLocation);
            Assert.Null(snapshot.Data.Evolutions);
            Assert.Equal(PokeType.Undefined, snapshot.Data.PokeType);
        }
示例#7
0
        public async Task writes_data_as_batch()
        {
            var sut                = CrossFirebaseFirestore.Current;
            var bulbasur           = PokemonFactory.CreateBulbasur();
            var charmander         = PokemonFactory.CreateCharmander();
            var squirtle           = PokemonFactory.CreateSquirtle();
            var documentBulbasur   = sut.GetDocument("testing/1");
            var documentCharmander = sut.GetDocument("testing/4");
            var documentSquirtle   = sut.GetDocument("testing/7");
            await documentBulbasur.SetDataAsync(bulbasur);

            await documentCharmander.SetDataAsync(charmander);

            var batch = sut.CreateBatch();

            batch.SetData(documentSquirtle, squirtle);
            batch.UpdateData(documentCharmander, ("sighting_count", 1337));
            batch.DeleteDocument(documentBulbasur);
            await batch.CommitAsync();

            Assert.Equal(squirtle, (await documentSquirtle.GetDocumentSnapshotAsync <Pokemon>()).Data);
            Assert.Equal(1337, (await documentCharmander.GetDocumentSnapshotAsync <Pokemon>()).Data.SightingCount);
            Assert.Null((await documentBulbasur.GetDocumentSnapshotAsync <Pokemon>()).Data);
        }
示例#8
0
        public async Task gets_real_time_updates_on_single_document()
        {
            var sut      = CrossFirebaseFirestore.Current;
            var document = sut.GetDocument("testing/1");
            await document.SetDataAsync(PokemonFactory.CreateBulbasur());

            var sightingCounts = new List <long>();
            var disposable     = document.AddSnapshotListener <Pokemon>(x => {
                if (x.Data != null)
                {
                    sightingCounts.Add(x.Data.SightingCount);
                }
            });

            for (var i = 0; i < 3; i++)
            {
                await document.UpdateDataAsync(("sighting_count", i));

                await Task.Delay(TimeSpan.FromMilliseconds(100));
            }

            Assert.Equal(new[] { 0L, 1L, 2L }, sightingCounts.Distinct());
            disposable.Dispose();
        }