'Test foreign key constraint failure
Test whether foreign key constraint failure during purge
operation is raising DBReferenceError or not.'

def test_purge_fk_constraint_failure(self):

    session = db_api.get_session()
    engine = db_api.get_engine()
    connection = engine.connect()
    dialect = engine.url.get_dialect()
    if (dialect == sqlite.dialect):
        if test_utils.is_sqlite_version_prior_to(3, 7):
            self.skipTest('sqlite    version    too    old    for    reliable    SQLA    foreign_keys')
        connection.execute('PRAGMA    foreign_keys    =    ON')
    images = sqlalchemyutils.get_table(engine, 'images')
    image_tags = sqlalchemyutils.get_table(engine, 'image_tags')
    uuidstr = uuid.uuid4().hex
    created_time = (timeutils.utcnow() - datetime.timedelta(days=20))
    deleted_time = (created_time + datetime.timedelta(days=5))
    images_row_fixture = {'id': uuidstr, 'status': 'status', 'created_at': created_time, 'deleted_at': deleted_time, 'deleted': 1, 'visibility': 'public', 'min_disk': 1, 'min_ram': 1, 'protected': 0}
    ins_stmt = images.insert().values(**images_row_fixture)
    connection.execute(ins_stmt)
    image_tags_row_fixture = {'image_id': uuidstr, 'value': 'tag_value', 'created_at': created_time, 'deleted': 0}
    ins_stmt = image_tags.insert().values(**image_tags_row_fixture)
    connection.execute(ins_stmt)
    self.assertRaises(db_exception.DBReferenceError, db_api.purge_deleted_rows, self.adm_context, age_in_days=10, max_rows=50)
    images_rows = session.query(images).count()
    self.assertEqual(4, images_rows)
