public void GetEdmType_Returns_EdmTypeInitializedByCtor()
        {
            IEdmTypeReference elementType = new EdmComplexTypeReference(new EdmComplexType("NS", "Complex"), isNullable: false);
            IEdmCollectionTypeReference collectionType = new EdmCollectionTypeReference(new EdmCollectionType(elementType), isNullable: false);

            var edmObject = new EdmComplexObjectCollection(collectionType);
            Assert.Same(collectionType, edmObject.GetEdmType());
        }
        public void TryGetValue_ThrowsInvalidOperation_EdmComplexObjectNullRef()
        {
            IEdmComplexTypeReference edmType = new EdmComplexTypeReference(new EdmComplexType("NS", "ComplexType"), isNullable: true);
            NullEdmComplexObject nullComplexObject = new NullEdmComplexObject(edmType);
            object propertyValue;

            Assert.Throws<InvalidOperationException>(() => nullComplexObject.TryGetPropertyValue("property", out propertyValue),
                "Cannot get property 'property' of a null EDM object of type '[NS.ComplexType Nullable=True]'.");
        }
        public void GetEdmType_Returns_CtorInitializedValue()
        {
            IEdmComplexTypeReference edmType = new EdmComplexTypeReference(new EdmComplexType("NS", "ComplexType"), isNullable: true);
            NullEdmComplexObject nullComplexObject = new NullEdmComplexObject(edmType);

            IEdmTypeReference result = nullComplexObject.GetEdmType();

            Assert.Same(edmType, result);
        }
        private static object ConvertComplexValue(ODataComplexValue complexValue, ref IEdmTypeReference propertyType,
            ODataDeserializerProvider deserializerProvider, ODataDeserializerContext readContext)
        {
            IEdmComplexTypeReference edmComplexType;
            if (propertyType == null)
            {
                // open complex property
                Contract.Assert(!String.IsNullOrEmpty(complexValue.TypeName),
                    "ODataLib should have verified that open complex value has a type name since we provided metadata.");
                IEdmModel model = readContext.Model;
                IEdmType edmType = model.FindType(complexValue.TypeName);
                Contract.Assert(edmType.TypeKind == EdmTypeKind.Complex, "ODataLib should have verified that complex value has a complex resource type.");
                edmComplexType = new EdmComplexTypeReference(edmType as IEdmComplexType, isNullable: true);
            }
            else
            {
                edmComplexType = propertyType.AsComplex();
            }

            ODataEdmTypeDeserializer deserializer = deserializerProvider.GetEdmTypeDeserializer(edmComplexType);
            return deserializer.ReadInline(complexValue, propertyType, readContext);
        }
        public void GetDefaultValue_NonNullableComplex()
        {
            IEdmTypeReference nonNullableComplexType = new EdmComplexTypeReference(new EdmComplexType("NS", "Complex"), isNullable: false);

            var result = EdmStructuredObject.GetDefaultValue(nonNullableComplexType);

            var complexObject = Assert.IsType<EdmComplexObject>(result);
            Assert.Equal(nonNullableComplexType, complexObject.GetEdmType(), new EdmTypeReferenceEqualityComparer());
        }
        public void GetDefaultValue_NonNullableComplexCollection()
        {
            IEdmTypeReference elementType = new EdmComplexTypeReference(new EdmComplexType("NS", "Complex"), isNullable: true);
            IEdmCollectionTypeReference complexCollectionType = new EdmCollectionTypeReference(new EdmCollectionType(elementType), isNullable: false);

            var result = EdmStructuredObject.GetDefaultValue(complexCollectionType);

            var complexCollectionObject = Assert.IsType<EdmComplexObjectCollection>(result);
            Assert.Equal(complexCollectionType, complexCollectionObject.GetEdmType(), new EdmTypeReferenceEqualityComparer());
        }
