Exemplo n.º 1
0
        /// <summary>
        /// The factory method for constructing the EntityType object.
        /// </summary>
        /// <param name="name">The name of the entity type.</param>
        /// <param name="namespaceName">The namespace of the entity type.</param>
        /// <param name="dataSpace">The dataspace in which the EntityType belongs to.</param>
        /// <param name="baseType">The base type.</param>
        /// <param name="keyMemberNames">Name of key members for the type.</param>
        /// <param name="members">Members of the entity type (primitive and navigation properties).</param>
        /// <param name="metadataProperties">Metadata properties to be associated with the instance.</param>
        /// <returns>The EntityType object.</returns>
        /// <exception cref="System.ArgumentException">Thrown if either name, namespace arguments are null.</exception>
        /// <remarks>The newly created EntityType will be read only.</remarks>
        public static EntityType Create(
            string name,
            string namespaceName,
            DataSpace dataSpace,
            EntityType baseType,
            IEnumerable <string> keyMemberNames,
            IEnumerable <EdmMember> members,
            IEnumerable <MetadataProperty> metadataProperties)
        {
            Check.NotEmpty(name, "name");
            Check.NotEmpty(namespaceName, "namespaceName");
            Check.NotNull(baseType, "baseType");

            var entity = new EntityType(name, namespaceName, dataSpace, keyMemberNames, members)
            {
                BaseType = baseType
            };

            if (metadataProperties != null)
            {
                entity.AddMetadataProperties(metadataProperties);
            }

            entity.SetReadOnly();
            return(entity);
        }
            public void Returns_rows_affected_when_there_is_a_reader()
            {
                var mockPrimitiveType = new Mock<PrimitiveType>();
                mockPrimitiveType.Setup(m => m.BuiltInTypeKind).Returns(BuiltInTypeKind.PrimitiveType);
                mockPrimitiveType.Setup(m => m.PrimitiveTypeKind).Returns(PrimitiveTypeKind.Int32);
                mockPrimitiveType.Setup(m => m.DataSpace).Returns(DataSpace.CSpace);
                string memberName = "property";
                var edmProperty = new EdmProperty(memberName, TypeUsage.Create(mockPrimitiveType.Object));

                var entityType = new EntityType("", "", DataSpace.CSpace, Enumerable.Empty<string>(), new[] { edmProperty });
                entityType.SetReadOnly();

                var mockUpdateTranslator = new Mock<UpdateTranslator>(MockBehavior.Strict);
                mockUpdateTranslator.Setup(m => m.CommandTimeout).Returns(() => null);
                var entityConnection = new Mock<EntityConnection>().Object;
                mockUpdateTranslator.Setup(m => m.Connection).Returns(entityConnection);

                var mockDbModificationCommandTree = new Mock<DbModificationCommandTree>();
                mockDbModificationCommandTree.SetupGet(m => m.HasReader).Returns(true);

                var mockDynamicUpdateCommand = new Mock<DynamicUpdateCommand>(new Mock<TableChangeProcessor>().Object, mockUpdateTranslator.Object,
                    ModificationOperator.Delete, PropagatorResult.CreateSimpleValue(PropagatorFlags.NoFlags, value: 0),
                    PropagatorResult.CreateStructuralValue(new[] { PropagatorResult.CreateSimpleValue(PropagatorFlags.NoFlags, value: 0) },
                        entityType,
                        isModified: false),
                    mockDbModificationCommandTree.Object, /*outputIdentifiers*/ null)
                {
                    CallBase = true
                };

                var mockDbCommand = new Mock<DbCommand>();

                int dbValue = 66;
                var mockDbDataReader = new Mock<DbDataReader>();
                mockDbDataReader.Setup(m => m.GetValue(It.IsAny<int>())).Returns(dbValue);
                int rowsToRead = 2;
                mockDbDataReader.Setup(m => m.Read()).Returns(() =>
                {
                    rowsToRead--;
                    return rowsToRead > 0;
                });
                mockDbDataReader.Setup(m => m.FieldCount).Returns(1);
                mockDbDataReader.Setup(m => m.GetName(0)).Returns(memberName);
                mockDbCommand.Protected().Setup<DbDataReader>("ExecuteDbDataReader", CommandBehavior.SequentialAccess).Returns(mockDbDataReader.Object);

                var identifierValues = new Dictionary<int, object>();

                mockDynamicUpdateCommand.Protected().Setup<DbCommand>("CreateCommand", identifierValues).Returns(mockDbCommand.Object);

                var generatedValues = new List<KeyValuePair<PropagatorResult, object>>();

                var rowsAffectedResult = mockDynamicUpdateCommand.Object.Execute(identifierValues, generatedValues);

                Assert.Equal(1, rowsAffectedResult);
                Assert.Equal(1, generatedValues.Count);
                Assert.Equal(dbValue, generatedValues[0].Value);
                Assert.Equal(0, generatedValues[0].Key.GetSimpleValue());
            }
