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'
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()
def test_file_create_with_data_but_not_as_arg(mock_write, mock_exists, mock_chmod, mock_chown): """Tests file creation with data.""" mock_parent_dir = MockParentDir(True) mock_exists.return_value = False mock_chown.return_value = mock_chmod.return_value = True File.parent_dir = mock_parent_dir the_file = File(DEFAULT_ARGS) m_open = mock_open() with patch(BUILTINS + '.open', m_open, create=True): data = 'The data...' the_file.data = data assert the_file.create() m_open.assert_called_once_with(DEFAULT_ARGS['path'], 'w') mock_write.assert_called_once_with(data, False, m_open()) m_open().close.assert_called_once_with() assert mock_parent_dir.exists_called == 1 assert mock_parent_dir.create_called == 0
def test_website_pack_unpack(): """Integration test: initializes, installs, verifies, packs, removes, unpacks and removes website.""" with patch(_INPUT, return_value="y"): website = Website("example.com") assert not website.is_installed(), "Website 'example.com' should not exist on this server." assert not website.verify(), "Verification on a non-existing website should fail." with patch(_INPUT, return_value="y"): website.install() # Create a test file in htdocs the_file = File({"path": website.htdocs + "/index.html"}) the_file.data = "Test file." the_file.create() # Pack the site website.pack() website.remove(ask=False) assert not website.is_installed() assert not website.verify(), "Verification on a non-existing website should fail." assert not the_file.exists() with patch(_INPUT, return_value="y"): website.unpack() assert website.is_installed() def verify(website): """Verify function to wrap with localhost decorator.""" assert website.verify() localhost(verify)(website) assert the_file.exists() # Remove and check again for good measure website.remove(ask=False) assert not website.is_installed() assert not website.verify(), "Verification on a non-existing website should fail." assert not the_file.exists()
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...'