public async Task DeleteAsyncWithNosuchItemAgainstStringIdTable()
        {
            await EnsureEmptyTableAsync <ToDoWithStringId>();

            string[] testIdData = IdTestData.ValidStringIds;
            IMobileServiceTable <ToDoWithStringId> table = GetClient().GetTable <ToDoWithStringId>();

            foreach (string testId in testIdData)
            {
                ToDoWithStringId item = new ToDoWithStringId()
                {
                    Id = testId, Name = "Hey"
                };
                await table.InsertAsync(item);
            }

            foreach (string testId in testIdData)
            {
                ToDoWithStringId item = await table.LookupAsync(testId);

                await table.DeleteAsync(item);

                item.Id = testId;

                var exception = await Assert.ThrowsAnyAsync <MobileServiceInvalidOperationException>(() => table.DeleteAsync(item));

                Assert.Equal(HttpStatusCode.NotFound, exception.Response.StatusCode);
                Assert.True(exception.Message == "The item does not exist" ||
                            exception.Message == "The request could not be completed.  (Not Found)");
            }
        }
        public async Task InsertAsyncWithExistingItemAgainstStringIdTable()
        {
            await EnsureEmptyTableAsync <ToDoWithStringId>();

            string[] testIdData = IdTestData.ValidStringIds;
            IMobileServiceTable <ToDoWithStringId> table = GetClient().GetTable <ToDoWithStringId>();

            foreach (string testId in testIdData)
            {
                ToDoWithStringId item = new ToDoWithStringId()
                {
                    Id = testId, Name = "Hey"
                };
                await table.InsertAsync(item);
            }

            foreach (string testId in testIdData)
            {
                ToDoWithStringId item = await table.LookupAsync(testId);

                item.Name = "No we're talking!";

                var exception = await Assert.ThrowsAnyAsync <MobileServiceInvalidOperationException>(() => table.InsertAsync(item));

                Assert.Equal(HttpStatusCode.Conflict, exception.Response.StatusCode);
                Assert.True(exception.Message.Contains("Could not insert the item because an item with that id already exists.") ||
                            exception.Message == "The request could not be completed.  (Conflict)");
            }
        }
        public async Task InsertAsyncWithEmptyStringIdAgainstStringIdTable()
        {
            string[] emptyIdData = IdTestData.EmptyStringIds.Concat(
                new string[] { null }).ToArray();
            IMobileServiceTable <ToDoWithStringId> table = GetClient().GetTable <ToDoWithStringId>();

            int count = 0;
            List <ToDoWithStringId> itemsToDelete = new List <ToDoWithStringId>();

            foreach (string emptyId in emptyIdData)
            {
                ToDoWithStringId item = new ToDoWithStringId()
                {
                    Id = emptyId, Name = (++count).ToString()
                };
                await table.InsertAsync(item);

                Assert.NotNull(item.Id);
                Assert.Equal(count.ToString(), item.Name);
                itemsToDelete.Add(item);
            }

            foreach (var item in itemsToDelete)
            {
                await table.DeleteAsync(item);
            }
        }
        public async Task RefreshAsyncWithNoSuchItemAgainstStringIdTable()
        {
            await EnsureEmptyTableAsync <ToDoWithStringId>();

            string[] testIdData = IdTestData.ValidStringIds;
            IMobileServiceTable <ToDoWithStringId> table = GetClient().GetTable <ToDoWithStringId>();

            foreach (string testId in testIdData)
            {
                ToDoWithStringId item = new ToDoWithStringId()
                {
                    Id = testId, Name = "Hey"
                };
                await table.InsertAsync(item);
            }

            foreach (string testId in testIdData)
            {
                ToDoWithStringId item = await table.LookupAsync(testId);

                await table.DeleteAsync(item);

                item.Id = testId;

                await Assert.ThrowsAsync <InvalidOperationException>(() => table.RefreshAsync(item));
            }
        }
        public async Task OrderingReadAsyncWithValidStringIdAgainstStringIdTable()
        {
            await EnsureEmptyTableAsync <ToDoWithStringId>();

            string[] testIdData = new string[] { "a", "b", "C", "_A", "_B", "_C", "1", "2", "3" };
            IMobileServiceTable <ToDoWithStringId> table = GetClient().GetTable <ToDoWithStringId>();

            foreach (string testId in testIdData)
            {
                ToDoWithStringId item = new ToDoWithStringId()
                {
                    Id = testId, Name = "Hey"
                };
                await table.InsertAsync(item);
            }

            IEnumerable <ToDoWithStringId> results = await table.OrderBy(p => p.Id).ToEnumerableAsync();

            ToDoWithStringId[] items = results.ToArray();

            Assert.Equal(9, items.Count());
            Assert.Equal("_A", items[0].Id);
            Assert.Equal("_B", items[1].Id);
            Assert.Equal("_C", items[2].Id);
            Assert.Equal("1", items[3].Id);
            Assert.Equal("2", items[4].Id);
            Assert.Equal("3", items[5].Id);
            Assert.Equal("a", items[6].Id);
            Assert.Equal("b", items[7].Id);
            Assert.Equal("C", items[8].Id);

            results = await table.OrderByDescending(p => p.Id).ToEnumerableAsync();

            items = results.ToArray();

            Assert.Equal(9, items.Count());
            Assert.Equal("_A", items[8].Id);
            Assert.Equal("_B", items[7].Id);
            Assert.Equal("_C", items[6].Id);
            Assert.Equal("1", items[5].Id);
            Assert.Equal("2", items[4].Id);
            Assert.Equal("3", items[3].Id);
            Assert.Equal("a", items[2].Id);
            Assert.Equal("b", items[1].Id);
            Assert.Equal("C", items[0].Id);

            foreach (string testId in testIdData)
            {
                ToDoWithStringId item = new ToDoWithStringId()
                {
                    Id = testId
                };
                await table.DeleteAsync(item);
            }
        }
        public async Task FilterReadAsyncWithEmptyStringIdAgainstStringIdTable()
        {
            await EnsureEmptyTableAsync <ToDoWithStringId>();

            string[] testIdData = IdTestData.ValidStringIds;
            IMobileServiceTable <ToDoWithStringId> table = GetClient().GetTable <ToDoWithStringId>();

            foreach (string testId in testIdData)
            {
                ToDoWithStringId item = new ToDoWithStringId()
                {
                    Id = testId, Name = "Hey"
                };
                await table.InsertAsync(item);
            }

            string[] invalidIdData = IdTestData.EmptyStringIds.Concat(
                IdTestData.InvalidStringIds).Concat(
                new string[] { null }).ToArray();

            foreach (string invalidId in invalidIdData)
            {
                IEnumerable <ToDoWithStringId> results = await table.Where(p => p.Id == invalidId).ToEnumerableAsync();

                ToDoWithStringId[] items = results.ToArray();

                Assert.Empty(items);
            }

            foreach (string testId in testIdData)
            {
                ToDoWithStringId item = new ToDoWithStringId()
                {
                    Id = testId
                };
                await table.DeleteAsync(item);
            }
        }