Exemplo n.º 3
0
            public void IsReadOnly_is_implemented()
            {
                var metadataItem = new EntityType("E", "N", DataSpace.CSpace);

                Assert.False(metadataItem.Annotations.IsReadOnly);

                metadataItem.SetReadOnly();

                Assert.True(metadataItem.Annotations.IsReadOnly);
            }
Exemplo n.º 4
0
        public void Properties_collection_is_live_until_entity_goes_readonly()
        {
            var entityType = new EntityType();

            Assert.False(entityType.IsReadOnly);
            Assert.NotSame(entityType.Properties, entityType.Properties);

            entityType.SetReadOnly();

            Assert.Same(entityType.Properties, entityType.Properties);
        }
        public void Properties_collection_is_live_until_entity_goes_readonly()
        {
            var entityType = new EntityType("E", "N", DataSpace.CSpace);

            Assert.False(entityType.IsReadOnly);
            Assert.NotSame(entityType.Properties, entityType.Properties);

            entityType.SetReadOnly();

            Assert.Same(entityType.Properties, entityType.Properties);
        }
Exemplo n.º 6
0
        public void AddAnnotation_throws_if_metadata_item_is_readonly()
        {
            var entityType = new EntityType("E", "N", DataSpace.CSpace);
            var value      = new object();

            entityType.SetReadOnly();

            Assert.Equal(
                Strings.OperationOnReadOnlyCollection,
                Assert.Throws <InvalidOperationException>(() => entityType.AddAnnotation("name", value))
                .Message);
        }
Exemplo n.º 7
0
        public void Setting_annotation_value_throws_if_metadata_item_is_readonly()
        {
            var entityType = new EntityType("E", "N", DataSpace.CSpace);
            var value1     = new object();
            var value2     = new object();

            entityType.AddAnnotation("name", value1);
            entityType.SetReadOnly();

            Assert.Equal(
                Strings.OperationOnReadOnlyItem,
                Assert.Throws <InvalidOperationException>(() => entityType.Annotations.Single().Value = value2)
                .Message);
        }
        public void Setting_annotation_value_throws_if_metadata_item_is_readonly()
        {
            var entityType = new EntityType("E", "N", DataSpace.CSpace);
            var value1 = new object();
            var value2 = new object();

            entityType.AddAnnotation("name", value1);
            entityType.SetReadOnly();

            Assert.Equal(
                Strings.OperationOnReadOnlyItem,
                Assert.Throws<InvalidOperationException>(() => entityType.Annotations.Single().Value = value2)
                    .Message);
        }
        /// <summary>
        /// The factory method for constructing the EntityType object.
        /// </summary>
        /// <param name="name">The name of the entity type.</param>
        /// <param name="namespaceName">The namespace of the entity type.</param>
        /// <param name="dataSpace">The dataspace in which the EntityType belongs to.</param>
        /// <param name="keyMemberNames">Name of key members for the type.</param>
        /// <param name="members">Members of the entity type (primitive and navigation properties).</param>
        /// <param name="metadataProperties">Metadata properties to be associated with the instance.</param>
        /// <returns>The EntityType object.</returns>
        /// <exception cref="T:System.ArgumentException">Thrown if either name, namespace arguments are null.</exception>
        /// <remarks>The newly created EntityType will be read only.</remarks>
        public static EntityType Create(
            string name,
            string namespaceName,
            DataSpace dataSpace,
            IEnumerable <string> keyMemberNames,
            IEnumerable <EdmMember> members,
            IEnumerable <MetadataProperty> metadataProperties)
        {
            Check.NotEmpty(name, nameof(name));
            Check.NotEmpty(namespaceName, nameof(namespaceName));
            EntityType entityType = new EntityType(name, namespaceName, dataSpace, keyMemberNames, members);

            if (metadataProperties != null)
            {
                entityType.AddMetadataProperties(metadataProperties.ToList <MetadataProperty>());
            }
            entityType.SetReadOnly();
            return(entityType);
        }
