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) hospital = Hospital(name='Hospital') user = User(first_name='userone', last_name='userone', username='******', email='*****@*****.**', password='******') user.hospital = hospital patient = Patient('Patient', '100', '*****@*****.**') user.patients.append(patient) treatment = Treatment(name='Treatment 1') treatment.hospital = hospital table1 = TreatmentTable(patient=patient, name='Table 1') entry1 = TreatmentTableEntry(amount='Entry1Amount', note='Entry1Note', treatment=treatment, treatment_table=table1) db.session.add_all( [hospital, user, patient, treatment, table1, entry1]) db.session.commit()
def test_treatment_table_entry_treatment_relationship(self): entry1 = TreatmentTableEntry() entry2 = TreatmentTableEntry() treatment1 = Treatment(name='Treatment 1') treatment2 = Treatment(name='Treatment 2') # before connecting self.assertEqual(len(treatment1.treatment_table_entries.all()), 0) self.assertEqual(len(treatment2.treatment_table_entries.all()), 0) # after connecting entry1.treatment = treatment1 entry2.treatment = treatment1 self.assertEqual(len(treatment1.treatment_table_entries.all()), 2) self.assertEqual(treatment1, entry1.treatment) self.assertEqual(treatment1, entry2.treatment) self.assertTrue(entry1 in treatment1.treatment_table_entries.all()) self.assertTrue(entry2 in treatment1.treatment_table_entries.all()) self.assertEqual(len(treatment2.treatment_table_entries.all()), 0) self.assertNotEqual(treatment2, entry1.treatment) self.assertNotEqual(treatment2, entry2.treatment) self.assertFalse(entry1 in treatment2.treatment_table_entries.all()) self.assertFalse(entry2 in treatment2.treatment_table_entries.all())
def test_treatment_table_treatment_table_entry_relationship(self): table1 = TreatmentTable(name='Table 1') table2 = TreatmentTable(name='Table 2') entry1 = TreatmentTableEntry() entry2 = TreatmentTableEntry() # before connecting self.assertEqual(len(table1.treatment_table_entries.all()), 0) self.assertEqual(len(table2.treatment_table_entries.all()), 0) # after connecting entry1.treatment_table = table1 entry2.treatment_table = table1 self.assertEqual(len(table1.treatment_table_entries.all()), 2) self.assertEqual(table1, entry1.treatment_table) self.assertEqual(table1, entry2.treatment_table) self.assertTrue(entry1 in table1.treatment_table_entries.all()) self.assertTrue(entry2 in table1.treatment_table_entries.all()) self.assertEqual(len(table2.treatment_table_entries.all()), 0) self.assertNotEqual(table2, entry1.treatment_table) self.assertNotEqual(table2, entry2.treatment_table) self.assertFalse(entry1 in table2.treatment_table_entries.all()) self.assertFalse(entry2 in table2.treatment_table_entries.all())
def add_entry(treatment_table_id): # retrieve and validate treatment table table = TreatmentTable.query.get_or_404(treatment_table_id) patient = table.patient if not patient in current_user.patients.all(): abort(403) # form processing form = TreatmentTableEntryAddForm() treatments = current_user.hospital.treatments.all() form.treatment.choices = get_treatment_tuple(treatments) if form.validate_on_submit(): entry = TreatmentTableEntry() db.session.add(entry) db.session.commit() flash('Entry Successfully Added') return redirect(url_for('treatment_tables.table',treatment_table_id=table.id)) return render_template('treatment_tables/add_entry.html',form=form)
def test_attributes_assignment(self): date = datetime.utcnow() amount = "1 mg Dose" note = "Additional Note" treatment = Treatment(name='Treatment 1') patient = Patient(first_name='John', last_name='Doe', email='*****@*****.**') table = TreatmentTable(name='Table 1', patient=patient) entry = TreatmentTableEntry(timestamp=date, amount=amount, note=note, treatment=treatment, treatment_table=table) self.assertEqual(entry.timestamp, date) self.assertEqual(entry.amount, '1 mg Dose') self.assertEqual(entry.note, "Additional Note") self.assertEqual(entry.treatment, treatment) self.assertEqual(entry.treatment_table, table)
def test_invalid_url_parameters(self): patient2 = Patient('Patient', '200', '*****@*****.**') table2 = TreatmentTable(patient=patient2, name='Table 2') entry2 = TreatmentTableEntry(amount='Amount', note='Note', treatment_table=table2) db.session.add_all([patient2, table2, entry2]) db.session.commit() with self.client: self.client.post(url_for('auth.login'), data={ 'email': '*****@*****.**', 'password': '******' }) # list response = self.client.get( url_for('treatment_tables.list', patient_id=100)) self.assertEqual(response.status_code, 404) response = self.client.get( url_for('treatment_tables.list', patient_id=2)) self.assertEqual(response.status_code, 403) # table response = self.client.get( url_for('treatment_tables.table', treatment_table_id=100)) self.assertEqual(response.status_code, 404) response = self.client.get( url_for('treatment_tables.table', treatment_table_id=table2.id)) self.assertEqual(response.status_code, 403) # add_table response = self.client.get( url_for('treatment_tables.add_table', patient_id=100)) self.assertEqual(response.status_code, 404) response = self.client.get( url_for('treatment_tables.add_table', patient_id=patient2.id)) self.assertEqual(response.status_code, 403) # edit_table response = self.client.get( url_for('treatment_tables.edit_table', treatment_table_id=100)) self.assertEqual(response.status_code, 404) response = self.client.get( url_for('treatment_tables.edit_table', treatment_table_id=table2.id)) self.assertEqual(response.status_code, 403) # delete_table response = self.client.get( url_for('treatment_tables.delete_table', treatment_table_id=100)) self.assertEqual(response.status_code, 404) response = self.client.get( url_for('treatment_tables.delete_table', treatment_table_id=table2.id)) self.assertEqual(response.status_code, 403) # delete_entry response = self.client.get( url_for('treatment_tables.delete_entry', treatment_entry_id=100)) self.assertEqual(response.status_code, 404) response = self.client.get( url_for('treatment_tables.delete_entry', treatment_entry_id=entry2.id)) self.assertEqual(response.status_code, 403)
def test_id(self): entry = TreatmentTableEntry() db.session.add(entry) db.session.commit() self.assertEqual(entry.id, 1)
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) # TreatmentTable and TreatmentTableEntry table = TreatmentTable(name='table') entry = TreatmentTableEntry() entry.treatment_table = table db.session.add_all([table, entry]) db.session.commit() db.session.delete(table) db.session.commit() self.assertEqual(len(TreatmentTableEntry.query.all()), 0) # TreatmentTable and Patient table = TreatmentTable(name='table') patient = Patient(first_name='patient1', last_name='patient1', email='*****@*****.**') table.patient = patient db.session.add_all([table, patient]) db.session.commit() db.session.delete(patient) db.session.commit() self.assertEqual(len(TreatmentTable.query.all()), 0) # TreatmentTableEntry and Treatment treatment = Treatment(name='treatment') entry = TreatmentTableEntry() entry.treatment = treatment db.session.add_all([treatment, entry]) db.session.commit() db.session.delete(treatment) db.session.commit() self.assertEqual(len(TreatmentTableEntry.query.all()), 0)