public void InsertAndUpdatePropertyValueUsingLinq() { TimeSpan timespan = new TimeSpan((new Random()).Next()); var queryable = TestClientContext.Orders.Where(c => c.ShelfLife == timespan) as ODataClient.DataServiceQuery <Order>; Assert.IsTrue(queryable.RequestUri.OriginalString.EndsWith("/Orders?$filter=ShelfLife eq duration'" + XmlConvert.ToString(timespan) + "'", StringComparison.Ordinal)); var result1 = queryable.ToList(); Assert.IsTrue(result1.Count == 0); int orderID = (new Random()).Next(); // create an entity Order order = new Order() { OrderID = orderID, OrderDate = new DateTimeOffset(new DateTime(2011, 3, 4, 16, 3, 57)), ShelfLife = timespan, OrderShelfLifes = new ObservableCollection <TimeSpan>() { timespan } }; TestClientContext.AddToOrders(order); TestClientContext.SaveChanges(); // query and verify var result2 = queryable.ToList(); Assert.AreEqual(1, result2.Count); Assert.AreEqual(orderID, result2[0].OrderID); // update the Duration properties timespan = new TimeSpan((new Random()).Next()); order.ShelfLife = timespan; order.OrderShelfLifes = new ObservableCollection <TimeSpan>() { timespan }; TestClientContext.UpdateObject(order); TestClientContext.SaveChanges(ODataClient.SaveChangesOptions.ReplaceOnUpdate); // query Duration property var queryable2 = TestClientContext.Orders.Where(c => c.OrderID == orderID).Select(c => c.ShelfLife).FirstOrDefault(); Assert.IsTrue(queryable2 != null); Assert.AreEqual(timespan, queryable2); // query collection of Duration property var queryable3 = (from c in TestClientContext.Orders where c.OrderID == orderID select c.OrderShelfLifes).FirstOrDefault(); Assert.IsTrue(queryable3.Count == 1); Assert.AreEqual(timespan, queryable3[0]); // delete entity and validate TestClientContext.DeleteObject(order); TestClientContext.SaveChanges(ODataClient.SaveChangesOptions.ReplaceOnUpdate); var queryable4 = TestClientContext.Execute <Order>(new Uri("Orders()?$filter=ShelfLife eq duration'" + XmlConvert.ToString(timespan) + "'", UriKind.Relative)); Assert.IsTrue(queryable4.Count() == 0); }
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 EntitySetDeclaredInReferencedModelE2E() { // Query Entity Set in GPS TestClientContext.MergeOption = MergeOption.OverwriteChanges; var vehicleGPSSetInGPS = TestClientContext.VehicleGPSSetInGPS; foreach (var vehicleGPS in vehicleGPSSetInGPS) { Assert.IsTrue(vehicleGPS != null); } Assert.AreEqual(3, vehicleGPSSetInGPS.Count()); // Create an entity in VehicleGPSSetInGPS var newVehicleGPS = new VehicleGPSType() { Key = "101", 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.AddToVehicleGPSSetInGPS(newVehicleGPS); TestClientContext.SaveChanges(); // Get the created entity var queryable = TestClientContext.VehicleGPSSetInGPS.Where(vehicleGPS => vehicleGPS.Key == "101"); VehicleGPSType newCreated = queryable.Single(); Assert.AreEqual(100.1, newCreated.VehicleSpeed); // Update the created entity newCreated.VehicleSpeed = 200.1; TestClientContext.UpdateObject(newCreated); TestClientContext.SaveChanges(); // Query and Delete entity VehicleGPSType updated = queryable.Single(); Assert.AreEqual(200.1, newCreated.VehicleSpeed); TestClientContext.DeleteObject(updated); TestClientContext.SaveChanges(); Assert.AreEqual(3, vehicleGPSSetInGPS.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); }