예제 #1
0
        public Appointment CreateAppointment(int patientCheckId)
        {
            PatientCheck check = db.PatientChecks.Find(patientCheckId);

            Patient dbPatient = db.Patients.Find(check.PatientID);

            Specialty preferredSpecialty = db.Specialties.FirstOrDefault(d => d.SpecialtyID == check.SpecialtyID);

            Doctor doctor = null;
            Appointment appointment = null;

            // If patient is under 16, search for pediatrician,
            //  otherwise look for requested specialty
            if (dbPatient.Age < 16)
            {
                doctor = findDoctorBySpecialty("Pediatrics");
            }
            else
            {
                doctor = findDoctorBySpecialty(preferredSpecialty.SpecialtyName);
            }

            // If no doctor found, search for a GP;
            //  if none found, search for any available doctor
            if (doctor == null)
            {
                doctor = findDoctorBySpecialty("General Practice");

                if (doctor == null)
                {
                    doctor = findCheckedInDoctor(dbPatient);
                }
            }

            // If doctor was found, get the exam room for the appointment
            if (doctor != null)
            {
                ExamRoom examRoom = examRoomDoctorMatcher(doctor);

                // If exam room was found, create the appointment
                if (examRoom != null)
                {
                    appointment = new Appointment
                    {
                        DoctorID = doctor.DoctorID,
                        PatientID = dbPatient.PatientID,
                        ExamRoomID = examRoom.ExamRoomID,
                        CheckinDateTime = check.CheckinDateTime
                    };

                    db.Appointments.Add(appointment);

                    db.SaveChanges();
                }
            }

            return appointment;
        }
        public IHttpActionResult PostAppointment(AppointmentModel appointment)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }
            var dbAppt = new Appointment();

            dbAppt.Update(appointment);
            db.Appointments.Add(dbAppt);
            try
            {
                db.SaveChanges();
            }
            catch (Exception)
            {

                throw new Exception("Unable to add the appointment to the database");
            }

            appointment.AppointmentID = dbAppt.AppointmentID;

            return CreatedAtRoute("DefaultApi", new { id = appointment.AppointmentID }, appointment);
        }
        public void Patient16OverGetsDoctorWithFewestApptsIfNoSpecialistAndNoGP()
        {
            using (IMedAgendaDbContext db = new TestMedAgendaDbContext())
            {
                int appointmentIDNeurologistFewerAppts = 1;
                int appointmentIDSurgeonMoreAppts = 2;
                int appointmentIDSurgeonMoreApptsAnother = 3;

                int doctorIDSurgeonMoreAppts = 1;
                int doctorIDNeurologistFewerAppts = 2;
                int doctorIDCardiologyReq = 3;

                int doctorCheckIDSurgeonMoreAppts = 1;
                int doctorCheckIDNeurologistFewerAppts = 2;
                int doctorCheckIDCardiologyReq = 3;

                int examRoomIDForTest = 1;

                int patientID16Over = 1;
                int patientIDNeurologistFewerAppts = 2;
                int patientIDSurgeonMoreAppts = 3;
                int patientIDSurgeonMoreApptsAnother = 4;

                int patientCheckID16Over = 1;

                int specialtyIDSurgeon = 1;
                int specialtyIDNeurology = 2;
                int specialtyIDCardiology = 3;

                #region Add some patients
                // Patient over 16
                Patient patient = db.Patients.Add(new Patient
                {
                    PatientID = patientID16Over,
                    Birthdate = DateTime.Now.AddYears(-78),
                    FirstName = "Old",
                    LastName = "Man"
                });

                // Patients to be used in appointments
                Patient patientNeurologistFewerAppts = db.Patients.Add(new Patient
                {
                    PatientID = patientIDNeurologistFewerAppts,
                    Birthdate = DateTime.Now.AddYears(-68),
                    FirstName = "Ima",
                    LastName = "Patient"
                });

                Patient patientSurgeonMoreAppts = db.Patients.Add(new Patient
                {
                    PatientID = patientIDSurgeonMoreAppts,
                    Birthdate = DateTime.Now.AddYears(-48),
                    FirstName = "Ineeda",
                    LastName = "Doctor"
                });

                Patient patientSurgeonMoreApptsAnother = db.Patients.Add(new Patient
                {
                    PatientID = patientIDSurgeonMoreApptsAnother,
                    Birthdate = DateTime.Now.AddYears(-58),
                    FirstName = "Iwanta",
                    LastName = "Doctor"
                });

                #endregion

                #region Add some specialties

                Specialty surgeonSpecialty = db.Specialties.Add(new Specialty
                {
                    SpecialtyID = specialtyIDSurgeon,
                    SpecialtyName = "Surgeon"
                });

                Specialty neurologistSpecialty = db.Specialties.Add(new Specialty
                {
                    SpecialtyID = specialtyIDNeurology,
                    SpecialtyName = "Neurology"
                });

                Specialty cardiologySpecialty = db.Specialties.Add(new Specialty
                {
                    SpecialtyID = specialtyIDCardiology,
                    SpecialtyName = "Cardiology"
                });
                #endregion

                #region Check in a patient

                // Check in the patient under 16 with requested specialty cardiology
                PatientCheck patientcheck = db.PatientChecks.Add(new PatientCheck
                {
                    PatientCheckID = patientCheckID16Over,
                    PatientID = patientID16Over,
                    Patient = patient,
                    CheckinDateTime = DateTime.Now,
                    SpecialtyID = specialtyIDCardiology,
                    Specialty = cardiologySpecialty
                });
                #endregion

                #region Add some doctors

                // Add a surgeon
                Doctor johnSmithSurgeonMoreAppts = db.Doctors.Add(new Doctor
                {
                    DoctorID = doctorIDSurgeonMoreAppts,
                    FirstName = "John",
                    LastName = "Smith",
                    SpecialtyID = surgeonSpecialty.SpecialtyID,
                    Specialty = surgeonSpecialty
                });

                // Add a neurologist
                Doctor juliaSmithNeurologistFewerAppts = db.Doctors.Add(new Doctor
                {
                    DoctorID = doctorIDNeurologistFewerAppts,
                    FirstName = "Julia",
                    LastName = "Smith",
                    SpecialtyID = neurologistSpecialty.SpecialtyID,
                    Specialty = neurologistSpecialty
                });

                // Add a cardiologist
                Doctor cardiologistReq = db.Doctors.Add(new Doctor
                {
                    DoctorID = doctorIDCardiologyReq,
                    FirstName = "Bob",
                    LastName = "Smith",
                    SpecialtyID = cardiologySpecialty.SpecialtyID,
                    Specialty = cardiologySpecialty
                });
                #endregion

                #region Add an Exam Room

                ExamRoom examRoom = db.ExamRooms.Add(new ExamRoom
                {
                    ExamRoomID = examRoomIDForTest,
                    ExamRoomName = "ExamRoom 1"
                });
                #endregion

                #region Check in Doctors
                // Check in surgeon and neurologist
                var johnSmithSurgeonMoreApptsCheckIn = new DoctorCheck
                {
                    DoctorCheckID = doctorCheckIDSurgeonMoreAppts,
                    CheckinDateTime = DateTime.Now,
                    DoctorID = doctorIDSurgeonMoreAppts,
                    ExamRoomID = examRoomIDForTest,
                    ExamRoom = examRoom,
                    Doctor = johnSmithSurgeonMoreAppts
                };

                johnSmithSurgeonMoreAppts.DoctorChecks.Add(johnSmithSurgeonMoreApptsCheckIn);
                db.DoctorChecks.Add(johnSmithSurgeonMoreApptsCheckIn);

                var juliaSmithNeurologistFewerApptsCheckIn = new DoctorCheck
                {
                    DoctorCheckID = doctorCheckIDNeurologistFewerAppts,
                    CheckinDateTime = DateTime.Now,
                    DoctorID = doctorIDNeurologistFewerAppts,
                    ExamRoomID = examRoomIDForTest,
                    ExamRoom = examRoom,
                    Doctor = juliaSmithNeurologistFewerAppts
                };

                juliaSmithNeurologistFewerAppts.DoctorChecks.Add(juliaSmithNeurologistFewerApptsCheckIn);
                db.DoctorChecks.Add(juliaSmithNeurologistFewerApptsCheckIn);

                // Check in and out cardiologist
                var cardiologistReqCheckInOut = new DoctorCheck
                {
                    DoctorCheckID = doctorCheckIDCardiologyReq,
                    CheckinDateTime = DateTime.Now.AddHours(-2),
                    CheckoutDateTime = DateTime.Now,
                    DoctorID = doctorIDCardiologyReq,
                    ExamRoomID = examRoomIDForTest,
                    ExamRoom = examRoom,
                    Doctor = cardiologistReq
                };

                cardiologistReq.DoctorChecks.Add(cardiologistReqCheckInOut);
                db.DoctorChecks.Add(cardiologistReqCheckInOut);
                #endregion

                #region Create appointments

                // Create 1 appointment for the neurologist, and 2 appointments for the surgeon
                var juliaNeurologistFewerApptsAppointment = new Appointment
                {
                    AppointmentID = appointmentIDNeurologistFewerAppts,
                    PatientID = patientIDNeurologistFewerAppts,
                    DoctorID = doctorIDNeurologistFewerAppts,
                    ExamRoomID = examRoomIDForTest,
                    CheckinDateTime = DateTime.Now.AddHours(-1),
                    Doctor = juliaSmithNeurologistFewerAppts,
                    ExamRoom = examRoom,
                    Patient = patientNeurologistFewerAppts
                };
                juliaSmithNeurologistFewerAppts.Appointments.Add(juliaNeurologistFewerApptsAppointment);
                db.Appointments.Add(juliaNeurologistFewerApptsAppointment);

                var johnSurgeonMoreApptsAppointment = new Appointment
                {
                    AppointmentID = appointmentIDSurgeonMoreAppts,
                    PatientID = patientIDSurgeonMoreAppts,
                    DoctorID = doctorIDSurgeonMoreAppts,
                    ExamRoomID = examRoomIDForTest,
                    CheckinDateTime = DateTime.Now.AddHours(-2),
                    Doctor = johnSmithSurgeonMoreAppts,
                    ExamRoom = examRoom,
                    Patient = patientSurgeonMoreAppts
                };
                johnSmithSurgeonMoreAppts.Appointments.Add(johnSurgeonMoreApptsAppointment);
                db.Appointments.Add(johnSurgeonMoreApptsAppointment);

                var johnSurgeonMoreApptsAppointmentAnother = new Appointment
                {
                    AppointmentID = appointmentIDSurgeonMoreApptsAnother,
                    PatientID = patientIDSurgeonMoreApptsAnother,
                    DoctorID = doctorIDSurgeonMoreAppts,
                    ExamRoomID = examRoomIDForTest,
                    CheckinDateTime = DateTime.Now.AddHours(-1),
                    Doctor = johnSmithSurgeonMoreAppts,
                    ExamRoom = examRoom,
                    Patient = patientSurgeonMoreApptsAnother
                };
                johnSmithSurgeonMoreAppts.Appointments.Add(johnSurgeonMoreApptsAppointmentAnother);
                db.Appointments.Add(johnSurgeonMoreApptsAppointmentAnother);

                #endregion

                using (var scheduler = new AppointmentScheduler(db))
                {
                    //ACT
                    scheduler.CreateAppointment(patientcheck.PatientCheckID);

                    //ASSERT
                    Assert.IsTrue(db.Appointments.Count() > 0);
                    var appointment = db.Appointments.Last();

                    // Verify that the patient is assigned to doctor with fewer appointments
                    Assert.IsTrue(appointment.PatientID == patientID16Over
                                    && appointment.DoctorID == doctorIDNeurologistFewerAppts);
                }
            }
        }
        public void ApptInPreferredExamRoomIfNoUpcomingAppointments()
        {
            using (IMedAgendaDbContext db = new TestMedAgendaDbContext())
            {
                int appointmentIDPastExamRoomPreferred = 1;

                int doctorIDSurgeonReq = 1;

                int doctorCheckIDSurgeonReq = 1;

                int examRoomIDForTest = 1;
                int examRoomIDPreferred = 2;

                int patientID16Over = 1;
                int patientIDForTest = 2;

                int patientCheckID16Over = 1;
                int patientCheckIDForTest = 2;

                int specialtyIDSurgeonReq = 2;

                #region Add a patient over 16, and another for appointment
                Patient patient = db.Patients.Add(new Patient
                {
                    PatientID = patientID16Over,
                    Birthdate = DateTime.Now.AddYears(-78),
                    FirstName = "Old",
                    LastName = "Man"
                });

                Patient patientForTest = db.Patients.Add(new Patient
                {
                    PatientID = patientIDForTest,
                    Birthdate = DateTime.Now.AddYears(-68),
                    FirstName = "Old",
                    LastName = "Ster"
                });

                #endregion

                #region Add some specialties

                Specialty surgeonSpecialtyReq = db.Specialties.Add(new Specialty
                {
                    SpecialtyID = specialtyIDSurgeonReq,
                    SpecialtyName = "Surgeon"
                });
                #endregion

                #region Check in a patient

                // Check in a patient
                PatientCheck patientcheck = db.PatientChecks.Add(new PatientCheck
                {
                    PatientCheckID = patientCheckID16Over,
                    PatientID = patientID16Over,
                    Patient = patient,
                    CheckinDateTime = DateTime.Now,
                    SpecialtyID = specialtyIDSurgeonReq,
                    Specialty = surgeonSpecialtyReq
                });

                PatientCheck patientcheckForTest = db.PatientChecks.Add(new PatientCheck
                {
                    PatientCheckID = patientCheckIDForTest,
                    PatientID = patientIDForTest,
                    Patient = patientForTest,
                    CheckinDateTime = DateTime.Now.AddHours(-1),
                    SpecialtyID = specialtyIDSurgeonReq,
                    Specialty = surgeonSpecialtyReq
                });
                #endregion

                #region Add some doctors
                Doctor docSurgeon = db.Doctors.Add(new Doctor
                {
                    DoctorID = doctorIDSurgeonReq,
                    FirstName = "Julia",
                    LastName = "Smith",
                    SpecialtyID = surgeonSpecialtyReq.SpecialtyID,
                    Specialty = surgeonSpecialtyReq
                });

                #endregion

                #region Add some Exam Rooms

                ExamRoom examRoomForTest = db.ExamRooms.Add(new ExamRoom
                {
                    ExamRoomID = examRoomIDForTest,
                    ExamRoomName = "ExamRoom 1"
                });

                ExamRoom examRoomPreferred = db.ExamRooms.Add(new ExamRoom
                {
                    ExamRoomID = examRoomIDPreferred,
                    ExamRoomName = "ExamRoom 2"
                });
                #endregion

                #region Check in Doctors

                // Check in the Surgeon
                var docSurgeonCheckIn = new DoctorCheck
                {
                    DoctorCheckID = doctorCheckIDSurgeonReq,
                    CheckinDateTime = DateTime.Now,
                    DoctorID = doctorIDSurgeonReq,
                    ExamRoomID = examRoomIDPreferred,
                    ExamRoom = examRoomPreferred,
                    Doctor = docSurgeon
                };

                docSurgeon.DoctorChecks.Add(docSurgeonCheckIn);
                db.DoctorChecks.Add(docSurgeonCheckIn);
                #endregion

                #region Create appointments

                // Create a past appointment for the surgeon in the preferred exam room
                var surgeonPastAppointment = new Appointment
                {
                    AppointmentID = appointmentIDPastExamRoomPreferred,
                    PatientID = patientIDForTest,
                    DoctorID = doctorIDSurgeonReq,
                    ExamRoomID = examRoomIDPreferred,
                    CheckinDateTime = DateTime.Now.AddHours(-8),
                    CheckoutDateTime = DateTime.Now,
                    Doctor = docSurgeon,
                    ExamRoom = examRoomPreferred,
                    Patient = patient
                };
                docSurgeon.Appointments.Add(surgeonPastAppointment);
                examRoomPreferred.Appointments.Add(surgeonPastAppointment);
                db.Appointments.Add(surgeonPastAppointment);

                #endregion

                using (var scheduler = new AppointmentScheduler(db))
                {
                    //ACT
                    scheduler.CreateAppointment(patientcheck.PatientCheckID);

                    //ASSERT
                    Assert.IsTrue(db.Appointments.Count() > 0);
                    var appointment = db.Appointments.Last();

                    // Verify that the appointment is in preferred exam room
                    Assert.IsTrue(appointment.PatientID == patientID16Over
                                    && appointment.ExamRoomID == examRoomIDPreferred);
                }
            }
        }
        public void ApptInExamRoomWithFewestUpcomingApptsIfUpcomingApptsForPreferredExamRoom()
        {
            using (IMedAgendaDbContext db = new TestMedAgendaDbContext())
            {
                int appointmentIDExamRoomMoreUpcomingAppts = 1;
                int appointmentIDExamRoomMoreUpcomingApptsAnother = 2;
                int appointmentIDExamRoomMoreUpcomingApptsYetAnother = 3;
                int appointmentIDExamRoomFewerUpcomingAppts = 4;
                int appointmentIDExamRoomFewerUpcomingApptsAnother = 5;
                int appointmentIDUpcomingExamRoomPreferred = 6;

                int doctorIDSurgeonReq = 1;

                int doctorCheckIDSurgeonReq = 1;

                int examRoomIDMoreUpcomingAppts = 1;
                int examRoomIDFewerUpcomingAppts = 2;
                int examRoomIDPreferred = 3;

                int patientID16Over = 1;
                int patientIDMoreUpcomingAppts = 2;
                int patientIDMoreUpcomingApptsAnother = 3;
                int patientIDMoreUpcomingApptsYetAnother = 4;
                int patientIDFewerUpcomingAppts = 5;
                int patientIDFewerUpcomingApptsAnother = 6;
                int patientIDExamRoomPreferred = 7;

                int patientCheckID16Over = 1;
                int patientCheckIDMoreUpcomingAppts = 2;
                int patientCheckIDMoreUpcomingApptsAnother = 3;
                int patientCheckIDMoreUpcomingApptsYetAnother = 4;
                int patientCheckIDFewerUpcomingAppts = 5;
                int patientCheckIDFewerUpcomingApptsAnother = 6;
                int patientCheckIDExamRoomPreferred = 7;

                int specialtyIDSurgeonReq = 2;

                #region Add a patient over 16, and patients for appointments
                Patient patient = db.Patients.Add(new Patient
                {
                    PatientID = patientID16Over,
                    Birthdate = DateTime.Now.AddYears(-78),
                    FirstName = "Old",
                    LastName = "Man"
                });

                Patient patientMoreUpcomingAppts = db.Patients.Add(new Patient
                {
                    PatientID = patientIDMoreUpcomingAppts,
                    Birthdate = DateTime.Now.AddYears(-68),
                    FirstName = "Old",
                    LastName = "Ster"
                });

                Patient patientMoreUpcomingApptsAnother = db.Patients.Add(new Patient
                {
                    PatientID = patientIDMoreUpcomingApptsAnother,
                    Birthdate = DateTime.Now.AddYears(-58),
                    FirstName = "Ray",
                    LastName = "Ster"
                });

                Patient patientMoreUpcomingApptsYetAnother = db.Patients.Add(new Patient
                {
                    PatientID = patientIDMoreUpcomingApptsYetAnother,
                    Birthdate = DateTime.Now.AddYears(-55),
                    FirstName = "Ray",
                    LastName = "Man"
                });

                Patient patientFewerUpcomingAppts = db.Patients.Add(new Patient
                {
                    PatientID = patientIDFewerUpcomingAppts,
                    Birthdate = DateTime.Now.AddYears(-48),
                    FirstName = "Old",
                    LastName = "Biz"
                });

                Patient patientFewerUpcomingApptsAnother = db.Patients.Add(new Patient
                {
                    PatientID = patientIDFewerUpcomingApptsAnother,
                    Birthdate = DateTime.Now.AddYears(-42),
                    FirstName = "Old",
                    LastName = "Fence"
                });

                Patient patientExamRoomPreferred = db.Patients.Add(new Patient
                {
                    PatientID = patientIDExamRoomPreferred,
                    Birthdate = DateTime.Now.AddYears(-88),
                    FirstName = "Alte",
                    LastName = "Manner"
                });

                #endregion

                #region Add some specialties

                Specialty surgeonSpecialtyReq = db.Specialties.Add(new Specialty
                {
                    SpecialtyID = specialtyIDSurgeonReq,
                    SpecialtyName = "Surgeon"
                });
                #endregion

                #region Check in the patients

                PatientCheck patientcheck = db.PatientChecks.Add(new PatientCheck
                {
                    PatientCheckID = patientCheckID16Over,
                    PatientID = patientID16Over,
                    Patient = patient,
                    CheckinDateTime = DateTime.Now,
                    SpecialtyID = specialtyIDSurgeonReq,
                    Specialty = surgeonSpecialtyReq
                });

                PatientCheck patientMoreUpcomingApptscheck = db.PatientChecks.Add(new PatientCheck
                {
                    PatientCheckID = patientCheckIDMoreUpcomingAppts,
                    PatientID = patientIDMoreUpcomingAppts,
                    Patient = patientMoreUpcomingAppts,
                    CheckinDateTime = DateTime.Now.AddHours(-3),
                    SpecialtyID = specialtyIDSurgeonReq,
                    Specialty = surgeonSpecialtyReq
                });

                PatientCheck patientMoreUpcomingApptsAnothercheck = db.PatientChecks.Add(new PatientCheck
                {
                    PatientCheckID = patientCheckIDMoreUpcomingApptsAnother,
                    PatientID = patientIDMoreUpcomingApptsAnother,
                    Patient = patientMoreUpcomingApptsAnother,
                    CheckinDateTime = DateTime.Now.AddHours(-2),
                    SpecialtyID = specialtyIDSurgeonReq,
                    Specialty = surgeonSpecialtyReq
                });

                PatientCheck patientMoreUpcomingApptsYetAnothercheck = db.PatientChecks.Add(new PatientCheck
                {
                    PatientCheckID = patientCheckIDMoreUpcomingApptsYetAnother,
                    PatientID = patientIDMoreUpcomingApptsYetAnother,
                    Patient = patientMoreUpcomingApptsYetAnother,
                    CheckinDateTime = DateTime.Now.AddHours(-1),
                    SpecialtyID = specialtyIDSurgeonReq,
                    Specialty = surgeonSpecialtyReq
                });

                PatientCheck patientFewerUpcomingApptscheck = db.PatientChecks.Add(new PatientCheck
                {
                    PatientCheckID = patientCheckIDFewerUpcomingAppts,
                    PatientID = patientIDFewerUpcomingAppts,
                    Patient = patientFewerUpcomingAppts,
                    CheckinDateTime = DateTime.Now.AddHours(-2),
                    SpecialtyID = specialtyIDSurgeonReq,
                    Specialty = surgeonSpecialtyReq
                });

                PatientCheck patientFewerUpcomingApptscheckAnother = db.PatientChecks.Add(new PatientCheck
                {
                    PatientCheckID = patientCheckIDFewerUpcomingApptsAnother,
                    PatientID = patientIDFewerUpcomingApptsAnother,
                    Patient = patientFewerUpcomingApptsAnother,
                    CheckinDateTime = DateTime.Now.AddHours(-1),
                    SpecialtyID = specialtyIDSurgeonReq,
                    Specialty = surgeonSpecialtyReq
                });

                PatientCheck patientExamRoomPreferredcheck = db.PatientChecks.Add(new PatientCheck
                {
                    PatientCheckID = patientCheckIDExamRoomPreferred,
                    PatientID = patientIDExamRoomPreferred,
                    Patient = patientExamRoomPreferred,
                    CheckinDateTime = DateTime.Now.AddHours(-2),
                    SpecialtyID = specialtyIDSurgeonReq,
                    Specialty = surgeonSpecialtyReq
                });

                #endregion

                #region Add some doctors
                Doctor docSurgeon = db.Doctors.Add(new Doctor
                {
                    DoctorID = doctorIDSurgeonReq,
                    FirstName = "Julia",
                    LastName = "Smith",
                    SpecialtyID = surgeonSpecialtyReq.SpecialtyID,
                    Specialty = surgeonSpecialtyReq
                });

                #endregion

                #region Add some Exam Rooms

                ExamRoom examRoomMoreUpcomingAppts = db.ExamRooms.Add(new ExamRoom
                {
                    ExamRoomID = examRoomIDMoreUpcomingAppts,
                    ExamRoomName = "ExamRoom 1"
                });

                ExamRoom examRoomFewerUpcomingAppts = db.ExamRooms.Add(new ExamRoom
                {
                    ExamRoomID = examRoomIDFewerUpcomingAppts,
                    ExamRoomName = "ExamRoom 2"
                });

                ExamRoom examRoomPreferred = db.ExamRooms.Add(new ExamRoom
                {
                    ExamRoomID = examRoomIDPreferred,
                    ExamRoomName = "ExamRoom 3"
                });
                #endregion

                #region Check in Doctors

                var docSurgeonCheckIn = new DoctorCheck
                {
                    DoctorCheckID = doctorCheckIDSurgeonReq,
                    CheckinDateTime = DateTime.Now,
                    DoctorID = doctorIDSurgeonReq,
                    ExamRoomID = examRoomIDPreferred,
                    ExamRoom = examRoomPreferred,
                    Doctor = docSurgeon
                };

                docSurgeon.DoctorChecks.Add(docSurgeonCheckIn);
                db.DoctorChecks.Add(docSurgeonCheckIn);
                #endregion

                #region Create appointments

                // Create three upcoming appointment in one exam room with more appointments,
                // two in the exam room with fewer appointments, and one in the preferred
                var upcomingAppointmentMore = new Appointment
                {
                    AppointmentID = appointmentIDExamRoomMoreUpcomingAppts,
                    PatientID = patientIDMoreUpcomingAppts,
                    DoctorID = doctorIDSurgeonReq,
                    ExamRoomID = examRoomIDMoreUpcomingAppts,
                    CheckinDateTime = DateTime.Now.AddHours(-8),
                    Doctor = docSurgeon,
                    ExamRoom = examRoomMoreUpcomingAppts,
                    Patient = patientMoreUpcomingAppts
                };
                docSurgeon.Appointments.Add(upcomingAppointmentMore);
                examRoomMoreUpcomingAppts.Appointments.Add(upcomingAppointmentMore);
                db.Appointments.Add(upcomingAppointmentMore);

                var upcomingAppointmentMoreAnother = new Appointment
                {
                    AppointmentID = appointmentIDExamRoomMoreUpcomingApptsAnother,
                    PatientID = patientIDMoreUpcomingApptsAnother,
                    DoctorID = doctorIDSurgeonReq,
                    ExamRoomID = examRoomIDMoreUpcomingAppts,
                    CheckinDateTime = DateTime.Now.AddHours(-6),
                    Doctor = docSurgeon,
                    ExamRoom = examRoomMoreUpcomingAppts,
                    Patient = patientMoreUpcomingApptsAnother
                };
                docSurgeon.Appointments.Add(upcomingAppointmentMoreAnother);
                examRoomMoreUpcomingAppts.Appointments.Add(upcomingAppointmentMoreAnother);
                db.Appointments.Add(upcomingAppointmentMoreAnother);

                var upcomingAppointmentMoreYetAnother = new Appointment
                {
                    AppointmentID = appointmentIDExamRoomMoreUpcomingApptsYetAnother,
                    PatientID = patientIDMoreUpcomingApptsYetAnother,
                    DoctorID = doctorIDSurgeonReq,
                    ExamRoomID = examRoomIDMoreUpcomingAppts,
                    CheckinDateTime = DateTime.Now.AddHours(-6),
                    Doctor = docSurgeon,
                    ExamRoom = examRoomMoreUpcomingAppts,
                    Patient = patientMoreUpcomingApptsYetAnother
                };
                docSurgeon.Appointments.Add(upcomingAppointmentMoreYetAnother);
                examRoomMoreUpcomingAppts.Appointments.Add(upcomingAppointmentMoreYetAnother);
                db.Appointments.Add(upcomingAppointmentMoreYetAnother);

                var upcomingAppointmentFewer = new Appointment
                {
                    AppointmentID = appointmentIDExamRoomFewerUpcomingAppts,
                    PatientID = patientIDFewerUpcomingAppts,
                    DoctorID = doctorIDSurgeonReq,
                    ExamRoomID = examRoomIDFewerUpcomingAppts,
                    CheckinDateTime = DateTime.Now.AddHours(-8),
                    Doctor = docSurgeon,
                    ExamRoom = examRoomFewerUpcomingAppts,
                    Patient = patientFewerUpcomingAppts
                };
                docSurgeon.Appointments.Add(upcomingAppointmentFewer);
                examRoomFewerUpcomingAppts.Appointments.Add(upcomingAppointmentFewer);
                db.Appointments.Add(upcomingAppointmentFewer);

                var upcomingAppointmentFewerAnother = new Appointment
                {
                    AppointmentID = appointmentIDExamRoomFewerUpcomingApptsAnother,
                    PatientID = patientIDFewerUpcomingApptsAnother,
                    DoctorID = doctorIDSurgeonReq,
                    ExamRoomID = examRoomIDFewerUpcomingAppts,
                    CheckinDateTime = DateTime.Now.AddHours(-6),
                    Doctor = docSurgeon,
                    ExamRoom = examRoomFewerUpcomingAppts,
                    Patient = patientFewerUpcomingApptsAnother
                };
                docSurgeon.Appointments.Add(upcomingAppointmentFewerAnother);
                examRoomFewerUpcomingAppts.Appointments.Add(upcomingAppointmentFewerAnother);
                db.Appointments.Add(upcomingAppointmentFewerAnother);

                var upcomingAppointmentPreferredExamRoom = new Appointment
                {
                    AppointmentID = appointmentIDUpcomingExamRoomPreferred,
                    PatientID = patientIDExamRoomPreferred,
                    DoctorID = doctorIDSurgeonReq,
                    ExamRoomID = examRoomIDPreferred,
                    CheckinDateTime = DateTime.Now.AddHours(-2),
                    Doctor = docSurgeon,
                    ExamRoom = examRoomPreferred,
                    Patient = patientExamRoomPreferred
                };
                docSurgeon.Appointments.Add(upcomingAppointmentPreferredExamRoom);
                examRoomPreferred.Appointments.Add(upcomingAppointmentPreferredExamRoom);
                db.Appointments.Add(upcomingAppointmentPreferredExamRoom);

                #endregion

                using (var scheduler = new AppointmentScheduler(db))
                {
                    //ACT
                    scheduler.CreateAppointment(patientcheck.PatientCheckID);

                    //ASSERT
                    Assert.IsTrue(db.Appointments.Count() > 0);
                    var appointment = db.Appointments.Last();

                    // Verify that appointment is in exam room with fewer appointments
                    Assert.IsTrue(appointment.PatientID == patientID16Over
                                    && appointment.ExamRoomID == examRoomIDFewerUpcomingAppts);
                }
            }
        }
        public void Patient16OverGetsRequestedSpecialtyWithFewestUpcomingAppointments()
        {
            using (IMedAgendaDbContext db = new TestMedAgendaDbContext())
            {
                int appointmentIDSurgeonFewerAppts = 1;
                int appointmentIDSurgeonMoreAppts = 2;
                int appointmentIDSurgeonMoreApptsAnother = 3;

                int doctorIDSurgeonMoreAppts = 1;
                int doctorIDSurgeonFewerAppts = 2;

                int doctorCheckIDSurgeonMoreAppts = 1;
                int doctorCheckIDSurgeonFewerAppts = 2;

                int examRoomIDForTest = 1;

                int patientID16Over = 1;
                int patientIDSurgeonFewerAppts = 2;
                int patientIDSurgeonMoreAppts = 3;
                int patientIDSurgeonMoreApptsAnother = 4;

                int patientCheckID16Over = 1;

                int specialtyIDSurgeon = 1;

                #region Add some patients
                // Patient over 16
                Patient patient = db.Patients.Add(new Patient
                {
                    PatientID = patientID16Over,
                    Birthdate = DateTime.Now.AddYears(-78),
                    FirstName = "Old",
                    LastName = "Man"
                });

                // Patients to be used in appointments
                Patient patientSurgeonFewerAppts = db.Patients.Add(new Patient
                {
                    PatientID = patientIDSurgeonFewerAppts,
                    Birthdate = DateTime.Now.AddYears(-68),
                    FirstName = "Ima",
                    LastName = "Patient"
                });

                Patient patientSurgeonMoreAppts = db.Patients.Add(new Patient
                {
                    PatientID = patientIDSurgeonMoreAppts,
                    Birthdate = DateTime.Now.AddYears(-48),
                    FirstName = "Ineeda",
                    LastName = "Doctor"
                });

                Patient patientSurgeonMoreApptsAnother = db.Patients.Add(new Patient
                {
                    PatientID = patientIDSurgeonMoreApptsAnother,
                    Birthdate = DateTime.Now.AddYears(-58),
                    FirstName = "Iwanta",
                    LastName = "Doctor"
                });

                #endregion

                #region Add some specialties

                Specialty surgeonSpecialty = db.Specialties.Add(new Specialty
                {
                    SpecialtyID = specialtyIDSurgeon,
                    SpecialtyName = "Surgeon"
                });
                #endregion

                #region Check in a patient

                // Check in the patient over 16 with requested specialty surgeon
                PatientCheck patientcheck = db.PatientChecks.Add(new PatientCheck
                {
                    PatientCheckID = patientCheckID16Over,
                    PatientID = patientID16Over,
                    Patient = patient,
                    CheckinDateTime = DateTime.Now,
                    SpecialtyID = specialtyIDSurgeon,
                    Specialty = surgeonSpecialty
                });
                #endregion

                #region Add some doctors

                // Add two surgeons
                Doctor johnSmithSurgeonMoreAppts = db.Doctors.Add(new Doctor
                {
                    DoctorID = doctorIDSurgeonMoreAppts,
                    FirstName = "John",
                    LastName = "Smith",
                    SpecialtyID = surgeonSpecialty.SpecialtyID,
                    Specialty = surgeonSpecialty
                });

                Doctor juliaSmithSurgeonFewerAppts = db.Doctors.Add(new Doctor
                {
                    DoctorID = doctorIDSurgeonFewerAppts,
                    FirstName = "Julia",
                    LastName = "Smith",
                    SpecialtyID = surgeonSpecialty.SpecialtyID,
                    Specialty = surgeonSpecialty
                });
                #endregion

                #region Add an Exam Room

                ExamRoom examRoom = db.ExamRooms.Add(new ExamRoom
                {
                    ExamRoomID = examRoomIDForTest,
                    ExamRoomName = "ExamRoom 1"
                });
                #endregion

                #region Check in Doctors
                // Check in both surgeons
                var johnSmithSurgeonMoreApptsCheckIn = new DoctorCheck
                {
                    DoctorCheckID = doctorCheckIDSurgeonMoreAppts,
                    CheckinDateTime = DateTime.Now,
                    DoctorID = doctorIDSurgeonMoreAppts,
                    ExamRoomID = examRoomIDForTest,
                    ExamRoom = examRoom,
                    Doctor = johnSmithSurgeonMoreAppts
                };

                johnSmithSurgeonMoreAppts.DoctorChecks.Add(johnSmithSurgeonMoreApptsCheckIn);
                db.DoctorChecks.Add(johnSmithSurgeonMoreApptsCheckIn);

                var juliaSmithSurgeonFewerApptsCheckIn = new DoctorCheck
                {
                    DoctorCheckID = doctorCheckIDSurgeonFewerAppts,
                    CheckinDateTime = DateTime.Now,
                    DoctorID = doctorIDSurgeonFewerAppts,
                    ExamRoomID = examRoomIDForTest,
                    ExamRoom = examRoom,
                    Doctor = juliaSmithSurgeonFewerAppts
                };

                juliaSmithSurgeonFewerAppts.DoctorChecks.Add(juliaSmithSurgeonFewerApptsCheckIn);
                db.DoctorChecks.Add(juliaSmithSurgeonFewerApptsCheckIn);
                #endregion

                #region Create appointments

                // Create 1 appointment for one surgeon, and 2 appointments for the other
                var juliaSurgeonFewerApptsAppointment = new Appointment
                {
                    AppointmentID = appointmentIDSurgeonFewerAppts,
                    PatientID = patientIDSurgeonFewerAppts,
                    DoctorID = doctorIDSurgeonFewerAppts,
                    ExamRoomID = examRoomIDForTest,
                    CheckinDateTime = DateTime.Now.AddHours(-1),
                    Doctor = juliaSmithSurgeonFewerAppts,
                    ExamRoom = examRoom,
                    Patient = patientSurgeonFewerAppts
                };
                juliaSmithSurgeonFewerAppts.Appointments.Add(juliaSurgeonFewerApptsAppointment);
                db.Appointments.Add(juliaSurgeonFewerApptsAppointment);

                var johnSurgeonMoreApptsAppointment = new Appointment
                {
                    AppointmentID = appointmentIDSurgeonMoreAppts,
                    PatientID = patientIDSurgeonMoreAppts,
                    DoctorID = doctorIDSurgeonMoreAppts,
                    ExamRoomID = examRoomIDForTest,
                    CheckinDateTime = DateTime.Now.AddHours(-2),
                    Doctor = johnSmithSurgeonMoreAppts,
                    ExamRoom = examRoom,
                    Patient = patientSurgeonMoreAppts
                };
                johnSmithSurgeonMoreAppts.Appointments.Add(johnSurgeonMoreApptsAppointment);
                db.Appointments.Add(johnSurgeonMoreApptsAppointment);

                var johnSurgeonMoreApptsAppointmentAnother = new Appointment
                {
                    AppointmentID = appointmentIDSurgeonMoreApptsAnother,
                    PatientID = patientIDSurgeonMoreApptsAnother,
                    DoctorID = doctorIDSurgeonMoreAppts,
                    ExamRoomID = examRoomIDForTest,
                    CheckinDateTime = DateTime.Now.AddHours(-1),
                    Doctor = johnSmithSurgeonMoreAppts,
                    ExamRoom = examRoom,
                    Patient = patientSurgeonMoreApptsAnother
                };
                johnSmithSurgeonMoreAppts.Appointments.Add(johnSurgeonMoreApptsAppointmentAnother);
                db.Appointments.Add(johnSurgeonMoreApptsAppointmentAnother);

                #endregion

                using (var scheduler = new AppointmentScheduler(db))
                {
                    //ACT
                    scheduler.CreateAppointment(patientcheck.PatientCheckID);

                    //ASSERT
                    Assert.IsTrue(db.Appointments.Count() > 0);
                    var appointment = db.Appointments.Last();

                    // Verify that the patient is assigned to surgeon with fewer appointments
                    Assert.IsTrue(appointment.PatientID == patientID16Over
                                    && appointment.DoctorID == doctorIDSurgeonFewerAppts);
                }
            }
        }