import sys from pathlib import Path sys.path.append(str(Path(__file__).parent.parent)) import pytest from handlers.howoge_handler import HowogeHandler from handlers.gewobag_handler import GewobagHandler from handlers.degewo_handler import DegewoHandler from handlers.gesobau_handler import GesobauHandler from handlers.stadtundland_handler import StadtUndLandHandler from handlers.wbm_handler import WBMHandler from handlers.base_handler import BaseHandler from unittest.mock import AsyncMock, MagicMock class MockBaseHandler(BaseHandler): async def apply(self, listing: dict, result: dict) -> dict: return result @pytest.mark.asyncio async def test_howoge_handler(): context = AsyncMock() handler = HowogeHandler(context) listing = {"link": "https://www.howoge.de/example", "id": "testid"} result = {"success": False} await handler.apply(listing, result) assert "success" in result @pytest.mark.asyncio async def test_gewobag_handler(): context = AsyncMock() handler = GewobagHandler(context) listing = {"link": "https://www.gewobag.de/example"} result = {"success": False} await handler.apply(listing, result) assert "success" in result @pytest.mark.asyncio async def test_degewo_handler(): context = AsyncMock() handler = DegewoHandler(context) listing = {"link": "https://www.degewo.de/example"} result = {"success": False} await handler.apply(listing, result) assert "success" in result @pytest.mark.asyncio async def test_gesobau_handler(): context = AsyncMock() handler = GesobauHandler(context) listing = {"link": "https://www.gesobau.de/example"} result = {"success": False} await handler.apply(listing, result) assert "success" in result @pytest.mark.asyncio async def test_stadtundland_handler(): context = AsyncMock() handler = StadtUndLandHandler(context) listing = {"link": "https://www.stadtundland.de/example"} result = {"success": False} await handler.apply(listing, result) assert "success" in result @pytest.mark.asyncio async def test_wbm_handler(): context = AsyncMock() handler = WBMHandler(context) listing = {"link": "https://www.wbm.de/example"} result = {"success": False} await handler.apply(listing, result) assert "success" in result @pytest.mark.asyncio async def test_handle_cookies(): """Test the handle_cookies method in BaseHandler.""" context = AsyncMock() handler = MockBaseHandler(context) mock_page = AsyncMock() mock_cookie_btn = AsyncMock() mock_cookie_btn.is_visible = AsyncMock(return_value=True) mock_cookie_btn.click = AsyncMock() mock_page.query_selector = AsyncMock(return_value=mock_cookie_btn) await handler.handle_cookies(mock_page) mock_cookie_btn.click.assert_called_once() @pytest.mark.asyncio async def test_handle_consent(): """Test the handle_consent method in BaseHandler.""" context = AsyncMock() handler = MockBaseHandler(context) mock_page = AsyncMock() mock_consent_btn = AsyncMock() mock_consent_btn.is_visible = AsyncMock(return_value=True) mock_consent_btn.click = AsyncMock() mock_page.query_selector = AsyncMock(return_value=mock_consent_btn) await handler.handle_consent(mock_page) mock_consent_btn.click.assert_called_once() @pytest.mark.asyncio async def test_login(): """Test the login method in BaseHandler.""" context = AsyncMock() handler = MockBaseHandler(context, email="test@example.com", password="password123") mock_page = AsyncMock() # Mock the page interactions mock_page.goto = AsyncMock() mock_page.fill = AsyncMock() mock_page.click = AsyncMock() mock_page.wait_for_load_state = AsyncMock() mock_page.url = "https://www.inberlinwohnen.de/mein-bereich" mock_page.query_selector = AsyncMock(return_value=AsyncMock(is_visible=AsyncMock(return_value=True))) result = await handler.login(mock_page) # Assertions mock_page.goto.assert_called_once_with("https://www.inberlinwohnen.de/login", wait_until="networkidle") mock_page.fill.assert_any_call('input[name="email"], input[type="email"]', "test@example.com") mock_page.fill.assert_any_call('input[name="password"], input[type="password"]', "password123") mock_page.click.assert_called_once_with('button[type="submit"], input[type="submit"]') mock_page.wait_for_load_state.assert_called_once_with("networkidle") assert result is True # Test for fetch_listings method in BaseHandler @pytest.mark.asyncio async def test_fetch_listings(): context = AsyncMock() handler = MockBaseHandler(context) # Mock the fetch_listings method handler.fetch_listings = AsyncMock(return_value=[ {"id": "1", "title": "Listing 1", "price": 1000}, {"id": "2", "title": "Listing 2", "price": 1200} ]) listings = await handler.fetch_listings() # Assertions assert len(listings) == 2 assert listings[0]["id"] == "1" assert listings[1]["title"] == "Listing 2"