Exemplo n.º 1
0
def test_file_read_file_with_data(mock_exists):
    """Tests file read() a file that has data altered in memory."""
    mock_exists.return_value = True
    the_file = File(DEFAULT_ARGS)
    the_file.data = 'Test data'
    assert the_file.read() == the_file.data
    assert the_file.read() == 'Test data'
    the_file.data = 'New data'
    assert the_file.read() == the_file.data
    assert the_file.read() == 'New data'
Exemplo n.º 2
0
def test_file_read_file_force_flush_memory(mock_exists):
    """Tests file read method forcing read from memory."""
    mock_exists.return_value = True
    the_file = File(DEFAULT_ARGS)
    m_open = mock_open()
    with patch(BUILTINS + '.open', m_open, create=True):
        # pylint: disable=no-member
        m_open.return_value.read.return_value = data_on_disk = 'The data on disk..'
        the_file.data = 'The data...'
        assert the_file.read(True) == data_on_disk
        m_open.assert_called_once_with(DEFAULT_ARGS['path'], 'r')
        m_open().close.assert_called_once_with()
Exemplo n.º 3
0
def test_section_file_apply_to_file():
    """[Integration Test] Test apply_to_file() method of Section class."""
    # Setup a root dir to use to test
    root_dir = Dir({"path": "/tmp/ext_pylib/"})
    assert root_dir.remove(False)  # If it already exists, remove it.
    assert root_dir.create()
    assert root_dir.exists()

    file_without_section = File({"path": "/tmp/ext_pylib/file"})
    assert file_without_section.create()
    assert file_without_section.overwrite(FILE_CONTENTS_WITHOUT_SECTION)
    assert file_without_section.exists()

    class SectionFile(Section, File):
        """Dummy Class extending Section and File."""

    section = SectionFile({"path": "/tmp/ext_pylib/section"})
    assert section.create()
    assert section.overwrite(SECTION_FILE_CONTENTS)
    assert section.exists()

    file_with_section = File({"path": "/tmp/ext_pylib/file_with_section"})
    assert file_with_section.create()
    assert file_with_section.overwrite(FILE_CONTENTS_WITH_SECTION)
    assert file_with_section.exists()

    assert section.is_in(file_with_section.read())
    print("Without:")
    print(file_without_section.read())
    print("Without (Applied):")
    print(section.apply_to(file_without_section.read()))
    print("With:")
    print(file_with_section.read())
    assert section.apply_to(file_without_section.read()) == file_with_section.read()

    # Cleanup
    assert root_dir.remove(False)
    assert not root_dir.exists()
Exemplo n.º 4
0
def test_nonexisting_file_read_data(mock_exists):
    """Tests file read() a file that has data altered in memory."""
    mock_exists.return_value = False
    the_file = File(DEFAULT_ARGS)
    the_file.read()
    assert the_file.data == ''
Exemplo n.º 5
0
def test_file_read_nonexisting_file_with_data_in_memory(mock_exists):
    """Tests File read method."""
    mock_exists.return_value = False
    the_file = File(DEFAULT_ARGS)
    the_file.data = 'Data is in memory...'
    assert the_file.read() == 'Data is in memory...'
Exemplo n.º 6
0
def test_file_read_nonexisting_file(mock_exists):
    """Tests File read method."""
    mock_exists.return_value = False
    the_file = File(DEFAULT_ARGS)
    assert the_file.read() == ''