Ejemplo n.º 1
0
        public void ComplexTypeShouldBeEscaped()
        {
            var edmType   = ComplexType.Create("string", "Transient", DataSpace.CSSpace, Enumerable.Empty <EdmMember>(), null);
            var typeUsage = TypeUsage.Create(edmType, Enumerable.Empty <Facet>());

            this.escaper.Escape(typeUsage).Should().Be("@string");
        }
        public void Crate_creates_store_function_for_complex_type_function_import()
        {
            var model = new DbModelBuilder()
                        .Build(new DbProviderInfo("System.Data.SqlClient", "2012"));

            var complexType = ComplexType.Create("CT", "ns", DataSpace.CSpace,
                                                 new[]
            {
                EdmProperty.Create("Street",
                                   TypeUsage.CreateDefaultTypeUsage(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String))),
                EdmProperty.Create("ZipCode",
                                   TypeUsage.CreateDefaultTypeUsage(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32))),
            },
                                                 null);

            var functionDescriptor =
                new FunctionDescriptor(
                    "f",
                    new[]
                    { new ParameterDescriptor("p1", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String), null, false) },
                    new EdmType[] { complexType },
                    "ResultCol",
                    "dbo",
                    StoreFunctionKind.StoredProcedure);

            var storeFunction = new StoreFunctionBuilder(model, "docs", "ns").Create(functionDescriptor);

            Assert.Null(storeFunction.ReturnParameter);

            Assert.Equal(1, storeFunction.Parameters.Count);
            Assert.Equal("p1", storeFunction.Parameters[0].Name);
            Assert.Equal("nvarchar(max)", storeFunction.Parameters[0].TypeName);
            Assert.Equal(ParameterMode.In, storeFunction.Parameters[0].Mode);
            Assert.False(storeFunction.IsComposableAttribute);
        }
Ejemplo n.º 3
0
        public void Can_create_mapping_and_get_property()
        {
            var complexType = ComplexType.Create("CT", "NS", DataSpace.CSpace, new EdmMember[0], null);
            var property    = EdmProperty.CreateComplex("P", complexType);
            var mapping     = new ComplexPropertyMapping(property);

            Assert.Same(property, mapping.Property);
        }
            private static ComplexType CreateComplexType()
            {
                var prop1 = EdmProperty.Create(
                    "Prop1",
                    TypeUsage.CreateDefaultTypeUsage(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32)));
                var prop2 = EdmProperty.Create(
                    "Prop2",
                    TypeUsage.CreateDefaultTypeUsage(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)));

                return
                    (ComplexType.Create("TestComplexType", "Model", DataSpace.CSpace, new[] { prop1, prop2 }, null));
            }
        public async Task WriteToStreamAsync_WritesComplexType()
        {
            // Arrange
            var input = ComplexType.Create();

            // Act
            await _formatter.WriteToStreamAsync(typeof(ComplexType), input, _content.Stream, _content, _context);

            // Assert
            var result = await _content.ReadObjectAsync <ComplexType>();

            Assert.NotEqual(0, _content.Headers.ContentLength);
            result.Verify();
        }
        public async Task WriteToStreamAsync_WritesComplexType()
        {
            // Arrange
            var input   = ComplexType.Create();
            var content = new StreamMessagePackHttpContent();

            // Act
            await _formatter.WriteToStreamAsync(typeof(ComplexType), input, content.Stream, content, _context);

            // Assert
            var result = await content.ReadObjectAsync <ComplexType>(_serializerOptions);

            result.Verify();
        }
