예제 #1
0
        public void TestODataSimplifiedServiceCreateEntity()
        {
            TestClientContext.EnableWritingODataAnnotationWithoutPrefix = true;
            TestClientContext.SendingRequest2 += (sender, eventArgs) => ((Microsoft.OData.Client.HttpClientRequestMessage)eventArgs.RequestMessage).SetHeader("Accept", "application/json;odata.metadata=full");

            var person = new Person
            {
                PersonId  = 99,
                FirstName = "Jack",
                LastName  = "Jones",
                Address   = new Address
                {
                    Road = "Ziyue Road",
                    City = "Shanghai"
                },
                Descriptions = new ObservableCollection <string> {
                    "Kindly"
                }
            };

            TestClientContext.AddToPeople(person);
            TestClientContext.SaveChanges();

            TestClientContext.Detach(person);
            var personReturned = TestClientContext.People.ByKey(new Dictionary <string, object> {
                { "PersonId", person.PersonId }
            }).GetValue();

            Assert.Equal(person.PersonId, personReturned.PersonId);
        }
예제 #2
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));
        }