コード例 #1
0
        public void HtmlAttributePropertyHelper_ReturnsNameCorrectly()
        {
            // Arrange
            var anonymous = new { foo = "bar" };
            var property = anonymous.GetType().GetTypeInfo().DeclaredProperties.First();

            // Act
            var helper = new HtmlAttributePropertyHelper(property);

            // Assert
            Assert.Equal("foo", property.Name);
            Assert.Equal("foo", helper.Name);
        }
コード例 #2
0
        public void HtmlAttributePropertyHelper_ReturnsValueCorrectly_ForValueTypes()
        {
            // Arrange
            var anonymous = new { foo = 32 };
            var property = anonymous.GetType().GetProperties().First();

            // Act
            var helper = new HtmlAttributePropertyHelper(property);

            // Assert
            Assert.Equal("foo", helper.Name);
            Assert.Equal(32, helper.GetValue(anonymous));
        }
コード例 #3
0
        public void HtmlAttributePropertyHelper_RenamesPropertyNames()
        {
            // Arrange
            var anonymous = new { bar_baz = "foo" };
            var property = anonymous.GetType().GetTypeInfo().DeclaredProperties.First();

            // Act
            var helper = new HtmlAttributePropertyHelper(property);

            // Assert
            Assert.Equal("bar_baz", property.Name);
            Assert.Equal("bar-baz", helper.Name);
        }
コード例 #4
0
        public void PropertyHelperReturnsValueCorrectly()
        {
            // Arrange
            var anonymous = new { bar = "baz" };
            PropertyInfo property = anonymous.GetType().GetProperties().First();

            // Act
            PropertyHelper helper = new PropertyHelper(property);

            // Assert
            Assert.Equal("bar", helper.Name);
            Assert.Equal("baz", helper.GetValue(anonymous));
        }
コード例 #5
0
        public void PropertyHelperReturnsValueCorrectlyForValueTypes()
        {
            // Arrange
            var anonymous = new { foo = 32 };
            PropertyInfo property = anonymous.GetType().GetProperties().First();

            // Act
            PropertyHelper helper = new PropertyHelper(property);

            // Assert
            Assert.Equal("foo", helper.Name);
            Assert.Equal(32, helper.GetValue(anonymous));
        }
コード例 #6
0
        public void PropertyHelperReturnsNameCorrectly()
        {
            // Arrange
            var anonymous = new { foo = "bar" };
            PropertyInfo property = anonymous.GetType().GetProperties().First();

            // Act
            PropertyHelper helper = new PropertyHelper(property);

            // Assert
            Assert.Equal("foo", property.Name);
            Assert.Equal("foo", helper.Name);
        }
コード例 #7
0
        public void GetEntityKeyValue_MultipleKeys_DerivedType()
        {
            // Arrange
            Mock<IEntityTypeConfiguration> baseEntityType = new Mock<IEntityTypeConfiguration>();

            var entityInstance = new { Key1 = "key1", Key2 = 2, Key3 = true };
            PrimitivePropertyConfiguration[] keys = 
            {
                new PrimitivePropertyConfiguration(entityInstance.GetType().GetProperty("Key1"), baseEntityType.Object),
                new PrimitivePropertyConfiguration(entityInstance.GetType().GetProperty("Key2"), baseEntityType.Object),
                new PrimitivePropertyConfiguration(entityInstance.GetType().GetProperty("Key3"), baseEntityType.Object),
            };

            baseEntityType.Setup(e => e.Keys).Returns(keys);

            Mock<IEntityTypeConfiguration> derivedEntityType = new Mock<IEntityTypeConfiguration>();
            derivedEntityType.Setup(e => e.BaseType).Returns(baseEntityType.Object);

            // Act
            var keyValue = ConventionsHelpers.GetEntityKeyValue(new EntityInstanceContext { EntityInstance = entityInstance }, derivedEntityType.Object);

            // Assert
            Assert.Equal("Key1='key1',Key2=2,Key3=true", keyValue);
        }
コード例 #8
0
        public void GetEntityKeyValue_DerivedType()
        {
            // Arrange
            var entityInstance = new { Key = "key" };

            Mock<IEntityTypeConfiguration> baseEntityType = new Mock<IEntityTypeConfiguration>();
            PrimitivePropertyConfiguration[] keys = { new PrimitivePropertyConfiguration(entityInstance.GetType().GetProperty("Key"), baseEntityType.Object) };
            baseEntityType.Setup(e => e.Keys).Returns(keys);

            Mock<IEntityTypeConfiguration> derivedEntityType = new Mock<IEntityTypeConfiguration>();
            derivedEntityType.Setup(e => e.BaseType).Returns(baseEntityType.Object);

            // Act
            var keyValue = ConventionsHelpers.GetEntityKeyValue(new EntityInstanceContext { EntityInstance = entityInstance }, derivedEntityType.Object);

            // Assert
            Assert.Equal("'key'", keyValue);
        }
