Esempio n. 1
0
        public void CreateUpdateEntityFromODataClient()
        {
            for (int i = 1; i < 2; i++)
            {
                if (i == 0)
                {
                    //TestClientContext.Format.UseAtom();
                }
                else
                {
                    TestClientContext.Format.UseJson(Model);
                }

                string tmpName   = Guid.NewGuid().ToString();
                var    queryable = TestClientContext.Products.AddQueryOption("$filter", string.Format("Name eq '{0}'", tmpName));

                // query and verify
                var result1 = queryable.ToList();
                Assert.AreEqual(0, result1.Count);

                // create an entity
                Product product = new Product()
                {
                    ProductID       = (new Random()).Next(),
                    Name            = tmpName,
                    SkinColor       = Color.Red,
                    Discontinued    = false,
                    QuantityInStock = 23,
                    QuantityPerUnit = "my quantity per unit",
                    UnitPrice       = 23.01f,
                    UserAccess      = AccessLevel.ReadWrite,
                    CoverColors     = new ObservableCollection <Color>()
                    {
                        Color.Red,
                        Color.Blue
                    }
                };
                TestClientContext.AddToProducts(product);
                TestClientContext.SaveChanges();

                // query and verify
                var result2 = queryable.ToList();
                Assert.AreEqual(1, result2.Count);
                Assert.AreEqual(Color.Red, result2[0].SkinColor);

                // update the Enum properties
                product.SkinColor  = Color.Green;
                product.UserAccess = AccessLevel.Execute;
                TestClientContext.UpdateObject(product);
                TestClientContext.SaveChanges();

                // query and verify
                var result3 = queryable.ToList();
                Assert.AreEqual(1, result3.Count);
                Assert.AreEqual(Color.Green, result3[0].SkinColor);
            }
        }
Esempio n. 2
0
        public void DelayQueryOnEntitySet()
        {
            TestClientContext.MergeOption = MergeOption.OverwriteChanges;

            //Post an Product
            var product = Product.CreateProduct(10001, "10001", "2", 2.0f, 2, true);

            TestClientContext.AddToProducts(product);
            TestClientContext.SaveChanges();

            //Function Bound on EntitySet
            //TODO: Enable this support on server side
            var querySeniorEmployee = (TestClientContext.People.OfType <Employee>() as DataServiceQuery <Employee>).GetSeniorEmployees();

            Assert.IsTrue(querySeniorEmployee.RequestUri.OriginalString.EndsWith(
                              "People/Microsoft.Test.OData.Services.ODataWCFService.Employee/Microsoft.Test.OData.Services.ODataWCFService.GetSeniorEmployees()"));
            var queryEmployeeAddress = querySeniorEmployee.GetHomeAddress();

            Assert.IsTrue(queryEmployeeAddress.RequestUri.OriginalString.EndsWith(
                              "People/Microsoft.Test.OData.Services.ODataWCFService.Employee/Microsoft.Test.OData.Services.ODataWCFService.GetSeniorEmployees()/Microsoft.Test.OData.Services.ODataWCFService.GetHomeAddress()"));
            //var employee = querySeniorEmployee.GetValue();
            //Assert.IsNotNull(employee);

            //Action Bound on EntitySet
            var discountAction = TestClientContext.Products.Discount(50);

            Assert.IsTrue(discountAction.RequestUri.OriginalString.EndsWith("/Products/Microsoft.Test.OData.Services.ODataWCFService.Discount"));
            var products = discountAction.Execute();

            Assert.IsTrue(products.Count() > 0);

            //ByKey
            var queryProduct = TestClientContext.Products.ByKey(new Dictionary <string, object> {
                { "ProductID", 10001 }
            });

            product = queryProduct.GetValue();
            Assert.AreEqual(1, product.UnitPrice);
            Assert.IsTrue(queryProduct.RequestUri.OriginalString.EndsWith("/Products(10001)"));

            //Action Bound on Entity
            var expectedAccessLevel = AccessLevel.ReadWrite | AccessLevel.Execute;
            var accessLevelAction   = TestClientContext.Products.ByKey(new Dictionary <string, object> {
                { "ProductID", 10001 }
            }).AddAccessRight(expectedAccessLevel);

            Assert.IsTrue(accessLevelAction.RequestUri.OriginalString.EndsWith("/Products(10001)/Microsoft.Test.OData.Services.ODataWCFService.AddAccessRight"));
            var accessLevel = accessLevelAction.GetValue();

            Assert.IsTrue(accessLevel.Value.HasFlag(expectedAccessLevel));

            //Function Bound on Entity and return Collection of Entity
            var getProductDetailsAction = TestClientContext.Products.ByKey(new Dictionary <string, object> {
                { "ProductID", 5 }
            }).GetProductDetails(1);

            Assert.IsTrue(getProductDetailsAction.RequestUri.OriginalString.EndsWith("/Products(5)/Microsoft.Test.OData.Services.ODataWCFService.GetProductDetails(count=1)"));
            foreach (var pd in getProductDetailsAction)
            {
                //Check whether GetEnumerator works
                Assert.AreEqual(5, pd.ProductID);
            }

            //Composable Function
            //Won't execute since ODL doesn't support it now.
            var getRelatedProductAction = getProductDetailsAction.ByKey(new Dictionary <string, object> {
                { "ProductID", 5 }, { "ProductDetailID", 2 }
            }).GetRelatedProduct();

            Assert.IsTrue(getRelatedProductAction.RequestUri.OriginalString.EndsWith("/Products(5)/Microsoft.Test.OData.Services.ODataWCFService.GetProductDetails(count=1)(ProductID=5,ProductDetailID=2)/Microsoft.Test.OData.Services.ODataWCFService.GetRelatedProduct()"));
            //getRelatedProductAction.GetValue();

            //Complex query option
            var queryProducts = TestClientContext.Products.Where(p => p.ProductID > 3)
                                .Select(p => new Product()
            {
                Details = p.Details, Name = p.Name, ProductID = p.ProductID
            })
                                .Skip(3).Take(2) as DataServiceQuery <Product>;

            Assert.IsTrue(queryProducts.RequestUri.OriginalString.EndsWith("/Products?$filter=ProductID gt 3&$skip=3&$top=2&$expand=Details&$select=Name,ProductID"));
            foreach (var p in queryProducts)
            {
                Assert.IsTrue(p.ProductID > 3);
            }
        }