Exemplo n.º 7
0
        public async Task Insert_ThenPush_ThenPull_ThenRead_ThenUpdate_ThenRefresh_ThenDelete_ThenLookup_ThenPush_ThenPurge_ThenRead()
        {
            ResetDatabase(TestTable);

            var hijack = new TestHttpHandler();

            hijack.AddResponseContent("{\"id\":\"b\",\"String\":\"Hey\"}");                                       // insert response
            hijack.AddResponseContent("[{\"id\":\"b\",\"String\":\"Hey\"},{\"id\":\"a\",\"String\":\"World\"}]"); // pull response
            hijack.AddResponseContent("[]");                                                                      // pull last page

            IMobileServiceClient service = await CreateTodoClient(hijack);

            IMobileServiceSyncTable <ToDoWithStringId> table = service.GetSyncTable <ToDoWithStringId>();

            // first insert an item
            await table.InsertAsync(new ToDoWithStringId()
            {
                Id = "b", String = "Hey"
            });

            // then push it to server
            await service.SyncContext.PushAsync();

            // then pull changes from server
            await table.PullAsync(null, null);

            // order the records by id so we can assert them predictably
            IList <ToDoWithStringId> items = await table.OrderBy(i => i.Id).ToListAsync();

            // we should have 2 records
            Assert.Equal(2, items.Count);

            // according to ordering a id comes first
            Assert.Equal("a", items[0].Id);
            Assert.Equal("World", items[0].String);

            // then comes b record
            Assert.Equal("b", items[1].Id);
            Assert.Equal("Hey", items[1].String);

            // we made 2 requests, one for push and two for pull
            Assert.Equal(3, hijack.Requests.Count);

            // recreating the client from state in the store
            service = await CreateTodoClient(hijack);

            table = service.GetSyncTable <ToDoWithStringId>();

            // update the second record
            items[1].String = "Hello";
            await table.UpdateAsync(items[1]);

            // create an empty record with same id as modified record
            var second = new ToDoWithStringId()
            {
                Id = items[1].Id
            };
            // refresh the empty record
            await table.RefreshAsync(second);

            // make sure it is same as modified record now
            Assert.Equal(second.String, items[1].String);

            // now delete the record
            await table.DeleteAsync(second);

            // now try to get the deleted record
            ToDoWithStringId deleted = await table.LookupAsync(second.Id);

            // this should be null
            Assert.Null(deleted);

            // try to get the non-deleted record
            ToDoWithStringId first = await table.LookupAsync(items[0].Id);

            // this should still be there;
            Assert.NotNull(first);

            // make sure it is same as
            Assert.Equal(first.String, items[0].String);

            // recreating the client from state in the store
            service = await CreateTodoClient(hijack);

            table = service.GetSyncTable <ToDoWithStringId>();

            await service.SyncContext.PushAsync();

            // now purge the remaining records
            await table.PurgeAsync();

            // now read one last time
            IEnumerable <ToDoWithStringId> remaining = await table.ReadAsync();

            // There shouldn't be anything remaining
            Assert.Empty(remaining);
        }
        public async Task AsyncTableOperationsWithValidStringIdAgainstStringIdTable()
        {
            await EnsureEmptyTableAsync <ToDoWithStringId>();

            string[] testIdData = IdTestData.ValidStringIds;
            IMobileServiceTable <ToDoWithStringId> table = GetClient().GetTable <ToDoWithStringId>();

            foreach (string testId in testIdData)
            {
                ToDoWithStringId item = new ToDoWithStringId()
                {
                    Id = testId, Name = "Hey"
                };
                await table.InsertAsync(item);

                // Read
                IEnumerable <ToDoWithStringId> results = await table.ReadAsync();

                ToDoWithStringId[] items = results.ToArray();

                Assert.Single(items);
                Assert.Equal(testId, items[0].Id);
                Assert.Equal("Hey", items[0].Name);

                // Filter
                results = await table.Where(i => i.Id == testId).ToEnumerableAsync();

                items = results.ToArray();

                Assert.Single(items);
                Assert.Equal(testId, items[0].Id);
                Assert.Equal("Hey", items[0].Name);

                // Projection
                var projectedResults = await table.Select(i => new { XId = i.Id, XString = i.Name }).ToEnumerableAsync();

                var projectedItems = projectedResults.ToArray();

                Assert.Single(projectedItems);
                Assert.Equal(testId, projectedItems[0].XId);
                Assert.Equal("Hey", projectedItems[0].XString);

                // Lookup
                item = await table.LookupAsync(testId);

                Assert.Equal(testId, item.Id);
                Assert.Equal("Hey", item.Name);

                // Update
                item.Name = "What?";
                await table.UpdateAsync(item);

                Assert.Equal(testId, item.Id);
                Assert.Equal("What?", item.Name);

                // Refresh
                item = new ToDoWithStringId()
                {
                    Id = testId, Name = "Hey"
                };
                await table.RefreshAsync(item);

                Assert.Equal(testId, item.Id);
                Assert.Equal("What?", item.Name);

                // Read Again
                results = await table.ReadAsync();

                items = results.ToArray();

                Assert.Single(items);
                Assert.Equal(testId, items[0].Id);
                Assert.Equal("What?", items[0].Name);

                await table.DeleteAsync(item);
            }
        }