예제 #1
0
        public void CreateNamedPropertyExpression_WithMultipleProperty_ReturnsMemberAccessExpression(HandleNullPropagationOption options)
        {
            SelectExpandBinder binder = GetBinder <Customer>(_model, options);

            //Expression customer = Expression.Parameter(typeof(Customer)); output will be different.
            Expression             customer             = Expression.Constant(new Customer());
            IEdmStructuralProperty homeLocationProperty = _customer.StructuralProperties().Single(c => c.Name == "HomeLocation");

            IEdmStructuralProperty streetProperty = _address.StructuralProperties().Single(c => c.Name == "Street");

            ODataSelectPath selectPath = new ODataSelectPath(new PropertySegment(homeLocationProperty),
                                                             new PropertySegment(streetProperty));

            PathSelectItem pathSelectItem = new PathSelectItem(selectPath);

            NamedPropertyExpression namedProperty = binder.CreateNamedPropertyExpression(customer, _customer, pathSelectItem);

            Assert.NotNull(namedProperty);

            /*
             * Assert.NotNull(namedProperty);
             * Assert.Equal("\"VipAddress\"", namedProperty.Name.ToString());
             *
             * if (options != HandleNullPropagationOption.True)
             * {
             *  Assert.Equal("(value(NS.Customer) As VipCustomer).VipAddress", namedProperty.Value.ToString());
             * }
             * else
             * {
             *  Assert.Equal("IIF(((value(NS.Customer) As VipCustomer) == null)," +
             *      " null," +
             *      " (value(NS.Customer) As VipCustomer).VipAddress)", namedProperty.Value.ToString());
             * }*/
        }
