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 Update_WhenChecklistIsMapped_UpdatesAnswers()
        {
            // Arrange.
            var request = new TestRequest();
            request.SourceItems.Add(new TestData { Text = "aaa", Number = 111 });
            request.SourceItems.Add(new TestData { Text = "bbb", Number = 222 });

            var dataContext = new SourceData(request);

            var childMapping1 = new SimpleFieldMapping(
                ReflectionHelper.GetProperty<TestAnswer>(x => x.Text),
                dc => dc.GetPropertyValue("SourceItems.Text"),
                true,
                new SafeTypeConverter());
            var childMapping2 = new SimpleFieldMapping(
                ReflectionHelper.GetProperty<TestAnswer>(x => x.Number),
                dc => dc.GetPropertyValue("SourceItems.Number"),
                false,
                new SafeTypeConverter());

            var dtm = Mock.Create<IDynamicTypeManager>(Behavior.Loose);
            Mock.Arrange(() => dtm.NewEditableChild<IEditableRoot>(AnswerProcessName)).Returns(() => new TestAnswer());

            var mapping = new ChecklistFieldMapping(
                ReflectionHelper.GetProperty<TestEdit>(x => x.Checklist),
                dc => dc.EnumerateItems("SourceItems"),
                new[] { childMapping1, childMapping2 },
                dtm);

            var editItem = new TestEdit();

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

            // Assert.
            Assert.AreEqual(2, editItem.Checklist.AnswerProcessList.Count);

            var answer1 = (TestAnswer)editItem.Checklist.AnswerProcessList[0];
            Assert.AreEqual("aaa", answer1.Text);
            Assert.AreEqual(111, answer1.Number);

            var answer2 = (TestAnswer)editItem.Checklist.AnswerProcessList[1];
            Assert.AreEqual("bbb", answer2.Text);
            Assert.AreEqual(222, answer2.Number);
        }
        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 TryCreateFilterTest()
        {
            const string Text = "TestValue";

            var value = new object();
            var valueNull = new object();
            var valueEmptyString = new object();
            var valueMinDate = new object();
            var dataContext = Mock.Create<IDataContext>(Behavior.Loose);

            var typeConverter = Mock.Create<ITypeConverter>(Behavior.Loose);
            Mock.Arrange(() => typeConverter.Convert(value, typeof(string))).Returns(Text);
            Mock.Arrange(() => typeConverter.Convert(valueNull, typeof(string))).Returns(() => null);
            Mock.Arrange(() => typeConverter.Convert(valueEmptyString, typeof(string))).Returns(string.Empty);
            Mock.Arrange(() => typeConverter.Convert(valueMinDate, typeof(string))).Returns(DateTime.MinValue);

            // Value is not null.
            var mapping = new SimpleFieldMapping(ReflectionHelper.GetProperty<TestEditable>(x => x.Text), dc => value, true, typeConverter);

            // Act / Assert.
            ColumnFilter filter;

            Assert.IsTrue(mapping.TryCreateFilter(dataContext, out filter));
            Assert.IsNotNull(filter);
            Assert.AreEqual(ReflectionHelper.GetPropertyName<TestEditable>(x => x.Text), filter.ColumnName);
            Assert.AreEqual(Text, filter.Value);

            // Value is null.
            var mapping2 = new SimpleFieldMapping(ReflectionHelper.GetProperty<TestEditable>(x => x.Text), dc => valueNull, true, typeConverter);

            // Act / Assert.
            Assert.IsFalse(mapping2.TryCreateFilter(dataContext, out filter));

            // Value is string.Empty.
            var mapping3 = new SimpleFieldMapping(ReflectionHelper.GetProperty<TestEditable>(x => x.Text), dc => valueEmptyString, true, typeConverter);

            // Act / Assert.
            Assert.IsFalse(mapping3.TryCreateFilter(dataContext, out filter));

            // Value is DateTime.MinValue.
            var mapping4 = new SimpleFieldMapping(ReflectionHelper.GetProperty<TestEditable>(x => x.Text), dc => valueMinDate, true, typeConverter);

            // Assert.
            Assert.IsFalse(mapping4.TryCreateFilter(dataContext, out filter));
        }
        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 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 Update_WhenChecklistIsNotMapped_UpdatesAnswers()
        {
            // Arrange.
            var request = new TestRequest { RootText = "abc", RootNumber = 123 };
            var dataContext = new SourceData(request);

            var childMapping1 = new SimpleFieldMapping(
                ReflectionHelper.GetProperty<TestAnswer>(x => x.Text),
                dc => dc.GetPropertyValue("RootText"),
                false,
                new SafeTypeConverter());
            var childMapping2 = new SimpleFieldMapping(
                ReflectionHelper.GetProperty<TestAnswer>(x => x.Number),
                dc => dc.GetPropertyValue("RootNumber"),
                true,
                new SafeTypeConverter());

            var dtm = Mock.Create<IDynamicTypeManager>(Behavior.Loose);
            Mock.Arrange(() => dtm.NewEditableChild<IEditableRoot>(AnswerProcessName)).Returns(() => new TestAnswer());

            var mapping = new ChecklistFieldMapping(ReflectionHelper.GetProperty<TestEdit>(x => x.Checklist), null, new[] { childMapping1, childMapping2 }, dtm);

            var editItem = new TestEdit();

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

            // Assert.
            Assert.AreEqual(1, editItem.Checklist.AnswerProcessList.Count);

            var answer = (TestAnswer)editItem.Checklist.AnswerProcessList[0];
            Assert.AreEqual("abc", answer.Text);
            Assert.AreEqual(123, answer.Number);
        }