public void ContainedEntityQuery() { TestClientContext.MergeOption = Microsoft.OData.Client.MergeOption.OverwriteChanges; // Query a single contained entity var q1 = TestClientContext.CreateQuery <PaymentInstrumentPlus>("Accounts(103)/MyPaymentInstruments(103902)"); Assert.IsTrue(q1.RequestUri.OriginalString.EndsWith("Accounts(103)/MyPaymentInstruments(103902)", StringComparison.Ordinal)); List <PaymentInstrumentPlus> r1 = q1.ToList(); Assert.AreEqual(1, r1.Count); Assert.AreEqual(103902, r1[0].PaymentInstrumentIDPlus); Assert.AreEqual("103 second PI", r1[0].FriendlyNamePlus); // Query a contained entity set with query option var q2 = TestClientContext.CreateQuery <PaymentInstrumentPlus>("Accounts(103)/MyPaymentInstruments").Expand(pi => pi.BillingStatementsPlus).Where(pi => pi.PaymentInstrumentIDPlus == 103901); PaymentInstrumentPlus r2 = q2.Single(); Assert.IsNotNull(r2.BillingStatementsPlus); // Invoke a bounded Function. double result = TestClientContext.Execute <double>(new Uri(ServiceBaseUri.AbsoluteUri + "Accounts(101)/MyGiftCard/Microsoft.Test.OData.Services.ODataWCFService.GetActualAmount(bonusRate=0.2)", UriKind.Absolute), "GET", true).Single(); Assert.AreEqual(23.88, result); }
public void PostPartialProperties() { this.TestClientContext.MergeOption = MergeOption.OverwriteChanges; int expectedPropertyCount = 0; ODataResource address = null; ODataResource accountInfo = null; this.TestClientContext.Configurations.RequestPipeline.OnEntryEnding((arg) => { if (arg.Entry.TypeName.EndsWith("HomeAddressPlus")) { address = arg.Entry; } if (arg.Entry.TypeName.EndsWith("AccountInfoPlus")) { accountInfo = arg.Entry; } if (arg.Entry.TypeName.EndsWith("CustomerPlus")) { Assert.Equal(expectedPropertyCount, arg.Entry.Properties.Count()); } }); //Entity of Derived type //Entity has a complex property of derived type DataServiceCollection <CustomerPlus> people = new DataServiceCollection <CustomerPlus>(this.TestClientContext, "People", null, null); var person = new CustomerPlus(); people.Add(person); person.FirstNamePlus = "Nelson"; person.LastNamePlus = "Black"; person.NumbersPlus = new ObservableCollection <string> { }; person.EmailsPlus = new ObservableCollection <string> { "*****@*****.**" }; person.PersonIDPlus = 10001; person.CityPlus = "London"; person.TimeBetweenLastTwoOrdersPlus = new TimeSpan(1); person.HomeAddressPlus = new HomeAddressPlus() { CityPlus = "Redmond", PostalCodePlus = "98052", StreetPlus = "1 Microsoft Way" }; //Post entity into an entity set expectedPropertyCount = 7; this.TestClientContext.SaveChanges(SaveChangesOptions.PostOnlySetProperties); //Validate that entire complex type was sent to server side. Assert.Equal(4, address.Properties.Count()); //Post Navigation property to an entitySet var people1 = new DataServiceCollection <CustomerPlus>(this.TestClientContext.CustomersPlus.Where(p => p.PersonIDPlus == 10001)); OrderPlus order = new OrderPlus(); people1[0].OrdersPlus.Add(order); order.OrderIDPlus = 10001; order.OrderDatePlus = new DateTimeOffset(new DateTime(2011, 5, 29, 14, 21, 12)); order.OrderShelfLifesPlus = new ObservableCollection <TimeSpan>(); expectedPropertyCount = 3; this.TestClientContext.SaveChanges(SaveChangesOptions.PostOnlySetProperties); //Validate var customer = this.TestClientContext.CustomersPlus.Expand(c => c.OrdersPlus).Where(p => p.PersonIDPlus == 10001).FirstOrDefault(); Assert.NotNull(customer); Assert.Equal("London", ((CustomerPlus)customer).CityPlus); Assert.Equal(0, customer.NumbersPlus.Count); Assert.Null(((HomeAddressPlus)customer.HomeAddressPlus).FamilyNamePlus); Assert.Null(customer.MiddleNamePlus); var order1 = customer.OrdersPlus.Where(o => o.OrderIDPlus == 10001).Single(); Assert.Null(order1.ShelfLifePlus); //Post Entity with open complex type and with contained Navigation DataServiceCollection <AccountPlus> accounts = new DataServiceCollection <AccountPlus>(this.TestClientContext); var account = new AccountPlus(); accounts.Add(account); account.AccountIDPlus = 110; account.CountryRegionPlus = "CN"; account.AccountInfoPlus = new AccountInfoPlus() { FirstNamePlus = "New", LastNamePlus = "Base", IsActivePlus = true, }; var expectedMiddleName = account.AccountInfoPlus.MiddleNamePlus; expectedPropertyCount = 3; this.TestClientContext.SaveChanges(SaveChangesOptions.PostOnlySetProperties); //Validate entire open complex type was sent to server side. Assert.Equal(4, accountInfo.Properties.Count()); //Post Contained Navigation property PaymentInstrumentPlus pi = new PaymentInstrumentPlus(); account.MyPaymentInstrumentsPlus.Add(pi); pi.PaymentInstrumentIDPlus = 1003; pi.CreatedDatePlus = DateTimeOffset.Now; pi.FriendlyNamePlus = "FriendlyName"; expectedPropertyCount = 3; this.TestClientContext.SaveChanges(SaveChangesOptions.PostOnlySetProperties); //Post Contained Single Navigation property //var gcs = new DataServiceCollection<GiftCardPlus>(this.TestClientContext, "Accounts(110)/MyGiftCard", null, null); GiftCardPlus gc = new GiftCardPlus(); account.MyGiftCardPlus = gc; gc.GiftCardIDPlus = 10001; gc.GiftCardNOPlus = "10001"; gc.AmountPlus = 2000; gc.ExperationDatePlus = DateTimeOffset.Now; expectedPropertyCount = 4; this.TestClientContext.SaveChanges(SaveChangesOptions.PostOnlySetProperties); var account1 = this.TestClientContext.AccountsPlus.Expand(a => a.MyPaymentInstrumentsPlus).Expand(a => a.MyGiftCardPlus).Where(a => a.AccountIDPlus == 110).Single(); Assert.Equal("CN", account1.CountryRegionPlus); Assert.Equal(expectedMiddleName, account1.AccountInfoPlus.MiddleNamePlus); Assert.Equal("Base", account1.AccountInfoPlus.LastNamePlus); Assert.True(account1.AccountInfoPlus.IsActivePlus); var pi1 = account1.MyPaymentInstrumentsPlus.Where(p => p.PaymentInstrumentIDPlus == 1003).SingleOrDefault(); Assert.NotNull(pi1); Assert.Equal("FriendlyName", pi1.FriendlyNamePlus); Assert.Null(account1.MyGiftCardPlus.OwnerNamePlus); Assert.Equal(2000, account1.MyGiftCardPlus.AmountPlus); //Post Entity with Enum property and collection of Enum property DataServiceCollection <ProductPlus> products = new DataServiceCollection <ProductPlus>(this.TestClientContext); ProductPlus product = new ProductPlus(); products.Add(product); product.NamePlus = "Apple"; product.ProductIDPlus = 1000000; product.QuantityInStockPlus = 20; product.QuantityPerUnitPlus = "Pound"; product.UnitPricePlus = 0.35f; product.DiscontinuedPlus = false; product.CoverColorsPlus = new ObservableCollection <ColorPlus>(); product.SkinColorPlus = null; expectedPropertyCount = 8; this.TestClientContext.SaveChanges(SaveChangesOptions.PostOnlySetProperties); var product2 = this.TestClientContext.ProductsPlus.Where(p => p.ProductIDPlus == 1000000).FirstOrDefault(); Assert.NotNull(product2); Assert.Equal("Apple", product2.NamePlus); //Post Navigation property under derived singleton DataServiceCollection <CompanyPlus> publicCompany = new DataServiceCollection <CompanyPlus>(this.TestClientContext.PublicCompanyPlus); AssetPlus asset = new AssetPlus(); (publicCompany[0] as PublicCompanyPlus).AssetsPlus.Add(asset); asset.AssetIDPlus = 4; asset.NumberPlus = 50; expectedPropertyCount = 2; this.TestClientContext.SaveChanges(SaveChangesOptions.PostOnlySetProperties); var pc = this.TestClientContext.PublicCompanyPlus.Expand(p => (p as PublicCompanyPlus).AssetsPlus).GetValue() as PublicCompanyPlus; var asset2 = pc.AssetsPlus.Where(a => a.AssetIDPlus == 4).First(); Assert.Equal(50, asset2.NumberPlus); Assert.Null(asset2.NamePlus); //Post Navigation property of Singleton DataServiceCollection <CompanyPlus> company = new DataServiceCollection <CompanyPlus>(this.TestClientContext.CompanyPlus); DepartmentPlus department = new DepartmentPlus(); company[0].DepartmentsPlus.Add(department); department.DepartmentIDPlus = 10001; department.NamePlus = "D1"; expectedPropertyCount = 2; this.TestClientContext.SaveChanges(SaveChangesOptions.PostOnlySetProperties); var company2 = this.TestClientContext.CompanyPlus.Expand(p => p.DepartmentsPlus).GetValue(); var department2 = company2.DepartmentsPlus.Where(d => d.DepartmentIDPlus == 10001).First(); Assert.Equal("D1", department.NamePlus); Assert.Null(department.DepartmentNOPlus); }
public void BasicModify() { TestClientContext.MergeOption = Microsoft.OData.Client.MergeOption.OverwriteChanges; TestClientContext.IgnoreMissingProperties = true; // AddRelatedObject AccountPlus newAccount1 = new AccountPlus() { AccountIDPlus = 110, CountryRegionPlus = "CN", AccountInfoPlus = new AccountInfoPlus() { FirstNamePlus = "New", LastNamePlus = "Boy" } }; PaymentInstrumentPlus newPI = new PaymentInstrumentPlus() { PaymentInstrumentIDPlus = 110901, FriendlyNamePlus = "110's first PI", CreatedDatePlus = new DateTimeOffset(new DateTime(2012, 12, 10)) }; TestClientContext.AddToAccountsPlus(newAccount1); TestClientContext.AddRelatedObject(newAccount1, "MyPaymentInstruments", newPI); TestClientContext.SaveChanges(); var r1 = TestClientContext.AccountsPlus.Where(account => account.AccountIDPlus == 110).Single(); Assert.AreEqual("Boy", r1.AccountInfoPlus.LastNamePlus); var r2 = TestClientContext.CreateQuery <PaymentInstrumentPlus>("Accounts(110)/MyPaymentInstruments") .Where(pi => pi.PaymentInstrumentIDPlus == 110901).Single(); Assert.AreEqual("110's first PI", r2.FriendlyNamePlus); //UpdateObject newAccount1.CountryRegionPlus = "US"; TestClientContext.UpdateObject(newAccount1); TestClientContext.SaveChanges(); r1 = TestClientContext.AccountsPlus.Where(account => account.AccountIDPlus == 110).Single(); Assert.AreEqual("US", r1.CountryRegionPlus); //UpdateRelatedObject var myGiftCard = new GiftCardPlus() { GiftCardIDPlus = 11111, GiftCardNOPlus = "11111", AmountPlus = 20, ExperationDatePlus = new DateTimeOffset(2015, 12, 1, 0, 0, 0, new TimeSpan(0)) }; TestClientContext.UpdateRelatedObject(newAccount1, "MyGiftCard", myGiftCard); TestClientContext.SaveChanges(); r1 = TestClientContext.AccountsPlus.Expand(account => account.MyGiftCardPlus).Where(account => account.AccountIDPlus == 110).Single(); Assert.AreEqual(11111, r1.MyGiftCardPlus.GiftCardIDPlus); //Add Derived Object CustomerPlus customerPlus = new CustomerPlus() { FirstNamePlus = "Nelson", MiddleNamePlus = "S.", LastNamePlus = "Black", NumbersPlus = new ObservableCollection <string> { "111-111-1111" }, EmailsPlus = new ObservableCollection <string> { "*****@*****.**" }, PersonIDPlus = 10001, BirthdayPlus = new DateTimeOffset(new DateTime(1957, 4, 3)), CityPlus = "London", HomePlus = GeographyPoint.Create(32.1, 23.1), TimeBetweenLastTwoOrdersPlus = new TimeSpan(1), HomeAddressPlus = new HomeAddressPlus() { CityPlus = "London", PostalCodePlus = "98052", StreetPlus = "1 Microsoft Way", FamilyNamePlus = "Black's Family" }, }; var ordersPlus = new ODataClient.DataServiceCollection <OrderPlus>(TestClientContext) { new OrderPlus() { OrderIDPlus = 11111111, OrderDatePlus = new DateTimeOffset(new DateTime(2011, 5, 29, 14, 21, 12)), ShelfLifePlus = new TimeSpan(1), OrderShelfLifesPlus = new ObservableCollection <TimeSpan>() { new TimeSpan(1) } } }; TestClientContext.AddToPeoplePlus(customerPlus); TestClientContext.SaveChanges(); var customer1 = TestClientContext.CustomersPlus.Where(c => c.PersonIDPlus == 10001).Single(); TestClientContext.AddLink(customer1, "Orders", ordersPlus[0]); TestClientContext.SaveChanges(); TestClientContext.Detach(customerPlus); TestClientContext.SaveChanges(); var customer = TestClientContext.CustomersPlus.Expand(p => (p as CustomerPlus).OrdersPlus).Where(p => p.PersonIDPlus == 10001).SingleOrDefault(); Assert.AreEqual(((CustomerPlus)customer).CityPlus, "London"); Assert.AreEqual(((HomeAddressPlus)(customer.HomeAddressPlus)).FamilyNamePlus, "Black's Family"); Assert.AreEqual(((CustomerPlus)customer).OrdersPlus.Count, 1); var order = TestClientContext.OrdersPlus.Where(p => p.OrderIDPlus == 11111111).SingleOrDefault(); Assert.AreEqual(order.OrderShelfLifesPlus.Count, 1); // DeleteObject TestClientContext.DeleteObject(newAccount1); TestClientContext.SaveChanges(); var accounts = TestClientContext.AccountsPlus.ToList(); Assert.IsTrue(!accounts.Any(ac => ac.AccountIDPlus == 110)); // SetLink var person1 = TestClientContext.PeoplePlus.Where((p) => p.PersonIDPlus == 1).Single(); var person2 = TestClientContext.PeoplePlus.Where((p) => p.PersonIDPlus == 2).Single(); TestClientContext.SetLink(person1, "Parent", person2); TestClientContext.SaveChanges(); person1 = TestClientContext.PeoplePlus.Expand(d => d.ParentPlus).Where((p) => p.PersonIDPlus == 1).Single(); Assert.IsNotNull(person1.ParentPlus); Assert.IsNotNull(person1.ParentPlus.PersonIDPlus == 2); // SetLink : Bug, SetLink to Null will not update the client object. TestClientContext.SetLink(person1, "Parent", null); TestClientContext.SaveChanges(); person1.ParentPlus = null; var person3 = TestClientContext.PeoplePlus.Expand(d => d.ParentPlus).Where((p) => p.PersonIDPlus == 1).Single(); Assert.IsNull(person3.ParentPlus); //AddLink var companyPlus = TestClientContext.CompanyPlus.GetValue(); DepartmentPlus department = new DepartmentPlus() { DepartmentIDPlus = 100001, NamePlus = "ID" + 100001, }; TestClientContext.AddToDepartmentsPlus(department); TestClientContext.AddLink(companyPlus, "Departments", department); TestClientContext.SaveChanges(); TestClientContext.LoadProperty(companyPlus, "Departments"); Assert.IsTrue(companyPlus.DepartmentsPlus.Any(d => d.DepartmentIDPlus == department.DepartmentIDPlus)); //Delete Link TestClientContext.DeleteLink(companyPlus, "Departments", department); TestClientContext.SaveChanges(); TestClientContext.LoadProperty(companyPlus, "Departments"); Assert.IsTrue(!companyPlus.DepartmentsPlus.Any(d => d.DepartmentIDPlus == department.DepartmentIDPlus)); }