public void Apply_RemovesAllPropertiesThatAreNotDataMembers()
        {
            // Arrange
            ODataConventionModelBuilder builder = ODataConventionModelBuilderHelper.CreateWithModelAliasing(modelAliasing: false);
            Mock <Type> clrType = new Mock <Type>();

            clrType.Setup(t => t.GetCustomAttributes(It.IsAny <bool>())).Returns(new[] { new DataContractAttribute() });

            Mock <StructuralTypeConfiguration> type = new Mock <StructuralTypeConfiguration>(MockBehavior.Strict);

            type.Setup(t => t.ClrType).Returns(clrType.Object);

            var mockPropertyWithoutAttributes = CreateMockProperty();

            type.Object.ExplicitProperties.Add(new MockPropertyInfo(), CreateMockProperty(new DataMemberAttribute()));
            type.Object.ExplicitProperties.Add(new MockPropertyInfo(), CreateMockProperty(new DataMemberAttribute()));
            type.Object.ExplicitProperties.Add(new MockPropertyInfo(), mockPropertyWithoutAttributes);

            type.Setup(t => t.RemoveProperty(mockPropertyWithoutAttributes.PropertyInfo)).Verifiable();

            // Act
            _convention.Apply(type.Object, builder);

            // Assert
            type.Verify();
        }
        public void Apply_NameAndNamespaceNotAliased_IfAddedExplicitly()
        {
            // Arrange
            ODataConventionModelBuilder builder = ODataConventionModelBuilderHelper.CreateWithModelAliasing(modelAliasing: true);

            Mock <Type> clrType = new Mock <Type>();

            clrType.Setup(t => t.GetCustomAttributes(It.IsAny <bool>()))
            .Returns(new[] { new DataContractAttribute {
                                 Name = "NameAlias", Namespace = "com.contoso"
                             } });

            Mock <StructuralTypeConfiguration> type = new Mock <StructuralTypeConfiguration> {
                CallBase = true
            };

            type.Setup(t => t.ClrType).Returns(clrType.Object);
            StructuralTypeConfiguration configuration = type.Object;

            configuration.Name      = "Name";
            configuration.Namespace = "Namespace";

            // Act
            _convention.Apply(configuration, builder);

            // Assert
            Assert.Equal("Name", configuration.Name);
            Assert.Equal("Namespace", configuration.Namespace);
            Assert.True(configuration.AddedExplicitly);
        }
