public void GetEditForm_AdverseEvent_CorrectView_Ajax_Test()
            {
                //Arrange
                EmulateControllerContext(formController, true);
                var form = new Form {
                    Id = 42,
                    Caption = "TestForm43",
                    Visit = new Visit {Caption = "TestVisit44", Patient = new Patient {PatientNumber = 45}},
                    FormType = FormType.AdverseEvent
                };
                fRep.Setup(fr => fr.GetForm(form.Visit.Patient.PatientNumber, form.Visit.Caption, form.Caption)).Returns(form);
                SetupFormDataRepositories(form);

                //Act
                var viewResult = formController.EditForm(form.Visit.Patient.PatientNumber, form.Visit.Caption, form.Caption);

                //Assert
                Assert.That(viewResult, Is.Not.Null);
                Assert.That(viewResult is PartialViewResult);
                var viewResultBase = viewResult as ViewResultBase;
                Assert.That(viewResultBase.Model, Is.Not.Null);
                Assert.That(viewResultBase.ViewName, Is.EqualTo("_EditAdverseEventForm"));
                Assert.That(viewResultBase.Model is AdverseEventFormViewModel);

                VerifyRepositoriesCalls(form);
            }
        public void GetOpenQueries()
        {
            //Arrange
            var dataStorage = new Mock<IDataStorage>();
            var clinic = new Clinic {Caption = "Clinic1"};
            var doctor1 = new User {FirstName = "DoctorFirst1", LastName = "DoctorLast1", Clinic = clinic};
            var doctor2 = new User {FirstName = "DoctorFirst2", LastName = "DoctorLast2", Clinic = clinic};
            var patient1 = new Patient {PatientNumber = 11, Doctor = doctor1};
            var patient2 = new Patient {PatientNumber = 12, Doctor = doctor2};
            var visit1 = new Visit {Caption = "Visit1", Patient = patient1};
            var visit2 = new Visit {Caption = "Visit2", Patient = patient2};
            var form1 = new Form {FormType = FormType.Happiness, Visit = visit1};
            var form2 = new Form {FormType = FormType.Demographics, Visit = visit2};
            var question1 = new Question {Form = form1};
            var question2 = new Question {Form = form2};
            var query1 = new Query {Id = 1, QueryText = "Text1", Question = question1};
            var query2 = new Query {Id = 2, QueryText = "Text2", AnswerText = "Answer1", Question = question2};

            var repository = new QueryRepository(dataStorage.Object);
            dataStorage.Setup(ds => ds.GetData<Query>()).Returns(new List<Query> {query1, query2});

            //Act
            var result = repository.GetOpenQueries();

            //Assert
            Assert.That(result, Is.Not.Null);
            Assert.That(result.Count(), Is.EqualTo(1));
            var query = result.ToList()[0];
            Assert.That(query.FormType, Is.EqualTo(FormType.Happiness));
            Assert.That(query.ClinicName, Is.EqualTo("Clinic1"));
            Assert.That(query.DoctorName, Is.EqualTo("DoctorLast1"));
            Assert.That(query.QuestionText, Is.EqualTo("Text1"));
            Assert.That(query.PatientNumber, Is.EqualTo(11));
            Assert.That(query.VisitName, Is.EqualTo("Visit1"));
        }
        public void GetFormTest()
        {
            //Arrange
            const int patientNumber = 123;
            const string visitName = "TestVisit";
            const string formName = "TestForm";
            var form = new Form() {
                Visit = new Visit {Caption = visitName, Patient = new Patient {PatientNumber = patientNumber}},
                Caption = formName
            };

            DataStorage.Setup(ds => ds.GetData<Form>()).Returns(new List<Form> {form});
            //Act
            var result = Repository.GetForm(patientNumber, visitName, formName);

            //Assert
            Assert.That(result, Is.Not.Null);
            Assert.That(result, Is.EqualTo(form));
        }
            public void NotFilledInFutureVisit()
            {
                //Arrange
                Visit visit = new Visit() {
                    Id = 5,
                    VisitDate = DateTime.Now.AddDays(+1),
                    Patient = GetTestPatient()
                };

                Form form1 = new Form() { Visit = visit, FormState = FormState.Incomplete, FormType = FormType.Demographics };

                DataStorage.Setup(ds => ds.GetData<Form>()).Returns(new List<Form> { form1 });
                //Act
                IList<FormDto> result = Repository.GetUnfinishedCrfs();

                //Assert
                Assert.That(result, Is.Not.Null);
                Assert.That(result.Count, Is.EqualTo(0));
            }
            public void PostEditVitalsFormData_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 VitalsFormData {
                    Id = 12,
                    Form = form,
                    ActualTime = new Question() { Id = 100, Value = new DateTime(2011, 10, 11, 12, 13, 14).ToString(CultureInfo.InvariantCulture) },
                    Height = new Question() { Id = 101, Value = "170" },
                    Weight = new Question() { Id = 102, Value = "50" },
                    HeartRate = new Question() { Id = 103, Value = "20" },
                    Temperature = new Question() { Id = 104, Value = "30" },
                    BloodPressureSystolic = new Question() { Id = 105, Value = "120" },
                    BloodPressureDiastolic = new Question() { Id = 106, Value = "70" }
                };
                var incomingModel = new VitalsFormViewModel {
                    Id = 12,
                    FormId = 13,
                    ActualTime = new DateTime(2012, 10, 11, 15, 16, 17),
                    Height = 171,
                    Weight = 51,
                    HeartRate = 21,
                    Temperature = 31,
                    BloodPressureSystolic = "121",
                    BloodPressureDiastolic = "71"
                };

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

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

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

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

                //Assert
                CheckEditFormDataRedirectRouteValues(result, form);

                vfdRep.Verify(r => r.GetByKey(incomingModel.Id), Times.Once());
                vfdRep.Verify(r => r.Edit(It.IsAny<VitalsFormData>()), Times.Once());
                vfdRep.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.ActualTime.Value,
                            Is.EqualTo(new DateTime(2012, 10, 11, 15, 16, 17).ToString(CultureInfo.InvariantCulture)));
                Assert.That(savedFormData.Height.Value, Is.EqualTo("171"));
                Assert.That(savedFormData.Weight.Value, Is.EqualTo("51"));
                Assert.That(savedFormData.HeartRate.Value, Is.EqualTo("21"));
                Assert.That(savedFormData.Temperature.Value, Is.EqualTo("31"));
                Assert.That(savedFormData.BloodPressureSystolic.Value, Is.EqualTo("121"));
                Assert.That(savedFormData.BloodPressureDiastolic.Value, Is.EqualTo("71"));

                Assert.That(savedForm, Is.Not.Null);
                Assert.That(savedForm.Id, Is.EqualTo(incomingModel.FormId));
                Assert.That(savedForm.FormState, Is.EqualTo(FormState.Completed));
            }
            public void PostEditVitalsFormData_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 VitalsFormData {
                    Id = 12,
                    Form = form,
                    ActualTime = new Question() { Id = 100, Value = new DateTime(2011, 10, 11, 12, 13, 14).ToString(CultureInfo.InvariantCulture) },
                    Height = new Question() { Id = 101, Value = "170" },
                    Weight = new Question() { Id = 102, Value = "50" },
                    HeartRate = new Question() { Id = 103, Value = "20" },
                    Temperature = new Question() { Id = 104, Value = "30" },
                    BloodPressureSystolic = new Question() { Id = 105, Value = "120" },
                    BloodPressureDiastolic = new Question() { Id = 106, Value = "70" }
                };
                var incomingModel = new VitalsFormViewModel {
                    Id = 12,
                    FormId = 13,
                    ActualTime = new DateTime(2012, 10, 11, 15, 16, 17),
                    Height = 171,
                    Weight = 51,
                    HeartRate = 21,
                    Temperature = 31,
                    BloodPressureSystolic = "121",
                    BloodPressureDiastolic = "71"
                };

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

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

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

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

                AssertChangeInfo(formData.Height, "170", "171", model);
                AssertChangeInfo(formData.Weight, "50", "51", model);
                AssertChangeInfo(formData.HeartRate, "20", "21", model);
                AssertChangeInfo(formData.Temperature, "30", "31", model);
                AssertChangeInfo(formData.BloodPressureSystolic, "120", "121", model);
                AssertChangeInfo(formData.BloodPressureDiastolic, "70", "71", model);
                AssertChangeInfo(formData.ActualTime, "12:13 PM", "3:16 PM", model);
            }
            private void PrepareQueriesReportData()
            {
                supervisor1000 = new User {Id = 1000, FirstName = "First1000", LastName = "Last1000"};
                supervisor2000 = new User {Id = 2000, FirstName = "First2000", LastName = "Last2000"};

                doctor1 = new User {Id = 1, FirstName = "First1", LastName = "Last1"};
                doctor2 = new User {Id = 2, FirstName = "First2", LastName = "Last2"};
                doctor3 = new User {Id = 3, FirstName = "First3", LastName = "Last3"};

                form11 = new Form {Id = 11, Caption = "CRF11"};
                form12 = new Form {Id = 12, Caption = "CRF12"};
                form13 = new Form {Id = 13, Caption = "CRF13"};

                form21 = new Form {Id = 21, Caption = "CRF21"};
                form22 = new Form {Id = 22, Caption = "CRF22"};

                form31 = new Form {Id = 31, Caption = "CRF31"};
                form32 = new Form {Id = 32, Caption = "CRF32"};
                form33 = new Form {Id = 33, Caption = "CRF33"};
                form34 = new Form {Id = 34, Caption = "CRF34"};
                form35 = new Form {Id = 35, Caption = "CRF35"};

                question111 = new Question {Id = 111, Caption = "Question111", Form = form11};

                question121 = new Question {Id = 121, Caption = "Question121", Form = form12};
                question122 = new Question {Id = 122, Caption = "Question122", Form = form12};

                question211 = new Question {Id = 211, Caption = "Question211", Form = form21};
                question212 = new Question {Id = 212, Caption = "Question212", Form = form21};
                question213 = new Question {Id = 213, Caption = "Question213", Form = form21};

                question311 = new Question {Id = 311, Caption = "Question311", Form = form31};
                question321 = new Question {Id = 321, Caption = "Question321", Form = form32};
                question331 = new Question {Id = 331, Caption = "Question331", Form = form33};
                question341 = new Question {Id = 341, Caption = "Question341", Form = form34};
                question351 = new Question {Id = 351, Caption = "Question351", Form = form35};

                query1110 = new Query { Id = 1110, Question = question111, QueryAuthor = supervisor1000, QueryTime = DateTime.Today.AddDays(-1110), QueryText = "Query1110", AnswerAuthor = doctor1, AnswerText = "Reply1110", AnswerTime = DateTime.Today.AddDays(1110)};
                query1210 = new Query { Id = 1210, Question = question121, QueryAuthor = supervisor2000, QueryTime = DateTime.Today.AddDays(-1210), QueryText = "Query1210", AnswerAuthor = doctor1, AnswerText = "Reply1210", AnswerTime = DateTime.Today.AddDays(1210) };
                query1220 = new Query { Id = 1220, Question = question122, QueryAuthor = supervisor1000, QueryTime = DateTime.Today.AddDays(-1220), QueryText = "Query1220", AnswerAuthor = doctor1, AnswerText = "Reply1220", AnswerTime = DateTime.Today.AddDays(1220) };
                query2110 = new Query { Id = 2110, Question = question211, QueryAuthor = supervisor2000, QueryTime = DateTime.Today.AddDays(-2110), QueryText = "Query2110", AnswerAuthor = doctor2, AnswerText = "Reply2110", AnswerTime = DateTime.Today.AddDays(2110) };
                query2120 = new Query { Id = 2120, Question = question212, QueryAuthor = supervisor1000, QueryTime = DateTime.Today.AddDays(-2120), QueryText = "Query2120", AnswerAuthor = doctor2, AnswerText = "Reply2120", AnswerTime = DateTime.Today.AddDays(2120) };
                query2130 = new Query { Id = 2130, Question = question213, QueryAuthor = supervisor2000, QueryTime = DateTime.Today.AddDays(-2130), QueryText = "Query2130", AnswerAuthor = doctor2, AnswerText = "Reply2130", AnswerTime = DateTime.Today.AddDays(2130) };
                query3110 = new Query { Id = 3110, Question = question311, QueryAuthor = supervisor1000, QueryTime = DateTime.Today.AddDays(-3110), QueryText = "Query3110"};
                query3210 = new Query { Id = 3210, Question = question321, QueryAuthor = supervisor2000, QueryTime = DateTime.Today.AddDays(-3210), QueryText = "Query3210"};
                query3310 = new Query { Id = 3310, Question = question331, QueryAuthor = supervisor1000, QueryTime = DateTime.Today.AddDays(-3310), QueryText = "Query3310"};
                query3410 = new Query { Id = 3410, Question = question341, QueryAuthor = supervisor2000, QueryTime = DateTime.Today.AddDays(-3410), QueryText = "Query3410"};
                query3510 = new Query { Id = 3510, Question = question351, QueryAuthor = supervisor1000, QueryTime = DateTime.Today.AddDays(-3510), QueryText = "Query3510"};
            }
            public void GetEditForm_Electrocardiogram_NonExistingFormData_CorrectView_Test()
            {
                //Arrange
                EmulateControllerContext(formController, false);
                var form = new Form {
                    Id = 106,
                    Caption = "TestForm106",
                    Visit = new Visit {Caption = "TestVisit106", Patient = new Patient {PatientNumber = 206}},
                    FormType = FormType.Electrocardiogram
                };
                fRep.Setup(fr => fr.GetForm(form.Visit.Patient.PatientNumber, form.Visit.Caption, form.Caption)).Returns(form);
                efdRep.Setup(er => er.GetFormDataByFormId(form.Id)).Returns((ElectrocardiogramFormData) null);

                //Act
                var viewResult = formController.ViewForm(form.Visit.Patient.PatientNumber, form.Visit.Caption, form.Caption);

                //Assert
                Assert.That(viewResult, Is.Not.Null);
                Assert.That(viewResult is ViewResultBase);
                var viewResultBase = viewResult as ViewResultBase;
                Assert.That(viewResultBase.Model, Is.Not.Null);
                Assert.That(viewResultBase.ViewName, Is.EqualTo("ErrorInfo"));
                Assert.That(viewResultBase.Model is ErrorViewModel);
                var model = viewResultBase.Model as ErrorViewModel;
                Assert.That(model.Caption, Is.EqualTo("Form is not found"));

                VerifyRepositoriesCalls(form);
            }
            public void PostEditDemographicFormData_ChangedConfirmedValues_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 DemographicFormData {
                    Id = 12,
                    Form = form,
                    DateOfBirth = new Question() { Id = 14, Caption = "Date of Birth", Value = new DateTime(2011, 10, 11).ToString(CultureInfo.InvariantCulture) },
                    Other = new Question() { Id = 15, Caption = "Other", Value = String.Empty },
                    Race = new Question() { Id = 16, Caption = "Race", Value = "4" },
                    Sex = new Question() { Id = 17, Caption = "Sex", Value = "1" }
                };
                var incomingModel = new DemographicFormViewModel {
                    Id = 12,
                    FormId = 13,
                    DateOfBirth = new DateTime(2012, 1, 24),
                    Race = 3,
                    Sex = 0,
                    Other = "Some text"
                };
                incomingModel.ChangeInfos = new List<ChangeNoteViewModel> {
                    new ChangeNoteViewModel{QuestionId = formData.DateOfBirth.Id, ChangeReason = "DateOfBirth Reason"},
                    new ChangeNoteViewModel{QuestionId = formData.Race.Id, ChangeReason = "Race Reason"},
                    new ChangeNoteViewModel{QuestionId = formData.Sex.Id, ChangeReason = "Sex Reason"},
                    new ChangeNoteViewModel{QuestionId = formData.Other.Id, ChangeReason = "Other Reason"},
                };

                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
                dfdRep.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
                DemographicFormData demogFormData = null;
                dfdRep.Setup(r => r.Edit(It.IsAny<DemographicFormData>())).Callback<DemographicFormData>(fd => demogFormData = fd);
                //Act
                var result = formController.EditDemographicForm(incomingModel);

                //Assert
                CheckEditFormDataRedirectRouteValues(result, form);

                dfdRep.Verify(r => r.GetByKey(incomingModel.Id), Times.Once());
                dfdRep.Verify(r => r.Edit(It.IsAny<DemographicFormData>()), Times.Once());
                dfdRep.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(demogFormData, Is.Not.Null);
                Assert.That(demogFormData.Id, Is.EqualTo(incomingModel.Id));

                Assert.That(demogFormData.DateOfBirth.Value,
                            Is.EqualTo(new DateTime(2012, 1, 24).ToString(CultureInfo.InvariantCulture)));
                Assert.That(demogFormData.Race.Value, Is.EqualTo("3"));
                Assert.That(demogFormData.Sex.Value, Is.EqualTo("0"));
                Assert.That(demogFormData.Other.Value, Is.EqualTo("Some text"));
                changeNoteBuilder.Verify(b => b.CreateChangeNote(It.IsAny<Question>(),
                    It.IsAny<string>(),
                    It.IsAny<string>(),
                    It.IsAny<string>()), Times.Exactly(4));
            }
            public void GetEditForm_UndefinedFormType_CorrectView_Test()
            {
                //Arrange
                EmulateControllerContext(formController, false);
                var form = new Form {
                    Id = 102,
                    Caption = "TestForm102",
                    Visit = new Visit {Caption = "TestVisit102", Patient = new Patient {PatientNumber = 202}},
                    FormType = FormType.None
                };
                fRep.Setup(fr => fr.GetForm(form.Visit.Patient.PatientNumber, form.Visit.Caption, form.Caption)).Returns(form);

                //Act
                var viewResult = formController.EditForm(form.Visit.Patient.PatientNumber, form.Visit.Caption, form.Caption);

                //Assert
                Assert.That(viewResult, Is.Not.Null);
                Assert.That(viewResult is ViewResultBase);
                var viewResultBase = viewResult as ViewResultBase;
                Assert.That(viewResultBase.Model, Is.Not.Null);
                Assert.That(viewResultBase.ViewName, Is.EqualTo("ErrorInfo"));
                Assert.That(viewResultBase.Model is ErrorViewModel);
                var model = viewResultBase.Model as ErrorViewModel;
                Assert.That(model.Caption, Is.EqualTo("Form is not found"));

                VerifyRepositoriesCalls(form, true);
            }
            public void PostEditHappinessFormData_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 HappinessFormData {
                    Id = 12,
                    Form = form,
                    HappinessLevel = new Question() { Id = 75, Value = "75" }
                };
                var incomingModel = new HappinessFormViewModel {
                    Id = 12,
                    FormId = 13,
                    HappinessLevel = 50
                };

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

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

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

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

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

                AssertChangeInfo(formData.HappinessLevel, "Pretty Happy", "Happy", model);
            }
            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);
            }
            public void PostEditDemographicFormData_NonExistingFormData_Test()
            {
                //Arrange
                var form = new Form {
                    Id = 13,
                    FormState = FormState.Incomplete
                };
                var model = new DemographicFormViewModel {
                    Id = 12,
                    FormId = 13,
                    DateOfBirth = new DateTime(2012, 1, 24),
                    Race = 1,
                    Sex = 2,
                    Other = "Some text"
                };
                fRep.Setup(r => r.GetByKey(13)).Returns(form);

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

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

                //Assert
                CheckErrorInfoViewResult(result);
            }
            public void PostEditDemographicFormData_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 DemographicFormData {
                    Id = 12,
                    Form = form,
                    DateOfBirth = new Question() { Value = new DateTime(2011, 10, 11).ToString(CultureInfo.InvariantCulture) },
                    Other = new Question() { Value = String.Empty },
                    Race = new Question() { Value = "4" },
                    Sex = new Question() { Value = "2" }
                };
                var model = new DemographicFormViewModel {
                    Id = 12,
                    FormId = 13,
                    DateOfBirth = new DateTime(2012, 1, 24),
                    Race = 3,
                    Sex = 1,
                    Other = "Some text"
                };

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

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

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

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

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

                //Assert
                CheckEditFormDataRedirectRouteValues(result, form);

                dfdRep.Verify(r => r.GetByKey(model.Id), Times.Once());
                dfdRep.Verify(r => r.Edit(It.IsAny<DemographicFormData>()), Times.Once());
                dfdRep.Verify(r => r.Save(), Times.Once());

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

                Assert.That(demogFormData, Is.Not.Null);
                Assert.That(demogFormData.Id, Is.EqualTo(model.Id));
                Assert.That(demogFormData.DateOfBirth.Value,
                            Is.EqualTo(new DateTime(2012, 1, 24).ToString(CultureInfo.InvariantCulture)));
                Assert.That(demogFormData.Race.Value, Is.EqualTo("3"));
                Assert.That(demogFormData.Sex.Value, Is.EqualTo("1"));
                Assert.That(demogFormData.Other.Value, Is.EqualTo("Some text"));

                Assert.That(demogForm, Is.Not.Null);
                Assert.That(demogForm.Id, Is.EqualTo(model.FormId));
                Assert.That(demogForm.FormState, Is.EqualTo(FormState.Completed));
            }
            public void PostEditDemographicFormData_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 DemographicFormData {
                    Id = 12,
                    Form = form,
                    DateOfBirth = new Question() { Id = 14, Caption  = "Date of Birth", Value = new DateTime(2011, 10, 11).ToString(CultureInfo.InvariantCulture)},
                    Other = new Question() { Id = 15, Caption = "Other", Value = String.Empty },
                    Race = new Question() { Id = 16, Caption = "Race", Value = "4" },
                    Sex = new Question() { Id = 17, Caption = "Sex", Value = "1" }
                };
                var incomingModel = new DemographicFormViewModel {
                    Id = 12,
                    FormId = 13,
                    DateOfBirth = new DateTime(2012, 1, 24),
                    Race = 3,
                    Sex = 0,
                    Other = "Some text"
                };

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

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

                //Assert
                dfdRep.Verify(r => r.GetByKey(incomingModel.Id), Times.Once());
                dfdRep.Verify(r => r.Edit(It.IsAny<DemographicFormData>()), Times.Never());
                dfdRep.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);
                DemographicFormViewModel model = viewResultBase.Model as DemographicFormViewModel;
                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.DateOfBirth, DateTime.Parse(formData.DateOfBirth.Value).ToShortDateString(), incomingModel.DateOfBirth.Value.ToShortDateString(), model);
                AssertChangeInfo(formData.Race, "American Indian", "Asian", model);
                AssertChangeInfo(formData.Sex, "Female", "Male", model);
                AssertChangeInfo(formData.Other, string.Empty, "Some text", model);
            }
            public void PostEditVitalsFormData_NonExistingFormData_Test()
            {
                //Arrange
                var form = new Form {
                    Id = 13,
                    FormState = FormState.Incomplete
                };
                var model = new VitalsFormViewModel {
                    Id = 12,
                    FormId = 13,
                    ActualTime = new DateTime(2012, 10, 11, 15, 16, 17),
                    Height = 171,
                    Weight = 51,
                    HeartRate = 21,
                    Temperature = 31,
                    BloodPressureSystolic = "121",
                    BloodPressureDiastolic = "71"
                };
                fRep.Setup(r => r.GetByKey(13)).Returns(form);

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

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

                //Assert
                CheckErrorInfoViewResult(result);
            }
            private void CheckEditFormDataRedirectRouteValues(ActionResult result, Form form)
            {
                Assert.That(result, Is.Not.Null);
                Assert.That(result is RedirectToRouteResult);

                var viewResultBase = result as RedirectToRouteResult;
                Assert.That(viewResultBase.RouteValues, Is.Not.Null);
                Assert.That(viewResultBase.RouteValues.Count, Is.GreaterThanOrEqualTo(5));

                Assert.That(viewResultBase.RouteValues.Where(x => x.Key == "action").FirstOrDefault(), Is.Not.Null);
                Assert.That(viewResultBase.RouteValues.Where(x => x.Key == "action").FirstOrDefault().Value, Is.EqualTo("ViewForm"));

                Assert.That(viewResultBase.RouteValues.Where(x => x.Key == "controller").FirstOrDefault(), Is.Not.Null);
                Assert.That(viewResultBase.RouteValues.Where(x => x.Key == "controller").FirstOrDefault().Value, Is.EqualTo("Form"));

                Assert.That(viewResultBase.RouteValues.Where(x => x.Key == "patientNumber").FirstOrDefault(), Is.Not.Null);
                Assert.That(viewResultBase.RouteValues.Where(x => x.Key == "patientNumber").FirstOrDefault().Value,
                            Is.EqualTo(form.Visit.Patient.PatientNumber));

                Assert.That(viewResultBase.RouteValues.Where(x => x.Key == "visitName").FirstOrDefault(), Is.Not.Null);
                Assert.That(viewResultBase.RouteValues.Where(x => x.Key == "visitName").FirstOrDefault().Value,
                            Is.EqualTo(form.Visit.Caption));

                Assert.That(viewResultBase.RouteValues.Where(x => x.Key == "formName").FirstOrDefault(), Is.Not.Null);
                Assert.That(viewResultBase.RouteValues.Where(x => x.Key == "formName").FirstOrDefault().Value,
                            Is.EqualTo(form.Caption));
            }
 private void SetupFormDataRepositories(Form form)
 {
     //Arrange
     dfdRep.Setup(dr => dr.GetFormDataByFormId(form.Id)).Returns(new DemographicFormData {
         Form = form,
         DateOfBirth =
                                                                     new Question(),
         Other = new Question(),
         Race = new Question(),
         Sex = new Question()
     });
     vfdRep.Setup(vr => vr.GetFormDataByFormId(form.Id)).Returns(new VitalsFormData {
         Form = form,
         ActualTime = new Question(),
         Height = new Question(),
         Weight = new Question(),
         Temperature = new Question(),
         HeartRate = new Question(),
         BloodPressureDiastolic =
                                                                     new Question(),
         BloodPressureSystolic =
                                                                     new Question()
     });
     hfdRep.Setup(hr => hr.GetFormDataByFormId(form.Id)).Returns(new HappinessFormData {
         Form = form,
         HappinessLevel = new Question()
     });
     efdRep.Setup(er => er.GetFormDataByFormId(form.Id)).Returns(new ElectrocardiogramFormData {
         Form = form,
         ElectrocardiogramActualTime =
                                                                     new Question()
     });
     ifdRep.Setup(ir => ir.GetFormDataByFormId(form.Id)).Returns(new InventoryFormData {
         Form = form,
         BatchNumber =
                                                                     new Question(),
         MedicationUsage = new List<RepeatableInventoryData>(),
         QuantityShipped =
                                                                     new Question(),
         ReceiptDate =
                                                                     new Question(),
         ShipDate = new Question()
     });
     afdRep.Setup(ar => ar.GetFormDataByFormId(form.Id)).Returns(new AdverseEventFormData {
         Form = form,
         AdverseExperience =
                                                                     new Question(),
         EndDate =
                                                                     new Question(),
         EndTime =
                                                                     new Question(),
         Intensity =
                                                                     new Question(),
         OnsetDate =
                                                                     new Question(),
         OnsetTime =
                                                                     new Question(),
         Outcome =
                                                                     new Question(),
         RelationshipToInvestigationalDrug
                                                                     = new Question()
     });
 }
            public void PostEditHappinessFormData_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 HappinessFormData {
                    Id = 12,
                    Form = form,
                    HappinessLevel = new Question() { Id = 75, Value = "75" }
                };
                var model = new HappinessFormViewModel {
                    Id = 12,
                    FormId = 13,
                    HappinessLevel = 91
                };

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

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

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

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

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

                //Assert
                CheckEditFormDataRedirectRouteValues(result, form);

                hfdRep.Verify(r => r.GetByKey(model.Id), Times.Once());
                hfdRep.Verify(r => r.Edit(It.IsAny<HappinessFormData>()), Times.Once());
                hfdRep.Verify(r => r.Save(), Times.Once());

                fRep.Verify(r => r.GetByKey(model.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(model.Id));
                Assert.That(savedFormData.HappinessLevel.Value, Is.EqualTo("91"));

                Assert.That(savedForm, Is.Not.Null);
                Assert.That(savedForm.Id, Is.EqualTo(model.FormId));
                Assert.That(savedForm.FormState, Is.EqualTo(FormState.Completed));
            }
            public void GetEditForm_Demographic_CorrectView_Test()
            {
                //Arrange
                EmulateControllerContext(formController, false);
                var form = new Form {
                    Id = 31,
                    Caption = "TestForm32",
                    Visit = new Visit {Caption = "TestVisit33", Patient = new Patient {PatientNumber = 34}},
                    FormType = FormType.Demographics,
                    FormState = FormState.Completed
                };
                fRep.Setup(fr => fr.GetForm(form.Visit.Patient.PatientNumber, form.Visit.Caption, form.Caption)).Returns(form);
                SetupFormDataRepositories(form);

                //Act
                var viewResult = formController.ViewForm(form.Visit.Patient.PatientNumber, form.Visit.Caption, form.Caption);

                //Assert
                Assert.That(viewResult, Is.Not.Null);
                Assert.That(viewResult is PartialViewResult);
                var viewResultBase = viewResult as ViewResultBase;
                Assert.That(viewResultBase.Model, Is.Not.Null);
                Assert.That(viewResultBase.ViewName, Is.EqualTo("_ViewDemographicForm"));
                Assert.That(viewResultBase.Model is DemographicFormViewModel);

                VerifyRepositoriesCalls(form);
            }
 private void VerifyRepositoriesCalls(Form form, bool zeroCallsOnFormData = false)
 {
     fRep.Verify(r => r.GetForm(form.Visit.Patient.PatientNumber, form.Visit.Caption, form.Caption), Times.Once());
     dfdRep.Verify(r => r.GetFormDataByFormId(form.Id),
                   !zeroCallsOnFormData && form.FormType == FormType.Demographics ? Times.Once() : Times.Never());
     vfdRep.Verify(r => r.GetFormDataByFormId(form.Id),
                   !zeroCallsOnFormData && form.FormType == FormType.Vitals ? Times.Once() : Times.Never());
     hfdRep.Verify(r => r.GetFormDataByFormId(form.Id),
                   !zeroCallsOnFormData && form.FormType == FormType.Happiness ? Times.Once() : Times.Never());
     efdRep.Verify(r => r.GetFormDataByFormId(form.Id),
                   !zeroCallsOnFormData && form.FormType == FormType.Electrocardiogram ? Times.Once() : Times.Never());
     ifdRep.Verify(r => r.GetFormDataByFormId(form.Id),
                   !zeroCallsOnFormData && form.FormType == FormType.Inventory ? Times.Once() : Times.Never());
     afdRep.Verify(r => r.GetFormDataByFormId(form.Id),
                   !zeroCallsOnFormData && form.FormType == FormType.AdverseEvent ? Times.Once() : Times.Never());
 }
            public void PostEditHappinessFormData_NonExistingFormData_Test()
            {
                //Arrange
                var form = new Form {
                    Id = 13,
                    FormState = FormState.Incomplete
                };
                var model = new HappinessFormViewModel {
                    Id = 12,
                    FormId = 13,
                    HappinessLevel = 90
                };
                fRep.Setup(r => r.GetByKey(13)).Returns(form);

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

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

                //Assert
                CheckErrorInfoViewResult(result);
            }
            public void GetEditForm_Electrocardiogram_CorrectView_Test()
            {
                //Arrange
                EmulateControllerContext(formController, false);
                var form = new Form {
                    Id = 37,
                    Caption = "TestForm38",
                    Visit = new Visit {Caption = "TestVisit39", Patient = new Patient {PatientNumber = 40}},
                    FormType = FormType.Electrocardiogram
                };
                fRep.Setup(fr => fr.GetForm(form.Visit.Patient.PatientNumber, form.Visit.Caption, form.Caption)).Returns(form);
                SetupFormDataRepositories(form);

                //Act
                var viewResult = formController.ViewForm(form.Visit.Patient.PatientNumber, form.Visit.Caption, form.Caption);

                //Assert
                Assert.That(viewResult, Is.Not.Null);
                Assert.That(viewResult is PartialViewResult);
                var viewResultBase = viewResult as ViewResultBase;
                Assert.That(viewResultBase.Model, Is.Not.Null);
                Assert.That(viewResultBase.ViewName, Is.EqualTo("_ViewElectrocardiogramForm"));
                Assert.That(viewResultBase.Model is ElectrocardiogramFormViewModel);

                VerifyRepositoriesCalls(form);
            }
            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);
            }
            private void PreparePatientsReportData()
            {
                form11111 = new Form {Id = 11112, FormState = FormState.Incomplete};
                form11112 = new Form {Id = 11112, FormState = FormState.Completed};
                form11121 = new Form {Id = 11122, FormState = FormState.Completed};
                form11122 = new Form {Id = 11122, FormState = FormState.Incomplete};

                visit1111 = new Visit {
                    Id = 1111,
                    Caption = "Visit1111",
                    VisitDate = DateTime.Today.AddDays(-11),
                    Forms = new List<Form> {form11111, form11112}
                };
                visit1112 = new Visit {
                    Id = 1112,
                    Caption = "Visit1112",
                    VisitDate = DateTime.Today.AddDays(-12),
                    Forms = new List<Form> {form11121, form11122}
                };

                patient111 = new Patient {
                    Id = 111,
                    PatientInitials = "P111",
                    RandomisationDate = DateTime.Today.AddDays(-11),
                    RandomisationNumber = 111,
                    EnrollDate = DateTime.Today.AddDays(11),
                    IsEnrolled = true,
                    IsActive = true,
                    Visits = new List<Visit> {visit1111, visit1112}
                };
                visit1111.Patient = visit1112.Patient = patient111;

                form12111 = new Form {Id = 12111, FormState = FormState.Incomplete};
                form12112 = new Form {Id = 12112, FormState = FormState.Completed};
                form12121 = new Form {Id = 12121, FormState = FormState.Incomplete};
                form12122 = new Form {Id = 12122, FormState = FormState.Incomplete};
                form12211 = new Form {Id = 12211, FormState = FormState.Completed};
                form12212 = new Form {Id = 12212, FormState = FormState.Completed};
                form12221 = new Form {Id = 12221, FormState = FormState.Incomplete};
                form12222 = new Form {Id = 12222, FormState = FormState.Completed};

                visit1211 = new Visit {
                    Id = 1211,
                    Caption = "Visit1211",
                    VisitDate = DateTime.Today.AddDays(-11),
                    Forms = new List<Form> {form12111, form12112}
                };
                visit1212 = new Visit {
                    Id = 1212,
                    Caption = "Visit1212",
                    VisitDate = DateTime.Today.AddDays(-12),
                    Forms = new List<Form> {form12121, form12122}
                };
                visit1221 = new Visit {
                    Id = 1221,
                    Caption = "Visit1221",
                    VisitDate = DateTime.Today.AddDays(-21),
                    Forms = new List<Form> {form12211, form12212}
                };
                visit1222 = new Visit {
                    Id = 1222,
                    Caption = "Visit1222",
                    VisitDate = DateTime.Today.AddDays(-22),
                    Forms = new List<Form> {form12221, form12222}
                };

                patient121 = new Patient {
                    Id = 121,
                    PatientInitials = "P121",
                    RandomisationDate = DateTime.Today.AddDays(-21),
                    RandomisationNumber = 121,
                    EnrollDate = DateTime.Today.AddDays(21),
                    IsEnrolled = true,
                    IsActive = true,
                    Visits = new List<Visit> {visit1211, visit1212}
                };
                visit1211.Patient = visit1212.Patient = patient121;

                patient122 = new Patient {
                    Id = 122,
                    PatientInitials = "P122",
                    RandomisationDate = DateTime.Today.AddDays(-22),
                    RandomisationNumber = 122,
                    EnrollDate = DateTime.Today.AddDays(22),
                    IsEnrolled = true,
                    IsActive = true,
                    Visits = new List<Visit> {visit1221, visit1222}
                };
                visit1221.Patient = visit1222.Patient = patient122;

                form21111 = new Form {Id = 21111, FormState = FormState.Incomplete};
                form21112 = new Form {Id = 21112, FormState = FormState.Completed};
                form21121 = new Form {Id = 21121, FormState = FormState.Completed};
                form21122 = new Form {Id = 21122, FormState = FormState.Incomplete};
                form21211 = new Form {Id = 21211, FormState = FormState.Incomplete};
                form21212 = new Form {Id = 21212, FormState = FormState.Completed};
                form21221 = new Form {Id = 21221, FormState = FormState.Completed};
                form21222 = new Form {Id = 21222, FormState = FormState.Incomplete};

                visit2111 = new Visit {
                    Id = 2111,
                    Caption = "Visit2111",
                    VisitDate = DateTime.Today.AddDays(-11),
                    Forms = new List<Form> {form21111, form21112}
                };
                visit2112 = new Visit {
                    Id = 2112,
                    Caption = "Visit2112",
                    VisitDate = DateTime.Today.AddDays(-12),
                    Forms = new List<Form> {form21121, form21122}
                };
                visit2121 = new Visit {
                    Id = 2121,
                    Caption = "Visit2121",
                    VisitDate = DateTime.Today.AddDays(-11),
                    Forms = new List<Form> {form21211, form21212}
                };
                visit2122 = new Visit {
                    Id = 2122,
                    Caption = "Visit2122",
                    VisitDate = DateTime.Today.AddDays(-12),
                    Forms = new List<Form> {form21221, form21222}
                };

                patient211 = new Patient {
                    Id = 211,
                    PatientInitials = "P211",
                    RandomisationDate = DateTime.Today.AddDays(-11),
                    RandomisationNumber = 211,
                    EnrollDate = DateTime.Today.AddDays(11),
                    IsEnrolled = true,
                    IsActive = true,
                    Visits = new List<Visit> {visit2111, visit2112}
                };
                visit2111.Patient = visit2112.Patient = patient211;

                patient212 = new Patient {
                    Id = 212,
                    PatientInitials = "P212",
                    RandomisationDate = DateTime.Today.AddDays(-11),
                    RandomisationNumber = 212,
                    EnrollDate = DateTime.Today.AddDays(11),
                    IsEnrolled = true,
                    IsActive = true,
                    Visits = new List<Visit> {visit2121, visit2122}
                };
                visit2121.Patient = visit2122.Patient = patient212;
            }
            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));
            }
        private void FillVitalsBasedOnPreviousVisit(VitalsFormData vitalsOriginalFormData, VitalsFormData vitalsFormData,
		                                            Form vitalsForm)
        {
            vitalsForm.FormState = FormState.Completed;
            vitalsFormData.Height.Value = vitalsOriginalFormData.Height.Value;
            double weight = double.Parse(vitalsOriginalFormData.Weight.Value);
            weight = Math.Round(weight + randomGenerator.NextDouble()*3 - 1.5d, 1);
            vitalsFormData.Weight.Value = weight.ToString();

            vitalsFormData.BloodPressureDiastolic.Value = vitalsOriginalFormData.BloodPressureDiastolic.Value;
            vitalsFormData.BloodPressureSystolic.Value = vitalsOriginalFormData.BloodPressureSystolic.Value;

            var temperature = GetRandomTemperature();
            vitalsFormData.Temperature.Value = temperature.ToString();
            var heartRate = GetRandomHeartRate();
            vitalsFormData.HeartRate.Value = heartRate.ToString();
        }
            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);
            }