public void AddDeleteLink(string mimeType) { TestClientContext.MergeOption = MergeOption.OverwriteChanges; TestClientContext.SendingRequest2 += (sender, eventArgs) => ((Microsoft.OData.Client.HttpWebRequestMessage)eventArgs.RequestMessage).SetHeader("Accept", mimeType); TestClientContext.Format.UseJson(Model); //preparation //currently service does not support $expand, so need to query the navigation Orders first var uri = new Uri(ServiceBaseUri + "Products(5)/Details"); var detailsInAProdct = TestClientContext.Execute <ProductDetail>(uri); var intOriginalOrderCount = detailsInAProdct.Count(); var prodct = TestClientContext.Products.Where(c => c.ProductID == 5).SingleOrDefault(); TestClientContext.SendingRequest2 += (sender, eventArgs) => ((Microsoft.OData.Client.HttpWebRequestMessage)eventArgs.RequestMessage).SetHeader(TestHeader, "EditLink");//this is make sure EditLink is replaced with a random value. var productDetail = TestClientContext.ProductDetails.Where(o => o.ProductDetailID == 1 && o.ProductID == 6).SingleOrDefault(); //add a link between customer and order TestClientContext.AddLink(prodct, "Details", productDetail); TestClientContext.SaveChanges(); detailsInAProdct = TestClientContext.Execute <ProductDetail>(uri); var intOrderCountAfterAddLink = detailsInAProdct.Count(); Assert.AreEqual(intOriginalOrderCount + 1, intOrderCountAfterAddLink, "The link is added."); //delete the added link TestClientContext.DeleteLink(prodct, "Details", productDetail); TestClientContext.SaveChanges(); detailsInAProdct = TestClientContext.Execute <ProductDetail>(uri); var intOrderCountAfterDeleteLink = detailsInAProdct.Count(); Assert.AreEqual(intOriginalOrderCount, intOrderCountAfterDeleteLink, "The added link is deleted."); }
public void PostTunning() { this.TestClientContext.MergeOption = MergeOption.OverwriteChanges; this.TestClientContext.UsePostTunneling = true; //Entity of Derived type //Entity has a complex property of derived type DataServiceCollection <PersonPlus> people = new DataServiceCollection <PersonPlus>(this.TestClientContext, "People", null, null) { 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" }, } }; DataServiceCollection <OrderPlus> orders = new 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) } } }; //Singleton of derived type //Singleton is of an open entity type DataServiceCollection <CompanyPlus> publicCompany = new DataServiceCollection <CompanyPlus>(this.TestClientContext.PublicCompanyPlus); //Entity with open complex type //Entity with contained Navigation DataServiceCollection <AccountPlus> accounts = new DataServiceCollection <AccountPlus>(this.TestClientContext) { new AccountPlus() { AccountIDPlus = 110, CountryRegionPlus = "CN", AccountInfoPlus = new AccountInfoPlus() { FirstNamePlus = "New", LastNamePlus = "Boy" } } }; var gc = new GiftCardPlus() { GiftCardIDPlus = 30000, GiftCardNOPlus = "AAA123A", AmountPlus = 19.9, ExperationDatePlus = new DateTimeOffset(new DateTime(2013, 12, 30)) }; //Entity with Enum property and collection of Enum property DataServiceCollection <ProductPlus> products = new DataServiceCollection <ProductPlus>(this.TestClientContext) { new ProductPlus() { NamePlus = "Apple", ProductIDPlus = 1000000, QuantityInStockPlus = 20, QuantityPerUnitPlus = "Pound", UnitPricePlus = 0.35f, DiscontinuedPlus = false, SkinColorPlus = ColorPlus.RedPlus, CoverColorsPlus = new ObservableCollection <ColorPlus>() { ColorPlus.BluePlus }, UserAccessPlus = AccessLevelPlus.ReadPlus } }; TestClientContext.UpdateRelatedObject(accounts[0], "MyGiftCard", gc); this.TestClientContext.SaveChanges(); var customer = this.TestClientContext.CustomersPlus.Where(c => c.PersonIDPlus == people[0].PersonIDPlus).First(); TestClientContext.AddLink(customer, "Orders", orders[0]); this.TestClientContext.SaveChanges(); bool isEntity = true; int expectedPropertyCount = 0; Action <WritingEntryArgs> onEntryEndingAction = (arg) => { if (isEntity) { Assert.AreEqual(expectedPropertyCount, arg.Entry.Properties.Count()); } }; this.TestClientContext.Configurations.RequestPipeline.OnEntryEnding(onEntryEndingAction); //Update Enum type and collection of enum property in entity products[0].CoverColorsPlus.Add(ColorPlus.GreenPlus); products[0].UserAccessPlus = AccessLevelPlus.ExecutePlus; products[0].SkinColorPlus = ColorPlus.GreenPlus; expectedPropertyCount = 3; this.TestClientContext.SaveChanges(); //this.TestClientContext.Detach(products[0]); var product = this.TestClientContext.ProductsPlus.Where((it) => it.ProductIDPlus == products[0].ProductIDPlus).First(); Assert.AreEqual(2, product.CoverColorsPlus.Count); Assert.AreEqual(ColorPlus.GreenPlus, product.SkinColorPlus); Assert.AreEqual(AccessLevelPlus.ExecutePlus, product.UserAccessPlus); Assert.AreEqual(2, product.CoverColorsPlus.Count); // Update primitive type and collection property under entity people[0].FirstNamePlus = "Balck"; people[0].EmailsPlus.Add("*****@*****.**"); expectedPropertyCount = 2; this.TestClientContext.SaveChanges(); // Update primitive type and collection property under entity (inherited) var datetime = new DateTimeOffset(new DateTime(1957, 4, 3));; ((CustomerPlus)people[0]).BirthdayPlus = new DateTimeOffset(new DateTime(1957, 4, 3)); expectedPropertyCount = 1; this.TestClientContext.SaveChanges(); // Update the property under complex type. people[0].HomeAddressPlus.CityPlus = "Redmond"; bool isComplex = true; isEntity = false; Action <WritingEntryArgs> onEntryEndingAction1 = (arg) => { if (isComplex && arg.Entry.TypeName.EndsWith("HomeAddressPlus")) { Assert.AreEqual("Redmond", arg.Entry.Properties.Single(p => p.Name == "City").Value); } }; this.TestClientContext.Configurations.RequestPipeline.OnEntryEnding(onEntryEndingAction1); this.TestClientContext.SaveChanges(); // Update the property under complex type (inherited). ((HomeAddressPlus)people[0].HomeAddressPlus).FamilyNamePlus = "Microsoft"; Action <WritingEntryArgs> onEntryEndingAction2 = (arg) => { if (isComplex && arg.Entry.TypeName.EndsWith("HomeAddressPlus")) { Assert.AreEqual("Microsoft", arg.Entry.Properties.Single(p => p.Name == "FamilyName").Value); } }; this.TestClientContext.Configurations.RequestPipeline.OnEntryEnding(onEntryEndingAction2); this.TestClientContext.SaveChanges(); isComplex = false; isEntity = true; this.TestClientContext.LoadProperty(customer, "Orders"); // Update Navigation property customer.OrdersPlus.First().OrderDatePlus = datetime; customer.OrdersPlus.First().OrderShelfLifesPlus.Add(new TimeSpan(2)); expectedPropertyCount = 2; this.TestClientContext.SaveChanges(); //Verify all updated property //this.TestClientContext.Detach(customer); var people0 = this.TestClientContext.CustomersPlus.Expand(it => it.OrdersPlus).Where((it) => it.PersonIDPlus == people[0].PersonIDPlus).Single(); Assert.AreEqual(2, people0.EmailsPlus.Count); Assert.AreEqual(datetime, (people0 as CustomerPlus).BirthdayPlus); Assert.AreEqual("Redmond", people0.HomeAddressPlus.CityPlus); Assert.AreEqual("Microsoft", (people0.HomeAddressPlus as HomeAddressPlus).FamilyNamePlus); Assert.AreEqual("98052", people0.HomeAddressPlus.PostalCodePlus); Assert.AreEqual(datetime, people0.OrdersPlus.First().OrderDatePlus); Assert.AreEqual(2, people0.OrdersPlus.First().OrderShelfLifesPlus.Count()); TestClientContext.LoadProperty(accounts[0], "MyGiftCard"); // Update single vlue navigation property . accounts[0].MyGiftCardPlus.ExperationDatePlus = datetime; expectedPropertyCount = 1; this.TestClientContext.SaveChanges(); // Update open complex type accounts[0].AccountInfoPlus.MiddleNamePlus = "S."; isEntity = false; isComplex = true; Action <WritingEntryArgs> onEntryEndingAction3 = (arg) => { if (isComplex && arg.Entry.TypeName.EndsWith("AccountInfoPlus")) { Assert.AreEqual("S.", arg.Entry.Properties.Single(p => p.Name == "MiddleName").Value); } }; this.TestClientContext.Configurations.RequestPipeline.OnEntryEnding(onEntryEndingAction3); this.TestClientContext.SaveChanges(); isComplex = false; isEntity = true; //this.TestClientContext.Detach(accounts[0]); var account = this.TestClientContext.AccountsPlus.Expand("MyGiftCard").Where((it) => it.AccountIDPlus == accounts[0].AccountIDPlus).Single(); Assert.AreEqual(datetime, account.MyGiftCardPlus.ExperationDatePlus); Assert.AreEqual("S.", account.AccountInfoPlus.MiddleNamePlus); Assert.AreEqual("New", account.AccountInfoPlus.FirstNamePlus); // Update property in open singleton publicCompany.Single().TotalAssetsPlus = 10; publicCompany.Single().FullNamePlus = "MS Ltd."; expectedPropertyCount = 2; this.TestClientContext.SaveChanges(); //this.TestClientContext.Detach(publicCompany); var company = this.TestClientContext.PublicCompanyPlus.GetValue(); Assert.AreEqual(company.TotalAssetsPlus, 10); Assert.AreEqual(company.FullNamePlus, "MS Ltd."); // Update object by update object without change => redo the PATCH all this.TestClientContext.UpdateObject(people[0]); isEntity = false; Action <WritingEntryArgs> onEntryEndingAction4 = (arg) => { if (arg.Entry.TypeName.EndsWith("PersonPlus")) { Assert.AreEqual(expectedPropertyCount, arg.Entry.Properties.Count()); } }; this.TestClientContext.Configurations.RequestPipeline.OnEntryEnding(onEntryEndingAction4); expectedPropertyCount = 10; this.TestClientContext.SaveChanges(); }
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)); }
public void SingletonClientTest() { Random rand = new Random(); ODataFormat[] formats = { ODataFormat.Json }; foreach (var format in formats) { //Query Singleton TestClientContext.MergeOption = Microsoft.OData.Client.MergeOption.OverwriteChanges; var company = TestClientContext.Company.GetValue(); Assert.IsTrue(company != null); //Update Singleton Property and Verify company.CompanyCategory = CompanyCategory.Communication; company.Name = "UpdatedName"; company.Address.City = "UpdatedCity"; TestClientContext.UpdateObject(company); TestClientContext.SaveChanges(Microsoft.OData.Client.SaveChangesOptions.ReplaceOnUpdate); //Query Singleton Property - Select var companyCategory = TestClientContext.Company.Select(c => c.CompanyCategory).GetValue(); Assert.IsTrue(companyCategory == CompanyCategory.Communication); var city = TestClientContext.CreateSingletonQuery <string>("Company/Address/City").Execute().Single(); Assert.IsTrue(city == "UpdatedCity"); var name = TestClientContext.Execute <string>(new Uri("Company/Name", UriKind.Relative)).Single(); Assert.IsTrue(name == "UpdatedName"); //Projection with properties - Select company = TestClientContext.Company.Select(c => new Company { CompanyID = c.CompanyID, Address = c.Address, Name = c.Name }).GetValue(); Assert.IsTrue(company != null); Assert.IsTrue(company.Name == "UpdatedName"); //Load Navigation Property //Singleton TestClientContext.LoadProperty(company, "VipCustomer"); Assert.IsTrue(company.VipCustomer != null); //Collection TestClientContext.LoadProperty(company, "Departments"); Assert.IsTrue(company.Departments != null); Assert.IsTrue(company.Departments.Count > 0); //Single Entity TestClientContext.LoadProperty(company, "CoreDepartment"); Assert.IsTrue(company.CoreDepartment != null); //Add Navigation Property - Collection company = TestClientContext.Company.GetValue(); int tmpDepartmentID = rand.Next(); int tmpCoreDepartmentID = rand.Next(); Department department = new Department() { DepartmentID = tmpDepartmentID, Name = "ID" + tmpDepartmentID, }; Department coreDepartment = new Department() { DepartmentID = tmpCoreDepartmentID, Name = "ID" + tmpCoreDepartmentID, }; TestClientContext.AddToDepartments(department); TestClientContext.AddLink(company, "Departments", department); TestClientContext.SaveChanges(); TestClientContext.AddToDepartments(coreDepartment); TestClientContext.AddLink(company, "Departments", coreDepartment); TestClientContext.SaveChanges(); TestClientContext.Departments.ToList(); //Projection with Navigation properties - Select company = TestClientContext.Company.Select(c => new Company { CompanyID = c.CompanyID, Departments = c.Departments }).GetValue(); Assert.IsTrue(company != null); Assert.IsTrue(company.Departments.Any(c => c.DepartmentID == tmpDepartmentID)); Assert.IsTrue(company.Departments.Any(c => c.DepartmentID == tmpCoreDepartmentID)); //Update Navigation Property - Single Entity TestClientContext.SetLink(company, "CoreDepartment", coreDepartment); TestClientContext.SaveChanges(); //Projection with Navigation properties - Select company = TestClientContext.Company.Select(c => new Company { CompanyID = c.CompanyID, CoreDepartment = c.CoreDepartment }).GetValue(); Assert.IsTrue(company != null); Assert.IsTrue(company.CoreDepartment.DepartmentID == tmpCoreDepartmentID); //Update EntitySet's Navigation Property - Singleton TestClientContext.SetLink(department, "Company", company); TestClientContext.SaveChanges(Microsoft.OData.Client.SaveChangesOptions.ReplaceOnUpdate); //Query(Expand) EntitySet's Navigation Property - Singleton department = TestClientContext.Departments.Expand(d => d.Company).Where(d => d.DepartmentID == tmpDepartmentID).Single(); Assert.IsTrue(department != null); Assert.IsTrue(department.Company.CompanyID == company.CompanyID); //Delete Navigation Property - EntitySet TestClientContext.DeleteLink(company, "Departments", department); TestClientContext.SaveChanges(); //Projection with Navigation Property - EntitySet company.Departments = null; company = TestClientContext.Company.Select(c => new Company { CompanyID = c.CompanyID, Departments = c.Departments }).GetValue(); Assert.IsTrue(company != null); Assert.IsTrue(!company.Departments.Any(c => c.DepartmentID == tmpDepartmentID)); //Query Singleton's Navigation Property - Singleton company = TestClientContext.Company.Select(c => new Company { CompanyID = c.CompanyID, VipCustomer = c.VipCustomer }).GetValue(); Assert.IsTrue(company != null); Assert.IsTrue(company.VipCustomer != null); //Query Singleton again with Execute var vipCustomer = TestClientContext.Execute <Customer>(new Uri("VipCustomer", UriKind.Relative)).Single(); //Update Singleton's Navigation property - Singleton vipCustomer.LastName = "UpdatedLastName"; TestClientContext.UpdateRelatedObject(company, "VipCustomer", vipCustomer); TestClientContext.SaveChanges(); company.VipCustomer = null; company = TestClientContext.Company.Expand(c => c.VipCustomer).GetValue(); Assert.IsTrue(company != null); Assert.IsTrue(company.VipCustomer != null); Assert.IsTrue(company.VipCustomer.LastName == "UpdatedLastName"); //Update Navigation Property - Delete the Singleton navigation TestClientContext.SetLink(company, "VipCustomer", null); TestClientContext.SaveChanges(); //Expand Navigation Property - Singleton company.VipCustomer = null; company = TestClientContext.Company.Expand(c => c.VipCustomer).GetValue(); Assert.IsTrue(company != null); Assert.IsTrue(company.VipCustomer == null); //TODO: AttachTo doesn't support singleton. //TestClientContext = new InMemoryEntities(ServiceBaseUri); //TestClientContext.AttachTo("Company", company); //TestClientContext.AttachTo("VipCustomer", vipCustomer); //TestClientContext.SetLink(company, "VipCustomer", vipCustomer); //TestClientContext.SaveChanges(); //Update Navigation Property - Singleton vipCustomer = TestClientContext.VipCustomer.GetValue(); TestClientContext.SetLink(company, "VipCustomer", vipCustomer); TestClientContext.SaveChanges(); company = TestClientContext.Company.Select(c => new Company { CompanyID = c.CompanyID, VipCustomer = c.VipCustomer }).GetValue(); Assert.IsTrue(company != null); Assert.IsTrue(company.VipCustomer != null); } }