Ejemplo n.º 3
0
        public void ApplyTo_PropertyAliased_IfEnabled(bool modelAliasing, string propertyName)
        {
            // Arrange
            var builder = ODataConventionModelBuilderHelper.CreateWithModelAliasing(modelAliasing);

            builder.EntitySet <PropertyAlias>("PropertyAliases");
            var model = builder.GetEdmModel();

            var context = new ODataQueryContext(model, typeof(PropertyAlias))
            {
                RequestContainer = new MockContainer(model)
            };
            var orderByOption = new OrderByQueryOption(propertyName, context);
            IEnumerable <PropertyAlias> propertyAliases = FilterQueryOptionTest.PropertyAliasTestData;

            // Act
            IQueryable queryable = orderByOption.ApplyTo(propertyAliases.AsQueryable());

            // Assert
            Assert.NotNull(queryable);
            IEnumerable <PropertyAlias> actualCustomers = Assert.IsAssignableFrom <IEnumerable <PropertyAlias> >(queryable);

            Assert.Equal(
                new[] { "abc", "def", "xyz" },
                actualCustomers.Select(propertyAlias => propertyAlias.FirstName));
        }
        public void DataMemberAttributeEdmPropertyConvention_PropertyAliased_IfEnabled(string propertyAlias, bool modelAliasing)
        {
            // Arrange
            MockType relatedType =
                new MockType("RelatedEntity")
                .Property <int>("ID");
            MockType type =
                new MockType("Entity")
                .Property(typeof(int), "ID")
                .Property(relatedType, "RelatedEntity", new DataMemberAttribute {
                Name = "AliasRelatedEntity"
            });

            type.Setup(t => t.GetCustomAttributes(It.IsAny <Type>(), It.IsAny <bool>()))
            .Returns(new[] { new DataContractAttribute() });

            // Act
            ODataConventionModelBuilder builder = ODataConventionModelBuilderHelper.CreateWithModelAliasing(modelAliasing);

            builder.AddEntityType(type);
            IEdmModel model = builder.GetEdmModel();

            // Assert
            IEdmEntityType entity = model.AssertHasEntityType(type);

            entity.AssertHasKey(model, "ID", EdmPrimitiveTypeKind.Int32);
            entity.AssertHasNavigationProperty(
                model,
                propertyAlias,
                relatedType,
                isNullable: true,
                multiplicity: EdmMultiplicity.ZeroOrOne);
        }
        public void Apply_AliasNotSet_NoPropertyAlias(bool modelAliasing)
        {
            // Arrange
            ODataConventionModelBuilder modelBuilder = ODataConventionModelBuilderHelper.CreateWithModelAliasing(modelAliasing: modelAliasing);

            MockType type =
                new MockType("Mocktype")
                .Property(typeof(string), "Property", new[] { new DataMemberAttribute() });

            type.Setup(t => t.GetCustomAttributes(It.IsAny <Type>(), It.IsAny <bool>())).Returns(new[] { new DataContractAttribute() });

            Mock <StructuralTypeConfiguration> structuralType = new Mock <StructuralTypeConfiguration>();

            structuralType.Setup(t => t.ClrType).Returns(type);

            PropertyInfo property = type.GetProperty("Property");
            Mock <StructuralPropertyConfiguration> structuralProperty = new Mock <StructuralPropertyConfiguration>(property, structuralType.Object);

            structuralProperty.Object.AddedExplicitly = false;

            // Act
            new DataMemberAttributeEdmPropertyConvention().Apply(structuralProperty.Object, structuralType.Object, modelBuilder);

            // Assert
            Assert.Equal("Property", structuralProperty.Object.Name);
        }
        public void Apply_DoesnotRemove_ExplicitlyAddedProperties()
        {
            // Arrange
            ODataConventionModelBuilder builder  = ODataConventionModelBuilderHelper.CreateWithModelAliasing(modelAliasing: false);
            PropertyInfo            propertyInfo = typeof(TestEntity).GetProperty("ExplicitlyAddedProperty");
            EntityTypeConfiguration entity       = builder.AddEntityType(typeof(TestEntity));
            PropertyConfiguration   property     = entity.AddProperty(propertyInfo);

            // Act
            _convention.Apply(entity, builder);

            // Assert
            Assert.Contains(propertyInfo, entity.ExplicitProperties.Keys);
            Assert.DoesNotContain(propertyInfo, entity.RemovedProperties);
        }
Ejemplo n.º 7
0
        public void CreateCollection_PropertyAliased_IfEnabled(bool modelAliasing, string typeName, string propertyName)
        {
            // Arrange
            ODataConventionModelBuilder builder = ODataConventionModelBuilderHelper.CreateWithModelAliasing(modelAliasing);

            builder.EntitySet <PropertyAlias>("entityset");

            IEdmModel      model      = builder.GetEdmModel();
            IEdmEntityType entityType = model.SchemaElements.Single(t => t.Name == typeName) as IEdmEntityType;

            Assert.NotNull(entityType); // Guard
            IEdmEntitySet entitySet = model.EntityContainer.FindEntitySet("entityset");

            Assert.NotNull(entitySet); // Guard

            ODataQueryOptionParser parser = new ODataQueryOptionParser(model, entityType, entitySet,
                                                                       new Dictionary <string, string> {
                { "$orderby", propertyName + " desc, Id asc" }
            });
            OrderByClause orderbyNode = parser.ParseOrderBy();

            // Act
            ICollection <OrderByNode> nodes = OrderByNode.CreateCollection(orderbyNode);

            // Assert
            Assert.False(nodes.OfType <OrderByItNode>().Any());
            IEnumerable <OrderByPropertyNode> propertyNodes = nodes.OfType <OrderByPropertyNode>();

            Assert.Equal(2, propertyNodes.Count());
            Assert.Equal(propertyName, propertyNodes.First().Property.Name);
            Assert.Equal(OrderByDirection.Descending, propertyNodes.First().Direction);
            Assert.Equal(propertyName, propertyNodes.First().PropertyPath);

            Assert.Equal("Id", propertyNodes.Last().Property.Name);
            Assert.Equal(OrderByDirection.Ascending, nodes.Last().Direction);
            Assert.Equal("Id", propertyNodes.Last().PropertyPath);
        }