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 AddNavigationProperty() { TestClientContext.MergeOption = MergeOption.OverwriteChanges; var truck = (TestClientContext.Trucks.Where(t => t.Key == "Key1")).Single(); // Add Navigation Property to VehicleGPS in TruckType var newVehicleGPS = new VehicleGPSType() { Key = "99", VehicleSpeed = 100.1, StartLocation = new GeoLocation() { Lat = 1, Long = 2, }, EndLocation = new GeoLocation() { Lat = 3, Long = 4, }, CurrentLocation = new GeoLocation() { Lat = 1.2, Long = 2.4, }, LostSignalAlarm = new GPSLostSignalAlarmType() { Severity = 1, LastKnownLocation = new GeoLocation() { Lat = 2.1, Long = 1.2, } }, Map = new MapType() { MBytesDownloaded = 1.2, ProviderName = "TESTNEW", Uri = "TESTNEW.TEST", } }; TestClientContext.AddRelatedObject(truck, "VehicleGPSGroup", newVehicleGPS); TestClientContext.SaveChanges(); TestClientContext.LoadProperty(truck, "VehicleGPSGroup"); Assert.AreEqual(2, truck.VehicleGPSGroup.Count); // Add Navigation Property to VehicleGPSGroupFromGPS in TruckType var newVehicleGPSInGPS = new VehicleGPSType() { Key = "102", VehicleSpeed = 100.1, StartLocation = new GeoLocation() { Lat = 1, Long = 2, }, EndLocation = new GeoLocation() { Lat = 3, Long = 4, }, CurrentLocation = new GeoLocation() { Lat = 1.2, Long = 2.4, }, LostSignalAlarm = new GPSLostSignalAlarmType() { Severity = 1, LastKnownLocation = new GeoLocation() { Lat = 2.1, Long = 1.2, } }, Map = new MapType() { MBytesDownloaded = 1.2, ProviderName = "TESTNEW", Uri = "TESTNEW.TEST", } }; TestClientContext.AddRelatedObject(truck, "VehicleGPSGroupFromGPS", newVehicleGPSInGPS); TestClientContext.SaveChanges(); TestClientContext.LoadProperty(truck, "VehicleGPSGroupFromGPS"); Assert.AreEqual(2, truck.VehicleGPSGroupFromGPS.Count); }
public void DerivedTypeSingletonClientTest() { //Query Singleton TestClientContext.MergeOption = Microsoft.OData.Client.MergeOption.OverwriteChanges; var company = TestClientContext.PublicCompany.GetValue(); Assert.IsTrue(company != null); //Update DerivedType Property and Verify PublicCompany publicCompany = company as PublicCompany; publicCompany.Name = "UpdatedName"; publicCompany.StockExchange = "Updated StockExchange"; TestClientContext.UpdateObject(publicCompany); TestClientContext.SaveChanges(Microsoft.OData.Client.SaveChangesOptions.ReplaceOnUpdate); //Query Singleton Property - Select var name = TestClientContext.PublicCompany.Select(c => c.Name).GetValue(); Assert.IsTrue(name == "UpdatedName"); company = TestClientContext.CreateSingletonQuery <Company>("PublicCompany").Single(); Assert.IsTrue((company as PublicCompany).StockExchange == "Updated StockExchange"); //Query properties of DerivedType var stockExchange = TestClientContext.PublicCompany.Select(c => (c as PublicCompany).StockExchange).GetValue(); Assert.IsTrue(stockExchange == "Updated StockExchange"); //Projection with properties - Select publicCompany = TestClientContext.PublicCompany.Select(c => new PublicCompany { CompanyID = c.CompanyID, Name = c.Name, StockExchange = (c as PublicCompany).StockExchange }).GetValue(); Assert.IsTrue(publicCompany != null); Assert.IsTrue(publicCompany.Name == "UpdatedName"); Assert.IsTrue(publicCompany.StockExchange == "Updated StockExchange"); company = TestClientContext.CreateSingletonQuery <Company>("PublicCompany").Single(); //Load Navigation Property //Collection TestClientContext.LoadProperty(company, "Assets"); Assert.IsTrue((company as PublicCompany).Assets != null); Assert.IsTrue((company as PublicCompany).Assets.Count == 2); ////Single Enity TestClientContext.LoadProperty(company, "Club"); Assert.IsTrue((company as PublicCompany).Club != null); //Singleton TestClientContext.LoadProperty(publicCompany, "LabourUnion"); Assert.IsTrue((company as PublicCompany).LabourUnion != null); //Add Contained Navigation Property - Collection of derived type Random rand = new Random(); int tmpAssertId = rand.Next(); Asset tmpAssert = new Asset() { AssetID = tmpAssertId, Name = tmpAssertId + "Name", Number = tmpAssertId }; TestClientContext.AddRelatedObject(publicCompany, "Assets", tmpAssert); TestClientContext.SaveChanges(); //Query contained Navigation Property - Collection of derived type company = TestClientContext.PublicCompany.Expand(c => (c as PublicCompany).Assets).GetValue(); Assert.IsTrue(company != null); Assert.IsTrue((company as PublicCompany).Assets.Any(a => a.AssetID == tmpAssertId)); TestClientContext.DeleteObject(tmpAssert); TestClientContext.SaveChanges(); company = TestClientContext.PublicCompany.Expand(c => (c as PublicCompany).Assets).GetValue(); Assert.IsTrue(company != null); Assert.IsTrue(!(company as PublicCompany).Assets.Any(a => a.AssetID == tmpAssertId)); company = TestClientContext.PublicCompany.Expand(c => (c as PublicCompany).Club).GetValue(); Assert.IsTrue(company != null); Assert.IsTrue((company as PublicCompany).Club != null); //Updated Conatined Navigation Property - SingleEntity of derived type var club = (company as PublicCompany).Club; club.Name = "UpdatedClubName"; TestClientContext.UpdateRelatedObject(company, "Club", club); TestClientContext.SaveChanges(); //Query Contained Navigation Property - Single Entity of derived type publicCompany = TestClientContext.PublicCompany.Select(c => new PublicCompany { CompanyID = c.CompanyID, Club = (c as PublicCompany).Club }).GetValue(); Assert.IsTrue(publicCompany != null); Assert.IsTrue(publicCompany.Club != null); Assert.IsTrue(publicCompany.Club.Name == "UpdatedClubName"); //Projection with Navigation property of derived type - Singleton company = TestClientContext.PublicCompany.Expand(c => (c as PublicCompany).LabourUnion).GetValue(); //Update Navigation property of derived Type - Singleton var labourUnion = (company as PublicCompany).LabourUnion; labourUnion.Name = "UpdatedLabourUnionName"; TestClientContext.UpdateRelatedObject(publicCompany, "LabourUnion", labourUnion); TestClientContext.SaveChanges(); //Query singleton of derived type. publicCompany = TestClientContext.PublicCompany.Select(c => new PublicCompany { CompanyID = c.CompanyID, LabourUnion = (c as PublicCompany).LabourUnion }).GetValue(); Assert.IsTrue(publicCompany != null); Assert.IsTrue(publicCompany.LabourUnion != null); }