예제 #2
0
        public void ComplexType_reference_extensions()
        {
            IEdmModel       edmModel           = this.GetEdmModel();
            IEdmComplexType derivedComplexType = edmModel.SchemaElements.OfType <IEdmComplexType>().First(c => c.BaseType != null);
            IEdmComplexType baseComplexType    = derivedComplexType.BaseComplexType();

            Assert.IsNotNull(baseComplexType, "Base complex type should not be null!");

            IEdmComplexTypeReference derivedComplexTypeRef = (IEdmComplexTypeReference)derivedComplexType.ToTypeReference();

            Assert.AreEqual(baseComplexType, derivedComplexTypeRef.BaseComplexType(), "ComplexTypeReference.BaseComplexType()");
            Assert.AreEqual(baseComplexType, derivedComplexTypeRef.BaseType(), "ComplexTypeReference.BaseType()");

            Assert.AreEqual(derivedComplexType.IsAbstract, derivedComplexTypeRef.IsAbstract(), "StructuralTypeReference.IsAbstract()");
            Assert.AreEqual(derivedComplexType.IsOpen, derivedComplexTypeRef.IsOpen(), "StructuralTypeReference.IsOpen()");

            Assert.AreEqual(derivedComplexType.DeclaredStructuralProperties().Count(), derivedComplexTypeRef.DeclaredStructuralProperties().Count(), "StructuralTypeReference.DeclaredStructuralProperties()");
            Assert.AreEqual(derivedComplexType.StructuralProperties().Count(), derivedComplexTypeRef.StructuralProperties().Count(), "StructuralTypeReference.StructuralProperties()");
        }
        public void ConvertComplexType_Inheritance()
        {
            var taupoModel = new EntityModelSchema()
            {
                new ComplexType("NS1", "BaseComplex")
                {
                    new MemberProperty("p1", EdmDataTypes.Int16),
                },
                new ComplexType("NS2", "DerivedComplex")
                {
                    BaseType   = "BaseComplex",
                    Properties = { new MemberProperty("p2", EdmDataTypes.Int16) },
                },
            }
            .Resolve();

            IEdmModel result = this.converter.ConvertToEdmModel(taupoModel);

            Assert.IsNull(result.EntityContainer);
            Assert.AreEqual(2, result.SchemaElements.Count());
            Assert.AreEqual(2, result.SchemaElements.OfType <IEdmComplexType>().Count());

            IEdmComplexType baseComplex = result.SchemaElements.OfType <IEdmComplexType>().ElementAt(0);

            Assert.AreEqual("NS1.BaseComplex", baseComplex.FullName());
            Assert.AreEqual(1, baseComplex.DeclaredStructuralProperties().Count());
            Assert.AreEqual("p1", baseComplex.DeclaredStructuralProperties().First().Name);

            IEdmComplexType derivedComplex = result.SchemaElements.OfType <IEdmComplexType>().ElementAt(1);

            Assert.AreEqual("NS2.DerivedComplex", derivedComplex.FullName());
            Assert.AreEqual(1, derivedComplex.DeclaredStructuralProperties().Count());
            Assert.AreEqual("p2", derivedComplex.DeclaredStructuralProperties().First().Name);

            Assert.AreEqual(2, derivedComplex.StructuralProperties().Count());
            Assert.AreEqual(2, derivedComplex.Properties().Count());
            Assert.AreEqual(baseComplex, derivedComplex.BaseComplexType());
        }
        public void ConvertComplexType()
        {
            var taupoModel = new EntityModelSchema()
            {
                new ComplexType("NS1", "Complex1")
                {
                    new MemberProperty("p1", EdmDataTypes.Int32),
                    new MemberProperty("p2", EdmDataTypes.Int32.Nullable())
                    {
                        DefaultValue = 100,
                        Annotations  =
                        {
                            new ConcurrencyTokenAnnotation(),
                            new AttributeAnnotation()
                            {
                                Content = new XAttribute(this.annotationNamespace + "foo1", "bar1")
                            },
                        },
                    },
                    new AttributeAnnotation()
                    {
                        Content = new XAttribute(this.annotationNamespace + "foo2", "bar2")
                    },
                },
            };

            IEdmModel result = this.converter.ConvertToEdmModel(taupoModel);

            Assert.IsNull(result.EntityContainer);
            Assert.AreEqual(1, result.SchemaElements.Count());
            Assert.AreEqual(1, result.SchemaElements.OfType <IEdmComplexType>().Count());

            IEdmComplexType complex = result.SchemaElements.OfType <IEdmComplexType>().Single();

            Assert.AreEqual("NS1.Complex1", complex.FullName());
            Assert.IsNull(complex.BaseComplexType());
            Assert.AreEqual(2, complex.DeclaredStructuralProperties().Count());
            Assert.AreEqual(2, complex.StructuralProperties().Count());

            IEdmStructuralProperty property     = complex.DeclaredStructuralProperties().ElementAt(0);
            IEdmTypeReference      propertyType = property.Type;

            Assert.AreEqual("p1", property.Name);
            Assert.AreEqual("Edm.Int32", propertyType.FullName());
            Assert.AreEqual(EdmTypeKind.Primitive, propertyType.TypeKind());
            Assert.IsFalse(propertyType.IsNullable);

            Assert.AreEqual(complex, property.DeclaringType);
            Assert.AreEqual(EdmConcurrencyMode.None, property.ConcurrencyMode);
            Assert.IsNull(property.DefaultValueString);

            Assert.AreEqual(0, result.DirectValueAnnotations(property).Count());

            property     = complex.DeclaredStructuralProperties().ElementAt(1);
            propertyType = property.Type;

            Assert.AreEqual("p2", property.Name);
            Assert.AreEqual("Edm.Int32", propertyType.FullName());
            Assert.AreEqual(EdmTypeKind.Primitive, propertyType.TypeKind());
            Assert.IsTrue(propertyType.IsNullable);

            Assert.AreEqual(complex, property.DeclaringType);
            Assert.AreEqual(EdmConcurrencyMode.Fixed, property.ConcurrencyMode);
            Assert.AreEqual("100", property.DefaultValueString);

            Assert.AreEqual(1, result.DirectValueAnnotations(complex).Count());
            Assert.AreEqual("bogus", result.DirectValueAnnotations(complex).First().NamespaceUri);
            Assert.AreEqual("foo2", result.DirectValueAnnotations(complex).First().Name);
            Assert.AreEqual("bar2", (((IEdmDirectValueAnnotation)result.DirectValueAnnotations(complex).First()).Value as IEdmStringValue).Value);

            Assert.AreEqual(1, result.DirectValueAnnotations(property).Count());
            Assert.AreEqual("bogus", result.DirectValueAnnotations(property).First().NamespaceUri);
            Assert.AreEqual("foo1", result.DirectValueAnnotations(property).First().Name);
            Assert.AreEqual("bar1", (((IEdmDirectValueAnnotation)result.DirectValueAnnotations(property).First()).Value as IEdmStringValue).Value);
        }
