def test_message_delete(db, gmail_account):
 api_client = new_api_client(db, gmail_account.namespace)
 generic_thread = add_fake_thread(db.session, gmail_account.namespace.id)
 gen_message = add_fake_message(db.session, gmail_account.namespace.id, generic_thread)
 category_ids = []
 for i in xrange(10):
  po_data = api_client.post_data('/labels/', {'display_name': str(i)})
  assert (po_data.status_code == 200)
  category_ids.append(json.loads(po_data.data)['id'])
 data = {'label_ids': category_ids}
 resp = api_client.put_data('/messages/{}'.format(gen_message.public_id), data)
 assert (resp.status_code == 200)
 associated_mcs = db.session.query(MessageCategory).filter((MessageCategory.message_id == gen_message.id)).all()
 assert (len(associated_mcs) == 10)
 db.session.delete(gen_message)
 db.session.commit()
 assert (db.session.query(MessageCategory).filter((MessageCategory.message_id == gen_message.id)).all() == [])

def test_message_delete ( db , gmail_account ) : api_client = new_api_client ( db , gmail_account . namespace ) generic_thread = add_fake_thread ( db . session , gmail_account . namespace . id ) gen_message = add_fake_message ( db . session , gmail_account . namespace . id , generic_thread ) category_ids = [ ] for i in xrange ( 10 ) : po_data = api_client . post_data ( '/labels/' , { 'display_name' : str ( i ) } ) assert ( po_data . status_code == 200 ) category_ids . append ( json . loads ( po_data . data ) [ 'id' ] ) data = { 'label_ids' : category_ids } resp = api_client . put_data ( '/messages/{}' . format ( gen_message . public_id ) , data ) assert ( resp . status_code == 200 ) associated_mcs = db . session . query ( MessageCategory ) . filter ( ( MessageCategory . message_id == gen_message . id ) ) . all ( ) assert ( len ( associated_mcs ) == 10 ) db . session . delete ( gen_message ) db . session . commit ( ) assert ( db . session . query ( MessageCategory ) . filter ( ( MessageCategory . message_id == gen_message . id ) ) . all ( ) == [ ] )
ensure that all associated messagecategories are deleted when a message is deleted .