public void MappingTest()
        {
            //Arrange
            var dataStorage = new Mock<IDataStorage>();
            var entity = new ElectrocardiogramFormData() {
                Id = 1,
                ElectrocardiogramActualTime = new Question {Id = 2, Value = "2"},
                ElectrocardiogramAttachment = new Question {Id = 3, Value = "3"}
            };
            var updatedEntity = new ElectrocardiogramFormData() {
                Id = 1,
                ElectrocardiogramActualTime = new Question {Id = 3, Value = "3"},
                ElectrocardiogramAttachment = new Question {Id = 4, Value = "4"}
            };
            var repository = new ElectrocardiogramFormDataRepository(dataStorage.Object);

            dataStorage.Setup(ds => ds.GetData<ElectrocardiogramFormData>()).Returns(new List<ElectrocardiogramFormData> {entity});
            //Act
            repository.Edit(updatedEntity);
            //Assert
            //Values are equal
            Assert.That(entity.ElectrocardiogramActualTime.Value, Is.EqualTo(updatedEntity.ElectrocardiogramActualTime.Value));
            Assert.That(entity.ElectrocardiogramAttachment.Value, Is.EqualTo(updatedEntity.ElectrocardiogramAttachment.Value));

            //Objects are not equal
            Assert.That(entity.ElectrocardiogramActualTime, Is.Not.EqualTo(updatedEntity.ElectrocardiogramActualTime));
            Assert.That(entity.ElectrocardiogramAttachment, Is.Not.EqualTo(updatedEntity.ElectrocardiogramAttachment));
        }
        public void ElectrocardiogramFormDataToViewModel_FullData()
        {
            //Arrange
            var eFormData = new ElectrocardiogramFormData() {
                Id = 4,
                Form =
                    new Form {
                        Id = 14,
                        Caption = "Form14",
                        FormState = FormState.Completed,
                        Visit =
                            new Visit {
                                Caption = "Visit14",
                                Patient = new Patient {PatientNumber = 140}
                            }
                    },
                ElectrocardiogramActualTime =
                    new Question {
                        Id = 3,
                        DataType = QuestionDataType.Date,
                        Value =
                            new DateTime(2012, 1, 1, 10, 11, 12).ToString(
                                CultureInfo.InvariantCulture)
                    },
                ElectrocardiogramAttachment = new Question() {
                    DataType = QuestionDataType.Attachment,
                    Id = 212,
                    Value = null,
                    File = new Attachment() {
                        Id = 414,
                        FileName = "test.pdf",
                        MimeType = "application/pdf",
                        FileSize = 4096
                    }
                }
            };
            //Act
            var electrocardiogram = formController.MapElectrocardiogramFormDataToViewModel(eFormData);

            //Assert
            Assert.That(electrocardiogram.FormId, Is.EqualTo(14));
            Assert.That(electrocardiogram.FormCaption, Is.EqualTo("Form14"));
            Assert.That(electrocardiogram.IsCompleted, Is.True);
            Assert.That(electrocardiogram.VisitName, Is.EqualTo("Visit14"));
            Assert.That(electrocardiogram.PatientNumber, Is.EqualTo(140));
            Assert.That(electrocardiogram.Id, Is.EqualTo(4));
            Assert.That(electrocardiogram.ElectrocardiogramActualTime, Is.Not.Null);
            Assert.That(electrocardiogram.ElectrocardiogramActualTime.Value.ToLongTimeString(),
                        Is.EqualTo(new DateTime(1980, 1, 1, 10, 11, 12).ToLongTimeString()));
            Assert.That(electrocardiogram.AttachmentId, Is.EqualTo(414));
            Assert.That(electrocardiogram.AttachmentName, Is.EqualTo("test.pdf"));
            Assert.That(electrocardiogram.ElectrocardiogramValidationSettings, Is.Not.Null);

            Assert.That(electrocardiogram.ActualTimeQuestionId, Is.EqualTo(eFormData.ElectrocardiogramActualTime.Id));
            Assert.That(electrocardiogram.ElectrocardiogramAttachmentQuestionId,
                        Is.EqualTo(eFormData.ElectrocardiogramAttachment.Id));
        }
            public void PostEditElectrocardiogramFormData_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 ElectrocardiogramFormData {
                    Id = 12,
                    Form = form,
                    ElectrocardiogramActualTime =
                        new Question() { Id = 101, Value = new DateTime(2011, 10, 11, 12, 13, 14).ToString(CultureInfo.InvariantCulture) },
                    ElectrocardiogramAttachment = new Question() { Id = 102 }
                };
                var incomingModel = new ElectrocardiogramFormViewModel {
                    Id = 12,
                    FormId = 13,
                    ElectrocardiogramActualTime = new DateTime(2012, 1, 2, 3, 4, 5),
                    AttachmentId = 14
                };

                fRep.Setup(r => r.GetByKey(incomingModel.FormId)).Returns(form);
                attachmentRep
                    .Setup(r => r.GetByKey(incomingModel.AttachmentId.Value))
                    .Returns(new Attachment() {
                        Id = 14,
                        FileName = "test.pdf",
                        FileSize = 1234,
                        MimeType = "application/pdf",
                        StorageFileName = "abc.pdf"
                    });

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

                //here we return "formdata before editing" - this data should be overwritten from model
                efdRep.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
                ElectrocardiogramFormData savedFormData = null;
                efdRep.Setup(r => r.Edit(It.IsAny<ElectrocardiogramFormData>())).Callback<ElectrocardiogramFormData>(fd => savedFormData = fd);

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

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

                //Assert
                CheckEditFormDataRedirectRouteValues(result, form);

                efdRep.Verify(r => r.GetByKey(incomingModel.Id), Times.Once());
                efdRep.Verify(r => r.Edit(It.IsAny<ElectrocardiogramFormData>()), Times.Once());
                efdRep.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.ElectrocardiogramActualTime.Value,
                            Is.EqualTo(new DateTime(2012, 1, 2, 3, 4, 5).ToString(CultureInfo.InvariantCulture)));

                Assert.That(savedForm, Is.Not.Null);
                Assert.That(savedForm.Id, Is.EqualTo(incomingModel.FormId));
                Assert.That(savedForm.FormState, Is.EqualTo(FormState.Completed));
            }
            public void PostEditElectrocardiogramFormData_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 ElectrocardiogramFormData {
                    Id = 12,
                    Form = form,
                    ElectrocardiogramActualTime =
                        new Question() { Id = 101, Value = new DateTime(2011, 10, 11, 12, 13, 14).ToString(CultureInfo.InvariantCulture) },
                    ElectrocardiogramAttachment = new Question() { Id = 102 }
                };
                var incomingModel = new ElectrocardiogramFormViewModel {
                    Id = 12,
                    FormId = 13,
                    ElectrocardiogramActualTime = new DateTime(2012, 1, 2, 15, 16, 17),
                    AttachmentId = 14
                };

                fRep.Setup(r => r.GetByKey(incomingModel.FormId)).Returns(form);
                attachmentRep
                    .Setup(r => r.GetByKey(incomingModel.AttachmentId.Value))
                    .Returns(new Attachment() {
                        Id = 14,
                        FileName = "test.pdf",
                        FileSize = 1234,
                        MimeType = "application/pdf",
                        StorageFileName = "abc.pdf"
                    });

                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
                efdRep.Setup(r => r.GetByKey(incomingModel.Id)).Returns(formData);

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

                //Assert
                efdRep.Verify(r => r.GetByKey(incomingModel.Id), Times.Once());
                efdRep.Verify(r => r.Edit(It.IsAny<ElectrocardiogramFormData>()), Times.Never());
                efdRep.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);
                ElectrocardiogramFormViewModel model = viewResultBase.Model as ElectrocardiogramFormViewModel;
                Assert.That(model, Is.Not.Null);

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

                AssertChangeInfo(formData.ElectrocardiogramActualTime, "12:13 PM", "3:16 PM", model);
                AssertChangeInfo(formData.ElectrocardiogramAttachment, "N/A", "test.pdf", model);
            }
 private ElectrocardiogramFormData AddElectrocardiogramFormData(Form form)
 {
     var formData = new ElectrocardiogramFormData() {
         Form = form,
         ElectrocardiogramActualTime = new Question { Caption = "Actual Time", DataType = QuestionDataType.Date, Form = form },
         ElectrocardiogramAttachment = new Question { Caption = "Electrocardiogram Data File", DataType = QuestionDataType.Attachment, Form = form }
     };
     ElectrocardiogramFormDataRepository.Add(formData);
     QuestionRepository.Add(formData.ElectrocardiogramActualTime);
     QuestionRepository.Add(formData.ElectrocardiogramAttachment);
     return formData;
 }