public void EntityContainer_GetChanges_DetachAfterInvoke()
        {
            TestEntityContainer container = new TestEntityContainer();
            container.LoadEntities(_cities);
            EntityChangeSet changeset = container.GetChanges();
            Assert.IsTrue(changeset.IsEmpty);

            // invoke then delete the entity. Verify the ModifiedEntities are updated
            _cities[0].InvokeAction(_assignCityZone.Name, _assignCityZone.Parameters.ToArray<object>());
            Assert.AreEqual(1, _cities[0].EntityActions.Count());
            Assert.AreEqual(1, container.GetChanges().ModifiedEntities.Count());

            container.GetEntitySet<City>().Detach(_cities[0]);
            Assert.AreEqual(0, container.GetChanges().ModifiedEntities.Count());
        }
        public void Entity_Invoke_Validates_Required_Properties()
        {
            TestEntityContainer container = new TestEntityContainer();
            var invocableCity = new City();
            container.GetEntitySet<City>().Add(invocableCity);

            RequiredAttribute expectedAttribute = new RequiredAttribute();
            string expectedMember = "CityName";
            string expectedError = expectedAttribute.FormatErrorMessage(expectedMember);

            ExceptionHelper.ExpectValidationException(delegate
            {
                invocableCity.AssignCityZone("West");
            }, expectedError, expectedAttribute.GetType(), null);
        }
        public void Entity_Invoke_Validates_Property_Validation()
        {
            TestEntityContainer container = new TestEntityContainer();
            var invocableCity = new City("This is an invalid state name");
            container.GetEntitySet<City>().Add(invocableCity);

            invocableCity.Name = "Redmond";

            StringLengthAttribute expectedAttribute = new StringLengthAttribute(2);
            string expectedMember = "StateName";
            string expectedError = expectedAttribute.FormatErrorMessage(expectedMember);

            ExceptionHelper.ExpectValidationException(delegate
            {
                invocableCity.AssignCityZone("West");
            }, expectedError, expectedAttribute.GetType(), invocableCity.StateName);
        }
        public void Entity_DeleteAfterInvoke()
        {
            TestEntityContainer container = new TestEntityContainer();
            container.LoadEntities(_cities);

            // first invoke the action
            City city = _cities.First();
            city.InvokeAction(_assignCityZone.Name, _assignCityZone.Parameters.ToArray());
            Assert.IsFalse(city.CanInvokeAction(_assignCityZone.Name));
            Assert.AreEqual(1, city.EntityActions.Count());

            // delete the entity - expect the actions to be cleared
            container.GetEntitySet<City>().Remove(city);
            Assert.AreEqual(0, city.EntityActions.Count());
            Assert.IsFalse(city.CanInvokeAction(_assignCityZone.Name));
        }
 public void Entity_InvokeOnDeleted()
 {
     TestEntityContainer container = new TestEntityContainer();
     container.LoadEntities(_cities);
     container.GetEntitySet<City>().Remove(_cities[2]);
     Assert.AreEqual(EntityState.Deleted, _cities[2].EntityState);
     Assert.IsFalse(_cities[2].CanInvokeAction(_assignCityZone.Name));
     ExceptionHelper.ExpectException<InvalidOperationException>(delegate
     {
         _cities[2].InvokeAction(_assignCityZone.Name, _assignCityZone.Parameters.ToArray());
     }, Resource.Entity_InvokeOnDeletedEntity);
     Assert.AreEqual(0, _cities[2].EntityActions.Count());
     Assert.AreEqual(EntityState.Deleted, _cities[2].EntityState);
 }
        public void Entity_InvokeOnAdded()
        {
            TestEntityContainer container = new TestEntityContainer();
            City city = new City { Name = "Redmond", CountyName = "King", StateName = "WA" };
            container.GetEntitySet<City>().Add(city);
            Assert.AreEqual(EntityState.New, city.EntityState);
            Assert.AreEqual(0, city.EntityActions.Count());
            Assert.IsTrue(city.CanInvokeAction(_assignCityZone.Name));

            city.InvokeAction(_assignCityZone.Name, _assignCityZone.Parameters.ToArray());
            Assert.AreEqual(1, city.EntityActions.Count());
            Assert.AreEqual(EntityState.New, city.EntityState);
            Assert.AreEqual<string>(_assignCityZone.Name, city.EntityActions.Single().Name);
            Assert.AreEqual<int>(_assignCityZone.Parameters.Count(), city.EntityActions.Single().Parameters.Count());
            Assert.IsFalse(city.CanInvokeAction(_assignCityZone.Name));
        }
        public void PropertyNotification_RaisedOnRejectChanges()
        {
            List<string> propChanged = new List<string>();
            TestEntityContainer container = new TestEntityContainer();
            container.LoadEntities(_cities);

            // invoke domain method and remove the entity
            _cities[0].AssignCityZone("Zone1");
            container.GetEntitySet<City>().Remove(_cities[0]);
            Assert.IsFalse(_cities[0].CanInvokeAction("AssignCityZone"));

            // subscribe to property changed events on entity and call RejectChanges
            _cities[0].PropertyChanged += delegate(object sender, System.ComponentModel.PropertyChangedEventArgs e)
            {
                propChanged.Add(e.PropertyName);
            };
            ((System.ComponentModel.IRevertibleChangeTracking)container).RejectChanges();

            // RejectChanges should raise property changed notification for guard properties
            EnqueueConditional(() => propChanged.Any<string>(p => p.StartsWith("Can")));
            EnqueueCallback(delegate
            {
                Assert.IsTrue(_cities[0].CanInvokeAction("AssignCityZone"));

                // verify RejectChanges does not cause multiple property change notifications
                Assert.IsNotNull(propChanged.Single<string>(p => p == "CanAssignCityZone"));
            });

            EnqueueTestComplete();
        }
        public void PropertyNotification_RaisedOnDelete()
        {
            List<string> propChanged = new List<string>();
            TestEntityContainer container = new TestEntityContainer();
            container.LoadEntities(_cities);
            Assert.IsTrue(_cities[0].CanInvokeAction("AssignCityZone"));

            // subscribe to property changed events on entity and Remove
            _cities[0].PropertyChanged += delegate(object sender, System.ComponentModel.PropertyChangedEventArgs e)
            {
                propChanged.Add(e.PropertyName);
            };
            container.GetEntitySet<City>().Remove(_cities[0]);

            // Deleting entity should raise property changed notification for guard properties
            EnqueueConditional(() => propChanged.Any<string>(p => p.StartsWith("Can")));
            EnqueueCallback(delegate
            {
                Assert.IsFalse(_cities[0].CanInvokeAction("AssignCityZone"));
            });

            EnqueueTestComplete();
        }