'Tests creating a fully-formed HDU object from a string containing the
bytes of the HDU.'

def test_hdu_fromstring(self):

    dat = open(self.data('test0.fits'), 'rb').read()
    offset = 0
    with fits.open(self.data('test0.fits')) as hdul:
        hdulen = (hdul[0]._data_offset + hdul[0]._data_size)
        hdu = fits.PrimaryHDU.fromstring(dat[:hdulen])
        assert isinstance(hdu, fits.PrimaryHDU)
        assert (hdul[0].header == hdu.header)
        assert (hdu.data is None)
    hdu.header['TEST'] = 'TEST'
    hdu.writeto(self.temp('test.fits'))
    with fits.open(self.temp('test.fits')) as hdul:
        assert isinstance(hdu, fits.PrimaryHDU)
        assert (hdul[0].header[:(-1)] == hdu.header[:(-1)])
        assert (hdul[0].header['TEST'] == 'TEST')
        assert (hdu.data is None)
    with fits.open(self.data('test0.fits')) as hdul:
        for ext_hdu in hdul[1:]:
            offset += hdulen
            hdulen = (len(str(ext_hdu.header)) + ext_hdu._data_size)
            hdu = fits.ImageHDU.fromstring(dat[offset:(offset + hdulen)])
            assert isinstance(hdu, fits.ImageHDU)
            assert (ext_hdu.header == hdu.header)
            assert (ext_hdu.data == hdu.data).all()
