public void InventoryViewModelToFormData_FullData()
        {
            //Arrange
            var iViewModel = new InventoryFormViewModel {
                Id = 5,
                ShipDate = new DateTime(2012, 1, 2),
                BatchNumber = 120,
                QuantityShipped = 4.5m,
                ReceiptDate = new DateTime(2012, 2, 1)
            };
            var iFormData = CreateEmptyInventoryFormData();

            //Act
            formController.MapInventoryViewModelToFormData(iViewModel, iFormData);

            //Assert
            Assert.That(iFormData.BatchNumber.Value, Is.EqualTo("120"));
            Assert.That(iFormData.ShipDate.Value, Is.EqualTo(new DateTime(2012, 1, 2).ToString(CultureInfo.InvariantCulture)));
            Assert.That(iFormData.ReceiptDate.Value, Is.EqualTo(new DateTime(2012, 2, 1).ToString(CultureInfo.InvariantCulture)));
            Assert.That(iFormData.QuantityShipped.Value, Is.EqualTo(4.5m.ToString(CultureInfo.InvariantCulture)));
        }
            public void PostEditInventoryFormData_NonExistingForm_Test()
            {
                //Arrange
                var model = new InventoryFormViewModel {
                    Id = 12,
                    FormId = 13,
                    ReceiptDate = new DateTime(2012, 10, 11, 15, 16, 17),
                    ShipDate = new DateTime(2011, 4, 5, 6, 7, 8),
                    BatchNumber = 171,
                    QuantityShipped = 51
                };
                fRep.Setup(r => r.GetByKey(13)).Returns((Form) null);

                //Act
                var result = formController.EditInventoryForm(model);

                //Assert
                CheckErrorInfoViewResult(result);
            }
            public void PostEditInventoryFormData_ChangedValues_IncompleteForm_Test()
            {
                //Arrange
                EmulateControllerContext(formController, false);
                var form = new Form {
                    Id = 13,
                    FormState = FormState.Incomplete,
                    Caption = "TestForm",
                    Visit = new Visit { Caption = "TestVisit", Patient = new Patient { PatientNumber = 123 } }
                };

                var formData = new InventoryFormData {
                    Id = 12,
                    Form = form,
                    ReceiptDate =
                        new Question() { Id = 101, Value = new DateTime(2011, 10, 11, 12, 13, 14).ToString(CultureInfo.InvariantCulture) },
                    ShipDate = new Question() { Id = 102, Value = new DateTime(2012, 1, 2, 3, 4, 5).ToString(CultureInfo.InvariantCulture) },
                    BatchNumber = new Question() { Id = 103, Value = "170" },
                    QuantityShipped = new Question() { Id = 104, Value = "50" }
                };
                var incomingModel = new InventoryFormViewModel {
                    Id = 12,
                    FormId = 13,
                    ReceiptDate = new DateTime(2012, 10, 11, 15, 16, 17),
                    ShipDate = new DateTime(2011, 4, 5, 6, 7, 8),
                    BatchNumber = 171,
                    QuantityShipped = 51
                };

                fRep.Setup(r => r.GetByKey(incomingModel.FormId)).Returns(form);

                //here we return "formdata before editing" - this data should be overwritten from model
                ifdRep.Setup(r => r.GetByKey(incomingModel.Id)).Returns(formData);

                //as checking of saved formdata is quite complicated, we will save the passed object and inspect it later
                InventoryFormData savedFormData = null;
                ifdRep.Setup(r => r.Edit(It.IsAny<InventoryFormData>())).Callback<InventoryFormData>(fd => savedFormData = fd);

                Form savedForm = null;
                fRep.Setup(r => r.Edit(It.IsAny<Form>())).Callback<Form>(f => savedForm = f);

                //Act
                var result = formController.EditInventoryForm(incomingModel);

                //Assert
                CheckEditFormDataRedirectRouteValues(result, form);

                ifdRep.Verify(r => r.GetByKey(incomingModel.Id), Times.Once());
                ifdRep.Verify(r => r.Edit(It.IsAny<InventoryFormData>()), Times.Once());
                ifdRep.Verify(r => r.Save(), Times.Once());

                fRep.Verify(r => r.GetByKey(incomingModel.FormId), Times.Once());
                fRep.Verify(r => r.Edit(It.IsAny<Form>()), Times.Once());
                fRep.Verify(r => r.Save(), Times.Once());

                Assert.That(savedFormData, Is.Not.Null);
                Assert.That(savedFormData.Id, Is.EqualTo(incomingModel.Id));
                Assert.That(savedFormData.ReceiptDate.Value,
                            Is.EqualTo(new DateTime(2012, 10, 11, 15, 16, 17).ToString(CultureInfo.InvariantCulture)));
                Assert.That(savedFormData.ShipDate.Value,
                            Is.EqualTo(new DateTime(2011, 4, 5, 6, 7, 8).ToString(CultureInfo.InvariantCulture)));
                Assert.That(savedFormData.BatchNumber.Value, Is.EqualTo("171"));
                Assert.That(savedFormData.QuantityShipped.Value, Is.EqualTo("51"));

                Assert.That(savedForm, Is.Not.Null);
                Assert.That(savedForm.Id, Is.EqualTo(incomingModel.FormId));
                Assert.That(savedForm.FormState, Is.EqualTo(FormState.Completed));
            }
            public void PostEditInventoryFormData_NonExistingFormData_Test()
            {
                //Arrange
                var form = new Form {
                    Id = 13,
                    FormState = FormState.Incomplete
                };
                var model = new InventoryFormViewModel {
                    Id = 12,
                    FormId = 13,
                    ReceiptDate = new DateTime(2012, 10, 11, 15, 16, 17),
                    ShipDate = new DateTime(2011, 4, 5, 6, 7, 8),
                    BatchNumber = 171,
                    QuantityShipped = 51
                };
                fRep.Setup(r => r.GetByKey(13)).Returns(form);

                //here we return "formdata before editing" - this data should be overwritten from model
                ifdRep.Setup(r => r.GetByKey(12)).Returns((InventoryFormData) null);

                //Act
                var result = formController.EditInventoryForm(model);

                //Assert
                CheckErrorInfoViewResult(result);
            }
            public void PostEditInventoryFormData_ChangedUnconfirmedValues_Test()
            {
                //Arrange
                EmulateControllerContext(formController, false);
                var form = new Form {
                    Id = 13,
                    FormState = FormState.Completed,
                    Caption = "TestForm",
                    Visit = new Visit { Caption = "TestVisit", Patient = new Patient { PatientNumber = 123 } }
                };

                var formData = new InventoryFormData {
                    Id = 12,
                    Form = form,
                    ReceiptDate =
                        new Question() { Id = 101, Value = new DateTime(2011, 10, 11, 12, 13, 14).ToString(CultureInfo.InvariantCulture) },
                    ShipDate = new Question() { Id = 102, Value = new DateTime(2012, 1, 2, 3, 4, 5).ToString(CultureInfo.InvariantCulture) },
                    BatchNumber = new Question() { Id = 103, Value = "170" },
                    QuantityShipped = new Question() { Id = 104, Value = "50" }
                };
                var incomingModel = new InventoryFormViewModel {
                    Id = 12,
                    FormId = 13,
                    ReceiptDate = new DateTime(2012, 10, 11, 15, 16, 17),
                    ShipDate = new DateTime(2011, 4, 5, 6, 7, 8),
                    BatchNumber = 171,
                    QuantityShipped = 51
                };

                fRep.Setup(r => r.GetByKey(incomingModel.FormId)).Returns(form);
                //here we return "formdata before editing" - this data should not be overwritten from model as no any data change note provided
                ifdRep.Setup(r => r.GetByKey(incomingModel.Id)).Returns(formData);

                //Act
                var result = formController.EditInventoryForm(incomingModel);

                //Assert
                ifdRep.Verify(r => r.GetByKey(incomingModel.Id), Times.Once());
                ifdRep.Verify(r => r.Edit(It.IsAny<InventoryFormData>()), Times.Never());
                ifdRep.Verify(r => r.Save(), Times.Never());

                fRep.Verify(r => r.GetByKey(incomingModel.FormId), Times.Once());
                fRep.Verify(r => r.Edit(It.IsAny<Form>()), Times.Never());
                fRep.Verify(r => r.Save(), Times.Never());

                Assert.That(result, Is.Not.Null);
                Assert.That(result is ViewResultBase);
                var viewResultBase = result as ViewResultBase;
                Assert.That(viewResultBase.Model, Is.Not.Null);
                InventoryFormViewModel model = viewResultBase.Model as InventoryFormViewModel;
                Assert.That(model, Is.Not.Null);

                Assert.That(model.DataChangeReasonRequired);
                Assert.That(model.ChangeInfos, Is.Not.Null);
                Assert.That(model.ChangeInfos.Count, Is.EqualTo(4));

                AssertChangeInfo(formData.ReceiptDate, DateTime.Parse(formData.ReceiptDate.Value).ToShortDateString(), incomingModel.ReceiptDate.Value.ToShortDateString(), model);
                AssertChangeInfo(formData.ShipDate, DateTime.Parse(formData.ShipDate.Value).ToShortDateString(), incomingModel.ShipDate.Value.ToShortDateString(), model);
                AssertChangeInfo(formData.BatchNumber, "170", "171", model);
                AssertChangeInfo(formData.QuantityShipped, "50", "51", model);
            }