コード例 #1
0
        public void Ctor_Throws_TypeMustBeEntityCollection()
        {
            EdmComplexType complexType = new EdmComplexType("namespace", "name");
            EdmCollectionType collectionType = new EdmCollectionType(new EdmComplexTypeReference(complexType, isNullable: true));

            Assert.Throws<NotSupportedException>(
                () => new ODataFeedSerializer(new EdmCollectionTypeReference(collectionType, isNullable: false), new DefaultODataSerializerProvider()),
                "namespace.name is not a collection of type IEdmEntityType. Only entity collections are supported.");
        }
コード例 #2
0
        public static IEdmModel SimpleCustomerOrderModel()
        {
            var model = new EdmModel();
            var customerType = new EdmEntityType("Default", "Customer");
            customerType.AddStructuralProperty("ID", EdmPrimitiveTypeKind.Int32);
            customerType.AddStructuralProperty("FirstName", EdmPrimitiveTypeKind.String);
            customerType.AddStructuralProperty("LastName", EdmPrimitiveTypeKind.String);
            IEdmTypeReference primitiveTypeReference = EdmCoreModel.Instance
                .GetPrimitive(EdmPrimitiveTypeKind.String, isNullable: true);
            customerType.AddStructuralProperty("City", primitiveTypeReference, defaultValue: null,
                concurrencyMode: EdmConcurrencyMode.Fixed);
            model.AddElement(customerType);

            var orderType = new EdmEntityType("Default", "Order");
            orderType.AddStructuralProperty("ID", EdmPrimitiveTypeKind.Int32);
            orderType.AddStructuralProperty("Name", EdmPrimitiveTypeKind.String);
            orderType.AddStructuralProperty("Shipment", EdmPrimitiveTypeKind.String);
            model.AddElement(orderType);

            var addressType = new EdmComplexType("Default", "Address");
            addressType.AddStructuralProperty("Street", EdmPrimitiveTypeKind.String);
            addressType.AddStructuralProperty("City", EdmPrimitiveTypeKind.String);
            addressType.AddStructuralProperty("State", EdmPrimitiveTypeKind.String);
            addressType.AddStructuralProperty("Country", EdmPrimitiveTypeKind.String);
            addressType.AddStructuralProperty("ZipCode", EdmPrimitiveTypeKind.String);
            model.AddElement(addressType);

            // Add navigations
            customerType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo()
            {
                Name = "Orders",
                Target = orderType,
                TargetMultiplicity = EdmMultiplicity.Many
            });
            orderType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo()
            {
                Name = "Customer",
                Target = customerType,
                TargetMultiplicity = EdmMultiplicity.One
            });

            var container = new EdmEntityContainer("Default", "Container");
            var customerSet = container.AddEntitySet("Customers", customerType);
            var orderSet = container.AddEntitySet("Orders", orderType);
            customerSet.AddNavigationTarget(customerType.NavigationProperties().Single(np => np.Name == "Orders"), orderSet);
            orderSet.AddNavigationTarget(orderType.NavigationProperties().Single(np => np.Name == "Customer"), customerSet);

            EntitySetLinkBuilderAnnotation linkAnnotation = new MockEntitySetLinkBuilderAnnotation();
            model.SetEntitySetLinkBuilder(customerSet, linkAnnotation);
            model.SetEntitySetLinkBuilder(orderSet, linkAnnotation);

            model.AddElement(container);
            return model;
        }
コード例 #3
0
        public static IEdmModel SimpleCustomerOrderModel()
        {
            var model = new EdmModel();
            var customerType = new EdmEntityType("Default", "Customer");
            customerType.AddStructuralProperty("ID", EdmPrimitiveTypeKind.Int32);
            customerType.AddStructuralProperty("FirstName", EdmPrimitiveTypeKind.String);
            customerType.AddStructuralProperty("LastName", EdmPrimitiveTypeKind.String);
            model.AddElement(customerType);

            var orderType = new EdmEntityType("Default", "Order");
            orderType.AddStructuralProperty("ID", EdmPrimitiveTypeKind.Int32);
            orderType.AddStructuralProperty("Name", EdmPrimitiveTypeKind.String);
            model.AddElement(orderType);

            var addressType = new EdmComplexType("Default", "Address");
            addressType.AddStructuralProperty("Street", EdmPrimitiveTypeKind.String);
            addressType.AddStructuralProperty("City", EdmPrimitiveTypeKind.String);
            addressType.AddStructuralProperty("State", EdmPrimitiveTypeKind.String);
            addressType.AddStructuralProperty("Country", EdmPrimitiveTypeKind.String);
            addressType.AddStructuralProperty("ZipCode", EdmPrimitiveTypeKind.String);
            model.AddElement(addressType);

            // Add navigations
            customerType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo() { Name = "Orders", Target = orderType, TargetMultiplicity = EdmMultiplicity.Many });
            orderType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo() { Name = "Customer", Target = customerType, TargetMultiplicity = EdmMultiplicity.One });

            var container = new EdmEntityContainer("Default", "Container");
            var customerSet = container.AddEntitySet("Customers", customerType);
            var orderSet = container.AddEntitySet("Orders", orderType);
            customerSet.AddNavigationTarget(customerType.NavigationProperties().Single(np => np.Name == "Orders"), orderSet);
            orderSet.AddNavigationTarget(orderType.NavigationProperties().Single(np => np.Name == "Customer"), customerSet);

            Mock<IEntitySetLinkBuilder> linkAnnotation = new Mock<IEntitySetLinkBuilder>();
            model.SetEntitySetLinkBuilderAnnotation(customerSet, linkAnnotation.Object);
            model.SetEntitySetLinkBuilderAnnotation(orderSet, linkAnnotation.Object);

            model.AddElement(container);
            return model;
        }
