Exemplo n.º 1
0
        public async Task Version()
        {
            // Create the untyped collection
            MobileServiceClient          client  = GetClient();
            IMobileServiceTable <ToDo>   table   = client.GetTable <ToDo>();
            IMobileServiceTable <ToDoV2> tableV2 = client.GetTable <ToDoV2>();

            // Get some V1 data
            ToDo first = new ToDo {
                Title = "Foo"
            };
            await table.InsertAsync(first);

            // Get the data in V2 format
            ToDoV2 second = await tableV2.LookupAsync(first.Id);

            Assert.AreEqual(first.Title, second.Title);

            // Add a tag
            second.Tag = "Bar";
            await tableV2.UpdateAsync(second);

            await table.RefreshAsync(first);

            Assert.AreEqual("Bar: Foo", first.Title);

            // Chagne the tag
            first.Title = "Baz: Quux";
            await table.UpdateAsync(first);

            await tableV2.RefreshAsync(second);

            Assert.AreEqual("Baz", second.Tag);
            Assert.AreEqual("Quux", second.Title);
        }
        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));
            }
        }
Exemplo n.º 3
0
        public async Task UseToDo()
        {
            // Insert a few records
            IMobileServiceTable <ToDo> table = GetClient().GetTable <ToDo>();
            ToDo first = new ToDo {
                Title = "Get Milk", Complete = false
            };
            await table.InsertAsync(first);

            await table.InsertAsync(new ToDo { Title = "Pick up dry cleaning", Complete = false });

            // Run a simple query and verify we get both items
            List <ToDo> items = await table.Where(i => i.Id >= first.Id).ToListAsync();

            Assert.AreEqual(2, items.Count);

            // Add another item
            await table.InsertAsync(new ToDo { Title = "Submit TPS report", Complete = false });

            // Check off the first item
            ToDo milk = items.Where(t => t.Title.Contains("Milk")).FirstOrDefault();

            milk.Complete = true;
            await table.UpdateAsync(milk);

            // Get the remaining items using a LINQ query
            IEnumerable <ToDo> remaining = await table.ReadAsync(
                from t in table
                where t.Complete == false && t.Id >= first.Id
                select t);

            Assert.AreEqual(2, remaining.Count());

            // Delete the first item
            await table.DeleteAsync(milk);

            items = await table.Where(t => t.Id >= first.Id).ToListAsync();

            Assert.AreEqual(2, items.Count);

            // Change the TPS report item without using the object
            // (to simulate the server object being changed by someone
            // else)
            ToDo    tps     = items.Where(t => t.Title.Contains("TPS")).FirstOrDefault();
            JObject jobject = new JObject();

            jobject["id"]   = (int)tps.Id;
            jobject["col1"] = tps.Title + " using the new cover sheet";
            await table.UpdateAsync(jobject);

            await table.RefreshAsync(tps);

            Assert.Contains(tps.Title, "cover sheet");
        }
        public async Task RefreshAsyncGeneric()
        {
            TestHttpHandler                  hijack  = new TestHttpHandler();
            IMobileServiceClient             service = new MobileServiceClient("http://www.test.com", "secret...", hijack);
            IMobileServiceTable <StringType> table   = service.GetTable <StringType>();

            StringType expected = new StringType();

            expected.Id = 12;

            hijack.SetResponseContent("{\"id\":12,\"String\":\"Goodbye\"}");
            await table.RefreshAsync(expected);

            Assert.Contains(hijack.Request.RequestUri.ToString(), "$filter=id eq 12");
            Assert.AreEqual(12, expected.Id);
            Assert.AreEqual("Goodbye", expected.String);
        }
        public async Task RefreshAsyncGenericWithUserParameters()
        {
            var userDefinedParameters = new Dictionary <string, string>()
            {
                { "state", "CA" }
            };

            TestHttpHandler                  hijack  = new TestHttpHandler();
            IMobileServiceClient             service = new MobileServiceClient("http://www.test.com", "secret...", hijack);
            IMobileServiceTable <StringType> table   = service.GetTable <StringType>();

            StringType expected = new StringType();

            expected.Id = 12;

            hijack.SetResponseContent("{\"id\":12,\"String\":\"Goodbye\"}");
            await table.RefreshAsync(expected, userDefinedParameters);

            Assert.Contains(hijack.Request.RequestUri.ToString(), "$filter=id eq 12");
            Assert.Contains(hijack.Request.RequestUri.Query, "state=CA");
            Assert.AreEqual(12, expected.Id);
            Assert.AreEqual("Goodbye", expected.String);
        }
        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, String = "Hey"
                };
                await table.InsertAsync(item);
            }

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

                await table.DeleteAsync(item);

                item.Id = testId;

                InvalidOperationException exception = null;
                try
                {
                    await table.RefreshAsync(item);
                }
                catch (InvalidOperationException e)
                {
                    exception = e;
                }

                Assert.IsNotNull(exception);
            }
        }
        public async Task AsyncTableOperationsWithStringIdAgainstIntegerIdTable()
        {
            await EnsureEmptyTableAsync <ToDoWithIntId>();

            IMobileServiceTable <ToDoWithIntId> table = GetClient().GetTable <ToDoWithIntId>();
            List <ToDoWithIntId> integerIdItems       = new List <ToDoWithIntId>();

            for (var i = 0; i < 10; i++)
            {
                ToDoWithIntId item = new ToDoWithIntId()
                {
                    String = i.ToString()
                };
                await table.InsertAsync(item);

                integerIdItems.Add(item);
            }

            string[] testIdData = IdTestData.ValidStringIds.ToArray();

            IMobileServiceTable <ToDoWithStringIdAgainstIntIdTable> stringIdTable = GetClient().GetTable <ToDoWithStringIdAgainstIntIdTable>();

            foreach (string testId in testIdData)
            {
                // Filter
                Exception exception = null;
                try
                {
                    IEnumerable <ToDoWithStringIdAgainstIntIdTable> results = await stringIdTable.Where(p => p.Id == testId).ToEnumerableAsync();

                    ToDoWithStringIdAgainstIntIdTable[] items = results.ToArray();
                }
                catch (Exception e)
                {
                    exception = e;
                }

                Assert.IsNotNull(exception);
                Assert.IsTrue(exception.Message.Contains("Bad request"));

                // Refresh
                exception = null;
                try
                {
                    ToDoWithStringIdAgainstIntIdTable item = new ToDoWithStringIdAgainstIntIdTable()
                    {
                        Id = testId, String = "Hey!"
                    };
                    await stringIdTable.RefreshAsync(item);
                }
                catch (Exception e)
                {
                    exception = e;
                }

                Assert.IsNotNull(exception);
                Assert.IsTrue(exception.Message.Contains("Bad request"));

                // Insert
                exception = null;
                try
                {
                    ToDoWithStringIdAgainstIntIdTable item = new ToDoWithStringIdAgainstIntIdTable()
                    {
                        Id = testId, String = "Hey!"
                    };
                    await stringIdTable.InsertAsync(item);
                }
                catch (Exception e)
                {
                    exception = e;
                }

                Assert.IsNotNull(exception);
                Assert.IsTrue(exception.Message.Contains("Error: A value cannot be specified for property 'id'"));

                // Lookup
                exception = null;
                try
                {
                    await stringIdTable.LookupAsync(testId);
                }
                catch (Exception e)
                {
                    exception = e;
                }

                Assert.IsNotNull(exception);
                Assert.IsTrue(exception.Message.Contains("Error: The value specified for 'id' must be a number."));

                // Update
                exception = null;
                try
                {
                    ToDoWithStringIdAgainstIntIdTable item = new ToDoWithStringIdAgainstIntIdTable()
                    {
                        Id = testId, String = "Hey!"
                    };
                    await stringIdTable.UpdateAsync(item);
                }
                catch (Exception e)
                {
                    exception = e;
                }

                Assert.IsNotNull(exception);
                Assert.IsTrue(exception.Message.Contains("Error: The value specified for 'id' must be a number."));

                // Delete
                exception = null;
                try
                {
                    ToDoWithStringIdAgainstIntIdTable item = new ToDoWithStringIdAgainstIntIdTable()
                    {
                        Id = testId, String = "Hey!"
                    };
                    await stringIdTable.DeleteAsync(item);
                }
                catch (Exception e)
                {
                    exception = e;
                }

                Assert.IsNotNull(exception);
                Assert.IsTrue(exception.Message.Contains("Error: The value specified for 'id' must be a number."));
            }

            foreach (ToDoWithIntId integerIdItem in integerIdItems)
            {
                await table.DeleteAsync(integerIdItem);
            }
        }
        public async Task AsyncTableOperationsWithIntegerAsStringIdAgainstIntIdTable()
        {
            await EnsureEmptyTableAsync <ToDoWithStringIdAgainstIntIdTable>();

            IMobileServiceTable <ToDoWithStringIdAgainstIntIdTable> stringIdTable = GetClient().GetTable <ToDoWithStringIdAgainstIntIdTable>();
            ToDoWithStringIdAgainstIntIdTable item = new ToDoWithStringIdAgainstIntIdTable()
            {
                String = "Hey"
            };

            // Insert
            await stringIdTable.InsertAsync(item);

            string testId = item.Id.ToString();

            // Read
            IEnumerable <ToDoWithStringIdAgainstIntIdTable> results = await stringIdTable.ReadAsync();

            ToDoWithStringIdAgainstIntIdTable[] items = results.ToArray();

            Assert.AreEqual(1, items.Count());
            Assert.AreEqual(testId, items[0].Id);
            Assert.AreEqual("Hey", items[0].String);

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

            items = results.ToArray();

            Assert.AreEqual(1, items.Count());
            Assert.AreEqual(testId, items[0].Id);
            Assert.AreEqual("Hey", items[0].String);

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

            var projectedItems = projectedResults.ToArray();

            Assert.AreEqual(1, projectedItems.Count());
            Assert.AreEqual(testId, projectedItems[0].XId);
            Assert.AreEqual("Hey", projectedItems[0].XString);

            // Lookup
            ToDoWithStringIdAgainstIntIdTable stringIdItem = await stringIdTable.LookupAsync(testId);

            Assert.AreEqual(testId, stringIdItem.Id);
            Assert.AreEqual("Hey", stringIdItem.String);

            // Update
            stringIdItem.String = "What?";
            await stringIdTable.UpdateAsync(stringIdItem);

            Assert.AreEqual(testId, stringIdItem.Id);
            Assert.AreEqual("What?", stringIdItem.String);

            // Refresh
            stringIdItem = new ToDoWithStringIdAgainstIntIdTable()
            {
                Id = testId, String = "Hey"
            };
            await stringIdTable.RefreshAsync(stringIdItem);

            Assert.AreEqual(testId, stringIdItem.Id);
            Assert.AreEqual("What?", stringIdItem.String);

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

            items = results.ToArray();

            Assert.AreEqual(1, items.Count());
            Assert.AreEqual(testId, items[0].Id);
            Assert.AreEqual("What?", items[0].String);

            // Delete
            await stringIdTable.DeleteAsync(item);
        }
        public async Task AsyncTableOperationsWithValidStringIdAgainstStringIdTable()
        {
            await EnsureEmptyTableAsync <ToDoWithStringId>();

            string[] testIdData = IdTestData.ValidStringIds;

            foreach (string testId in testIdData)
            {
                IMobileServiceTable <ToDoWithStringId> table = GetClient().GetTable <ToDoWithStringId>();

                ToDoWithStringId item = new ToDoWithStringId()
                {
                    Id = testId, String = "Hey"
                };
                await table.InsertAsync(item);

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

                ToDoWithStringId[] items = results.ToArray();

                Assert.AreEqual(1, items.Count());
                Assert.AreEqual(testId, items[0].Id);
                Assert.AreEqual("Hey", items[0].String);

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

                items = results.ToArray();

                Assert.AreEqual(1, items.Count());
                Assert.AreEqual(testId, items[0].Id);
                Assert.AreEqual("Hey", items[0].String);

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

                var projectedItems = projectedResults.ToArray();

                Assert.AreEqual(1, projectedItems.Count());
                Assert.AreEqual(testId, projectedItems[0].XId);
                Assert.AreEqual("Hey", projectedItems[0].XString);

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

                Assert.AreEqual(testId, item.Id);
                Assert.AreEqual("Hey", item.String);

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

                Assert.AreEqual(testId, item.Id);
                Assert.AreEqual("What?", item.String);

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

                Assert.AreEqual(testId, item.Id);
                Assert.AreEqual("What?", item.String);

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

                items = results.ToArray();

                Assert.AreEqual(1, items.Count());
                Assert.AreEqual(testId, items[0].Id);
                Assert.AreEqual("What?", items[0].String);

                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);
        }
