Exemple #1
0
    def set_bytes(self, b: bytes) -> None:
        """
        Set bytes to this data element in the form of a string.

        Not all implementations may support setting bytes (writing). See the
        ``writable`` method.

        :param b: bytes to set.
        :type b: bytes

        :raises ReadOnlyError: This data element can only be read from / does
            not support writing.

        """
        if not self._readonly:
            safe_file_write(self._filepath, b)
        else:
            raise ReadOnlyError("This file element is read only.")
    def test_safe_file_write_absolute(
            self, m_NTF: mock.MagicMock, m_remove: mock.MagicMock,
            m_rename: mock.MagicMock, m_scd: mock.MagicMock) -> None:
        # Experimental filepath and content.
        fp = '/some/absolute/dir/bar.txt'
        expected_bytes = 'hello world'

        # Mock return for temp file creation so we can check os.* calls.
        m_file = m_NTF.return_value
        test_tmp_fp = 'temp fp'
        m_file.name = test_tmp_fp

        safe_file_write(fp, expected_bytes)

        m_scd.assert_called_once_with('/some/absolute/dir')
        m_NTF.assert_called_once_with(suffix='.txt', prefix='bar.',
                                      dir='/some/absolute/dir', delete=False)
        m_file.write.assert_called_once_with(expected_bytes)
        m_file.__exit__.assert_called_once_with(None, None, None)
        self.assertEqual(m_remove.call_count, 0)
        m_rename.assert_called_once_with(test_tmp_fp, fp)