public void Set_returns_the_same_entity_set_for_the_same_entity_type()
        {
            var context         = new OEContext();
            var productSet      = context.Set <ProductPoco>();
            var productSetAgain = context.Set <ProductPoco>();

            Assert.AreSame(productSet, productSetAgain);
        }
        public void Attach_detach_means_no_changes()
        {
            var context = new OEContext();
            var product = new Product()
            {
                Id        = 1,
                Name      = "Test product",
                UnitPrice = 100
            };

            context.Set <Product>().Attach(product);
            Assert.AreEqual(false, context.HasChanges());
            context.Set <Product>().Detach(product);
            Assert.AreEqual(false, context.HasChanges());
        }
        public void Add_delete_means_no_changes()
        {
            var context = new OEContext();
            var product = new ProductPoco()
            {
                Id        = 1,
                Name      = "Test product",
                UnitPrice = 100
            };

            context.Set <ProductPoco>().Add(product);
            Assert.AreEqual(true, context.HasChanges());
            context.Set <ProductPoco>().Delete(product);
            Assert.AreEqual(false, context.HasChanges());
        }
Example #4
0
        public void Should_validate_when_unique_key_in_context_set()
        {
            var context    = new OEContext();
            var productSet = context.Set <ProductWithSingleKeyProperty>();
            var product1   = new ProductWithSingleKeyProperty()
            {
                Id        = 1,
                Name      = "First product",
                UnitPrice = 100
            };

            productSet.Add(product1);
            var product2 = new ProductWithSingleKeyProperty()
            {
                Id        = 2,
                Name      = "Second product",
                UnitPrice = 100
            };

            productSet.Add(product2);

            var validationResults = new List <ValidationResultWithSeverityLevel>();
            var result            = productSet.Validate(validationResults);

            Assert.AreEqual(true, result);
            Assert.AreEqual(0, validationResults.Count());

            validationResults = new List <ValidationResultWithSeverityLevel>();
            result            = context.Validate(validationResults);
            Assert.AreEqual(true, result);
            Assert.AreEqual(0, validationResults.Count());
        }
        public void Modified_attached_entity_should_add_multiple_modified_properties()
        {
            var context    = new OEContext();
            var productSet = context.Set <Product>();

            var product = new Product()
            {
                Id        = 1,
                Name      = "Test product",
                UnitPrice = 100
            };

            productSet.Attach(product);
            product.UnitPrice++;
            product.Name = "Modified Product";

            var productEntry = context.ChangeTracker.Entries().First();

            Assert.AreEqual(OEEntityState.Modified, productEntry.State);

            var modifiedProperties = productEntry.ModifiedProperties.ToArray();

            Assert.AreEqual(2, modifiedProperties.Count());

            var modifiedProperty = modifiedProperties[0];

            Assert.AreEqual("UnitPrice", modifiedProperty.Name);
            Assert.AreEqual(100M, modifiedProperty.OriginalValue);

            modifiedProperty = modifiedProperties[1];
            Assert.AreEqual("Name", modifiedProperty.Name);
            Assert.AreEqual("Test product", modifiedProperty.OriginalValue);
        }
        public void Should_not_validate_inherited_classes_with_errors_when_the_entity_set_is_registered_against_the_base_type()
        {
            var context    = new OEContext();
            var productSet = context.Set <BaseItem>();
            var product    = new InheritedItem()
            {
                Id          = -1,
                Name        = null,
                UnitPrice   = -1,
                Description = null
            };

            productSet.Add(product);

            var validationResults = new List <ValidationResultWithSeverityLevel>();
            var result            = productSet.Validate(validationResults);

            Assert.AreEqual(false, result);
            Assert.AreEqual(2, validationResults.Count());
            Assert.AreSame(product, validationResults[0].Entity);
            Assert.AreEqual("Name", validationResults[0].MemberNames.ElementAt(0));
            Assert.AreSame(product, validationResults[1].Entity);
            Assert.AreEqual("Description", validationResults[1].MemberNames.ElementAt(0));

            validationResults = new List <ValidationResultWithSeverityLevel>();
            result            = context.Validate(validationResults);
            Assert.AreEqual(false, result);
            Assert.AreEqual(2, validationResults.Count());
            Assert.AreSame(product, validationResults[0].Entity);
            Assert.AreEqual("Name", validationResults[0].MemberNames.ElementAt(0));
            Assert.AreSame(product, validationResults[1].Entity);
            Assert.AreEqual("Description", validationResults[1].MemberNames.ElementAt(0));
        }
        public void GetChanges_should_not_return_added_deleted()
        {
            var context = new OEContext();
            var product = new ProductPoco()
            {
                Id        = 1,
                Name      = "Test product",
                UnitPrice = 100
            };

            context.Set <ProductPoco>().Add(product);
            context.Set <ProductPoco>().Delete(product);

            var changes = context.GetChanges();

            Assert.AreEqual(0, changes.Count());
        }
        public void GetChanges_should_return_attached_deleted()
        {
            var context = new OEContext();
            var product = new ProductPoco()
            {
                Id        = 1,
                Name      = "Test product",
                UnitPrice = 100
            };

            context.Set <ProductPoco>().Attach(product);
            context.Set <ProductPoco>().Delete(product);

            var changes = context.GetChanges();

            Assert.AreSame(product, changes.FirstOrDefault().Entity);
        }
        public void ApplyChanges_applies_added_changes()
        {
            var context = new OEContext();
            var product = new ProductPoco()
            {
                Id        = 1,
                Name      = "Test product",
                UnitPrice = 100
            };

            context.Set <ProductPoco>().Add(product);
            Assert.AreEqual(true, context.HasChanges());
            Assert.AreEqual(1, context.GetChanges().Count());
            context.ApplyChanges();
            Assert.AreEqual(false, context.HasChanges());
            Assert.AreEqual(0, context.GetChanges().Count());
            Assert.AreEqual(1, context.Set <ProductPoco>().GetAll().Count());
        }
        public void EntityChanged_should_be_triggered_once_on_attached_deleted()
        {
            int changesCount = 0;
            var context      = new OEContext();

            context.EntityChanged += (s, e) => changesCount++;

            var product = new ProductPoco()
            {
                Id        = 1,
                Name      = "Test product",
                UnitPrice = 100
            };

            context.Set <ProductPoco>().Attach(product);
            context.Set <ProductPoco>().Delete(product);
            Assert.AreEqual(1, changesCount);
        }
        public void CancelChanges_cancels_attached_deleted_changes()
        {
            var context = new OEContext();
            var product = new ProductPoco()
            {
                Id        = 1,
                Name      = "Test product",
                UnitPrice = 100
            };

            context.Set <ProductPoco>().Attach(product);
            context.Set <ProductPoco>().Delete(product);
            Assert.AreEqual(true, context.HasChanges());
            Assert.AreEqual(1, context.GetChanges().Count());
            context.CancelChanges();
            Assert.AreEqual(false, context.HasChanges());
            Assert.AreEqual(0, context.GetChanges().Count());
        }
        public void ApplyChanges_applies_attached_modified_changes()
        {
            var context = new OEContext();
            var product = new Product()
            {
                Id        = 1,
                Name      = "Test product",
                UnitPrice = 100
            };

            context.Set <Product>().Attach(product);
            product.UnitPrice++;
            Assert.AreEqual(true, context.HasChanges());
            Assert.AreEqual(1, context.GetChanges().Count());
            context.ApplyChanges();
            Assert.AreEqual(false, context.HasChanges());
            Assert.AreEqual(0, context.GetChanges().Count());
            Assert.AreEqual(1, context.Set <Product>().GetAll().Count());
        }
        public void Detached_added_entities_should_be_no_longer_tracked()
        {
            var context    = new OEContext();
            var productSet = context.Set <Product>();

            var product = new Product();

            productSet.Add(product);
            productSet.Detach(product);

            Assert.AreEqual(0, context.ChangeTracker.Entries().Count());
        }
        public void EntityChanged_should_be_triggered_on_added_modified_but_not_on_deleted()
        {
            int changesCount = 0;
            var context      = new OEContext();

            context.EntityChanged += (s, e) => changesCount++;

            var product = new Product()
            {
                Id        = 1,
                Name      = "Test product",
                UnitPrice = 100
            };

            context.Set <Product>().Add(product);
            Assert.AreEqual(1, changesCount);
            product.UnitPrice++;
            Assert.AreEqual(2, changesCount);
            context.Set <Product>().Delete(product);
            Assert.AreEqual(2, changesCount);
        }
        public void Add_means_changes()
        {
            var context = new OEContext();
            var product = new Product()
            {
                Id        = 1,
                Name      = "Test product",
                UnitPrice = 100
            };

            context.Set <Product>().Add(product);
            Assert.AreEqual(true, context.HasChanges());
        }
        public void Deleted_attached_entity_should_be_tracked_as_deleted()
        {
            var context    = new OEContext();
            var productSet = context.Set <Product>();

            var product = new Product();

            productSet.Attach(product);
            productSet.Delete(product);

            var productEntry = context.ChangeTracker.Entries().First();

            Assert.AreEqual(OEEntityState.Deleted, productEntry.State);
        }