Ejemplo n.º 7
0
        public void SetReadOnly_is_called_on_child_mapping_items()
        {
            var complexType = ComplexType.Create("CT", "NS", DataSpace.CSpace, new EdmMember[0], null);
            var property    = EdmProperty.CreateComplex("P", complexType);
            var mapping     = new ComplexPropertyMapping(property);

            var typeMapping = new ComplexTypeMapping(isPartial: false);

            mapping.AddTypeMapping(typeMapping);

            Assert.False(typeMapping.IsReadOnly);
            mapping.SetReadOnly();
            Assert.True(typeMapping.IsReadOnly);
        }
        public async Task ReadFromStreamAsync_ReadsComplexTypes()
        {
            // Arrange
            var input = ComplexType.Create();
            await _content.WriteObjectAsync(input);

            // Act
            var result = await _formatter.ReadFromStreamAsync(typeof(ComplexType), _content.Stream, _content, _logger);

            // Assert
            Assert.NotNull(result);
            var model = Assert.IsType <ComplexType>(result);

            model.Verify();
        }
        public void SerializationWorks_ComplexType(IByteConverter <ComplexType> byteConverter)
        {
            var complexType  = ComplexType.Create(3, 3);
            var serialized   = byteConverter.GetBytes(complexType);
            var deserialized = byteConverter.GetObject(serialized);

            var expected = complexType.BreadthFirstEnumeration().ToArray();
            var actual   = deserialized.BreadthFirstEnumeration().ToArray();

            for (var i = 0; i < expected.Length; i++)
            {
                var e = expected[i];
                var a = actual[i];
                Assert.Equal(e.Id, a.Id);
            }
        }
Ejemplo n.º 10
0
        public void Cannot_add_type_mapping_when_read_only()
        {
            var complexType = ComplexType.Create("CT", "NS", DataSpace.CSpace, new EdmMember[0], null);
            var property    = EdmProperty.CreateComplex("P", complexType);
            var mapping     = new ComplexPropertyMapping(property);

            mapping.SetReadOnly();

            Assert.True(mapping.IsReadOnly);

            var typeMapping = new ComplexTypeMapping(isPartial: false);

            Assert.Equal(
                Strings.OperationOnReadOnlyItem,
                Assert.Throws <InvalidOperationException>(
                    () => mapping.AddTypeMapping(typeMapping)).Message);
        }
Ejemplo n.º 11
0
        // internal for testing
        internal ComplexType CreateComplexTypeFromRowType(SimpleMappingContext mappingContext, RowType rowType, string typeName)
        {
            Debug.Assert(mappingContext != null, "mappingContext != null");
            Debug.Assert(!string.IsNullOrEmpty(typeName), "typeName cannot be null or empty string.");
            Debug.Assert(rowType != null, "rowType != null");

            var uniquePropertyNameService = new UniqueIdentifierService();

            uniquePropertyNameService.AdjustIdentifier(typeName);

            return
                (ComplexType.Create(
                     typeName,
                     _namespaceName,
                     DataSpace.CSpace,
                     rowType.Properties.Select(p => GenerateScalarProperty(mappingContext, p, uniquePropertyNameService)),
                     null));
        }
Ejemplo n.º 12
0
        public void Can_add_get_remove_type_mappings()
        {
            var complexType = ComplexType.Create("CT", "NS", DataSpace.CSpace, new EdmMember[0], null);
            var property    = EdmProperty.CreateComplex("P", complexType);
            var mapping     = new ComplexPropertyMapping(property);

            Assert.Equal(0, mapping.TypeMappings.Count);

            var typeMapping = new ComplexTypeMapping(isPartial: false);

            mapping.AddTypeMapping(typeMapping);

            Assert.Equal(1, mapping.TypeMappings.Count);
            Assert.Same(typeMapping, mapping.TypeMappings[0]);

            mapping.RemoveTypeMapping(typeMapping);

            Assert.Equal(0, mapping.TypeMappings.Count);
        }
