Implemented a test for the cmd_scrape command that ensures playwright can be imported after running cmd_web.

This commit is contained in:
Paul Gauthier (aider) 2024-07-16 11:11:17 +01:00
parent 3e7663e914
commit 74f4bd106a

View file

@ -1,36 +1,29 @@
import unittest import unittest
from unittest.mock import patch, MagicMock from unittest.mock import patch
import sys
import io
from aider.commands import Commands from aider.commands import Commands
from aider.io import InputOutput
class TestScrape(unittest.TestCase): class TestScrape(unittest.TestCase):
@patch("aider.commands.install_playwright") def setUp(self):
@patch("aider.commands.Scraper") self.io = InputOutput()
def test_cmd_web_imports_playwright(self, mock_scraper, mock_install_playwright): self.commands = Commands(self.io, None)
# Mock the necessary objects and methods
mock_io = MagicMock()
mock_coder = MagicMock()
mock_install_playwright.return_value = True
mock_scraper_instance = MagicMock()
mock_scraper.return_value = mock_scraper_instance
mock_scraper_instance.scrape.return_value = "Mocked content"
# Create a Commands instance def test_cmd_web_imports_playwright(self):
commands = Commands(mock_io, mock_coder) # Capture stdout to suppress output during the test
captured_output = io.StringIO()
sys.stdout = captured_output
try:
# Run the cmd_web command # Run the cmd_web command
commands.cmd_web("https://example.com") self.commands.cmd_web("https://example.com")
# Check that install_playwright was called
mock_install_playwright.assert_called_once()
# Check that Scraper was instantiated
mock_scraper.assert_called_once()
# Try to import playwright # Try to import playwright
try: try:
import playwright # noqa: F401 import playwright
playwright_imported = True playwright_imported = True
except ImportError: except ImportError:
playwright_imported = False playwright_imported = False
@ -38,9 +31,16 @@ class TestScrape(unittest.TestCase):
# Assert that playwright was successfully imported # Assert that playwright was successfully imported
self.assertTrue( self.assertTrue(
playwright_imported, playwright_imported,
"Playwright should be importable after running cmd_web command" "Playwright should be importable after running cmd_web"
) )
# Check if the content from example.com was scraped
output = captured_output.getvalue()
self.assertIn("This domain is for use in illustrative examples in documents.", output)
finally:
# Restore stdout
sys.stdout = sys.__stdout__
if __name__ == "__main__": if __name__ == "__main__":
unittest.main() unittest.main()