public async Task AsyncFilterSelectOrderingOperationsNotImpactedBySystemProperties() { await EnsureEmptyTableAsync <RoundTripTableItemWithSystemPropertiesType>(); IMobileServiceTable <RoundTripTableItemWithSystemPropertiesType> table = GetClient().GetTable <RoundTripTableItemWithSystemPropertiesType>(); List <RoundTripTableItemWithSystemPropertiesType> items = new List <RoundTripTableItemWithSystemPropertiesType>(); // Insert some items for (int id = 0; id < 5; id++) { RoundTripTableItemWithSystemPropertiesType item = new RoundTripTableItemWithSystemPropertiesType() { Id = id.ToString(), Name = "a value" }; await table.InsertAsync(item); Assert.NotNull(item.CreatedAt); Assert.NotNull(item.UpdatedAt); Assert.NotNull(item.Version); items.Add(item); } // Selection var selectionResults = await table.Select(t => new { Id = t.Id, CreatedAt = t.CreatedAt }).ToEnumerableAsync(); var selectedItems = selectionResults.ToArray(); for (int i = 0; i < selectedItems.Length; i++) { var item = items.Where(t => t.Id == selectedItems[i].Id).FirstOrDefault(); Assert.Equal(item.CreatedAt, selectedItems[i].CreatedAt); } var selectionResults2 = await table.Select(t => new { Id = t.Id, UpdatedAt = t.UpdatedAt }).ToEnumerableAsync(); var selectedItems2 = selectionResults2.ToArray(); for (int i = 0; i < selectedItems2.Length; i++) { var item = items.Where(t => t.Id == selectedItems2[i].Id).FirstOrDefault(); Assert.Equal(item.UpdatedAt, selectedItems2[i].UpdatedAt); } var selectionResults3 = await table.Select(t => new { Id = t.Id, Version = t.Version }).ToEnumerableAsync(); var selectedItems3 = selectionResults3.ToArray(); for (int i = 0; i < selectedItems3.Length; i++) { var item = items.Where(t => t.Id == selectedItems3[i].Id).FirstOrDefault(); Assert.Equal(item.Version, selectedItems3[i].Version); } // Delete foreach (var item in items) { await table.DeleteAsync(item); } }
public async Task AsyncFilterSelectOrdering_FilterByCreatedAt_NotImpactedBySystemProperties() { // Set up the table. await EnsureEmptyTableAsync <RoundTripTableItemWithSystemPropertiesType>(); IMobileServiceTable <RoundTripTableItemWithSystemPropertiesType> table = GetClient().GetTable <RoundTripTableItemWithSystemPropertiesType>(); List <RoundTripTableItemWithSystemPropertiesType> items = new List <RoundTripTableItemWithSystemPropertiesType>(); for (int id = 0; id < 5; id++) { RoundTripTableItemWithSystemPropertiesType item = new RoundTripTableItemWithSystemPropertiesType() { Id = id.ToString(), Name = "a value" }; await table.InsertAsync(item); Assert.NotNull(item.CreatedAt); Assert.NotNull(item.UpdatedAt); Assert.NotNull(item.Version); items.Add(item); } // Run test var results = await table.Where(t => t.CreatedAt >= items[4].CreatedAt).ToListAsync(); RoundTripTableItemWithSystemPropertiesType[] filteredItems = results.ToArray(); for (int i = 0; i < filteredItems.Length - 1; i++) { Assert.True(filteredItems[i].CreatedAt >= items[4].CreatedAt); } // Cleanup items.ForEach(async t => await table.DeleteAsync(t)); }
public async Task UpdateAsyncWithMergeConflict_Generic() { await EnsureEmptyTableAsync <RoundTripTableItemWithSystemPropertiesType>(); string id = Guid.NewGuid().ToString(); IMobileServiceTable <RoundTripTableItemWithSystemPropertiesType> table = GetClient().GetTable <RoundTripTableItemWithSystemPropertiesType>(); RoundTripTableItemWithSystemPropertiesType item = new RoundTripTableItemWithSystemPropertiesType() { Id = id, Name = "a value" }; await table.InsertAsync(item); Assert.NotNull(item.CreatedAt); Assert.NotNull(item.UpdatedAt); Assert.NotNull(item.Version); string version = item.Version; // Update item.Name = "Hello!"; await table.UpdateAsync(item); Assert.NotNull(item.Version); Assert.NotEqual(item.Version, version); string newVersion = item.Version; // Update again but with the original version item.Version = version; item.Name = "But wait!"; var expectedException = await Assert.ThrowsAsync <MobileServicePreconditionFailedException <RoundTripTableItemWithSystemPropertiesType> >(() => table.UpdateAsync(item)); Assert.Equal(HttpStatusCode.PreconditionFailed, expectedException.Response.StatusCode); Assert.NotNull(expectedException.Item); string serverVersion = expectedException.Item.Version; string stringValue = expectedException.Item.Name; Assert.Equal(newVersion, serverVersion); Assert.Equal("Hello!", stringValue); // Update one last time with the version from the server item.Version = serverVersion; await table.UpdateAsync(item); Assert.NotNull(item.Version); Assert.Equal("But wait!", item.Name); Assert.NotEqual(item.Version, serverVersion); await table.DeleteAsync(item); }
public async Task DeleteAsync_ThrowsPreconditionFailedException_WhenMergeConflictOccurs_Generic() { string id = Guid.NewGuid().ToString(); var table = GetClient().GetTable <RoundTripTableItemWithSystemPropertiesType>(); // insert a new item var item = new RoundTripTableItemWithSystemPropertiesType() { Id = id, Name = "a value" }; await table.InsertAsync(item); Assert.NotNull(item.CreatedAt); Assert.NotNull(item.UpdatedAt); Assert.NotNull(item.Version); string version = item.Version; // Delete with wrong version item.Version = "3q3A3g=="; item.Name = "But wait!"; var expectedException = await Assert.ThrowsAsync <MobileServicePreconditionFailedException <RoundTripTableItemWithSystemPropertiesType> >(() => table.DeleteAsync(item)); Assert.Equal(HttpStatusCode.PreconditionFailed, expectedException.Response.StatusCode); string responseContent = await expectedException.Response.Content.ReadAsStringAsync(); RoundTripTableItemWithSystemPropertiesType serverItem = expectedException.Item; string serverVersion = serverItem.Version; string stringValue = serverItem.Name; Assert.Equal(version, serverVersion); Assert.Equal("a value", stringValue); Assert.NotNull(expectedException.Item); Assert.Equal(version, expectedException.Item.Version); Assert.Equal(stringValue, expectedException.Item.Name); // Delete one last time with the version from the server item.Version = serverVersion; await table.DeleteAsync(item); Assert.Null(item.Id); }
public async Task AsyncFilterSelectOrdering_OrderByUpdatedAt_NotImpactedBySystemProperties() { // Set up the table. await EnsureEmptyTableAsync <RoundTripTableItemWithSystemPropertiesType>(); IMobileServiceTable <RoundTripTableItemWithSystemPropertiesType> table = GetClient().GetTable <RoundTripTableItemWithSystemPropertiesType>(); List <RoundTripTableItemWithSystemPropertiesType> items = new List <RoundTripTableItemWithSystemPropertiesType>(); for (int id = 0; id < 5; id++) { RoundTripTableItemWithSystemPropertiesType item = new RoundTripTableItemWithSystemPropertiesType() { Id = id.ToString(), Name = "a value" }; await table.InsertAsync(item); Assert.NotNull(item.CreatedAt); Assert.NotNull(item.UpdatedAt); Assert.NotNull(item.Version); items.Add(item); } // Run test var results = await table.OrderBy(t => t.UpdatedAt).ToListAsync(); // Fails here with .NET runtime. Why?? RoundTripTableItemWithSystemPropertiesType[] orderItems = results.ToArray(); for (int i = 0; i < orderItems.Length - 1; i++) { Assert.True(int.Parse(orderItems[i].Id) < int.Parse(orderItems[i + 1].Id)); } // Cleanup foreach (var itemToDelete in items) { await table.DeleteAsync(itemToDelete); } }
public async Task AsyncTableOperationsWithSystemPropertiesSetExplicitly() { await EnsureEmptyTableAsync <RoundTripTableItemWithSystemPropertiesType>(); IMobileServiceTable <RoundTripTableItemWithSystemPropertiesType> allSystemPropertiesTable = GetClient().GetTable <RoundTripTableItemWithSystemPropertiesType>(); // Regular insert RoundTripTableItemWithSystemPropertiesType item = new RoundTripTableItemWithSystemPropertiesType() { Name = "a value" }; await allSystemPropertiesTable.InsertAsync(item); Assert.NotNull(item.CreatedAt); Assert.NotNull(item.UpdatedAt); Assert.NotNull(item.Version); // Explicit System Properties Read IEnumerable <RoundTripTableItemWithSystemPropertiesType> results = await allSystemPropertiesTable.Where(p => p.Id == item.Id).ToEnumerableAsync(); RoundTripTableItemWithSystemPropertiesType[] items = results.ToArray(); Assert.Single(items); Assert.NotNull(items[0].CreatedAt); Assert.NotNull(items[0].UpdatedAt); Assert.NotNull(items[0].Version); // Lookup var item3 = await allSystemPropertiesTable.LookupAsync(item.Id); Assert.Equal(item.CreatedAt, item3.CreatedAt); Assert.Equal(item.UpdatedAt, item3.UpdatedAt); Assert.NotNull(item3.Version); await allSystemPropertiesTable.DeleteAsync(item); }
public async Task AsyncTableOperationsWithAllSystemProperties() { await EnsureEmptyTableAsync <RoundTripTableItemWithSystemPropertiesType>(); string id = Guid.NewGuid().ToString(); IMobileServiceTable <RoundTripTableItemWithSystemPropertiesType> table = GetClient().GetTable <RoundTripTableItemWithSystemPropertiesType>(); RoundTripTableItemWithSystemPropertiesType item = new RoundTripTableItemWithSystemPropertiesType() { Id = id, Name = "a value" }; await table.InsertAsync(item); Assert.NotNull(item.CreatedAt); Assert.NotNull(item.UpdatedAt); Assert.NotNull(item.Version); // Read IEnumerable <RoundTripTableItemWithSystemPropertiesType> results = await table.ReadAsync(); RoundTripTableItemWithSystemPropertiesType[] items = results.ToArray(); Assert.Single(items); Assert.NotNull(items[0].CreatedAt); Assert.NotNull(items[0].UpdatedAt); Assert.NotNull(items[0].Version); // Filter against version // BUG #1706815 (OData query for version field (string <--> byte[] mismatch) /* * results = await table.Where(i => i.Version == items[0].Version).ToEnumerableAsync(); * RoundTripTableItemWithSystemPropertiesType[] filterItems = results.ToArray(); * * Assert.Equal(1, items.Count()); * Assert.Equal(filterItems[0].CreatedAt, items[0].CreatedAt); * Assert.Equal(filterItems[0].UpdatedAt, items[0].UpdatedAt); * Assert.Equal(filterItems[0].Version, items[0].Version); * * // Filter against createdAt * results = await table.Where(i => i.CreatedAt == items[0].CreatedAt).ToEnumerableAsync(); * RoundTripTableItemWithSystemPropertiesType[] filterItems = results.ToArray(); * * Assert.Equal(1, items.Count()); * Assert.Equal(filterItems[0].CreatedAt, items[0].CreatedAt); * Assert.Equal(filterItems[0].UpdatedAt, items[0].UpdatedAt); * Assert.Equal(filterItems[0].Version, items[0].Version); * * // Filter against updatedAt * results = await table.Where(i => i.UpdatedAt == items[0].UpdatedAt).ToEnumerableAsync(); * filterItems = results.ToArray(); * * Assert.Equal(1, items.Count()); * Assert.Equal(filterItems[0].CreatedAt, items[0].CreatedAt); * Assert.Equal(filterItems[0].UpdatedAt, items[0].UpdatedAt); * Assert.Equal(filterItems[0].Version, items[0].Version); */ // Projection var projectedResults = await table.Select(i => new { XId = i.Id, XCreatedAt = i.CreatedAt, XUpdatedAt = i.UpdatedAt, XVersion = i.Version }).ToEnumerableAsync(); var projectedItems = projectedResults.ToArray(); Assert.Single(projectedResults); Assert.Equal(projectedItems[0].XId, items[0].Id); Assert.Equal(projectedItems[0].XCreatedAt, items[0].CreatedAt); Assert.Equal(projectedItems[0].XUpdatedAt, items[0].UpdatedAt); Assert.Equal(projectedItems[0].XVersion, items[0].Version); // Lookup item = await table.LookupAsync(id); Assert.Equal(id, item.Id); Assert.Equal(item.Id, items[0].Id); Assert.Equal(item.CreatedAt, items[0].CreatedAt); Assert.Equal(item.UpdatedAt, items[0].UpdatedAt); Assert.Equal(item.Version, items[0].Version); // Refresh item = new RoundTripTableItemWithSystemPropertiesType() { Id = id }; await table.RefreshAsync(item); Assert.Equal(id, item.Id); Assert.Equal(item.Id, items[0].Id); Assert.Equal(item.CreatedAt, items[0].CreatedAt); Assert.Equal(item.UpdatedAt, items[0].UpdatedAt); Assert.Equal(item.Version, items[0].Version); // Update item.Name = "Hello!"; await table.UpdateAsync(item); Assert.Equal(item.Id, items[0].Id); Assert.Equal(item.CreatedAt, items[0].CreatedAt); Assert.True(item.UpdatedAt >= items[0].UpdatedAt); Assert.NotNull(item.Version); Assert.NotEqual(item.Version, items[0].Version); // Read Again results = await table.ReadAsync(); items = results.ToArray(); Assert.Equal(id, item.Id); Assert.Equal(item.Id, items[0].Id); Assert.Equal(item.CreatedAt, items[0].CreatedAt); Assert.Equal(item.UpdatedAt, items[0].UpdatedAt); Assert.Equal(item.Version, items[0].Version); await table.DeleteAsync(item); }