public async Task AllProducts_Exceptions()
        {
            var entityManager = new EntityManager(_serviceName);

            // All instances of Product
            var query = new EntityQuery <Product>();

            // Capture result using try-catch
            try
            {
                var results = await entityManager.ExecuteQuery(query);

                var count = results.Count();
                Assert.IsTrue(count > 0, "Product query returned " + count + " Products");
            }
            catch (Exception e)
            {
                var message = TestFns.FormatException(e);
                Assert.Fail(message);
            }
        }
        public async Task AllProducts_Task()
        {
            var entityManager = new EntityManager(_serviceName);

            // All instances of Product
            var query = new EntityQuery <Product>();                        // Alternate way to create a basic EntityQuery

            // Handle async Task results explicitly
            await entityManager.ExecuteQuery(query).ContinueWith(task =>
            {
                if (task.IsFaulted)
                {
                    var message = TestFns.FormatException(task.Exception);
                    Assert.Fail(message);
                }
                else
                {
                    var count = task.Result.Count();
                    Assert.IsTrue(count > 0, "Product query returned " + count + " Products");
                }
            });
        }
        public async Task Products_InlineCount()
        {
            var entityManager = new EntityManager(_serviceName);

            // All instances of Product
            var query = new EntityQuery <Product>().Where(i => i.ProductName.StartsWith("A")).InlineCount();

            // Handle async Task results explicitly
            await entityManager.ExecuteQuery(query).ContinueWith(task =>
            {
                if (task.IsFaulted)
                {
                    var message = TestFns.FormatException(task.Exception);
                    Assert.Fail(message);
                }
                else
                {
                    var count = ((IHasInlineCount)task.Result).InlineCount;
                    Assert.IsTrue(count == 2, "There are " + count + " products with name starting with 'A'");
                }
            });
        }
        public async Task Products_Slice()
        {
            var entityManager = new EntityManager(_serviceName);

            // All instances of Product
            var query = new EntityQuery <Product>().OrderBy(i => i.ProductName).Skip(10).Take(20);

            // Handle async Task results explicitly
            await entityManager.ExecuteQuery(query).ContinueWith(task =>
            {
                if (task.IsFaulted)
                {
                    var message = TestFns.FormatException(task.Exception);
                    Assert.Fail(message);
                }
                else
                {
                    var count = task.Result.Count();
                    Assert.IsTrue(count == 20, "Product query returned " + count + " Products");
                }
            });
        }
        public async Task SaveDeletedEntity()
        {
            var entityManager = await TestFns.NewEm(_serviceName);

            // Create a new customer
            var product = new Product {
                ID = Guid.NewGuid()
            };

            entityManager.AddEntity(product);
            product.ProductName = "Test3A " + DateTime.Now.ToString();
            Assert.IsTrue(product.EntityAspect.EntityState == EntityState.Added, "State of new entity should be Added");

            try
            {
                var saveResult = await entityManager.SaveChanges();

                var savedEntity = saveResult.Entities[0];
                Assert.IsTrue(savedEntity is Product && savedEntity == product, "After save, added entity should still exist");
                Assert.IsTrue(product.EntityAspect.EntityState == EntityState.Unchanged, "State of saved entity should be Unchanged");

                // Delete customer
                product.EntityAspect.Delete();
                Assert.IsTrue(product.EntityAspect.EntityState == EntityState.Deleted,
                              "After delete, entity state should be deleted, not " + product.EntityAspect.EntityState.ToString());
                saveResult = await entityManager.SaveChanges();

                savedEntity = saveResult.Entities[0];
                Assert.IsTrue(savedEntity.EntityAspect.EntityState == EntityState.Detached,
                              "After save of deleted entity, entity state should be detached, not " + savedEntity.EntityAspect.EntityState.ToString());
            }
            catch (Exception e)
            {
                var message = string.Format("Save of deleted customer {0} should have succeeded;  Received {1}: {2}",
                                            product.ProductName, e.GetType().Name, e.Message);
                Assert.Fail(message);
            }
        }