Ejemplo n.º 13
0
        public void Crate_creates_store_function_for_complex_type_withEnum_in_TableValuedFunction()
        {
            var model = new DbModelBuilder()
                        .Build(new DbProviderInfo("System.Data.SqlClient", "2012"));

            var enumType = EnumType.Create("TestEnum", "TestNs", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32), false, new EnumMember[] { EnumMember.Create("foo", 1, null) }, null);

            var complexType = ComplexType.Create("CT", "ns", DataSpace.CSpace,
                                                 new[]
            {
                EdmProperty.Create("Street",
                                   TypeUsage.CreateDefaultTypeUsage(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String))),
                EdmProperty.Create("ZipCode",
                                   TypeUsage.CreateDefaultTypeUsage(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32))),
                EdmProperty.Create("MyEnum", TypeUsage.CreateDefaultTypeUsage(enumType))
            },
                                                 null);

            var functionDescriptor =
                new FunctionDescriptor(
                    "f",
                    new[]
                    { new ParameterDescriptor("p1", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String), null, false) },
                    new EdmType[] { complexType },
                    "ResultCol",
                    "dbo",
                    StoreFunctionKind.TableValuedFunction,
                    isBuiltIn: null,
                    isNiladic: null);

            var storeFunction = new StoreFunctionBuilder(model, "docs", "ns").Create(functionDescriptor);

            Assert.Equal(
                BuiltInTypeKind.CollectionType,
                storeFunction.ReturnParameter.TypeUsage.EdmType.BuiltInTypeKind);

            Assert.Single(storeFunction.Parameters);
            Assert.Equal("p1", storeFunction.Parameters[0].Name);
            Assert.Equal("nvarchar(max)", storeFunction.Parameters[0].TypeName);
            Assert.Equal(ParameterMode.In, storeFunction.Parameters[0].Mode);
            Assert.True(storeFunction.IsComposableAttribute);
        }
        public void Can_create_composable_function_import_with_complex_type_collection_result()
        {
            DbProviderManifest providerManifest;
            var containerMapping = GetContainerMapping(out providerManifest);

            var cTypeUsageString = TypeUsage.CreateDefaultTypeUsage(
                PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));
            var sTypeUsageString = providerManifest.GetStoreType(cTypeUsageString);

            var complexType = ComplexType.Create(
                "RT", "N", DataSpace.CSpace,
                new[]
            {
                EdmProperty.Create("P1", cTypeUsageString),
                EdmProperty.Create("P2", cTypeUsageString)
            },
                null);


            var rowType = RowType.Create(
                new[]
            {
                EdmProperty.Create("C1", sTypeUsageString),
                EdmProperty.Create("C2", sTypeUsageString)
            },
                null);

            var functionImport = EdmFunction.Create(
                "F", "N", DataSpace.CSpace,
                new EdmFunctionPayload
            {
                IsComposable     = true,
                ReturnParameters = new[]
                {
                    FunctionParameter.Create("R", complexType.GetCollectionType(), ParameterMode.ReturnValue)
                }
            },
                null);

            var targetFunction = EdmFunction.Create(
                "SF", "N", DataSpace.SSpace,
                new EdmFunctionPayload
            {
                IsComposable     = true,
                ReturnParameters = new[]
                {
                    FunctionParameter.Create("R", rowType.GetCollectionType(), ParameterMode.ReturnValue)
                }
            },
                null);

            var typeMapping = new FunctionImportComplexTypeMapping(
                complexType,
                new Collection <FunctionImportReturnTypePropertyMapping>()
            {
                new FunctionImportReturnTypeScalarPropertyMapping("P1", "C1"),
                new FunctionImportReturnTypeScalarPropertyMapping("P2", "C2"),
            });

            var resultMapping = new FunctionImportResultMapping();

            resultMapping.AddTypeMapping(typeMapping);

            var functionImportMapping = new FunctionImportMappingComposable(
                functionImport,
                targetFunction,
                resultMapping,
                containerMapping);

            Assert.Same(resultMapping, functionImportMapping.ResultMapping);
            Assert.Equal(1, functionImportMapping.StructuralTypeMappings.Count);
            Assert.Null(functionImportMapping.TvfKeys);
        }
