Ejemplo n.º 1
0
        private async Task SyncAsync()
        {
            if (Connectivity.NetworkAccess != NetworkAccess.Internet)
            {
                return;
            }

            await _nubeClient.PushChangesAsync();

            await _nubeClient.PullTableAsync <TodoItem>();

            await RefreshItemsAsync();
        }
Ejemplo n.º 2
0
        public async Task Queries_the_operations_until_there_are_no_more()
        {
            var i = 0;
            var existingOperations = new List <NubeOperation>()
            {
                new NubeOperation()
                {
                    ItemId = "otherId", Type = OperationType.Modified
                },
            };

            DataStore.GetOperationsAsync(Arg.Any <int>()).Returns(existingOperations.AsQueryable());
            DataStore.When(x => x.DeleteOperationsAsync(Arg.Any <NubeOperation[]>())).Do(
                x =>
            {
                if (i > 0)
                {
                    existingOperations = new List <NubeOperation>();
                    DataStore.GetOperationsAsync(Arg.Any <int>()).Returns(existingOperations.AsQueryable());
                }

                i++;
            });

            await NubeClient.PushChangesAsync();

            await DataStore.Received(2).DeleteOperationsAsync(Arg.Any <NubeOperation[]>());
        }
        public async Task Posts_the_operations_to_the_given_operations_url()
        {
            var operationsUrl = "/different";

            NubeClient = new NubeClient(DataStore, ServerUrl, Authentication, HttpClient, ChangeTracker, operationsUrl);

            var existingOperations = new List <NubeOperation>()
            {
                new NubeOperation()
                {
                    ItemId = "otherId", Type = OperationType.Modified
                },
            };

            DataStore.GetOperationsAsync(Arg.Any <int>()).Returns(existingOperations.AsQueryable());
            DataStore.When(x => x.DeleteOperationsAsync(Arg.Any <NubeOperation[]>())).Do(
                x =>
            {
                existingOperations = new List <NubeOperation>();
                DataStore.GetOperationsAsync(Arg.Any <int>()).Returns(existingOperations.AsQueryable());
            });

            await NubeClient.PushChangesAsync();

            Assert.Equal("https://myserver/different", HttpMessageHandler.LastRequest.RequestUri.AbsoluteUri);
        }
Ejemplo n.º 4
0
        public async Task Returns_true_when_there_are_no_operations()
        {
            DataStore.GetOperationsAsync(Arg.Any <int>()).Returns(new List <NubeOperation>().AsQueryable());

            var result = await NubeClient.PushChangesAsync();

            Assert.True(result);
        }
Ejemplo n.º 5
0
        public async Task Returns_false_when_cancelled()
        {
            var tokenSource = new CancellationTokenSource();

            tokenSource.Cancel();

            var result = await NubeClient.PushChangesAsync(tokenSource.Token);

            Assert.False(result);
        }
Ejemplo n.º 6
0
        public async Task Posts_the_operations()
        {
            var existingOperations = new List <NubeOperation>()
            {
                new NubeOperation()
                {
                    ItemId = "otherId", Type = OperationType.Modified
                },
                new NubeOperation()
                {
                    ItemId = Item.Id, Type = OperationType.Deleted
                },
                new NubeOperation()
                {
                    ItemId = Item.Id, Type = OperationType.Modified
                },
                new NubeOperation()
                {
                    ItemId = Item.Id, Type = OperationType.Modified
                },
                new NubeOperation()
                {
                    ItemId = Item.Id, Type = OperationType.Added
                },
            };
            var expectedContent = JsonSerializer.Serialize(existingOperations,
                                                           new JsonSerializerOptions {
                IgnoreNullValues = true
            });

            DataStore.GetOperationsAsync(Arg.Any <int>()).Returns(existingOperations.AsQueryable());
            DataStore.When(x => x.DeleteOperationsAsync(Arg.Any <NubeOperation[]>())).Do(
                x =>
            {
                existingOperations = new List <NubeOperation>();
                DataStore.GetOperationsAsync(Arg.Any <int>()).Returns(existingOperations.AsQueryable());
            });

            await NubeClient.PushChangesAsync();

            var content = await HttpMessageHandler.LastRequest.Content.ReadAsStringAsync();

            Assert.Equal(expectedContent, content);
            Assert.Equal("https://myserver/operations", HttpMessageHandler.LastRequest.RequestUri.AbsoluteUri);
        }
Ejemplo n.º 7
0
        public async Task Adds_the_installation_id_to_the_headers()
        {
            await AddTablesAsync();

            DataStore.InsertAsync(Arg.Any <TestItem>()).Returns(true);

            await NubeClient.PullTableAsync <TestItem>();

            var installationIdHeader = HttpClient.DefaultRequestHeaders.Where(h => h.Key == "NUBE-INSTALLATION-ID").First();

            Assert.NotNull(installationIdHeader.Value.First());

            await NubeClient.PushChangesAsync();

            var installationIdHeader2 = HttpClient.DefaultRequestHeaders.Where(h => h.Key == "NUBE-INSTALLATION-ID").First();

            Assert.Equal(installationIdHeader.Value.First(), installationIdHeader2.Value.First());
        }
Ejemplo n.º 8
0
        public async Task Deletes_the_operations_when_post_was_successful()
        {
            var existingOperations = new List <NubeOperation>()
            {
                new NubeOperation()
                {
                    ItemId = "otherId", Type = OperationType.Modified
                },
            };

            DataStore.GetOperationsAsync(Arg.Any <int>()).Returns(existingOperations.AsQueryable());
            DataStore.When(x => x.DeleteOperationsAsync(Arg.Any <NubeOperation[]>())).Do(
                x =>
            {
                existingOperations = new List <NubeOperation>();
                DataStore.GetOperationsAsync(Arg.Any <int>()).Returns(existingOperations.AsQueryable());
            });

            await NubeClient.PushChangesAsync();

            await DataStore.Received().DeleteOperationsAsync(Arg.Any <NubeOperation[]>());
        }
Ejemplo n.º 9
0
        public async Task Throws_when_post_failed()
        {
            HttpMessageHandler.HttpRequestFails = true;
            var existingOperations = new List <NubeOperation>()
            {
                new NubeOperation()
                {
                    ItemId = "otherId", Type = OperationType.Modified
                },
            };

            DataStore.GetOperationsAsync(Arg.Any <int>()).Returns(existingOperations.AsQueryable());

            var ex = await Assert.ThrowsAsync <PushOperationFailedException>(async() => await NubeClient.PushChangesAsync());

            Assert.Equal("Cannot push operations to the server: BadRequest some message", ex.Message);
        }
Ejemplo n.º 10
0
        public async Task Reads_the_operations_from_the_store()
        {
            await NubeClient.PushChangesAsync();

            await DataStore.Received().GetOperationsAsync(100);
        }