public void CreateAdverseEventTest()
        {
            //Arrange
            var studyDesign = new Mock<IClinicalStudyDesign>();
            clinicalDesignFactory.Setup(factory => factory.Create()).Returns(studyDesign.Object);
            EmulateControllerContext(controller, false);

            var visit = new Visit() {
                Id = 16,
                Caption = "Adverse Event 4",
                Patient = new Patient() {PatientNumber = 25}
            };

            studyDesign.Setup(sd => sd.AddAdverseEventVisit(3)).Returns(visit);

            //Act
            var result = controller.CreateAdverseEvent(3);

            //Assert
            studyDesign.Verify(sd => sd.AddAdverseEventVisit(3), Times.Once());
            Assert.That(result, Is.Not.Null);
            Assert.That(result is ContentResult);
            var contentResult = result as ContentResult;
            Assert.That(contentResult.Content, Is.EqualTo(visit.Caption));
        }
        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 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 MappingTest()
        {
            //Arrange
            var dataStorage = new Mock<IDataStorage>();
            var entity = new Visit {
                Id = 1,
                Caption = "old",
                OrderNo = 1,
                ExpectedVisitDate = DateTime.Today.AddDays(-1),
                VisitDate = DateTime.Today.AddDays(-2),
                VisitTime = DateTime.Now,
                VisitType = VisitType.AdverseEventVisit,
                Patient = new Patient()
            };
            var updatedEntity = new Visit
            {
                Id = 1,
                Caption = "new",
                OrderNo = 2,
                ExpectedVisitDate = DateTime.Today.AddDays(-2),
                VisitDate = DateTime.Today.AddDays(-3),
                VisitTime = DateTime.Now,
                VisitType = VisitType.Day1,
                Patient = new Patient()
            };
            var repository = new VisitRepository(dataStorage.Object);

            dataStorage.Setup(ds => ds.GetData<Visit>()).Returns(new List<Visit> { entity });
            //Act
            repository.Edit(updatedEntity);
            //Assert
            //Values are equal
            Assert.That(entity.Caption, Is.EqualTo(updatedEntity.Caption));
            Assert.That(entity.ExpectedVisitDate, Is.EqualTo(updatedEntity.ExpectedVisitDate));
            Assert.That(entity.OrderNo, Is.EqualTo(updatedEntity.OrderNo));
            Assert.That(entity.VisitDate, Is.EqualTo(updatedEntity.VisitDate));
            Assert.That(entity.VisitTime, Is.EqualTo(updatedEntity.VisitTime));
            Assert.That(entity.VisitType, Is.EqualTo(updatedEntity.VisitType));
            Assert.That(entity.Patient, Is.EqualTo(updatedEntity.Patient));
        }
        public void IndexModelTest()
        {
            //Arrange
            EmulateControllerContext(controller, false);

            var visit = new Visit() {
                Id = 1,
                Caption = "Baseline",
                ExpectedVisitDate = DateTime.Now.Date,
                OrderNo = 1,
                Forms = new List<Form>(),
                Patient =
                    new Patient {Caption = "Subj 027", PatientNumber = 27}
            };
            visitRepository
                .Setup(r => r.GetDailyVisits(CommonEmulatedUserName, DateTime.Now.Date))
                .Returns(new List<Visit> {
                    visit
                });
            //Act
            var result = controller.Index(null);

            //Assert
            Assert.That(result, Is.Not.Null);
            Assert.That(result is ViewResultBase);
            var partialResult = result as ViewResultBase;
            Assert.That(partialResult.Model, Is.Not.Null);
            var model = partialResult.Model as DailyScheduleViewModel;

            visitRepository.Verify(r => r.GetDailyVisits(CommonEmulatedUserName, DateTime.Now.Date), Times.Once());

            Assert.That(model.ScheduledVisits, Is.Not.Null);
            Assert.That(model.ScheduledVisits.Count, Is.EqualTo(1));
            var visitModel = model.ScheduledVisits[0];
            Assert.That(visitModel.VisitCaption, Is.EqualTo(visit.Caption));
            Assert.That(visitModel.PatientInitials, Is.EqualTo(visit.Patient.PatientInitials));
            Assert.That(visitModel.PatientNumber, Is.EqualTo(visit.Patient.PatientNumber));
            Assert.That(visitModel.VisitState, Is.EqualTo(visit.IsFullyCompleted ? "Completed" : "Incomplete"));
        }
        public void VisitDataContainerModel()
        {
            //Arrange
            EmulateControllerContext(controller, false);
            var visit = new Visit {
                Id = 125,
                Caption = "Baseline",
                Forms = new List<Form> {
                    new Form {Id = 100, Caption = "Vitals", OrderNo = 1},
                    new Form {Id = 200, Caption = "Demographic", OrderNo = 0}
                }
            };
            repository
                .Setup(r => r.GetVisitByPatientNumberAndVisitName(It.IsAny<int>(), It.IsAny<string>()))
                .Returns(visit);

            //Act
            var result = controller.VisitDataContainer(25, "Baseline", null);

            //Assert
            Assert.That(result, Is.Not.Null);
            Assert.That(result is ViewResultBase);
            var view = result as ViewResultBase;
            var model = view.Model as DataContainerViewModel;
            Assert.That(model, Is.Not.Null);

            Assert.That(model.Id, Is.EqualTo(visit.Id));
            Assert.That(model.PatientNumber, Is.EqualTo(25));
            Assert.That(model.SelectedVisitName, Is.EqualTo(visit.Caption));
            Assert.That(model.Children, Is.Not.Null);
            Assert.That(model.Children.Count, Is.EqualTo(2));
            Assert.That(model.Children[0].Id, Is.EqualTo(200));
            Assert.That(model.Children[0].Caption, Is.EqualTo("Demographic"));

            Assert.That(model.Children[1].Id, Is.EqualTo(100));
            Assert.That(model.Children[1].Caption, Is.EqualTo("Vitals"));
        }
        private void SetRealVisitDateAndTime(Visit visit, DateTime visitDate)
        {
            visit.VisitDate = visitDate;
            int visitHour = randomGenerator.Next(0, 8) + 8;
            int visitMinute = randomGenerator.Next(0, 60);
            visitMinute = visitMinute - visitMinute%10;

            visit.VisitTime = visit.VisitDate.Value.Date;
            visit.VisitTime = visit.VisitTime.Value.AddHours(visitHour);
            visit.VisitTime = visit.VisitTime.Value.AddMinutes(visitMinute);
            visit.IsCompleted = true;
        }
        private void PopulateDay10Visit(Visit day10Visit, Visit day1Visit)
        {
            //happiness form
            int dice = randomGenerator.Next(100);
            if (dice < FormIsFilledChance) {
                var happinessOriginalForm = day1Visit.Forms.First(f => f.FormType == FormType.Happiness);
                if (happinessOriginalForm.FormState == FormState.Completed) {
                    var happinessOriginalFormData = happinessFormDataRepository.GetFormDataByFormId(happinessOriginalForm.Id);

                    var happinessForm = day10Visit.Forms.First(f => f.FormType == FormType.Happiness);
                    var happinessFormData = happinessFormDataRepository.GetFormDataByFormId(happinessForm.Id);

                    int originalHappinessLevel = int.Parse(happinessOriginalFormData.HappinessLevel.Value);

                    dice = randomGenerator.Next(100);
                    int happinessLevel = 0;
                    if (dice < 50)
                        happinessLevel = originalHappinessLevel + 25;
                    else if (dice < 75)
                        happinessLevel = originalHappinessLevel + 50;
                    else if (dice < 95)
                        happinessLevel = originalHappinessLevel;
                    else
                        happinessLevel = originalHappinessLevel - 25;

                    happinessLevel = Math.Min(happinessLevel, 100);
                    happinessLevel = Math.Max(happinessLevel, 0);
                    happinessFormData.HappinessLevel.Value = happinessLevel.ToString();

                    happinessForm.FormState = FormState.Completed;
                }
            }
            //inventory form
            dice = randomGenerator.Next(100);
            if (dice < FormIsFilledChance) {
                var inventoryForm = day10Visit.Forms.First(f => f.FormType == FormType.Inventory);
                inventoryForm.FormState = FormState.Completed;
                var inventoryFormData = inventoryFormDataRepository.GetFormDataByFormId(inventoryForm.Id);
                var quantityShipped = Math.Round((double) randomGenerator.Next(8)/2, 1) + 4;
                var shipDate = day1Visit.VisitDate.Value.AddDays(randomGenerator.Next(2));
                inventoryFormData.QuantityShipped.Value = quantityShipped.ToString();
                inventoryFormData.ReceiptDate.Value = day1Visit.VisitDate.ToString();
                inventoryFormData.ShipDate.Value = shipDate.ToString();
                inventoryFormData.BatchNumber.Value = randomGenerator.Next(1000).ToString();

                FillInvetnoryFormMedicationUsage(quantityShipped, shipDate, inventoryFormData);
            }

            //vitals form
            dice = randomGenerator.Next(100);
            if (dice < FormIsFilledChance) {
                var vitalsOriginalForm = day1Visit.Forms.First(f => f.FormType == FormType.Vitals);
                if (vitalsOriginalForm.FormState == FormState.Completed) {

                    var vitalsOriginalFormData = vitalsFormDataRepository.GetFormDataByFormId(vitalsOriginalForm.Id);

                    var vitalsForm = day10Visit.Forms.First(f => f.FormType == FormType.Vitals);
                    vitalsForm.FormState = FormState.Completed;
                    var vitalsFormData = vitalsFormDataRepository.GetFormDataByFormId(vitalsForm.Id);

                    vitalsFormData.ActualTime.Value = day10Visit.VisitTime.Value.AddMinutes(randomGenerator.Next(18)*10).ToString();

                    FillVitalsBasedOnPreviousVisit(vitalsOriginalFormData, vitalsFormData, vitalsForm);
                }
            }
        }
 private void CheckVisitData(DataRow dataRow, Patient patient, Visit visit)
 {
     Assert.That(dataRow is PatientsReportDataSet.VisitsRow);
     var row = dataRow as PatientsReportDataSet.VisitsRow;
     Assert.That(row, Is.Not.Null);
     Assert.That(row.VisitId, Is.EqualTo(visit.Id));
     Assert.That(row.PatientId, Is.EqualTo(patient.Id));
     Assert.That(row.VisitDate, Is.EqualTo(visit.VisitDate));
     Assert.That(row.VisitName, Is.EqualTo(visit.Caption));
     Assert.That(row.Progress, Is.EqualTo(visit.Forms.Average(x => x.FormState == FormState.Completed ? 1 : 0)));
 }
        private ActionResult ViewEditVisit(bool? isVisitEditing, Visit visit)
        {
            if (visit == null) {
                var errorModel = new ErrorViewModel {
                    Caption = "Visit is not found",
                    ErrorMessage = string.Format("Visit is not found in clinical data")
                };
                return View("ErrorInfo", errorModel);
            }

            var model = new VisitViewModel {
                Id = visit.Id,
                Caption = visit.Caption,
                VisitType = visit.VisitType,
                ExpectedVisitDate = visit.ExpectedVisitDate,
                VisitDate = visit.VisitDate,
                VisitTime = visit.VisitTime,
                OrderNo = visit.OrderNo,
                NoExpectedVisitDate = visit.VisitType == VisitType.AdverseEventVisit
            };

            if (visit.IsCompleted && !(isVisitEditing ?? false))
                return PartialView("_ViewVisit", model);
            return PartialView("_EditVisit", model);
        }
            public void CorrectModelMapping()
            {
                //Arrange
                var dataStorage = new Mock<IDataStorage>();
                var repository = new VisitRepository(dataStorage.Object);

                var clinic = new Clinic { Caption = "Clinic" };
                var doctor1 = new User { FirstName = "DoctorFirst1", LastName = "DoctorLast1", Clinic = clinic };
                var patient1 = new Patient { Doctor = doctor1 };
                var visit1B = new Visit { Caption = "Visit1B", Patient = patient1, VisitType = VisitType.Baseline, Forms = new List<Form>{new Form { Id = 1, FormType = FormType.Demographics}}};
                var visit1Ae1 = new Visit { Caption = "Visit1Ae1", Patient = patient1, VisitType = VisitType.AdverseEventVisit, Forms = new List<Form> { new Form { Id = 2, FormType = FormType.AdverseEvent } } };
                var visit1Ae2 = new Visit { Caption = "Visit1Ae2", Patient = patient1, VisitType = VisitType.AdverseEventVisit, Forms = new List<Form> { new Form { Id = 3, FormType = FormType.AdverseEvent } } };
                patient1.Visits = new List<Visit> {visit1B, visit1Ae1, visit1Ae2};

                var doctor2 = new User { FirstName = "DoctorFirst2", LastName = "DoctorLast2", Clinic = clinic };
                var patient2 = new Patient { Doctor = doctor2 };
                var visit2B = new Visit { Caption = "Visit2B", Patient = patient2, VisitType = VisitType.Baseline, Forms = new List<Form> { new Form { Id = 13, FormType = FormType.Demographics } } };
                var visit2Ae1 = new Visit { Caption = "Visit2Ae1", Patient = patient2, VisitType = VisitType.AdverseEventVisit, Forms = new List<Form> { new Form { Id = 23, FormType = FormType.AdverseEvent } } };
                patient2.Visits = new List<Visit> { visit2B, visit2Ae1 };

                dataStorage.Setup(ds => ds.GetData<Visit>()).Returns(new List<Visit> { visit1Ae1, visit1Ae2, visit1B, visit2B, visit2Ae1 });

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

                //Assert
                Assert.That(result, Is.Not.Null);
                Assert.That(result.Count, Is.EqualTo(3));
                var dto = result[0];
                Assert.That(dto.ClinicName, Is.EqualTo("Clinic"));
                Assert.That(dto.DoctorName, Is.EqualTo("DoctorFirst1 DoctorLast1"));
                Assert.That(dto.DemographicFormId, Is.EqualTo(1));
                Assert.That(dto.AeFormId, Is.EqualTo(2));

                dto = result[1];
                Assert.That(dto.ClinicName, Is.EqualTo("Clinic"));
                Assert.That(dto.DoctorName, Is.EqualTo("DoctorFirst1 DoctorLast1"));
                Assert.That(dto.DemographicFormId, Is.EqualTo(1));
                Assert.That(dto.AeFormId, Is.EqualTo(3));

                dto = result[2];
                Assert.That(dto.ClinicName, Is.EqualTo("Clinic"));
                Assert.That(dto.DoctorName, Is.EqualTo("DoctorFirst2 DoctorLast2"));
                Assert.That(dto.DemographicFormId, Is.EqualTo(13));
                Assert.That(dto.AeFormId, Is.EqualTo(23));
            }
            public void YesterdaysVisitNotListed()
            {
                //Arrange
                string login = "******";
                Visit visit1 = new Visit() { ExpectedVisitDate = DateTime.Now.Date.AddDays(-1), Patient = new Patient() { Doctor = new User() { Login = login } } };

                Mock<IDataStorage> dataStorage = new Mock<IDataStorage>();
                dataStorage.Setup(ds => ds.GetData<Visit>()).Returns(new List<Visit> {visit1});
                VisitRepository repository = new VisitRepository(dataStorage.Object);
                //Act
                IList<Visit> result = repository.GetDailyVisits(login, DateTime.Now.Date);

                //Assert
                Assert.That(result, Is.Not.Null);
                Assert.That(result.Count, Is.EqualTo(0));
            }
            public void SingleExpectedToday()
            {
                //Arrange
                string login = "******";
                Visit visit1 = new Visit() { ExpectedVisitDate = DateTime.Now.Date, Patient = new Patient() { Doctor = new User() { Login = login }, IsActive = true } };

                Mock<IDataStorage> dataStorage = new Mock<IDataStorage>();
                dataStorage.Setup(ds => ds.GetData<Visit>()).Returns(new List<Visit> {visit1});
                VisitRepository repository = new VisitRepository(dataStorage.Object);
                //Act
                IList<Visit> result = repository.GetDailyVisits(login, DateTime.Now.Date);

                //Assert
                Assert.That(result, Is.Not.Null);
                Assert.That(result.Count, Is.EqualTo(1));
                Assert.That(result[0], Is.EqualTo(visit1));
            }
            public void SinglePatientVisit()
            {
                //Arrange
                Patient patient = new Patient() {Id = 5};
                Visit visit1 = new Visit() {Patient = patient};

                Mock<IDataStorage> dataStorage = new Mock<IDataStorage>();
                dataStorage.Setup(ds => ds.GetData<Visit>()).Returns(new List<Visit> {visit1});
                VisitRepository repository = new VisitRepository(dataStorage.Object);
                //Act
                IList<Visit> result = repository.GetVisitsForPatient(patient.Id);

                //Assert
                Assert.That(result, Is.Not.Null);
                Assert.That(result.Count, Is.EqualTo(1));
                Assert.That(result[0], Is.EqualTo(visit1));
            }
            public void GetPatientVisit()
            {
                //Arrange
                string visitName = "TestVisit";
                Patient patient = new Patient() {Id = 5, PatientNumber = 88};
                Visit visit = new Visit() {Patient = patient, Caption = visitName};

                Mock<IDataStorage> dataStorage = new Mock<IDataStorage>();
                dataStorage.Setup(ds => ds.GetData<Visit>()).Returns(new List<Visit> {visit});
                VisitRepository repository = new VisitRepository(dataStorage.Object);

                //Act
                Visit result = repository.GetVisitByPatientNumberAndVisitName(patient.PatientNumber, visitName);

                //Assert
                Assert.That(result, Is.Not.Null);
                Assert.That(result, Is.EqualTo(visit));
            }
            private Patient CreatePatient(int clinicId, int doctorId, int patId)
            {
                var clinic = new Clinic {Id = clinicId, Caption = String.Format("Clinic{0}", clinicId)};
                var doctor = new User {
                    Id = doctorId,
                    Clinic = clinic,
                    Role = ClinicalStudyRoles.Doctor,
                    FirstName = String.Format("First{0}", doctorId),
                    LastName = String.Format("Last{0}", doctorId)
                };
                var pat = new Patient {Id = patId, PatientNumber = 10 + patId, Doctor = doctor};

                var demog = new Form { FormType = FormType.Demographics, Id = 201 + patId};

                var hap1 = new Form {FormType = FormType.Happiness, Id = 202 + patId};
                var hap2 = new Form { FormType = FormType.Happiness, Id = 203 + patId };

                var visitB = new Visit
                             {Id = 101 + patId, Caption = String.Format("Visit{0}", 101 + patId), VisitType = VisitType.Baseline, Forms = new List<Form>{demog}};
                var visit1 = new Visit { Id = 102 + patId, Caption = String.Format("Visit{0}", 102 + patId), VisitType = VisitType.Day1, Forms = new List<Form>{hap1}};
                var visit10 = new Visit { Id = 103 + patId, Caption = String.Format("Visit{0}", 103 + patId), VisitType = VisitType.Day10, Forms = new List<Form> { hap2 } };

                pat.Visits = new List<Visit>{visitB, visit1, visit10};

                return pat;
            }
 private static void EnsureSqlServer2005Compatibility(Visit visit)
 {
     if (visit.VisitTime.HasValue && visit.VisitTime.Value.Year <= 1753)
         visit.VisitTime = visit.VisitTime.Value.AddYears(DateTime.Now.Year);
 }
            public void CorrectModelMapping()
            {
                //Arrange
                var dataStorage = new Mock<IDataStorage>();
                var repository = new QueryRepository(dataStorage.Object);

                var clinic = new Clinic {Caption = "Clinic"};
                var doctor = new User {FirstName = "DoctorFirst", LastName = "DoctorLast", Clinic = clinic};
                var patient = new Patient {Doctor = doctor};
                var visit = new Visit {Caption = "Visit", Patient = patient};
                var form1 = new Form {FormType = FormType.Happiness, Visit = visit};
                var question1 = new Question {Caption = "QuestionCaption1", Form = form1};
                var query1 = new Query {Id = 1, Question = question1};

                var form2 = new Form {FormType = FormType.Inventory, Visit = visit};
                var question2 = new Question {Caption = "QuestionCaption2", Form = form2};
                var query2 = new Query {Id = 2, Question = question2, AnswerText = "Answer"};

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

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

                //Assert
                Assert.That(result, Is.Not.Null);
                Assert.That(result.Count(), Is.EqualTo(2));
                var dto = result.ToList()[0];
                Assert.That(dto.FormType, Is.EqualTo(FormType.Happiness));
                Assert.That(dto.ClinicName, Is.EqualTo("Clinic"));
                Assert.That(dto.DoctorName, Is.EqualTo("DoctorFirst DoctorLast"));
                Assert.That(dto.QuestionName, Is.EqualTo("QuestionCaption1"));
                Assert.That(dto.IsOpen, Is.True);

                dto = result.ToList()[1];
                Assert.That(dto.FormType, Is.EqualTo(FormType.Inventory));
                Assert.That(dto.ClinicName, Is.EqualTo("Clinic"));
                Assert.That(dto.DoctorName, Is.EqualTo("DoctorFirst DoctorLast"));
                Assert.That(dto.QuestionName, Is.EqualTo("QuestionCaption2"));
                Assert.That(dto.IsOpen, Is.False);
            }
            public void VisitFormsForManyVisits()
            {
                //Arrange
                Visit visit = new Visit() {Id = 5};
                Form form1 = new Form() {Visit = visit};
                Visit anotherVisit = new Visit() {Id = 11};
                Form form2 = new Form() {Visit = anotherVisit};

                DataStorage.Setup(ds => ds.GetData<Form>()).Returns(new List<Form> {form1, form2});
                //Act
                IList<Form> result = Repository.GetVisitForms(visit.Id);

                //Assert
                Assert.That(result, Is.Not.Null);
                Assert.That(result.Count, Is.EqualTo(1));
                Assert.That(result[0], Is.EqualTo(form1));
            }
        private void AddAdverseEventForm(Visit visit)
        {
            var aeForm = new Form() {
                Caption = "Adverse Event",
                FormState = FormState.Incomplete,
                FormType = FormType.AdverseEvent,
                OrderNo = 0,
                Visit = visit
            };
            visit.Forms.Add(aeForm);
            FormRepository.Add(aeForm);

            AddAdverseEventFormData(aeForm);
        }
            public void NotFilledInRealVisit()
            {
                //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(1));
                Assert.That(result[0].ClinicName, Is.EqualTo(TestHospitalCaption));
                Assert.That(result[0].DoctorName, Is.EqualTo(TestDoctorLastName));
                Assert.That(result[0].FormType, Is.EqualTo(FormType.Demographics));
            }
        private Visit Create10thDayVisit(Patient patient)
        {
            var visit = new Visit() {
                Caption = "10th Day",
                OrderNo = 2,
                ExpectedVisitDate = DateTime.Now.AddDays(12).Date,
                Patient = patient,
                VisitType = VisitType.Day10
            };

            var happinessForm =
                new Form() {
                    Caption = "Happiness",
                    FormState = FormState.Incomplete,
                    FormType = FormType.Happiness,
                    OrderNo = 0,
                    Visit = visit
                };
            visit.Forms.Add(happinessForm);
            AddHappinessFormData(happinessForm);

            var inventoryForm =
                new Form() {
                    Caption = "Inventory",
                    FormState = FormState.Incomplete,
                    FormType = FormType.Inventory,
                    OrderNo = 1,
                    Visit = visit
                };
            visit.Forms.Add(inventoryForm);
            AddInventoryFormData(inventoryForm);

            var vitalsForm =
                new Form() {
                    Caption = "Vitals",
                    FormState = FormState.Incomplete,
                    FormType = FormType.Vitals,
                    OrderNo = 2,
                    Visit = visit
                };
            visit.Forms.Add(vitalsForm);
            AddVitalsFormData(vitalsForm);

            return visit;
        }
            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;
            }
        private Visit Create1stDayVisit(Patient patient)
        {
            var visit = new Visit() {
                Caption = "1st Day",
                OrderNo = 1,
                ExpectedVisitDate = DateTime.Now.AddDays(2).Date,
                Patient = patient,
                VisitType = VisitType.Day1
            };

            var happinessForm =
                new Form() {
                    Caption = "Happiness",
                    FormState = FormState.Incomplete,
                    FormType = FormType.Happiness,
                    OrderNo = 0,
                    Visit = visit
                };
            visit.Forms.Add(happinessForm);
            AddHappinessFormData(happinessForm);

            var electroForm =
                new Form() {
                    Caption = "Electrocardiogram",
                    FormState = FormState.Incomplete,
                    FormType = FormType.Electrocardiogram,
                    OrderNo = 1,
                    Visit = visit
                };
            visit.Forms.Add(electroForm);
            AddElectrocardiogramFormData(electroForm);

            var vitalsForm =
                new Form() {
                    Caption = "Vitals",
                    FormState = FormState.Incomplete,
                    FormType = FormType.Vitals,
                    OrderNo = 2,
                    Visit = visit
                };
            visit.Forms.Add(vitalsForm);
            AddVitalsFormData(vitalsForm);

            return visit;
        }
        private void PopulateBaselineVisit(Visit baselineVisit, bool isMale)
        {
            var year = DateTime.Now.Year - randomGenerator.Next(29) - 22;
            var month = randomGenerator.Next(12) + 1;
            var day = randomGenerator.Next(28) + 1;
            var dateOfBirth = new DateTime(year, month, day);

            var dice = randomGenerator.Next(100);
            if (dice < FormIsFilledChance) {
                var demographicForm = baselineVisit.Forms.First(f => f.FormType == FormType.Demographics);
                demographicForm.FormState = FormState.Completed;
                var demographicFormData = demographicFormDataRepository.GetFormDataByFormId(demographicForm.Id);
                demographicFormData.Sex.Value = isMale ? "0" : "1";
                demographicFormData.Race.Value = randomGenerator.Next(6).ToString();

                demographicFormData.DateOfBirth.Value = dateOfBirth.ToString();
                demographicFormData.Other.Value = string.Empty;
            }

            dice = randomGenerator.Next(100);
            if (dice < FormIsFilledChance) {
                var vitalsForm = baselineVisit.Forms.First(f => f.FormType == FormType.Vitals);
                vitalsForm.FormState = FormState.Completed;
                var vitalsFormData = vitalsFormDataRepository.GetFormDataByFormId(vitalsForm.Id);

                vitalsFormData.ActualTime.Value = baselineVisit.VisitTime.Value.AddMinutes(randomGenerator.Next(18)*10).ToString();
                double weight = Math.Round(((isMale ? 86.6 : 74.4) + randomGenerator.Next(10) - 5), 1);
                vitalsFormData.Weight.Value = weight.ToString();
                double height = Math.Round(((isMale ? 177d : 163d) + randomGenerator.Next(20) - 10), 0);
                vitalsFormData.Height.Value = height.ToString();

                var temperature = GetRandomTemperature();
                vitalsFormData.Temperature.Value = temperature.ToString();

                var heartRate = GetRandomHeartRate();
                vitalsFormData.HeartRate.Value = heartRate.ToString();
                int age = DateTime.Now.Year - dateOfBirth.Year;
                double systolic = 109d + (0.5*age) + (0.1d*weight);
                double diastolic = 63d + (0.1*age) + (0.15d*weight);
                vitalsFormData.BloodPressureSystolic.Value = ((int) systolic/5*5).ToString();
                vitalsFormData.BloodPressureDiastolic.Value = ((int) diastolic/5*5).ToString();
            }
        }
        private Visit CreateBaselineVisit(Patient patient)
        {
            var visit = new Visit() {
                Caption = "Baseline",
                OrderNo = 0,
                ExpectedVisitDate = DateTime.Now.Date,
                Patient = patient,
                VisitType = VisitType.Baseline
            };

            var demographicForm =
                new Form() {
                    Caption = "Demographics",
                    FormState = FormState.Incomplete,
                    FormType = FormType.Demographics,
                    OrderNo = 0,
                    Visit = visit
                };
            visit.Forms.Add(demographicForm);
            AddDemographicFormData(demographicForm);

            var vitalsForm =
                new Form() {
                    Caption = "Vitals",
                    FormState = FormState.Incomplete,
                    FormType = FormType.Vitals,
                    OrderNo = 1,
                    Visit = visit
                };
            visit.Forms.Add(vitalsForm);
            AddVitalsFormData(vitalsForm);

            return visit;
        }
 private void SetRealVisitDateAndTime(Visit visit)
 {
     var visitDate = visit.ExpectedVisitDate.Value.AddDays(randomGenerator.Next(100) > 95 ? 1 : 0);
     SetRealVisitDateAndTime(visit, visitDate);
 }
        public Visit AddAdverseEventVisit(int patientId)
        {
            var patient = PatientRepository.GetByKey(patientId);

            if (patient == null)
                return null;

            var existingVisits = VisitRepository.GetVisitsForPatient(patientId);
            var maxVisitOrderNo = existingVisits.Max(v => v.OrderNo);
            var adverseEventsCount = existingVisits.Count(v => v.VisitType == VisitType.AdverseEventVisit);
            var caption = "Adverse Event";
            if (adverseEventsCount > 0)
                caption = string.Format("{0} {1}", caption, adverseEventsCount + 1);

            var aeVisit = new Visit {
                Caption = caption,
                ExpectedVisitDate = null,
                VisitDate = DateTime.Today.Date,
                OrderNo = maxVisitOrderNo + 1,
                VisitType = VisitType.AdverseEventVisit,
                Patient = patient
            };
            VisitRepository.Add(aeVisit);
            patient.Visits.Add(aeVisit);

            AddAdverseEventForm(aeVisit);

            VisitRepository.Save();
            return aeVisit;
        }
        public void PopulateDay1Visit(Visit day1Visit, Visit baselineVisit)
        {
            //happiness form
            int dice;
            dice = randomGenerator.Next(100);
            if (dice < FormIsFilledChance) {
                var happinessForm = day1Visit.Forms.First(f => f.FormType == FormType.Happiness);
                happinessForm.FormState = FormState.Completed;
                var happinessFormData = happinessFormDataRepository.GetFormDataByFormId(happinessForm.Id);
                int happinessLevel;
                dice = randomGenerator.Next(100);
                if (dice < 5)
                    happinessLevel = 0;
                else if (dice < 25)
                    happinessLevel = 25;
                else if (dice < 90)
                    happinessLevel = 50;
                else if (dice < 98)
                    happinessLevel = 75;
                else
                    happinessLevel = 100;
                happinessFormData.HappinessLevel.Value = happinessLevel.ToString();
            }
            //electrocardiogram form
            dice = randomGenerator.Next(100);
            if (dice < FormIsFilledChance) {
                var electrocardiogramForm = day1Visit.Forms.First(f => f.FormType == FormType.Electrocardiogram);
                electrocardiogramForm.FormState = FormState.Completed;
                var electrocardiogramFormData = electrocardiogramFormDataRepository.GetFormDataByFormId(electrocardiogramForm.Id);
                electrocardiogramFormData.ElectrocardiogramAttachment.File = new Attachment {
                    FileName = string.Format("{0}_cardiogramm.png", day1Visit.Patient.Caption),
                    FileSize = 0,
                    Id = electrocardiogramForm.Id,
                    MimeType = "image/png",
                    StorageFileName = "ecg1.png"
                };
                attachmentRepository.Add(electrocardiogramFormData.ElectrocardiogramAttachment.File);
            }
            //vitals form

            dice = randomGenerator.Next(100);
            if (dice < FormIsFilledChance) {
                var vitalsOriginalForm = baselineVisit.Forms.First(f => f.FormType == FormType.Vitals);
                if (vitalsOriginalForm.FormState == FormState.Completed) {
                    var vitalsOriginalFormData = vitalsFormDataRepository.GetFormDataByFormId(vitalsOriginalForm.Id);

                    var vitalsForm = day1Visit.Forms.First(f => f.FormType == FormType.Vitals);
                    vitalsForm.FormState = FormState.Completed;
                    var vitalsFormData = vitalsFormDataRepository.GetFormDataByFormId(vitalsForm.Id);

                    vitalsFormData.ActualTime.Value = day1Visit.VisitTime.Value.AddMinutes(randomGenerator.Next(18)*10).ToString();

                    FillVitalsBasedOnPreviousVisit(vitalsOriginalFormData, vitalsFormData, vitalsForm);
                }
            }
        }
        public void GetEditVisit_CorrectBinding_Test()
        {
            //Arrange
            EmulateControllerContext(controller, false);
            int patientNumber = 88;

            var visit = new Visit {
                Id = 222,
                Caption = "TestVisit",
                VisitType = VisitType.Baseline,
                OrderNo = 2,
                ExpectedVisitDate = DateTime.Today.AddDays(4),
                VisitDate = DateTime.Today.AddDays(4),
                VisitTime = DateTime.Now
            };

            repository.Setup(r => r.GetVisitByPatientNumberAndVisitName(patientNumber, visit.Caption)).Returns(visit);

            //Act
            var result = controller.ViewEditVisit(null, patientNumber, visit.Caption);

            //Assert
            Assert.That(result, Is.Not.Null);
            Assert.That(result is ViewResultBase);
            var viewResultBase = result as ViewResultBase;
            Assert.That(viewResultBase.Model, Is.Not.Null);
            Assert.That(viewResultBase.Model is VisitViewModel);
            var model = viewResultBase.Model as VisitViewModel;
            Assert.That(model.Id, Is.EqualTo(visit.Id));
            Assert.That(model.Caption, Is.EqualTo(visit.Caption));
            Assert.That(model.VisitType, Is.EqualTo(visit.VisitType));
            Assert.That(model.OrderNo, Is.EqualTo(visit.OrderNo));
            Assert.That(model.ExpectedVisitDate, Is.EqualTo(visit.ExpectedVisitDate));
            Assert.That(model.VisitDate, Is.EqualTo(visit.VisitDate));
            Assert.That(model.VisitTime, Is.EqualTo(visit.VisitTime));

            repository.Verify(r => r.GetVisitByPatientNumberAndVisitName(patientNumber, visit.Caption), Times.Once());
        }