public void When_Adding_EntityType_With_All_Properties_Then_It_Must_Be_Added_To_Server_With_All_Properties()
        {
            using (var scenario =
                       new ODataScenario()
                       .WithProducts(Any.Products())
                       .Start())
            {
                var context = GetDataServiceContext(scenario.GetBaseAddress());
                context.MergeOption = MergeOption.OverwriteChanges;
                var dQuery = context.CreateQuery <Product>("/" + "Products");

                var products = context.ExecuteAsync <Product, IProduct>(dQuery).Result;
                products.CurrentPage.Count.Should().Be(5);

                var newProduct = new Product();
                newProduct.Id       = 6;
                newProduct.Name     = Any.CompanyName();
                newProduct.Price    = Any.Decimal();
                newProduct.Category = Any.AlphanumericString();
                newProduct.CallOnPropertyChanged("Id");
                newProduct.CallOnPropertyChanged("Name");
                newProduct.CallOnPropertyChanged("Price");
                newProduct.CallOnPropertyChanged("Category");

                context.AddObject("Products", newProduct);
                context.SaveChangesAsync().Wait();

                var updatedProducts = context.ExecuteAsync <Product, IProduct>(dQuery).Result;
                updatedProducts.CurrentPage.Count.Should().Be(6);
                updatedProducts.CurrentPage[5].Should().BeSameAs(newProduct);
            }
        }
        public void When_Updating_Property_On_Client_Without_OnPropertyChanged_Then_That_Property_Must_Not_Be_Updated_On_Server()
        {
            var prods = Any.Products().ToArray();

            using (var scenario =
                       new ODataScenario()
                       .WithProducts(prods.AsQueryable())
                       .Start())
            {
                var context = GetDataServiceContext(scenario.GetBaseAddress());
                context.MergeOption = MergeOption.OverwriteChanges;
                var dQuery   = context.CreateQuery <Product>("/" + "Products");
                var products = context.ExecuteAsync <Product, IProduct>(dQuery).Result;

                Product prod2   = products.CurrentPage[2] as Product;
                string  newName = Any.CompanyName();
                prod2.Name = newName;
                // Skip calling 'OnPropertyChanged' for 'Name'
                // So 'Name' property must not be updated on the server side.
                //prod2.CallOnPropertyChanged("Name");
                prod2.UpdateAsync().Wait();

                var updatedProducts = context.ExecuteAsync <Product, IProduct>(dQuery).Result;
                updatedProducts.CurrentPage.Count.Should().Be(5);
                (updatedProducts.CurrentPage[2] as Product).Name.Should().NotBe(newName, "The 'Product.Name' Property should not be changed");
            }
        }
        public void When_Updates_To_Properties_Are_Delayed_Then_They_Must_Be_Updated_After_Calling_SaveChanges()
        {
            var prods = Any.Products().ToArray();

            using (var scenario =
                       new ODataScenario()
                       .WithProducts(prods.AsQueryable())
                       .Start())
            {
                var context = GetDataServiceContext(scenario.GetBaseAddress());
                context.MergeOption = MergeOption.OverwriteChanges;
                var dQuery   = context.CreateQuery <Product>("/" + "Products");
                var products = context.ExecuteAsync <Product, IProduct>(dQuery).Result;

                Product prod1       = products.CurrentPage[1] as Product;
                string  newCategory = Any.AlphanumericString();
                prod1.Category = newCategory;
                prod1.CallOnPropertyChanged("Category");
                var productFetcher = TestRestShallowObjectFetcher.CreateFetcher(context, prod1);
                productFetcher.UpdateAsync(prod1, true).Wait();

                Product prod2   = products.CurrentPage[2] as Product;
                string  newName = Any.CompanyName();
                prod2.Name = newName;
                prod2.CallOnPropertyChanged("Name");
                productFetcher = TestRestShallowObjectFetcher.CreateFetcher(context, prod2);
                productFetcher.UpdateAsync(prod2, true).Wait();

                Product prod3    = products.CurrentPage[3] as Product;
                decimal newPrice = Any.Decimal();
                prod3.Price = newPrice;
                prod3.CallOnPropertyChanged("Price");
                productFetcher = TestRestShallowObjectFetcher.CreateFetcher(context, prod3);
                productFetcher.UpdateAsync(prod3, true).Wait();

                var response = context.SaveChangesAsync().Result;
                response.Count().Should().Be(3);

                // Make sure everything is updated on the server
                var updatedProducts = context.ExecuteAsync <Product, IProduct>(dQuery).Result;
                updatedProducts.CurrentPage.Count.Should().Be(5);
                (updatedProducts.CurrentPage[1] as Product).Category.Should().Be(newCategory);
                (updatedProducts.CurrentPage[2] as Product).Name.Should().Be(newName);
                (updatedProducts.CurrentPage[3] as Product).Price.Should().Be(newPrice);
            }
        }
