public async Task DeleteAsync_ThrowsPreconditionFailedException_WhenMergeConflictOccurs_Generic() { await EnsureEmptyTableAsync <ToDoWithSystemPropertiesType>(); string id = "an id"; IMobileServiceTable <ToDoWithSystemPropertiesType> table = GetClient().GetTable <ToDoWithSystemPropertiesType>(); // insert a new item var item = new ToDoWithSystemPropertiesType() { Id = id, String = "a value" }; await table.InsertAsync(item); Assert.IsNotNull(item.CreatedAt); Assert.IsNotNull(item.UpdatedAt); Assert.IsNotNull(item.Version); string version = item.Version; // Delete with wrong version item.Version = "abc"; item.String = "But wait!"; MobileServicePreconditionFailedException <ToDoWithSystemPropertiesType> expectedException = null; try { await table.DeleteAsync(item); } catch (MobileServicePreconditionFailedException <ToDoWithSystemPropertiesType> exception) { expectedException = exception; } Assert.IsNotNull(expectedException); Assert.AreEqual(expectedException.Response.StatusCode, HttpStatusCode.PreconditionFailed); string responseContent = await expectedException.Response.Content.ReadAsStringAsync(); JToken jtoken = responseContent.ParseToJToken(table.MobileServiceClient.SerializerSettings); string serverVersion = (string)jtoken["version"]; string stringValue = (string)jtoken["String"]; Assert.AreEqual(version, serverVersion); Assert.AreEqual(stringValue, "a value"); Assert.IsNotNull(expectedException.Item); Assert.AreEqual(version, expectedException.Item.Version); Assert.AreEqual(stringValue, expectedException.Item.String); // Delete one last time with the version from the server item.Version = serverVersion; await table.DeleteAsync(item); Assert.IsNull(item.Id); }
public async Task InsertAsync_ThrowsConflictException_WhenConflictOccurs_Generic() { await EnsureEmptyTableAsync <ToDoWithSystemPropertiesType>(); string id = "an id"; IMobileServiceTable <ToDoWithSystemPropertiesType> table = GetClient().GetTable <ToDoWithSystemPropertiesType>(); ToDoWithSystemPropertiesType item = new ToDoWithSystemPropertiesType() { Id = id, String = "a value" }; await table.InsertAsync(item); var expectedException = await ThrowsAsync <MobileServiceConflictException <ToDoWithSystemPropertiesType> >(() => table.InsertAsync(item)); Assert.IsNotNull(expectedException); }
public async Task AsyncTableOperationsWithSystemProperties() { await EnsureEmptyTableAsync <ToDoWithSystemPropertiesType>(); IMobileServiceTable <ToDoWithSystemPropertiesType> allSystemPropertiesTable = GetClient().GetTable <ToDoWithSystemPropertiesType>(); // Regular insert ToDoWithSystemPropertiesType item = new ToDoWithSystemPropertiesType() { String = "a value" }; await allSystemPropertiesTable.InsertAsync(item); Assert.IsNotNull(item.CreatedAt); Assert.IsNotNull(item.UpdatedAt); Assert.IsNotNull(item.Version); // Explicit System Properties insert ToDoWithSystemPropertiesType item2 = new ToDoWithSystemPropertiesType(); await allSystemPropertiesTable.InsertAsync(item2); Assert.IsNotNull(item2.CreatedAt); Assert.AreEqual(new DateTime(), item2.UpdatedAt); Assert.IsNotNull(item2.Version); // Explicit System Properties Read IEnumerable <ToDoWithSystemPropertiesType> results = await allSystemPropertiesTable.Where(p => p.Id == item2.Id).ToEnumerableAsync(); ToDoWithSystemPropertiesType[] items = results.ToArray(); Assert.AreEqual(1, items.Count()); Assert.AreEqual(new DateTime(), items[0].CreatedAt); Assert.IsNotNull(items[0].UpdatedAt); Assert.IsNotNull(items[0].Version); await allSystemPropertiesTable.DeleteAsync(item); await allSystemPropertiesTable.DeleteAsync(item2); }
public async Task AsyncFilterSelectOrderingOperationsNotImpactedBySystemProperties() { await EnsureEmptyTableAsync <ToDoWithSystemPropertiesType>(); IMobileServiceTable <ToDoWithSystemPropertiesType> table = GetClient().GetTable <ToDoWithSystemPropertiesType>(); List <ToDoWithSystemPropertiesType> items = new List <ToDoWithSystemPropertiesType>(); // Insert some items for (int id = 0; id < 5; id++) { ToDoWithSystemPropertiesType item = new ToDoWithSystemPropertiesType() { Id = id.ToString(), String = "a value" }; await table.InsertAsync(item); Assert.IsNotNull(item.CreatedAt); Assert.IsNotNull(item.UpdatedAt); Assert.IsNotNull(item.Version); items.Add(item); await Task.Delay(10); // to separate the items in time } // Ordering var results = await table.OrderBy(t => t.CreatedAt).ToEnumerableAsync(); ToDoWithSystemPropertiesType[] orderItems = results.ToArray(); for (int i = 0; i < orderItems.Length - 1; i++) { Assert.IsTrue(int.Parse(orderItems[i].Id) < int.Parse(orderItems[i + 1].Id)); } results = await table.OrderBy(t => t.UpdatedAt).ToEnumerableAsync(); orderItems = results.ToArray(); for (int i = 0; i < orderItems.Length - 1; i++) { Assert.IsTrue(int.Parse(orderItems[i].Id) < int.Parse(orderItems[i + 1].Id)); } results = await table.OrderBy(t => t.Version).ToEnumerableAsync(); orderItems = results.ToArray(); for (int i = 0; i < orderItems.Length - 1; i++) { Assert.IsTrue(int.Parse(orderItems[i].Id) < int.Parse(orderItems[i + 1].Id)); } // Filtering results = await table.Where(t => t.CreatedAt >= items[4].CreatedAt).ToEnumerableAsync(); ToDoWithSystemPropertiesType[] filteredItems = results.ToArray(); for (int i = 0; i < filteredItems.Length - 1; i++) { Assert.IsTrue(filteredItems[i].CreatedAt >= items[4].CreatedAt); } results = await table.Where(t => t.UpdatedAt >= items[4].UpdatedAt).ToEnumerableAsync(); filteredItems = results.ToArray(); for (int i = 0; i < filteredItems.Length - 1; i++) { Assert.IsTrue(filteredItems[i].UpdatedAt >= items[4].UpdatedAt); } results = await table.Where(t => t.Version == items[4].Version).ToEnumerableAsync(); filteredItems = results.ToArray(); for (int i = 0; i < filteredItems.Length - 1; i++) { Assert.IsTrue(filteredItems[i].Version == items[4].Version); } // 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.IsTrue(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.IsTrue(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.IsTrue(item.Version == selectedItems3[i].Version); } // Delete foreach (var item in items) { await table.DeleteAsync(item); } }
public async Task AsyncTableOperationsWithAllSystemProperties() { await EnsureEmptyTableAsync <ToDoWithSystemPropertiesType>(); string id = "an id"; IMobileServiceTable <ToDoWithSystemPropertiesType> table = GetClient().GetTable <ToDoWithSystemPropertiesType>(); ToDoWithSystemPropertiesType item = new ToDoWithSystemPropertiesType() { Id = id, String = "a value" }; await table.InsertAsync(item); Assert.IsNotNull(item.CreatedAt); Assert.IsNotNull(item.UpdatedAt); Assert.IsNotNull(item.Version); // Read IEnumerable <ToDoWithSystemPropertiesType> results = await table.ReadAsync(); ToDoWithSystemPropertiesType[] items = results.ToArray(); Assert.AreEqual(1, items.Count()); Assert.IsNotNull(items[0].CreatedAt); Assert.IsNotNull(items[0].UpdatedAt); Assert.IsNotNull(items[0].Version); // Filter against version results = await table.Where(i => i.Version == items[0].Version).ToEnumerableAsync(); ToDoWithSystemPropertiesType[] filterItems = results.ToArray(); Assert.AreEqual(1, items.Count()); Assert.AreEqual(filterItems[0].CreatedAt, items[0].CreatedAt); Assert.AreEqual(filterItems[0].UpdatedAt, items[0].UpdatedAt); Assert.AreEqual(filterItems[0].Version, items[0].Version); // Filter against createdAt results = await table.Where(i => i.CreatedAt == items[0].CreatedAt).ToEnumerableAsync(); filterItems = results.ToArray(); Assert.AreEqual(1, items.Count()); Assert.AreEqual(filterItems[0].CreatedAt, items[0].CreatedAt); Assert.AreEqual(filterItems[0].UpdatedAt, items[0].UpdatedAt); Assert.AreEqual(filterItems[0].Version, items[0].Version); // Filter against updatedAt results = await table.Where(i => i.UpdatedAt == items[0].UpdatedAt).ToEnumerableAsync(); filterItems = results.ToArray(); Assert.AreEqual(1, items.Count()); Assert.AreEqual(filterItems[0].CreatedAt, items[0].CreatedAt); Assert.AreEqual(filterItems[0].UpdatedAt, items[0].UpdatedAt); Assert.AreEqual(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.AreEqual(1, projectedResults.Count()); Assert.AreEqual(projectedItems[0].XId, items[0].Id); Assert.AreEqual(projectedItems[0].XCreatedAt, items[0].CreatedAt); Assert.AreEqual(projectedItems[0].XUpdatedAt, items[0].UpdatedAt); Assert.AreEqual(projectedItems[0].XVersion, items[0].Version); // Lookup item = await table.LookupAsync(id); Assert.AreEqual(id, item.Id); Assert.AreEqual(item.Id, items[0].Id); Assert.AreEqual(item.CreatedAt, items[0].CreatedAt); Assert.AreEqual(item.UpdatedAt, items[0].UpdatedAt); Assert.AreEqual(item.Version, items[0].Version); // Refresh item = new ToDoWithSystemPropertiesType() { Id = id }; await table.RefreshAsync(item); Assert.AreEqual(id, item.Id); Assert.AreEqual(item.Id, items[0].Id); Assert.AreEqual(item.CreatedAt, items[0].CreatedAt); Assert.AreEqual(item.UpdatedAt, items[0].UpdatedAt); Assert.AreEqual(item.Version, items[0].Version); // Update item.String = "Hello!"; await table.UpdateAsync(item); Assert.AreEqual(item.Id, items[0].Id); Assert.AreEqual(item.CreatedAt, items[0].CreatedAt); Assert.IsTrue(item.UpdatedAt >= items[0].UpdatedAt); Assert.IsNotNull(item.Version); Assert.AreNotEqual(item.Version, items[0].Version); // Read Again results = await table.ReadAsync(); items = results.ToArray(); Assert.AreEqual(id, item.Id); Assert.AreEqual(item.Id, items[0].Id); Assert.AreEqual(item.CreatedAt, items[0].CreatedAt); Assert.AreEqual(item.UpdatedAt, items[0].UpdatedAt); Assert.AreEqual(item.Version, items[0].Version); await table.DeleteAsync(item); }