def test_single_contact_life_cycle(self): contact_info = { 'first_name':'test_first_name', 'last_name':'test_last_name', 'email':'test_email', 'phone_number':'0123456789' } # Create the contact and assert it's existence and confirm the database created it contact_insertion = database.insert_contact(contact_info, self.user_id) self.assertTrue(contact_insertion['created']) self.assertIsNotNone(contact_insertion['contact_id']) contact_id = contact_insertion['contact_id'] # Read the contact just created contact = database.get_contact_with_id(contact_id) self.assertIsNotNone(contact) # Edit the contact just read update_info = { 'first_name':'update_first_name', 'last_name':'update_last_name', 'email':'update_email', 'phone_number':'0123456789876543210', 'contact_id':contact_id } contact_update = database.update_contact(update_info) self.assertTrue(contact_update['updated']) # Delete the contact just updated contact_deletion = database.delete_contact(contact_id) self.assertTrue(contact_deletion['deleted']) # To confirm, try to get the contact with the same id and make sure we get None back contact = database.get_contact_with_id(contact_id) self.assertIsNone(contact)
def contacts_html(): if not 'session_username' in session: return redirect('/') # some id exists, meaning either create new or edit existing, don't just get all if request.args.get("id") is not None: request_id = request.args.get("id") contact_form = forms.ContactForm(request.form) # if it's new, just return the form if str(request_id) == "new": return render_template('contact.html', form=contact_form, header_text='Insert Contact', submit_text='Create', method='post') # otherwise, try to get a contact model using the id try: id_num = int(request_id) contact = database.get_contact_with_id(id_num) # if no such contact, raise an error and the except will take care of the 404 if not contact: raise 'No such contact' contact_form.first_name.data = contact.first_name contact_form.last_name.data = contact.last_name contact_form.email.data = contact.email contact_form.phone_number.data = contact.phone_number return render_template('contact.html', form=contact_form, header_text='Edit Contact', submit_text='Update', contact_id=id_num, method='patch') # if it fails, 404 except: response = make_response(("", 404)) return response # if it hasn't terminated by now, it means get all contacts contacts = get_contacts_json() return render_template('contacts.html', contacts=contacts)