Ejemplo n.º 15
0
        public void Build_builds_valid_DbDatabaseMapping_for_functions()
        {
            var rowTypeProperty = CreateStoreProperty("p1", "int");

            var complexTypeProperty =
                EdmProperty.Create(
                    "p2",
                    TypeUsage.CreateDefaultTypeUsage(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32)));

            var functionImportReturnComplexType =
                ComplexType.Create(
                    "CT",
                    "entityModel",
                    DataSpace.CSpace,
                    new[] { complexTypeProperty }, null);

            var storeFunction = EdmFunction.Create(
                "f_s",
                "storeModel",
                DataSpace.SSpace,
                new EdmFunctionPayload
            {
                IsComposable     = true,
                IsFunctionImport = false,
                ReturnParameters =
                    new[]
                {
                    FunctionParameter.Create(
                        "ReturnType",
                        RowType.Create(new[] { rowTypeProperty }, null).GetCollectionType(),
                        ParameterMode.ReturnValue)
                }
            },
                null);

            var functionImport =
                EdmFunction.Create(
                    "f_c",
                    "entityModel",
                    DataSpace.CSpace,
                    new EdmFunctionPayload
            {
                IsComposable     = true,
                IsFunctionImport = true,
                ReturnParameters =
                    new[]
                {
                    FunctionParameter.Create(
                        "ReturnType",
                        functionImportReturnComplexType.GetCollectionType(),
                        ParameterMode.ReturnValue)
                }
            },
                    null);

            var modelContainer = EntityContainer.Create("C_C", DataSpace.CSpace, new EntitySet[0], new[] { functionImport }, null);
            var storeContainer = EntityContainer.Create("C_S", DataSpace.SSpace, new EntitySet[0], null, null);

            var storeModel = EdmModel.CreateStoreModel(storeContainer, null, null);

            storeModel.AddItem(storeFunction);

            var mappingContext = new SimpleMappingContext(storeModel, true);

            mappingContext.AddMapping(rowTypeProperty, complexTypeProperty);
            mappingContext.AddMapping(storeFunction, functionImport);
            mappingContext.AddMapping(storeContainer, modelContainer);

            var entityModel = DbDatabaseMappingBuilder.Build(mappingContext).ConceptualModel;

            Assert.NotNull(entityModel);
            Assert.Equal(new[] { "f_c" }, entityModel.Containers.Single().FunctionImports.Select(f => f.Name));
            Assert.Equal(new[] { "CT" }, entityModel.ComplexTypes.Select(t => t.Name));
        }
Ejemplo n.º 16
0
        public void BuildComposableFunctionMapping_creates_valid_function_mapping()
        {
            var rowTypeProperty = CreateStoreProperty("p1", "int");

            var complexTypeProperty =
                EdmProperty.Create(
                    "p2",
                    TypeUsage.CreateDefaultTypeUsage(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32)));

            var functionImportReturnComplexType =
                ComplexType.Create(
                    "c",
                    "entityModel",
                    DataSpace.CSpace,
                    new[] { complexTypeProperty }, null);

            var storeFunction = EdmFunction.Create(
                "f_s",
                "storeModel",
                DataSpace.SSpace,
                new EdmFunctionPayload
            {
                IsComposable     = true,
                IsFunctionImport = false,
                ReturnParameters =
                    new[]
                {
                    FunctionParameter.Create(
                        "ReturnType",
                        RowType.Create(new[] { rowTypeProperty }, null).GetCollectionType(),
                        ParameterMode.ReturnValue)
                }
            },
                null);

            var functionImport =
                EdmFunction.Create(
                    "f_c",
                    "entityModel",
                    DataSpace.CSpace,
                    new EdmFunctionPayload
            {
                IsComposable     = true,
                IsFunctionImport = false,
                ReturnParameters =
                    new[]
                {
                    FunctionParameter.Create(
                        "ReturnType",
                        functionImportReturnComplexType.GetCollectionType(),
                        ParameterMode.ReturnValue)
                }
            },
                    null);

            var mappingContext = new SimpleMappingContext(new EdmModel(DataSpace.SSpace), true);

            mappingContext.AddMapping(rowTypeProperty, complexTypeProperty);
            mappingContext.AddMapping(storeFunction, functionImport);

            var functionImportMapping =
                DbDatabaseMappingBuilder.BuildComposableFunctionMapping(storeFunction, mappingContext);

            Assert.NotNull(functionImportMapping);
            Assert.Same(storeFunction, functionImportMapping.TargetFunction);
            Assert.Same(functionImport, functionImportMapping.FunctionImport);

            var structuralTypeMappings = functionImportMapping.StructuralTypeMappings;

            Assert.NotNull(structuralTypeMappings);

            Assert.Same(functionImportReturnComplexType, structuralTypeMappings.Single().Item1);
            Assert.Empty(structuralTypeMappings.Single().Item2);
            Assert.Same(complexTypeProperty, structuralTypeMappings.Single().Item3.Single().Property);
            Assert.Same(rowTypeProperty, ((ScalarPropertyMapping)structuralTypeMappings.Single().Item3.Single()).Column);
        }