Example #17
0
        public void Modified_added_should_track_modified_properties_if_AlwaysTrackModifiedProperties()
        {
            var context = new OEContext();

            context.Set <Product>().AlwaysTrackModifiedProperties = true;
            var product = new Product()
            {
                Id        = 1,
                Name      = "Test product",
                UnitPrice = 100
            };

            context.Set <Product>().Add(product);
            product.UnitPrice = 150;
            var changes     = context.GetChanges();
            var entityEntry = changes.FirstOrDefault();

            Assert.AreSame(product, entityEntry.Entity);
            var modifiedProperties = entityEntry.ModifiedProperties.ToArray();

            Assert.AreEqual("UnitPrice", modifiedProperties[0].Name);
            Assert.AreEqual(100M, modifiedProperties[0].OriginalValue);
        }
        public void GetAll_should_return_added()
        {
            var context    = new OEContext();
            var productSet = context.Set <Product>();
            var product    = new Product()
            {
                Id        = 1,
                Name      = "Test product",
                UnitPrice = 100
            };

            productSet.Add(product);
            Assert.AreSame(product, productSet.GetAll().FirstOrDefault());
        }
        public void Added_entity_should_be_tracked_as_added()
        {
            var context    = new OEContext();
            var productSet = context.Set <Product>();

            var product = new Product();

            productSet.Add(product);

            var productEntry = context.ChangeTracker.Entries().First();

            Assert.AreSame(product, productEntry.Entity);
            Assert.AreEqual(OEEntityState.Added, productEntry.State);
        }
        public void Should_not_validate_and_return_errors_when_some_severity_levels_are_not_excluded()
        {
            var context = new OEContext();

            context.Configuration.ValidationSafeSeverityLevels = new Collection <object>()
            {
                ValidationSeverityLevel.Warning
            };
            // ValidationSeverityLevel.Error should fail validation

            var productSet = context.Set <ProductWithCustomValidationSeveritySupport>();
            var product    = new ProductWithCustomValidationSeveritySupport()
            {
                Id        = -1,
                Name      = null,
                UnitPrice = -1
            };

            productSet.Add(product);

            var validationResults = new List <ValidationResultWithSeverityLevel>();
            var result            = productSet.Validate(validationResults);

            Assert.AreEqual(false, result);
            Assert.AreEqual(3, validationResults.Count());
            Assert.AreSame(product, validationResults[0].Entity);
            Assert.AreEqual("Id", validationResults[0].MemberNames.ElementAt(0));
            Assert.AreEqual(ValidationSeverityLevel.Error, validationResults[0].ErrorSeverity);
            Assert.AreSame(product, validationResults[1].Entity);
            Assert.AreEqual("Name", validationResults[1].MemberNames.ElementAt(0));
            Assert.AreEqual(ValidationSeverityLevel.Error, validationResults[1].ErrorSeverity);
            Assert.AreSame(product, validationResults[2].Entity);
            Assert.AreEqual("UnitPrice", validationResults[2].MemberNames.ElementAt(0));
            Assert.AreEqual(ValidationSeverityLevel.Warning, validationResults[2].ErrorSeverity);

            validationResults = new List <ValidationResultWithSeverityLevel>();
            result            = context.Validate(validationResults);
            Assert.AreEqual(false, result);
            Assert.AreEqual(3, validationResults.Count());
            Assert.AreSame(product, validationResults[0].Entity);
            Assert.AreEqual("Id", validationResults[0].MemberNames.ElementAt(0));
            Assert.AreEqual(ValidationSeverityLevel.Error, validationResults[0].ErrorSeverity);
            Assert.AreSame(product, validationResults[1].Entity);
            Assert.AreEqual("Name", validationResults[1].MemberNames.ElementAt(0));
            Assert.AreEqual(ValidationSeverityLevel.Error, validationResults[1].ErrorSeverity);
            Assert.AreSame(product, validationResults[2].Entity);
            Assert.AreEqual("UnitPrice", validationResults[2].MemberNames.ElementAt(0));
            Assert.AreEqual(ValidationSeverityLevel.Warning, validationResults[2].ErrorSeverity);
        }
        public void Attach_modify_means_changes_at_the_end()
        {
            var context = new OEContext();
            var product = new ProductPoco()
            {
                Id        = 1,
                Name      = "Test product",
                UnitPrice = 100
            };

            context.Set <ProductPoco>().Attach(product);
            Assert.AreEqual(false, context.HasChanges());
            product.UnitPrice++;
            Assert.AreEqual(true, context.HasChanges());
        }
        public void GetAll_should_not_return_added_deleted()
        {
            var context    = new OEContext();
            var productSet = context.Set <Product>();
            var product    = new Product()
            {
                Id        = 1,
                Name      = "Test product",
                UnitPrice = 100
            };

            productSet.Add(product);
            productSet.Delete(product);
            Assert.AreEqual(0, productSet.GetAll().Count());
        }
        public void GetChanges_should_not_return_attached_unchanged()
        {
            var context = new OEContext();
            var product = new Product()
            {
                Id        = 1,
                Name      = "Test product",
                UnitPrice = 100
            };

            context.Set <Product>().Attach(product);
            var changes = context.GetChanges();

            Assert.AreEqual(0, changes.Count());
        }
        public void Add_modify_means_changes_on_each_op()
        {
            var context = new OEContext();
            var product = new ProductPoco()
            {
                Id        = 1,
                Name      = "Test product",
                UnitPrice = 100
            };

            context.Set <ProductPoco>().Add(product);
            Assert.AreEqual(true, context.HasChanges());
            product.UnitPrice++;
            Assert.AreEqual(true, context.HasChanges());
        }
        public void Ignore_Attach_modify_no_changes()
        {
            var context = new OEContext();
            var product = new Product()
            {
                Id        = 1,
                Name      = "Test product",
                UnitPrice = 100,
                Category  = "Test category"
            };

            context.Set <Product>().Attach(product);
            Assert.AreEqual(false, context.HasChanges());
            product.Category = "Modified category";
            Assert.AreEqual(false, context.HasChanges());
        }
        public void Attach_delete_means_changes()
        {
            var context    = new OEContext();
            var productSet = context.Set <Product>();
            var product    = new Product()
            {
                Id        = 1,
                Name      = "Test product",
                UnitPrice = 100
            };

            productSet.Attach(product);
            Assert.AreEqual(false, productSet.HasChanges());
            productSet.Delete(product);
            Assert.AreEqual(true, productSet.HasChanges());
        }
        public void GetChanges_should_return_added_modified()
        {
            var context = new OEContext();
            var product = new ProductPoco()
            {
                Id        = 1,
                Name      = "Test product",
                UnitPrice = 100
            };

            context.Set <ProductPoco>().Add(product);
            product.UnitPrice++;

            var changes = context.GetChanges();

            Assert.AreSame(product, changes.FirstOrDefault().Entity);
        }
        public void Should_validate_when_custom_validation_is_met_severity_support()
        {
            var context    = new OEContext();
            var productSet = context.Set <ProductWithCustomValidationSeveritySupport>();
            var product    = new ProductWithCustomValidationSeveritySupport()
            {
                Id        = 1,
                Name      = "Valid Product",
                UnitPrice = 1
            };

            productSet.Add(product);

            var validationResults = new List <ValidationResultWithSeverityLevel>();
            var result            = productSet.Validate(validationResults);

            Assert.AreEqual(true, result);
        }
        public void Detached_modified_attached_entities_should_be_no_longer_tracked()
        {
            var context    = new OEContext();
            var productSet = context.Set <Product>();

            var product = new Product()
            {
                Id        = 1,
                Name      = "Test product",
                UnitPrice = 100
            };

            productSet.Attach(product);
            product.UnitPrice++;
            productSet.Detach(product);

            Assert.AreEqual(0, context.ChangeTracker.Entries().Count());
        }
        public void EntityChanged_should_not_be_triggered_on_modified()
        {
            int changesCount = 0;
            var context      = new OEContext();

            context.EntityChanged += (s, e) => changesCount++;

            var product = new ProductPoco()
            {
                Id        = 1,
                Name      = "Test product",
                UnitPrice = 100
            };

            context.Set <ProductPoco>().Attach(product);
            product.UnitPrice++;
            Assert.AreEqual(0, changesCount);
        }