Files
jobs/tests/test_scheduler.py

41 lines
1.6 KiB
Python

import pytest
import time
from unittest.mock import patch, MagicMock
from web.craigslist import scrape_jobs_with_retry, run_scheduled_scraping
class TestScheduler:
def test_scrape_jobs_with_retry_success(self):
"""Test that scrape_jobs_with_retry succeeds on first attempt."""
with patch('web.craigslist.scrape_jobs') as mock_scrape:
result = scrape_jobs_with_retry()
assert result is True
mock_scrape.assert_called_once()
def test_scrape_jobs_with_retry_failure(self):
"""Test that scrape_jobs_with_retry handles failures properly."""
with patch('web.craigslist.scrape_jobs', side_effect=Exception("Test error")) as mock_scrape:
result = scrape_jobs_with_retry(max_retries=2)
assert result is False
assert mock_scrape.call_count == 2
def test_run_scheduled_scraping(self):
"""Test the scheduled scraping wrapper function."""
with patch('web.craigslist.scrape_jobs_with_retry') as mock_retry:
mock_retry.return_value = True
run_scheduled_scraping()
mock_retry.assert_called_once()
def test_scheduler_import(self):
"""Test that scheduler functions can be imported."""
from web.craigslist import start_scheduler
assert callable(start_scheduler)
@patch('web.craigslist.schedule')
def test_scheduler_setup(self, mock_schedule):
"""Test that scheduler setup works correctly."""
# This is a basic test to ensure the scheduler can be set up
from web.craigslist import schedule
assert schedule is not None