Ejemplo n.º 17
0
        public void Can_create_non_composable_function_with_multiple_results()
        {
            DbProviderManifest providerManifest;
            var containerMapping = GetContainerMapping(out providerManifest);

            var cTypeUsageInt = TypeUsage.Create(
                PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32),
                new[]
            {
                Facet.Create(MetadataItem.NullableFacetDescription, false)
            });
            var sTypeUsageInt = TypeUsage.Create(
                providerManifest.GetStoreType(cTypeUsageInt).EdmType,
                new[]
            {
                Facet.Create(MetadataItem.NullableFacetDescription, false)
            });
            var cTypeUsageString = TypeUsage.CreateDefaultTypeUsage(
                PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));
            var sTypeUsageString = providerManifest.GetStoreType(cTypeUsageString);

            var complexType = ComplexType.Create(
                "RT1", "N", DataSpace.CSpace,
                new[]
            {
                EdmProperty.Create("P1", cTypeUsageInt),
                EdmProperty.Create("P2", cTypeUsageString)
            },
                null);

            var entityType = EntityType.Create(
                "RT2", "N", DataSpace.CSpace,
                new[] { "P3" },
                new[]
            {
                EdmProperty.Create("P3", cTypeUsageInt),
                EdmProperty.Create("P4", cTypeUsageString),
            },
                null);

            var rowType1 = RowType.Create(
                new[]
            {
                EdmProperty.Create("C1", sTypeUsageInt),
                EdmProperty.Create("C2", sTypeUsageString)
            },
                null);

            var rowType2 = RowType.Create(
                new[]
            {
                EdmProperty.Create("C3", sTypeUsageInt),
                EdmProperty.Create("C4", sTypeUsageString)
            },
                null);

            var functionImport = EdmFunction.Create(
                "F", "N", DataSpace.CSpace,
                new EdmFunctionPayload
            {
                IsComposable     = false,
                ReturnParameters = new[]
                {
                    FunctionParameter.Create("R1", complexType.GetCollectionType(), ParameterMode.ReturnValue),
                    FunctionParameter.Create("R2", entityType.GetCollectionType(), ParameterMode.ReturnValue)
                },
                EntitySets = new[]
                {
                    new EntitySet(),
                    new EntitySet()
                }
            },
                null);

            var targetFunction = EdmFunction.Create(
                "SF", "N", DataSpace.SSpace,
                new EdmFunctionPayload
            {
                IsComposable     = false,
                ReturnParameters = new[]
                {
                    FunctionParameter.Create("R1", rowType1.GetCollectionType(), ParameterMode.ReturnValue),
                    FunctionParameter.Create("R2", rowType2.GetCollectionType(), ParameterMode.ReturnValue)
                },
                EntitySets = new []
                {
                    new EntitySet(),
                    new EntitySet()
                }
            },
                null);

            var resultMappings
                = new List <FunctionImportResultMapping>
                {
                new FunctionImportResultMapping(),
                new FunctionImportResultMapping()
                };

            resultMappings[0].AddTypeMapping(new FunctionImportComplexTypeMapping(
                                                 complexType,
                                                 new Collection <FunctionImportReturnTypePropertyMapping>()
            {
                new FunctionImportReturnTypeScalarPropertyMapping("P1", "C1"),
                new FunctionImportReturnTypeScalarPropertyMapping("P2", "C2"),
            }));

            resultMappings[1].AddTypeMapping(new FunctionImportEntityTypeMapping(
                                                 Enumerable.Empty <EntityType>(),
                                                 new [] { entityType },
                                                 new Collection <FunctionImportReturnTypePropertyMapping>()
            {
                new FunctionImportReturnTypeScalarPropertyMapping("P3", "C3"),
                new FunctionImportReturnTypeScalarPropertyMapping("P4", "C4")
            },
                                                 Enumerable.Empty <FunctionImportEntityTypeMappingCondition>()));

            var functionImportMapping = new FunctionImportMappingNonComposable(
                functionImport,
                targetFunction,
                resultMappings,
                containerMapping);

            Assert.Equal(resultMappings.Count, functionImportMapping.ResultMappings.Count);

            functionImportMapping.ResultMappings.Each(m => Assert.False(m.IsReadOnly));
            functionImportMapping.SetReadOnly();
            functionImportMapping.ResultMappings.Each(m => Assert.True(m.IsReadOnly));
        }