コード例 #4
0
        public void GetEdmType_AgreesWithPropertyIsNullable()
        {
            var complexType = new EdmComplexType("NS", "Complex");
            var edmObject = new TestEdmStructuredObject(complexType);
            edmObject.IsNullable = true;

            Assert.True(edmObject.GetEdmType().IsNullable);
        }
コード例 #5
0
        public void GetEdmType_HasSameDefinition_AsInitializedEdmType()
        {
            var complexType = new EdmComplexType("NS", "Complex");
            var edmObject = new TestEdmStructuredObject(complexType);

            Assert.Equal(complexType, edmObject.GetEdmType().Definition);
        }
コード例 #6
0
        public void TryGetPropertyType_ReturnsFalse_IfPropertyDoesNotExist()
        {
            EdmComplexType edmType = new EdmComplexType("NS", "Complex");
            var edmObject = new TestEdmStructuredObject(edmType);

            Type propertyType;
            bool result = edmObject.TryGetPropertyType("NotPresentProperty", out propertyType);

            Assert.False(result);
        }
コード例 #7
0
        public void TryGetPropertyType_ReturnsTrue_IfPropertyExists()
        {
            EdmComplexType edmType = new EdmComplexType("NS", "Complex");
            edmType.AddStructuralProperty("Property", EdmPrimitiveTypeKind.Int32);
            var edmObject = new TestEdmStructuredObject(edmType);

            Type propertyType;
            bool result = edmObject.TryGetPropertyType("Property", out propertyType);

            Assert.True(result);
        }
コード例 #8
0
        public void TryGetPropertyValue_Without_TrySetPropertyValue_ReturnsDefault()
        {
            string propertyName = "Property";
            EdmComplexType edmType = new EdmComplexType("NS", "Complex");
            edmType.AddStructuralProperty(propertyName, EdmPrimitiveTypeKind.Int32, isNullable: false);
            var edmObject = new TestEdmStructuredObject(edmType);

            object result;
            edmObject.TryGetPropertyValue(propertyName, out result);

            Assert.Equal(0, result);
        }
コード例 #9
0
        public void TryGetPropertyValue_After_TrySetPropertyValue()
        {
            string propertyName = "Property";
            EdmComplexType edmType = new EdmComplexType("NS", "Complex");
            edmType.AddStructuralProperty(propertyName, EdmPrimitiveTypeKind.Int32);
            var edmObject = new TestEdmStructuredObject(edmType);
            object propertyValue = new object();

            object result;
            edmObject.TrySetPropertyValue(propertyName, propertyValue);
            edmObject.TryGetPropertyValue(propertyName, out result);

            Assert.Same(propertyValue, result);
        }
コード例 #10
0
        public void TrySetPropertyValue_IfPropertyDoesNotExist_DoesNotUpdateGetChangedPropertyNames()
        {
            EdmComplexType edmType = new EdmComplexType("NS", "Complex");
            var edmObject = new TestEdmStructuredObject(edmType);

            edmObject.TrySetPropertyValue("NotPresentProperty", 42);

            Assert.DoesNotContain("Property", edmObject.GetChangedPropertyNames());
        }
コード例 #11
0
        public void TrySetPropertyValue_IfPropertyExists_UpdatesGetChangedPropertyNames()
        {
            EdmComplexType edmType = new EdmComplexType("NS", "Complex");
            edmType.AddStructuralProperty("Property", EdmPrimitiveTypeKind.Int32);
            var edmObject = new TestEdmStructuredObject(edmType);
            edmObject.TrySetPropertyValue("Property", 42);

            Assert.Contains("Property", edmObject.GetChangedPropertyNames());
        }