Example #4
0
        public void An_aggregate_can_record_an_event_for_a_command_that_fails_during_validation_using_HandleCommandValidationFailure()
        {
            var order = new Order(new CreateOrder(Any.FullName()))
                        .Apply(new AddItem
            {
                Price       = 10m,
                ProductName = Any.CompanyName()
            })
                        .Apply(new Ship())
                        .Apply(new ChargeCreditCard
            {
                Amount             = 10m,
                CallPaymentService = _ => { throw new ArgumentException("Insufficient funds!"); }
            });

            order.PendingEvents
            .Last()
            .Should()
            .BeOfType <Order.CreditCardChargeRejected>();
        }
        public void When_Updates_To_Entities_Are_Batched_Then_Entities_Must_Be_Updated_On_Server()
        {
            var prods = Any.Products().ToArray();

            using (var scenario =
                       new ODataScenario()
                       .WithProducts(prods.AsQueryable())
                       .Start())
            {
                var context = GetDataServiceContext(scenario.GetBaseAddress());
                context.MergeOption = MergeOption.OverwriteChanges;
                var dQuery   = context.CreateQuery <Product>("/" + "Products");
                var products = context.ExecuteAsync <Product, IProduct>(dQuery).Result;

                Product prod1       = products.CurrentPage[1] as Product;
                string  newCategory = Any.AlphanumericString();
                prod1.Category = newCategory;
                prod1.CallOnPropertyChanged("Category");

                Product prod2   = products.CurrentPage[2] as Product;
                string  newName = Any.CompanyName();
                prod2.Name = newName;
                prod2.CallOnPropertyChanged("Name");

                Product prod3    = products.CurrentPage[3] as Product;
                decimal newPrice = Any.Decimal();
                prod3.Price = newPrice;
                prod3.CallOnPropertyChanged("Price");

                var response = context.SaveChangesAsync(SaveChangesOptions.BatchWithSingleChangeset).Result;
                response.Count().Should().Be(3);
                response.IsBatchResponse.Should().BeTrue();

                var updatedProducts = context.ExecuteAsync <Product, IProduct>(dQuery).Result;
                updatedProducts.CurrentPage.Count.Should().Be(5);
                (updatedProducts.CurrentPage[1] as Product).Category.Should().Be(newCategory);
                (updatedProducts.CurrentPage[2] as Product).Name.Should().Be(newName);
                (updatedProducts.CurrentPage[3] as Product).Price.Should().Be(newPrice);
            }
        }
        public void When_Adding_EntityType_With_Partial_Properties_Then_It_Must_Be_Added_To_Server_With_Partial_Properties()
        {
            using (var scenario =
                       new ODataScenario()
                       .WithProducts(Any.Products())
                       .Start())
            {
                var context = GetDataServiceContext(scenario.GetBaseAddress());
                var dQuery  = context.CreateQuery <Product>("/" + "Products");

                var products = context.ExecuteAsync <Product, IProduct>(dQuery).Result;
                products.CurrentPage.Count.Should().Be(5);

                var    newProduct = new Product();
                string newName    = Any.CompanyName();
                newProduct.Id       = 6;
                newProduct.Name     = newName;
                newProduct.Price    = Any.Decimal();
                newProduct.Category = Any.AlphanumericString();

                // calling 'OnPropertyChanged' only for 'Id' and 'Name' properties
                newProduct.CallOnPropertyChanged("Id");
                newProduct.CallOnPropertyChanged("Name");

                context.AddObject("Products", newProduct);
                context.SaveChangesAsync().Wait();

                var updatedProducts = context.ExecuteAsync <Product, IProduct>(dQuery).Result;
                updatedProducts.CurrentPage.Count.Should().Be(6);

                // the 'Id' and 'Name' properties must be set
                updatedProducts.CurrentPage[5].Id.Should().Be(6);
                updatedProducts.CurrentPage[5].Name.Should().Be(newName);

                // the 'Price' and 'Category' properties must not be set
                updatedProducts.CurrentPage[5].Price.Should().Be(0);
                updatedProducts.CurrentPage[5].Category.Should().BeNull();
            }
        }
Example #7
0
        public void A_command_can_call_a_domain_service_and_cache_the_value_and_set_its_ETag_on_success()
        {
            var chargeCreditCard = new ChargeCreditCard
            {
                Amount = 10m
            };
            var order = new Order(new CreateOrder(Any.FullName()))
                        .Apply(new AddItem
            {
                Price       = 10m,
                ProductName = Any.CompanyName()
            })
                        .Apply(new Ship())
                        .Apply(chargeCreditCard);

            var charged = order.PendingEvents
                          .Last()
                          .As <Order.CreditCardCharged>();

            charged.ETag.Should().Be(chargeCreditCard.ETag);
            charged.Amount.Should().Be(chargeCreditCard.Amount);
        }