예제 #5
0
        public void ComplexValueIgnorePropertyNullValuesTest()
        {
            var versions = new Version[] {
                null,
                new Version(4, 0),
            };

            EdmModel        edmModel          = new EdmModel();
            IEdmComplexType countryRegionType = edmModel.ComplexType("CountryRegion")
                                                .Property("Name", EdmPrimitiveTypeKind.String)
                                                .Property("CountryRegionCode", EdmPrimitiveTypeKind.String);
            IEdmComplexType countryRegionNullType = edmModel.ComplexType("CountryRegionNull")
                                                    .Property("Name", EdmPrimitiveTypeKind.String)
                                                    .Property("CountryRegionCode", EdmPrimitiveTypeKind.String);
            IEdmComplexType addressType = edmModel.ComplexType("Address")
                                          .Property("Street", EdmPrimitiveTypeKind.String)
                                          .Property("StreetNull", EdmCoreModel.Instance.GetString(true) as EdmTypeReference)
                                          .Property("Numbers", EdmCoreModel.GetCollection(EdmCoreModel.Instance.GetInt32(false)) as EdmTypeReference)
                                          .Property("CountryRegion", new EdmComplexTypeReference(countryRegionType, false))
                                          .Property("CountryRegionNull", new EdmComplexTypeReference(countryRegionNullType, true));

            edmModel.EntityType("Customer")
            .KeyProperty("ID", EdmCoreModel.Instance.GetInt32(false) as EdmTypeReference)
            .Property("Address", new EdmComplexTypeReference(addressType, false));
            edmModel.Fixup();

            this.CombinatorialEngineProvider.RunCombinations(
                new ODataNullValueBehaviorKind[] { ODataNullValueBehaviorKind.Default, ODataNullValueBehaviorKind.DisableValidation, ODataNullValueBehaviorKind.IgnoreValue },
                versions,
                versions,
                TestReaderUtils.ODataBehaviorKinds,
                (nullPropertyValueReaderBehavior, dataServiceVersion, edmVersion, behaviorKind) =>
            {
                edmModel.SetEdmVersion(edmVersion);

                // Now we set the 'IgnoreNullValues' annotation on all properties
                IEdmComplexType edmAddressType = (IEdmComplexType)edmModel.FindType("TestModel.Address");
                foreach (IEdmStructuralProperty edmProperty in edmAddressType.StructuralProperties())
                {
                    edmModel.SetNullValueReaderBehavior(edmProperty, nullPropertyValueReaderBehavior);
                }

                EntityInstance customerPayload = PayloadBuilder.Entity("TestModel.Customer")
                                                 .PrimitiveProperty("ID", 1)
                                                 .Property("Address", PayloadBuilder.ComplexValue("TestModel.Address")
                                                           .PrimitiveProperty("Street", "One Microsoft Way")
                                                           .PrimitiveProperty("StreetNull", "One Microsoft Way")
                                                           .Property("Numbers", PayloadBuilder.PrimitiveMultiValue("Collection(Edm.Int32)").Item(1).Item(2))
                                                           .Property("CountryRegion", PayloadBuilder.ComplexValue("TestModel.CountryRegion")
                                                                     .PrimitiveProperty("Name", "Austria")
                                                                     .PrimitiveProperty("CountryRegionCode", "AUT"))
                                                           .Property("CountryRegionNull", PayloadBuilder.ComplexValue("TestModel.CountryRegionNull")
                                                                     .PrimitiveProperty("Name", "Austria")
                                                                     .PrimitiveProperty("CountryRegionCode", "AUT")));

                var testCases = new[]
                {
                    // Complex types that are not nullable should not allow null values.
                    // Null primitive property in the payload and non-nullable property in the model
                    new IgnoreNullValueTestCase
                    {
                        PropertyName = "Street",
                        ExpectedResponseException = ODataExpectedExceptions.ODataException("ReaderValidationUtils_NullNamedValueForNonNullableType", "Street", "Edm.String"),
                    },
                    // Null complex property in the payload and non-nullable property in the model
                    new IgnoreNullValueTestCase
                    {
                        PropertyName = "CountryRegion",
                        ExpectedResponseException = ODataExpectedExceptions.ODataException("ReaderValidationUtils_NullNamedValueForNonNullableType", "CountryRegion", "TestModel.CountryRegion"),
                    },
                    // Null collection property in the payload and non-nullable property in the model
                    new IgnoreNullValueTestCase
                    {
                        PropertyName = "Numbers",
                        ExpectedResponseException = ODataExpectedExceptions.ODataException("ReaderValidationUtils_NullNamedValueForNonNullableType", "Numbers", "Collection(Edm.Int32)"),
                    },
                    // Complex types that are nullable should allow null values.
                    // Null primitive property in the payload and nullable property in the model
                    new IgnoreNullValueTestCase
                    {
                        PropertyName = "StreetNull",
                    },
                    // Null complex property in the payload and nullable property in the model
                    new IgnoreNullValueTestCase
                    {
                        PropertyName = "CountryRegionNull",
                    },
                };

                Func <IgnoreNullValueTestCase, ReaderTestConfiguration, PayloadReaderTestDescriptor> createTestDescriptor =
                    (testCase, testConfig) =>
                {
                    EntityInstance payloadValue         = customerPayload.DeepCopy();
                    ComplexInstance payloadAddressValue = ((ComplexProperty)payloadValue.GetProperty("Address")).Value;
                    SetToNull(payloadAddressValue, testCase.PropertyName);

                    ComplexInstance resultValue = payloadValue;
                    if (testConfig.IsRequest && nullPropertyValueReaderBehavior == ODataNullValueBehaviorKind.IgnoreValue)
                    {
                        resultValue = customerPayload.DeepCopy();
                        ComplexInstance resultAddressValue = ((ComplexProperty)resultValue.GetProperty("Address")).Value;
                        resultAddressValue.Remove(resultAddressValue.GetProperty(testCase.PropertyName));
                    }

                    return(new PayloadReaderTestDescriptor(this.Settings)
                    {
                        PayloadElement = payloadValue,
                        PayloadEdmModel = edmModel,
                        ExpectedResultPayloadElement =
                            tc =>
                        {
                            if (tc.Format == ODataFormat.Json)
                            {
                                // under the client knob ODL will compute edit links, ids, etc
                                // so we need to update the expected payload
                                if (tc.RunBehaviorKind == TestODataBehaviorKind.WcfDataServicesClient)
                                {
                                    var entity = resultValue as EntityInstance;
                                    if (entity != null)
                                    {
                                        if (!tc.IsRequest)
                                        {
                                            entity.Id = "http://odata.org/test/Customer(1)";
                                            entity.EditLink = "http://odata.org/test/Customer(1)";
                                            entity.WithSelfLink("http://odata.org/test/Customer(1)");
                                        }
                                    }
                                }

                                var tempDescriptor = new PayloadReaderTestDescriptor(this.Settings)
                                {
                                    PayloadElement = resultValue,
                                    PayloadEdmModel = edmModel,
                                };

                                JsonLightPayloadElementFixup.Fixup(tempDescriptor);
                                return tempDescriptor.PayloadElement;
                            }

                            return resultValue;
                        },
                        ExpectedException = (testConfig.IsRequest && nullPropertyValueReaderBehavior != ODataNullValueBehaviorKind.Default) ? null : testCase.ExpectedResponseException
                    });
                };

                this.CombinatorialEngineProvider.RunCombinations(
                    testCases,
                    this.ReaderTestConfigurationProvider.ExplicitFormatConfigurations,
                    (testCase, testConfiguration) =>
                {
                    testConfiguration = testConfiguration.CloneAndApplyBehavior(behaviorKind);
                    testConfiguration.MessageReaderSettings.BaseUri = null;

                    PayloadReaderTestDescriptor testDescriptor = createTestDescriptor(testCase, testConfiguration);
                    testDescriptor.RunTest(testConfiguration);
                });
            });
        }