Esempio n. 7
0
        private static IEdmModel BuildModel(JObject viewObject, out IList<string> fieldsToIgnore)
        {
            var model = new EdmModel();
            fieldsToIgnore = new List<string>();

            var name = viewObject.PrimitivePropertyValue<string>("name");
            name = name.Replace(' ', '_');

            var entityType = new EdmEntityType(false, false, null, "OData4Socrata", name,
                                               Enumerable.Empty<IEdmStructuralProperty>());
            var entitySet = new EdmEntitySet(name, entityType);

            EdmComplexType phoneComplex = null;
            EdmComplexType locationComplex = null;
            EdmComplexType urlComplex = null;

            foreach (var column in viewObject.ArrayPropertyValue<JObject>("columns"))
            {
                var fieldName = column.PrimitivePropertyValue<string>("name");

                var sodaType = column.PrimitivePropertyValue<string>("dataTypeName");
                IEdmTypeReference typeReference;
                switch (sodaType)
                {
                    case "meta_data":
                        fieldsToIgnore.Add(fieldName);
                        continue;

                    case "text":

                        typeReference = EdmLibraryExtensions.GetPrimitiveTypeReference(typeof (string));
                        break;

                    case "url":
                        if (urlComplex == null)
                        {
                            urlComplex = new EdmComplexType(false, false, null, entityType.Namespace, "url");
                            new EdmStructuralProperty(urlComplex, "url", EdmLibraryExtensions.GetPrimitiveTypeReference(typeof (string)),
                                                      null, EdmConcurrencyMode.None);
                            model.AddElement(urlComplex);
                        }

                        typeReference = new EdmComplexTypeReference(urlComplex, false);
                        break;

                    case "phone":
                        if (phoneComplex == null)
                        {
                            phoneComplex = new EdmComplexType(false, false, null, entityType.Namespace, "phone");
                            new EdmStructuralProperty(phoneComplex, "phone_number",
                                                      EdmLibraryExtensions.GetPrimitiveTypeReference(typeof (string)), null,
                                                      EdmConcurrencyMode.None);
                            new EdmStructuralProperty(phoneComplex, "phone_type",
                                                      EdmLibraryExtensions.GetPrimitiveTypeReference(typeof (string)), null,
                                                      EdmConcurrencyMode.None);
                            model.AddElement(phoneComplex);
                        }

                        typeReference = new EdmComplexTypeReference(phoneComplex, false);
                        break;

                    case "location":
                        if (locationComplex == null)
                        {
                            locationComplex = new EdmComplexType(false, false, null, entityType.Namespace, "location");
                            new EdmStructuralProperty(locationComplex, "human_address",
                                                      EdmLibraryExtensions.GetPrimitiveTypeReference(typeof (string)), null,
                                                      EdmConcurrencyMode.None);
                            new EdmStructuralProperty(locationComplex, "latitude",
                                                      EdmLibraryExtensions.GetPrimitiveTypeReference(typeof (string)), null,
                                                      EdmConcurrencyMode.None);
                            new EdmStructuralProperty(locationComplex, "longitude",
                                                      EdmLibraryExtensions.GetPrimitiveTypeReference(typeof (string)), null,
                                                      EdmConcurrencyMode.None);
                            new EdmStructuralProperty(locationComplex, "machine_address",
                                                      EdmLibraryExtensions.GetPrimitiveTypeReference(typeof (string)), null,
                                                      EdmConcurrencyMode.None);
                            new EdmStructuralProperty(locationComplex, "needs_recoding",
                                                      EdmLibraryExtensions.GetPrimitiveTypeReference(typeof (bool)), null,
                                                      EdmConcurrencyMode.None);
                            model.AddElement(locationComplex);
                        }

                        typeReference = new EdmComplexTypeReference(locationComplex, false);
                        break;

                    default:
                        throw new Exception();
                }

                new EdmStructuralProperty(entityType, fieldName, typeReference, null, EdmConcurrencyMode.None);
            }

            model.AddElement(entityType);

            var entityContainer = new EdmEntityContainer();
            model.AddEntityContainer(entityContainer);
            entityContainer.AddElement(entitySet);

            return model;
        }
        public void CreateODataCollectionValue_CanSerialize_IEdmObjects()
        {
            // Arrange
            IEdmComplexObject[] collection = new IEdmComplexObject[] { new Mock<IEdmComplexObject>().Object };
            ODataSerializerContext serializerContext = new ODataSerializerContext();
            IEdmComplexTypeReference elementType = new EdmComplexTypeReference(new EdmComplexType("NS", "ComplexType"), isNullable: true);
            IEdmCollectionTypeReference collectionType = new EdmCollectionTypeReference(new EdmCollectionType(elementType), isNullable: false);

            Mock<ODataSerializerProvider> serializerProvider = new Mock<ODataSerializerProvider>();
            Mock<ODataComplexTypeSerializer> elementSerializer = new Mock<ODataComplexTypeSerializer>(MockBehavior.Strict, serializerProvider.Object);
            serializerProvider.Setup(s => s.GetEdmTypeSerializer(elementType)).Returns(elementSerializer.Object);
            elementSerializer.Setup(s => s.CreateODataComplexValue(collection[0], elementType, serializerContext)).Returns(new ODataComplexValue()).Verifiable();

            ODataCollectionSerializer serializer = new ODataCollectionSerializer(serializerProvider.Object);

            // Act
            var result = serializer.CreateODataCollectionValue(collection, elementType, serializerContext);

            // Assert
            elementSerializer.Verify();
        }
        private void CreateStructuralTypeBody(EdmStructuredType type, IStructuralTypeConfiguration config)
        {
            foreach (PropertyConfiguration property in config.Properties)
            {
                switch (property.Kind)
                {
                    case PropertyKind.Primitive:
                        PrimitivePropertyConfiguration primitiveProperty = property as PrimitivePropertyConfiguration;
                        type.AddStructuralProperty(
                            primitiveProperty.PropertyInfo.Name,
                            GetTypeKind(primitiveProperty.PropertyInfo.PropertyType),
                            primitiveProperty.OptionalProperty);
                        break;

                    case PropertyKind.Complex:
                        ComplexPropertyConfiguration complexProperty = property as ComplexPropertyConfiguration;
                        IEdmComplexType complexType = _types[complexProperty.RelatedClrType] as IEdmComplexType;

                        type.AddStructuralProperty(
                            complexProperty.PropertyInfo.Name,
                            new EdmComplexTypeReference(complexType, complexProperty.OptionalProperty));
                        break;

                    case PropertyKind.Collection:
                        CollectionPropertyConfiguration collectionProperty = property as CollectionPropertyConfiguration;
                        IEdmTypeReference elementTypeReference = null;
                        if (_types.ContainsKey(collectionProperty.ElementType))
                        {
                            IEdmComplexType elementType = _types[collectionProperty.ElementType] as IEdmComplexType;
                            elementTypeReference = new EdmComplexTypeReference(elementType, false);
                        }
                        else
                        {
                            elementTypeReference = EdmLibHelpers.GetEdmPrimitiveTypeReferenceOrNull(collectionProperty.ElementType);
                        }
                        type.AddStructuralProperty(
                            collectionProperty.PropertyInfo.Name,
                            new EdmCollectionTypeReference(
                                new EdmCollectionType(elementTypeReference),
                                collectionProperty.OptionalProperty));
                        break;

                    default:
                        break;
                }
            }
        }
        private void CreateStructuralTypeBody(EdmStructuredType type, StructuralTypeConfiguration config)
        {
            foreach (PropertyConfiguration property in config.Properties)
            {
                switch (property.Kind)
                {
                    case PropertyKind.Primitive:
                        PrimitivePropertyConfiguration primitiveProperty = property as PrimitivePropertyConfiguration;

                        EdmPrimitiveTypeKind typeKind = GetTypeKind(primitiveProperty.PropertyInfo.PropertyType);
                        IEdmTypeReference primitiveTypeReference = EdmCoreModel.Instance.GetPrimitive(
                            typeKind,
                            primitiveProperty.OptionalProperty);

                        var primitiveProp = new EdmStructuralProperty(
                            type,
                            primitiveProperty.PropertyInfo.Name,
                            primitiveTypeReference);

                        type.AddProperty(primitiveProp);

                        // Set Annotation StoreGeneratedPattern
                        if (config.Kind == EdmTypeKind.Entity
                            && primitiveProperty.StoreGeneratedPattern != DatabaseGeneratedOption.None)
                        {
                            _directValueAnnotations.Add(
                                new StoreGeneratedPatternAnnotation(primitiveProp, primitiveProperty.StoreGeneratedPattern));
                        }
                        break;

                    case PropertyKind.Complex:
                        ComplexPropertyConfiguration complexProperty = property as ComplexPropertyConfiguration;
                        IEdmComplexType complexType = _types[complexProperty.RelatedClrType] as IEdmComplexType;

                        type.AddStructuralProperty(
                            complexProperty.PropertyInfo.Name,
                            new EdmComplexTypeReference(complexType, complexProperty.OptionalProperty));
                        break;

                    case PropertyKind.Collection:
                        CollectionPropertyConfiguration collectionProperty = property as CollectionPropertyConfiguration;
                        IEdmTypeReference elementTypeReference = null;
                        if (_types.ContainsKey(collectionProperty.ElementType))
                        {
                            IEdmComplexType elementType = _types[collectionProperty.ElementType] as IEdmComplexType;
                            elementTypeReference = new EdmComplexTypeReference(elementType, false);
                        }
                        else
                        {
                            elementTypeReference = EdmLibHelpers.GetEdmPrimitiveTypeReferenceOrNull(collectionProperty.ElementType);
                        }
                        type.AddStructuralProperty(
                            collectionProperty.PropertyInfo.Name,
                            new EdmCollectionTypeReference(
                                new EdmCollectionType(elementTypeReference),
                                collectionProperty.OptionalProperty));
                        break;

                    default:
                        break;
                }
            }
        }
        public void CreateODataComplexValue_Understands_IEdmComplexTypeObject()
        {
            // Arrange
            EdmComplexType complexEdmType = new EdmComplexType("NS", "ComplexType");
            complexEdmType.AddStructuralProperty("Property", EdmPrimitiveTypeKind.Int32);
            IEdmComplexTypeReference edmTypeReference = new EdmComplexTypeReference(complexEdmType, isNullable: false);

            TypedEdmComplexObject edmObject = new TypedEdmComplexObject(new { Property = 42 }, edmTypeReference);
            ODataComplexTypeSerializer serializer = new ODataComplexTypeSerializer(new DefaultODataSerializerProvider());

            // Act
            ODataComplexValue result = serializer.CreateODataComplexValue(edmObject, edmTypeReference, new ODataSerializerContext());

            // Assert
            Assert.Equal("Property", result.Properties.Single().Name);
            Assert.Equal(42, result.Properties.Single().Value);
        }
        public void CreateODataComplexValue_ReturnsNull_ForNullEdmComplexObject()
        {
            IEdmComplexTypeReference edmType = new EdmComplexTypeReference(_addressType, isNullable: true);
            ODataComplexTypeSerializer serializer = new ODataComplexTypeSerializer(new DefaultODataSerializerProvider());

            var result = serializer.CreateODataComplexValue(new NullEdmComplexObject(edmType), edmType, new ODataSerializerContext());

            Assert.Null(result);
        }