def setUp(self): self.app = create_app('testing') self.app_context = self.app.app_context() self.app_context.push() db.create_all() self.client = self.app.test_client(use_cookies=True) @self.app.context_processor def utility_processor(): def get_curr_date(): return datetime.utcnow() def get_next_month(year, month): curr = datetime(year=year, month=month, day=1) next_month = curr + relativedelta(months=1) return next_month def get_prev_month(year, month): curr = datetime(year=year, month=month, day=1) prev_month = curr - relativedelta(months=1) return prev_month return dict(get_curr_date=get_curr_date, get_next_month=get_next_month, get_prev_month=get_prev_month) # model instances used in unit tests user1 = User(first_name='one', last_name='one', username='******', email='*****@*****.**', password='******') user2 = User(first_name='two', last_name='two', username='******', email='*****@*****.**', password='******') patient1 = Patient(first_name='patient1', last_name='patient1', email='*****@*****.**') patient2 = Patient(first_name='patient2', last_name='patient2', email='*****@*****.**') patient_note_1 = PatientNote(title='title1', notes='notes1') patient_note_2 = PatientNote(title='title2', notes='notes2') patient_note_1.user = user1 patient_note_2.user = user2 patient_note_1.patient = patient1 patient_note_2.patient = patient2 patient1.users.append(user1) patient2.users.append(user2) db.session.add_all( [user1, user2, patient1, patient2, patient_note_1, patient_note_2]) db.session.commit()
def test_patient_notes_relationship(self): patient = Patient(first_name='John', last_name='Elliot', email="*****@*****.**") user = User(first_name='John', last_name='Elliot', username='******', email="*****@*****.**", password='******') patient_note1 = PatientNote(title='title1', notes='note1') patient_note2 = PatientNote(title='title2', notes='note2') # before connecting self.assertEqual(len(patient.patient_notes.all()), 0) self.assertEqual(len(user.patient_notes.all()), 0) # after connecting patient_note1 patient_note1.patient = patient patient_note1.user = user db.session.add_all([patient, user, patient_note1, patient_note2]) db.session.commit() self.assertEqual(len(patient.patient_notes.all()), 1) self.assertTrue(patient_note1 in patient.patient_notes.all()) self.assertEqual(patient_note1.patient_id, patient.id) self.assertFalse(patient_note2 in patient.patient_notes.all()) self.assertNotEqual(patient_note2.patient_id, patient.id) self.assertEqual(len(user.patient_notes.all()), 1) self.assertTrue(patient_note1 in user.patient_notes.all()) self.assertEqual(patient_note1.user_id, user.id) self.assertFalse(patient_note2 in user.patient_notes.all()) self.assertNotEqual(patient_note2.user_id, user.id)
def add(patient_id): # validate patient patient = Patient.query.get_or_404(patient_id) if not patient in current_user.patients.all(): abort(403) # form processing form = PatientNoteAddForm() if form.validate_on_submit(): patient_note = PatientNote(title=form.title.data, notes=form.notes.data) patient_note.patient = patient patient_note.user = current_user db.session.add(patient_note) db.session.commit() flash('Patient Note Successfully Posted.') return redirect(url_for('patient_notes.list', patient_id=patient_id)) return render_template('patient_notes/add.html', form=form, patient=patient)
def test_database_cascade(self): # User and PatientNote user = User(first_name='one', last_name='one', username='******', email='*****@*****.**', password='******') patient_note = PatientNote(title='title', notes='notes') patient_note.user = user db.session.add_all([user, patient_note]) db.session.commit() db.session.delete(user) db.session.commit() self.assertEqual(len(PatientNote.query.all()), 0) # User and Appointment user = User(first_name='one', last_name='one', username='******', email='*****@*****.**', password='******') today = datetime.utcnow() appointment = Appointment(title='title', description='description', date_start=today, date_end=today) appointment.user = user db.session.add_all([user, appointment]) db.session.commit() db.session.delete(user) db.session.commit() self.assertEqual(len(Appointment.query.all()), 0) # Patient and PatientNote patient = Patient(first_name='patient1', last_name='patient1', email='*****@*****.**') patient_note = PatientNote(title='title', notes='notes') patient_note.patient = patient db.session.add_all([patient, patient_note]) db.session.commit() db.session.delete(patient) db.session.commit() self.assertEqual(len(PatientNote.query.all()), 0) # Patient and Appointment patient = Patient(first_name='patient1', last_name='patient1', email='*****@*****.**') appointment = Appointment(title='title', description='description', date_start=today, date_end=today) appointment.patient = patient db.session.add_all([patient, appointment]) db.session.commit() db.session.delete(patient) db.session.commit() self.assertEqual(len(Appointment.query.all()), 0) # Treatment and Appointment treatment = Treatment(name='treatment1') appointment = Appointment(title='title', description='description', date_start=today, date_end=today) appointment.treatment = treatment db.session.add_all([treatment, appointment]) db.session.commit() db.session.delete(treatment) db.session.commit() self.assertEqual(len(Appointment.query.all()), 0) # Hospital and User user = User(first_name='one', last_name='one', username='******', email='*****@*****.**', password='******') hospital = Hospital(name='hospital') user.hospital = hospital db.session.add_all([hospital, user]) db.session.commit() db.session.delete(hospital) db.session.commit() self.assertEqual(len(User.query.all()), 0) # Hospital and Treatment treatment = Treatment(name='treatment1') hospital = Hospital(name='hospital') treatment.hospital = hospital db.session.add_all([hospital, treatment]) db.session.commit() db.session.delete(hospital) db.session.commit() self.assertEqual(len(Treatment.query.all()), 0)