Exemplo n.º 10
0
        public static void Adding_a_NavigationProperty_to_an_EntityType_can_be_forced_when_read_only()
        {
            var typeUsage       = TypeUsage.CreateDefaultTypeUsage(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));
            var associationType = new AssociationType("AssociationType", "Namespace", true, DataSpace.CSpace);
            var source          = new EntityType("Source", "Namespace", DataSpace.CSpace);
            var target          = new EntityType("Target", "Namespace", DataSpace.CSpace);
            var sourceEnd       = new AssociationEndMember("SourceEnd", source);
            var targetEnd       = new AssociationEndMember("TargetEnd", target);

            var navigationProperty =
                NavigationProperty.Create(
                    "NavigationProperty",
                    typeUsage,
                    associationType,
                    sourceEnd,
                    targetEnd,
                    null);

            source.SetReadOnly();
            Assert.True(source.IsReadOnly);

            Assert.Equal(
                Resources.Strings.OperationOnReadOnlyItem,
                Assert.Throws <InvalidOperationException>(
                    () => source.AddMember(navigationProperty)).Message);

            Assert.Equal(0, source.Members.Count);

            source.AddNavigationProperty(navigationProperty);

            Assert.True(source.IsReadOnly);
            Assert.Equal(1, source.Members.Count);
            Assert.Same(navigationProperty, source.Members[0]);

            Assert.Equal(
                Resources.Strings.OperationOnReadOnlyItem,
                Assert.Throws <InvalidOperationException>(
                    () => source.AddMember(navigationProperty)).Message);
        }
        public static void Adding_a_NavigationProperty_to_an_EntityType_can_be_forced_when_read_only()
        {
            var typeUsage = TypeUsage.CreateDefaultTypeUsage(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));
            var associationType = new AssociationType("AssociationType", "Namespace", true, DataSpace.CSpace);
            var source = new EntityType("Source", "Namespace", DataSpace.CSpace);
            var target = new EntityType("Target", "Namespace", DataSpace.CSpace);
            var sourceEnd = new AssociationEndMember("SourceEnd", source);
            var targetEnd = new AssociationEndMember("TargetEnd", target);

            var navigationProperty =
                NavigationProperty.Create(
                    "NavigationProperty",
                    typeUsage,
                    associationType,
                    sourceEnd,
                    targetEnd);

            source.SetReadOnly();
            Assert.True(source.IsReadOnly);

            Assert.Equal(
                Resources.Strings.OperationOnReadOnlyItem,
                Assert.Throws<InvalidOperationException>(
                    () => source.AddMember(navigationProperty)).Message);

            Assert.Equal(0, source.Members.Count);

            source.AddNavigationProperty(navigationProperty);

            Assert.True(source.IsReadOnly);
            Assert.Equal(1, source.Members.Count);
            Assert.Same(navigationProperty, source.Members[0]);

            Assert.Equal(
                Resources.Strings.OperationOnReadOnlyItem,
                Assert.Throws<InvalidOperationException>(
                    () => source.AddMember(navigationProperty)).Message);
        }
