public void ElectrocardiogramViewModelToFormData_FullData_WithoutAttachment()
        {
            //Arrange
            var eViewModel = new ElectrocardiogramFormViewModel() {
                Id = 4,
                ElectrocardiogramActualTime = new DateTime(2011, 10, 11, 9, 10, 11),
            };
            var eFormData = CreateEmptyElectrocardiogramFormData();

            //Act
            formController.MapElectrocardiogramViewModelToFormData(eViewModel, eFormData, null);

            //Assert
            Assert.That(eFormData.ElectrocardiogramActualTime.Value,
                        Is.EqualTo(new DateTime(2011, 10, 11, 9, 10, 11).ToString(CultureInfo.InvariantCulture)));
        }
        public void ElectrocardiogramViewModelToFormData_FullData_WithAttachment()
        {
            //Arrange
            var eViewModel = new ElectrocardiogramFormViewModel() {
                Id = 4,
                ElectrocardiogramActualTime = new DateTime(2011, 10, 11, 9, 10, 11),
                AttachmentId = 14,
            };
            var eFormData = CreateEmptyElectrocardiogramFormData();
            Attachment attachment = new Attachment {
                Id = 14,
                FileName = "test.pdf",
                FileSize = 1234,
                MimeType = "application/pdf",
                StorageFileName = "abcdef.pdf"
            };

            //Act
            formController.MapElectrocardiogramViewModelToFormData(eViewModel, eFormData, attachment);

            //Assert
            Assert.That(eFormData.ElectrocardiogramActualTime.Value,
                        Is.EqualTo(new DateTime(2011, 10, 11, 9, 10, 11).ToString(CultureInfo.InvariantCulture)));

            Assert.That(eFormData.ElectrocardiogramAttachment, Is.Not.Null);
            var file = eFormData.ElectrocardiogramAttachment.File;
            Assert.That(file, Is.Not.Null);
            Assert.That(file.Id, Is.EqualTo(attachment.Id));
            Assert.That(file.FileName, Is.EqualTo(attachment.FileName));
            Assert.That(file.FileSize, Is.EqualTo(attachment.FileSize));
            Assert.That(file.MimeType, Is.EqualTo(attachment.MimeType));
            Assert.That(file.StorageFileName, Is.EqualTo(attachment.StorageFileName));
        }
            public void PostEditElectrocardiogramFormData_NonExistingForm_Test()
            {
                //Arrange
                var model = new ElectrocardiogramFormViewModel {
                    Id = 12,
                    FormId = 13,
                    ElectrocardiogramActualTime = new DateTime(2012, 1, 2, 3, 4, 5)
                };
                fRep.Setup(r => r.GetByKey(13)).Returns((Form) null);

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

                //Assert
                CheckErrorInfoViewResult(result);
            }
            public void PostEditElectrocardiogramFormData_NonExistingFormData_Test()
            {
                //Arrange
                var form = new Form {
                    Id = 13,
                    FormState = FormState.Incomplete
                };
                var model = new ElectrocardiogramFormViewModel {
                    Id = 12,
                    FormId = 13,
                    ElectrocardiogramActualTime = new DateTime(2012, 1, 2, 3, 4, 5)
                };
                fRep.Setup(r => r.GetByKey(13)).Returns(form);

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

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

                //Assert
                CheckErrorInfoViewResult(result);
            }
            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);
            }