コード例 #12
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;
        }
コード例 #13
0
 private void CreateComplexTypeBody(EdmComplexType type, IComplexTypeConfiguration config)
 {
     CreateStructuralTypeBody(type, config);
 }
        public CustomersModelWithInheritance()
        {
            EdmModel model = new EdmModel();

            // complex type address
            EdmComplexType address = new EdmComplexType("NS", "Address");
            address.AddStructuralProperty("Street", EdmPrimitiveTypeKind.String);
            address.AddStructuralProperty("City", EdmPrimitiveTypeKind.String);
            address.AddStructuralProperty("State", EdmPrimitiveTypeKind.String);
            address.AddStructuralProperty("ZipCode", EdmPrimitiveTypeKind.String);
            address.AddStructuralProperty("Country", EdmPrimitiveTypeKind.String);
            model.AddElement(address);

            // entity type customer
            EdmEntityType customer = new EdmEntityType("NS", "Customer");
            customer.AddKeys(customer.AddStructuralProperty("ID", EdmPrimitiveTypeKind.Int32));
            customer.AddStructuralProperty("Name", EdmPrimitiveTypeKind.String);
            customer.AddStructuralProperty("Address", new EdmComplexTypeReference(address, isNullable: true));
            model.AddElement(customer);

            // derived entity type special customer
            EdmEntityType specialCustomer = new EdmEntityType("NS", "SpecialCustomer", customer);
            specialCustomer.AddStructuralProperty("SpecialCustomerProperty", EdmPrimitiveTypeKind.Guid);
            model.AddElement(specialCustomer);

            // entity type order
            EdmEntityType order = new EdmEntityType("NS", "Order");
            order.AddKeys(order.AddStructuralProperty("ID", EdmPrimitiveTypeKind.Int32));
            order.AddStructuralProperty("Amount", EdmPrimitiveTypeKind.Int32);
            model.AddElement(order);

            // derived entity type special order
            EdmEntityType specialOrder = new EdmEntityType("NS", "SpecialOrder", order);
            specialOrder.AddStructuralProperty("SpecialOrderProperty", EdmPrimitiveTypeKind.Guid);
            model.AddElement(specialOrder);

            // entity sets
            EdmEntityContainer container = new EdmEntityContainer("NS", "ModelWithInheritance");
            model.AddElement(container);
            EdmEntitySet customers = container.AddEntitySet("Customers", customer);
            EdmEntitySet orders = container.AddEntitySet("Orders", order);

            // actions
            EdmFunctionImport upgradeCustomer = container.AddFunctionImport(
                "upgrade", returnType: null, entitySet: new EdmEntitySetReferenceExpression(customers), sideEffecting: true, composable: false, bindable: true);
            upgradeCustomer.AddParameter("entity", new EdmEntityTypeReference(customer, false));
            EdmFunctionImport upgradeSpecialCustomer = container.AddFunctionImport(
                "specialUpgrade", returnType: null, entitySet: new EdmEntitySetReferenceExpression(customers), sideEffecting: true, composable: false, bindable: true);
            upgradeSpecialCustomer.AddParameter("entity", new EdmEntityTypeReference(specialCustomer, false));

            // navigation properties
            customers.AddNavigationTarget(
                customer.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo
                {
                    Name = "Orders",
                    TargetMultiplicity = EdmMultiplicity.Many,
                    Target = order
                }),
                orders);
            orders.AddNavigationTarget(
                 order.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo
                 {
                     Name = "Customer",
                     TargetMultiplicity = EdmMultiplicity.ZeroOrOne,
                     Target = customer
                 }),
                customers);

            // navigation properties on derived types.
            customers.AddNavigationTarget(
                specialCustomer.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo
                {
                    Name = "SpecialOrders",
                    TargetMultiplicity = EdmMultiplicity.Many,
                    Target = order
                }),
                orders);
            orders.AddNavigationTarget(
                 specialOrder.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo
                 {
                     Name = "SpecialCustomer",
                     TargetMultiplicity = EdmMultiplicity.ZeroOrOne,
                     Target = customer
                 }),
                customers);
            model.SetAnnotationValue<BindableProcedureFinder>(model, new BindableProcedureFinder(model));

            // set properties
            Model = model;
            Container = container;
            Customer = customer;
            Order = order;
            Address = address;
            SpecialCustomer = specialCustomer;
            SpecialOrder = specialOrder;
            Orders = orders;
            Customers = customers;
            UpgradeCustomer = upgradeCustomer;
        }
コード例 #15
0
        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);
        }