public void UpdateTest()
        {
            // Arrange.
            const string TextValue = "Text Value";

            var value = new object();

            var typeConverter = new Mock<ITypeConverter>();
            typeConverter.Setup(x => x.Convert(value, typeof(string))).Returns(TextValue);

            var dataContext = Mock.Create<IDataContext>(Behavior.Loose);

            var mapping = new SimpleFieldMapping(
                ReflectionHelper.GetProperty<TestEditable>(x => x.Text),
                dc => dc == dataContext ? value : null,
                false,
                typeConverter.Object);

            var editItem = new TestEditable();

            // Act.
            mapping.Update(dataContext, editItem);

            // Assert.
            Assert.AreEqual(TextValue, editItem.Text);
        }
        public void UpdateLocalizedFieldTest()
        {
            // Arrange.
            var dataContext = Mock.Create<IDataContext>(Behavior.Loose);

            var mapping = new SimpleFieldMapping(
                ReflectionHelper.GetProperty<TestEditable>(x => x.Localized),
                dc => dc == dataContext ? GetLocalizedValue() : null,
                false,
                new SafeTypeConverter());


            var editItem = new TestEditable();

            // Act.
            mapping.Update(dataContext, editItem);

            // Assert.
            Assert.AreEqual("English Text", editItem.Localized_en_US);
            Assert.AreEqual("Spanish Text", editItem.Localized_es_ES);
            Assert.AreEqual("French Text", editItem.Localized_fr_FR);
        }
        public void UpdateCurrentStateTest()
        {
            // Arrange.
            const string StateName = "State 1";

            var state1 = new Mock<IState>();
            state1.Setup(x => x.Id).Returns(111);
            state1.Setup(x => x.Name).Returns("State 1");

            var state2 = new Mock<IState>();
            state2.Setup(x => x.Id).Returns(222);
            state2.Setup(x => x.Name).Returns("State 2");

            var stateManager = new StateManager();
            stateManager.States.Add(state1.Object);
            stateManager.States.Add(state2.Object);

            var value = new object();

            var typeConverter = new Mock<ITypeConverter>();
            typeConverter.Setup(x => x.Convert<string>(value)).Returns(StateName);
            typeConverter.Setup(x => x.Convert<string>(StateName)).Returns(StateName);

            var dataContext = Mock.Create<IDataContext>(Behavior.Loose);
            var mapping = new SimpleFieldMapping(
                ReflectionHelper.GetProperty<TestEditable>(x => x.CurrentState),
                dc => dc == dataContext ? value : null,
                false,
                typeConverter.Object);

            var editItem = new TestEditable { StateManager = stateManager };

            // Act.
            mapping.Update(dataContext, editItem);

            // Assert.
            Assert.AreEqual(111, editItem.CurrentState);
        }
        public void UpdateIdTest()
        {
            // Arrange.
            const int Id = 123;

            var value = new object();

            var typeConverter = new Mock<ITypeConverter>();
            typeConverter.Setup(x => x.Convert<int>(value)).Returns(Id);
            typeConverter.Setup(x => x.Convert<int>(Id)).Returns(Id);

            var dataContext = Mock.Create<IDataContext>(Behavior.Loose);

            var mapping = new SimpleFieldMapping(
                ReflectionHelper.GetProperty<TestEditable>(x => x.Id),
                dc => dc == dataContext ? value : null,
                false,
                typeConverter.Object);

            var editItem = new TestEditable();

            // Act.
            mapping.Update(dataContext, editItem);

            // Assert.
            Assert.AreEqual(Id, editItem.Id);
            Assert.IsTrue(editItem.DisableIdGeneration);
        }
        public void UpdateTest()
        {
            // Arrange.
            const int CrId1 = 111;
            const int CrId2 = 222;
            const string ReferencedProcessName = "CRProcess";

            var value1 = new object();
            var dataContext = Mock.Create<IDataContext>(Behavior.Loose);

            var typeConverter = new Mock<ITypeConverter>();
            typeConverter.Setup(x => x.Convert<int?>(value1)).Returns(CrId1);

            var filter = new ColumnFilter("TestField", null);

            var keyField = Mock.Create<IProcessFieldMapping>(Behavior.Loose);
            Mock.Arrange(() => keyField.IsKey).Returns(true);
            Mock.Arrange(() => keyField.TryCreateFilter(dataContext, out filter)).Returns(true);

            var dtm = new Mock<IDynamicTypeManager>();

            var runtimeDatabase = Mock.Create<IRuntimeDatabase>(Behavior.Loose);
            Mock.Arrange(() => runtimeDatabase.FindItems(ReferencedProcessName, Arg.IsAny<IEnumerable<ColumnFilter>>()))
                .Returns<string, IEnumerable<ColumnFilter>>(
                    (processName, filters) =>
                    {
                        var filtersArray = filters.ToArray();
                        if (filtersArray.Length == 1 && filtersArray.Contains(filter))
                            return new Collection<int> { CrId2 };

                        return new Collection<int>();
                    });

            // Update with direct value.
            var mapping = new SingleCrossReferenceFieldMapping(
                ReflectionHelper.GetProperty<TestEditable>(x => x.CR),
                dc => dc == dataContext ? value1 : null,
                false,
                typeConverter.Object,
                dtm.Object,
                runtimeDatabase,
                null);
            var editItem = new TestEditable();

            // Act.
            mapping.Update(dataContext, editItem);

            // Assert.
            Assert.AreEqual(CrId1, editItem.CR);

            // Update with key subfields.
            mapping = new SingleCrossReferenceFieldMapping(
                ReflectionHelper.GetProperty<TestEditable>(x => x.CR),
                null,
                false,
                typeConverter.Object,
                dtm.Object,
                runtimeDatabase,
                new[] { keyField });

            editItem = new TestEditable();

            // Act.
            mapping.Update(dataContext, editItem);

            // Assert.
            Assert.AreEqual(CrId2, editItem.CR);
        }