tests: Update test to be up-to-date with current version of the code

This commit is contained in:
Karim shoair
2025-09-14 16:30:59 +03:00
parent 0b3509a295
commit 1024ba6916
+17 -17
View File
@@ -3,12 +3,23 @@ from click.testing import CliRunner
from unittest.mock import patch, MagicMock from unittest.mock import patch, MagicMock
import pytest_httpbin import pytest_httpbin
from scrapling.parser import Selector
from scrapling.cli import ( from scrapling.cli import (
shell, mcp, get, post, put, delete, fetch, stealthy_fetch shell, mcp, get, post, put, delete, fetch, stealthy_fetch
) )
@pytest_httpbin.use_class_based_httpbin @pytest_httpbin.use_class_based_httpbin
def configure_selector_mock():
"""Helper function to create a properly configured Selector mock"""
mock_response = MagicMock(spec=Selector)
mock_response.body = "<html><body>Test content</body></html>"
mock_response.get_all_text.return_value = "Test content"
mock_response.css_first.return_value = mock_response
mock_response.css.return_value = [mock_response]
return mock_response
class TestCLI: class TestCLI:
"""Test CLI functionality""" """Test CLI functionality"""
@@ -45,11 +56,10 @@ class TestCLI:
output_file = tmp_path / "output.md" output_file = tmp_path / "output.md"
with patch('scrapling.fetchers.Fetcher.get') as mock_get: with patch('scrapling.fetchers.Fetcher.get') as mock_get:
mock_response = MagicMock() mock_response = configure_selector_mock()
mock_response.status = 200 mock_response.status = 200
mock_get.return_value = mock_response mock_get.return_value = mock_response
with patch('scrapling.cli.Convertor.write_content_to_file'):
result = runner.invoke( result = runner.invoke(
get, get,
[html_url, str(output_file)] [html_url, str(output_file)]
@@ -60,7 +70,6 @@ class TestCLI:
with patch('scrapling.fetchers.Fetcher.get') as mock_get: with patch('scrapling.fetchers.Fetcher.get') as mock_get:
mock_get.return_value = mock_response mock_get.return_value = mock_response
with patch('scrapling.cli.Convertor.write_content_to_file'):
result = runner.invoke( result = runner.invoke(
get, get,
[ [
@@ -81,10 +90,9 @@ class TestCLI:
output_file = tmp_path / "output.html" output_file = tmp_path / "output.html"
with patch('scrapling.fetchers.Fetcher.post') as mock_post: with patch('scrapling.fetchers.Fetcher.post') as mock_post:
mock_response = MagicMock() mock_response = configure_selector_mock()
mock_post.return_value = mock_response mock_post.return_value = mock_response
with patch('scrapling.cli.Convertor.write_content_to_file'):
result = runner.invoke( result = runner.invoke(
post, post,
[ [
@@ -101,10 +109,9 @@ class TestCLI:
output_file = tmp_path / "output.html" output_file = tmp_path / "output.html"
with patch('scrapling.fetchers.Fetcher.put') as mock_put: with patch('scrapling.fetchers.Fetcher.put') as mock_put:
mock_response = MagicMock() mock_response = configure_selector_mock()
mock_put.return_value = mock_response mock_put.return_value = mock_response
with patch('scrapling.cli.Convertor.write_content_to_file'):
result = runner.invoke( result = runner.invoke(
put, put,
[ [
@@ -121,10 +128,9 @@ class TestCLI:
output_file = tmp_path / "output.html" output_file = tmp_path / "output.html"
with patch('scrapling.fetchers.Fetcher.delete') as mock_delete: with patch('scrapling.fetchers.Fetcher.delete') as mock_delete:
mock_response = MagicMock() mock_response = configure_selector_mock()
mock_delete.return_value = mock_response mock_delete.return_value = mock_response
with patch('scrapling.cli.Convertor.write_content_to_file'):
result = runner.invoke( result = runner.invoke(
delete, delete,
[ [
@@ -139,10 +145,9 @@ class TestCLI:
output_file = tmp_path / "output.txt" output_file = tmp_path / "output.txt"
with patch('scrapling.fetchers.DynamicFetcher.fetch') as mock_fetch: with patch('scrapling.fetchers.DynamicFetcher.fetch') as mock_fetch:
mock_response = MagicMock() mock_response = configure_selector_mock()
mock_fetch.return_value = mock_response mock_fetch.return_value = mock_response
with patch('scrapling.cli.Convertor.write_content_to_file'):
result = runner.invoke( result = runner.invoke(
fetch, fetch,
[ [
@@ -160,10 +165,9 @@ class TestCLI:
output_file = tmp_path / "output.md" output_file = tmp_path / "output.md"
with patch('scrapling.fetchers.StealthyFetcher.fetch') as mock_fetch: with patch('scrapling.fetchers.StealthyFetcher.fetch') as mock_fetch:
mock_response = MagicMock() mock_response = configure_selector_mock()
mock_fetch.return_value = mock_response mock_fetch.return_value = mock_response
with patch('scrapling.cli.Convertor.write_content_to_file'):
result = runner.invoke( result = runner.invoke(
stealthy_fetch, stealthy_fetch,
[ [
@@ -182,10 +186,6 @@ class TestCLI:
result = runner.invoke(get) result = runner.invoke(get)
assert result.exit_code != 0 assert result.exit_code != 0
# Invalid output file extension
with patch('scrapling.cli.Convertor.write_content_to_file') as mock_write:
mock_write.side_effect = ValueError("Unknown file type")
_ = runner.invoke( _ = runner.invoke(
get, get,
[html_url, 'output.invalid'] [html_url, 'output.invalid']