'Test for https://aeon.stsci.edu/ssb/trac/pyfits/ticket/121
Save backup of file before flushing changes.'

def test_save_backup(self):

    self.copy_file('scale.fits')
    with ignore_warnings():
        with fits.open(self.temp('scale.fits'), mode='update', save_backup=True) as hdul:
            hdul[0].header['TEST'] = 'TEST'
            hdul[0].data[0] = 0
    assert os.path.exists(self.temp('scale.fits.bak'))
    with fits.open(self.data('scale.fits'), do_not_scale_image_data=True) as hdul1:
        with fits.open(self.temp('scale.fits.bak'), do_not_scale_image_data=True) as hdul2:
            assert (hdul1[0].header == hdul2[0].header)
            assert (hdul1[0].data == hdul2[0].data).all()
    with ignore_warnings():
        with fits.open(self.temp('scale.fits'), mode='update', save_backup=True) as hdul:
            hdul[0].header['TEST2'] = 'TEST'
            hdul[0].data[0] = 1
    assert os.path.exists(self.temp('scale.fits.bak'))
    assert os.path.exists(self.temp('scale.fits.bak.1'))