Exemplo n.º 11
0
        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.IsNotNull(item.CreatedAt);
            Assert.IsNotNull(item.UpdatedAt);
            Assert.IsNotNull(item.Version);

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

            RoundTripTableItemWithSystemPropertiesType[] 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
            // 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.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();
             * RoundTripTableItemWithSystemPropertiesType[] 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 RoundTripTableItemWithSystemPropertiesType()
            {
                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.Name = "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);
        }
 public async Task RefreshAsync(T instance)
 {
     await _table.RefreshAsync(instance);
 }
Exemplo n.º 13
0
        public async Task AsyncTableOperationsWithStringIdAgainstIntegerIdTable()
        {
            Log("This test fails with the .NET backend since in .NET the DTO always has string-id. In Node, querying an int-id column for a string causes an error.");

            await EnsureEmptyTableAsync <ToDoWithIntId>();

            IMobileServiceTable <ToDoWithIntId> table = GetClient().GetTable <ToDoWithIntId>();
            List <ToDoWithIntId> integerIdItems       = new List <ToDoWithIntId>();

            for (var i = 0; i < 10; i++)
            {
                ToDoWithIntId item = new ToDoWithIntId()
                {
                    Name = i.ToString()
                };
                await table.InsertAsync(item);

                integerIdItems.Add(item);
            }

            string[] testIdData = IdTestData.ValidStringIds.ToArray();

            IMobileServiceTable <ToDoWithStringIdAgainstIntIdTable> stringIdTable = GetClient().GetTable <ToDoWithStringIdAgainstIntIdTable>();

            foreach (string testId in testIdData)
            {
                // Filter
                MobileServiceInvalidOperationException exception = null;
                try
                {
                    IEnumerable <ToDoWithStringIdAgainstIntIdTable> results = await stringIdTable.Where(p => p.Id == testId).ToEnumerableAsync();

                    ToDoWithStringIdAgainstIntIdTable[] items = results.ToArray();
                }
                catch (MobileServiceInvalidOperationException e)
                {
                    exception = e;
                }

                Assert.IsNotNull(exception);
                Assert.Equals(exception.Response.StatusCode, HttpStatusCode.BadRequest);

                // Refresh
                exception = null;
                try
                {
                    ToDoWithStringIdAgainstIntIdTable item = new ToDoWithStringIdAgainstIntIdTable()
                    {
                        Id = testId, Name = "Hey!"
                    };
                    await stringIdTable.RefreshAsync(item);
                }
                catch (MobileServiceInvalidOperationException e)
                {
                    exception = e;
                }

                Assert.IsNotNull(exception);
                Assert.Equals(exception.Response.StatusCode, HttpStatusCode.BadRequest);

                // Lookup
                exception = null;
                try
                {
                    await stringIdTable.LookupAsync(testId);
                }
                catch (MobileServiceInvalidOperationException e)
                {
                    exception = e;
                }

                Assert.IsNotNull(exception);
                Assert.Equals(exception.Response.StatusCode, HttpStatusCode.BadRequest);

                // Update
                exception = null;
                try
                {
                    ToDoWithStringIdAgainstIntIdTable item = new ToDoWithStringIdAgainstIntIdTable()
                    {
                        Id = testId, Name = "Hey!"
                    };
                    await stringIdTable.UpdateAsync(item);
                }
                catch (MobileServiceInvalidOperationException e)
                {
                    exception = e;
                }

                Assert.IsNotNull(exception);
                Assert.Equals(exception.Response.StatusCode, HttpStatusCode.BadRequest);

                // Delete
                exception = null;
                try
                {
                    ToDoWithStringIdAgainstIntIdTable item = new ToDoWithStringIdAgainstIntIdTable()
                    {
                        Id = testId, Name = "Hey!"
                    };
                    await stringIdTable.DeleteAsync(item);
                }
                catch (MobileServiceInvalidOperationException e)
                {
                    exception = e;
                }

                Assert.IsNotNull(exception);
                Assert.Equals(exception.Response.StatusCode, HttpStatusCode.BadRequest);
            }

            foreach (ToDoWithIntId integerIdItem in integerIdItems)
            {
                await table.DeleteAsync(integerIdItem);
            }
        }