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 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); } }