def test_update(client): name_to_update_to = unique() first_name = unique() last_name = unique() c = Contact(username='******', first_name=first_name, last_name=last_name) db_session.add(c) db_session.commit() contact_id = c.id r = client.patch(f'/contact/{contact_id}', data=dict(username=name_to_update_to)) assert r.status_code == 200 # We need to clear the db session and reload the object # from the db if we truly want to test that the object # has been saved to the db with the new values. db_session.expunge_all() c = Contact.query.get(contact_id) assert c.username == name_to_update_to # Assert that the other fields are still their original values assert c.first_name == first_name assert c.last_name == last_name
def create_contact(): c = Contact(username=get_unique_string(), first_name='first', last_name='last') e = Email(email_address=f'{c.username}@localhost') e2 = Email(email_address=f'{c.username}@localhost2') c.emails = [e, e2] db_session.add(c) db_session.commit()
def test_list(client): for username in ('one', 'two', 'three'): c = Contact(username=username, first_name=username, last_name=username) db_session.add(c) db_session.commit() r = client.get('/contact') assert r.status_code == 200 assert len(r.json) == 3
def test_find(client): username_to_find = unique() c = Contact(username=username_to_find, first_name='first', last_name='last') db_session.add(c) db_session.commit() r = client.get(f'/contact?username={username_to_find}') assert r.status_code == 200 assert r.json.get('username') == username_to_find
def test_list_with_emails(client): for username in ('one', 'two', 'three'): c = Contact(username=username, first_name=username, last_name=username) e = Email(email_address=f'{username}@localhost') c.emails.append(e) db_session.add(c) db_session.commit() r = client.get('/contact') assert r.status_code == 200 assert len(r.json) == 3 assert len(r.json[0]['emails']) == 1
def test_duplicate_username_returns_409(client): name_to_duplicate = unique() c = Contact(username=name_to_duplicate, first_name='first', last_name='last') db_session.add(c) db_session.commit() r = client.post('/contact', data=dict(username=name_to_duplicate, first_name='first', last_name='last')) assert r.status_code == 409
def test_delete(client): c = Contact(username='******', first_name='To', last_name='Delete') db_session.add(c) db_session.commit() # `c` should be a deleted object by the end of this test, # so grab hold of the id here: contact_id = c.id r = client.delete(f'/contact/{contact_id}') assert r.status_code == 200 q = Contact.query.filter_by(id=contact_id) assert q.count() == 0
def post(self): kwargs = contact_post_parser.parse_args() if 'email' in kwargs: addresses = kwargs.pop('email') contact = Contact(**kwargs) db_session.add(contact) try: db_session.commit() # Should really catch sqlite.IntegrityError here but # doing so skips the except block... except Exception: db_session.rollback() raise ContactUsernameAlreadyExistsException if addresses: for address in addresses: contact.emails.append(Email(email_address=address)) db_session.add(contact) db_session.commit() return contact
def test_delete_cascade(client): c = Contact(username='******', first_name='Delete', last_name='Cascade') e = Email(email_address='delete.cascade@localhost') c.emails = [e] db_session.add(c) db_session.commit() # We'll need to expunge the session to test if objects truly are removed from the db, # so grab their ids now: contact_id = c.id email_id = e.id r = client.delete(f'/contact/{contact_id}') assert r.status_code == 200 db_session.expunge_all() q = Contact.query.filter_by(id=contact_id) assert q.count() == 0 q = Email.query.filter_by(id=email_id) assert q.count() == 0