Exemplo n.º 12
0
        /// <summary>
        ///     The factory method for constructing the EntityType object.
        /// </summary>
        /// <param name="name">The name of the entity type.</param>
        /// <param name="namespaceName">The namespace of the entity type.</param>
        /// <param name="dataSpace">The dataspace in which the EntityType belongs to.</param>
        /// <param name="keyMemberNames">Name of key members for the type.</param>
        /// <param name="members">Members of the entity type (primitive and navigation properties).</param>
        /// <param name="metadataProperties">Metadata properties to be associated with the instance.</param>
        /// <exception cref="System.ArgumentException">Thrown if either name, namespace arguments are null.</exception>
        /// <notes>The newly created EntityType will be read only.</notes>
        public static EntityType Create(
            string name,
            string namespaceName,
            DataSpace dataSpace,
            IEnumerable<string> keyMemberNames,
            IEnumerable<EdmMember> members,
            IEnumerable<MetadataProperty> metadataProperties)
        {
            Check.NotEmpty(name, "name");
            Check.NotEmpty(namespaceName, "namespaceName");

            var entity = new EntityType(name, namespaceName, dataSpace, keyMemberNames, members);

            if (metadataProperties != null)
            {
                entity.AddMetadataProperties(metadataProperties.ToList());
            }

            entity.SetReadOnly();
            return entity;
        }
            public void Returns_rows_affected_when_there_are_result_columns()
            {
                var mockPrimitiveType = new Mock<PrimitiveType>();
                mockPrimitiveType.Setup(m => m.BuiltInTypeKind).Returns(BuiltInTypeKind.PrimitiveType);
                mockPrimitiveType.Setup(m => m.PrimitiveTypeKind).Returns(PrimitiveTypeKind.Int32);
                mockPrimitiveType.Setup(m => m.DataSpace).Returns(DataSpace.CSpace);
                var edmProperty = new EdmProperty("property", TypeUsage.Create(mockPrimitiveType.Object));

                var entityType = new EntityType("", "", DataSpace.CSpace, Enumerable.Empty<string>(), new[] { edmProperty });
                entityType.SetReadOnly();

                var stateEntry = new ExtractedStateEntry(
                    EntityState.Unchanged,
                    PropagatorResult.CreateSimpleValue(PropagatorFlags.NoFlags, value: 0),
                    PropagatorResult.CreateStructuralValue(
                        new[] { PropagatorResult.CreateSimpleValue(PropagatorFlags.NoFlags, value: 0) },
                        entityType,
                        isModified: false),
                    new Mock<IEntityStateEntry>().Object);

                var updateTranslatorMock = new Mock<UpdateTranslator>();
                updateTranslatorMock.Setup(m => m.CommandTimeout).Returns(() => null);
                var entityConnection = new Mock<EntityConnection>().Object;
                updateTranslatorMock.Setup(m => m.Connection).Returns(entityConnection);
                updateTranslatorMock.Setup(m => m.InterceptionContext).Returns(new DbInterceptionContext());

                var dbCommandMock = new Mock<DbCommand>();
                var stateEntries = new ReadOnlyCollection<IEntityStateEntry>(new List<IEntityStateEntry>());

                var mockFunctionUpdateCommand = new Mock<FunctionUpdateCommand>(
                    updateTranslatorMock.Object, stateEntries, stateEntry, dbCommandMock.Object)
                                                    {
                                                        CallBase = true
                                                    };

                var dbValue = 66;
                var dbDataReaderMock = new Mock<DbDataReader>();
                dbDataReaderMock.Setup(m => m.GetValue(It.IsAny<int>())).Returns(dbValue);
                var rowsToRead = 2;
                dbDataReaderMock.Setup(m => m.Read()).Returns(
                    () =>
                        {
                            rowsToRead--;
                            return rowsToRead > 0;
                        });
                dbCommandMock.Protected().Setup<DbDataReader>("ExecuteDbDataReader", CommandBehavior.SequentialAccess).Returns(
                    dbDataReaderMock.Object);

                var timesSetInputIdentifiers = 0;
                var identifierValues = new Dictionary<int, object>();
                mockFunctionUpdateCommand.Setup(m => m.SetInputIdentifiers(It.IsAny<Dictionary<int, object>>()))
                    .Callback<Dictionary<int, object>>(
                        identifierValuesPassed =>
                            {
                                timesSetInputIdentifiers++;
                                Assert.Same(identifierValues, identifierValuesPassed);
                            });

                var generatedValues = new List<KeyValuePair<PropagatorResult, object>>();
                var mockObjectStateManager = new Mock<ObjectStateManager>();
                var objectStateEntryMock = new Mock<ObjectStateEntry>(mockObjectStateManager.Object, null, EntityState.Unchanged);
                var currentValueRecordMock = new Mock<CurrentValueRecord>(objectStateEntryMock.Object);

                var idColumn = new KeyValuePair<string, PropagatorResult>(
                    "ID",
                    PropagatorResult.CreateServerGenSimpleValue(
                        PropagatorFlags.NoFlags, /*value:*/ 0, currentValueRecordMock.Object, recordOrdinal: 0));
                mockFunctionUpdateCommand.Protected().Setup<List<KeyValuePair<string, PropagatorResult>>>("ResultColumns")
                    .Returns((new[] { idColumn }).ToList());

                var rowsAffectedResult = mockFunctionUpdateCommand.Object.Execute(identifierValues, generatedValues);

                Assert.Equal(1, rowsAffectedResult);
                Assert.Equal(1, timesSetInputIdentifiers);
                Assert.Equal(1, generatedValues.Count);
                Assert.Same(idColumn.Value, generatedValues[0].Key);
                Assert.Equal(dbValue, generatedValues[0].Value);
            }
Exemplo n.º 14
0
        public void AddAnnotation_throws_if_metadata_item_is_readonly()
        {
            var entityType = new EntityType("E", "N", DataSpace.CSpace);
            var value = new object();

            entityType.SetReadOnly();

            Assert.Equal(
                Strings.OperationOnReadOnlyCollection,
                Assert.Throws<InvalidOperationException>(() => entityType.AddAnnotation("name", value))
                    .Message);
        }