コード例 #9
0
        public void GetEntityKeyValue_ThrowsForNullKeys_WithMultipleKeys()
        {
            // Arrange
            IStructuralTypeConfiguration structuralType = new Mock<IStructuralTypeConfiguration>().Object;
            var entityInstance = new { Key1 = "abc", Key2 = "def", Key3 = (string)null };
            PrimitivePropertyConfiguration[] keys = 
            {
                new PrimitivePropertyConfiguration(entityInstance.GetType().GetProperty("Key1"), structuralType),
                new PrimitivePropertyConfiguration(entityInstance.GetType().GetProperty("Key2"), structuralType),
                new PrimitivePropertyConfiguration(entityInstance.GetType().GetProperty("Key3"), structuralType),
            };

            Mock<IEntityTypeConfiguration> entityType = new Mock<IEntityTypeConfiguration>();
            entityType.Setup(e => e.Keys).Returns(keys);
            entityType.Setup(e => e.FullName).Returns("EntityType");

            // Act & Assert
            Assert.Throws<InvalidOperationException>(
                () => ConventionsHelpers.GetEntityKeyValue(new EntityInstanceContext { EntityInstance = entityInstance }, entityType.Object),
                "Key property 'Key3' of type 'EntityType' is null. Key properties cannot have null values.");
        }
コード例 #10
0
        public void GetEntityKeyValue_SingleKey_DifferentDataTypes(object value, object expectedValue)
        {
            // Arrange
            IStructuralTypeConfiguration structuralType = new Mock<IStructuralTypeConfiguration>().Object;
            var entityInstance = new { Key = value };
            PrimitivePropertyConfiguration[] keys = { new PrimitivePropertyConfiguration(entityInstance.GetType().GetProperty("Key"), structuralType) };

            Mock<IEntityTypeConfiguration> entityType = new Mock<IEntityTypeConfiguration>();
            entityType
                .Setup(e => e.Keys)
                .Returns(keys);

            // Act
            var keyValue = ConventionsHelpers.GetEntityKeyValue(new EntityInstanceContext { EntityInstance = entityInstance }, entityType.Object);

            // Assert
            Assert.Equal(expectedValue, keyValue);
        }
 public void update_sql_is_sane()
 {
     var thing = new { ColA = "aaa", ColB = "bbb", Id = 1 };
     var sql = DataAccess.GetUpdateSqlFor(thing.GetType(), new[] { "Id" }, "Things");
     sql.Trim().ShouldBe("UPDATE Things SET ColA = @ColA, ColB = @ColB WHERE 1=1 AND Id = @Id");
 }
 public void insert_sql_is_sane()
 {
     var thing = new { ColA = "aaa", ColB = "bbb" };
     var sql = DataAccess.GetInsertSqlFor(thing.GetType(), "Things");
     sql.Trim().ShouldBe("INSERT INTO Things(ColA, ColB) VALUES(@ColA, @ColB)");
 }
コード例 #13
0
        public void GetEntityKeyValue_MultipleKeys()
        {
            // Arrange
            StructuralTypeConfiguration structuralType = new Mock<StructuralTypeConfiguration>().Object;
            var entityInstance = new { Key1 = "key1", Key2 = 2, Key3 = true };
            PrimitivePropertyConfiguration[] keys = 
            {
                new PrimitivePropertyConfiguration(entityInstance.GetType().GetProperty("Key1"), structuralType),
                new PrimitivePropertyConfiguration(entityInstance.GetType().GetProperty("Key2"), structuralType),
                new PrimitivePropertyConfiguration(entityInstance.GetType().GetProperty("Key3"), structuralType),
            };

            Mock<EntityTypeConfiguration> entityType = new Mock<EntityTypeConfiguration>();
            entityType
                .Setup(e => e.Keys)
                .Returns(keys);

            // Act
            var keyValue = ConventionsHelpers.GetEntityKeyValue(new EntityInstanceContext { EntityInstance = entityInstance }, entityType.Object);

            // Assert
            Assert.Equal("Key1='key1',Key2=2,Key3=true", keyValue);
        }
コード例 #14
0
        public void GetEntityKeyValue_SingleKey()
        {
            // Arrange
            StructuralTypeConfiguration structuralType = new Mock<StructuralTypeConfiguration>().Object;
            var entityInstance = new { Key = "key" };
            PrimitivePropertyConfiguration[] keys = { new PrimitivePropertyConfiguration(entityInstance.GetType().GetProperty("Key"), structuralType) };

            Mock<EntityTypeConfiguration> entityType = new Mock<EntityTypeConfiguration>();
            entityType
                .Setup(e => e.Keys)
                .Returns(keys);

            // Act
            var keyValue = ConventionsHelpers.GetEntityKeyValue(new EntityInstanceContext { EntityInstance = entityInstance }, entityType.Object);

            // Assert
            Assert.Equal("'key'", keyValue);
        }