Karim shoair commited on
Commit ·
3db6a0b
1
Parent(s): 9ffc9dd
tests: Update test to be up-to-date with current version of the code
Browse files- tests/cli/test_cli.py +91 -91
tests/cli/test_cli.py
CHANGED
|
@@ -3,12 +3,23 @@ from click.testing import CliRunner
|
|
| 3 |
from unittest.mock import patch, MagicMock
|
| 4 |
import pytest_httpbin
|
| 5 |
|
|
|
|
| 6 |
from scrapling.cli import (
|
| 7 |
shell, mcp, get, post, put, delete, fetch, stealthy_fetch
|
| 8 |
)
|
| 9 |
|
| 10 |
|
| 11 |
@pytest_httpbin.use_class_based_httpbin
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
class TestCLI:
|
| 13 |
"""Test CLI functionality"""
|
| 14 |
|
|
@@ -45,136 +56,129 @@ class TestCLI:
|
|
| 45 |
output_file = tmp_path / "output.md"
|
| 46 |
|
| 47 |
with patch('scrapling.fetchers.Fetcher.get') as mock_get:
|
| 48 |
-
mock_response =
|
| 49 |
mock_response.status = 200
|
| 50 |
mock_get.return_value = mock_response
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
assert result.exit_code == 0
|
| 58 |
|
| 59 |
# Test with various options
|
| 60 |
with patch('scrapling.fetchers.Fetcher.get') as mock_get:
|
| 61 |
mock_get.return_value = mock_response
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
assert result.exit_code == 0
|
| 78 |
|
| 79 |
def test_extract_post_command(self, runner, tmp_path, html_url):
|
| 80 |
"""Test extract `post` command"""
|
| 81 |
output_file = tmp_path / "output.html"
|
| 82 |
|
| 83 |
with patch('scrapling.fetchers.Fetcher.post') as mock_post:
|
| 84 |
-
mock_response =
|
| 85 |
mock_post.return_value = mock_response
|
| 86 |
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
assert result.exit_code == 0
|
| 98 |
|
| 99 |
def test_extract_put_command(self, runner, tmp_path, html_url):
|
| 100 |
"""Test extract `put` command"""
|
| 101 |
output_file = tmp_path / "output.html"
|
| 102 |
|
| 103 |
with patch('scrapling.fetchers.Fetcher.put') as mock_put:
|
| 104 |
-
mock_response =
|
| 105 |
mock_put.return_value = mock_response
|
| 106 |
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
assert result.exit_code == 0
|
| 118 |
|
| 119 |
def test_extract_delete_command(self, runner, tmp_path, html_url):
|
| 120 |
"""Test extract `delete` command"""
|
| 121 |
output_file = tmp_path / "output.html"
|
| 122 |
|
| 123 |
with patch('scrapling.fetchers.Fetcher.delete') as mock_delete:
|
| 124 |
-
mock_response =
|
| 125 |
mock_delete.return_value = mock_response
|
| 126 |
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
assert result.exit_code == 0
|
| 136 |
|
| 137 |
def test_extract_fetch_command(self, runner, tmp_path, html_url):
|
| 138 |
"""Test extract fetch command"""
|
| 139 |
output_file = tmp_path / "output.txt"
|
| 140 |
|
| 141 |
with patch('scrapling.fetchers.DynamicFetcher.fetch') as mock_fetch:
|
| 142 |
-
mock_response =
|
| 143 |
mock_fetch.return_value = mock_response
|
| 144 |
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
assert result.exit_code == 0
|
| 157 |
|
| 158 |
def test_extract_stealthy_fetch_command(self, runner, tmp_path, html_url):
|
| 159 |
"""Test extract fetch command"""
|
| 160 |
output_file = tmp_path / "output.md"
|
| 161 |
|
| 162 |
with patch('scrapling.fetchers.StealthyFetcher.fetch') as mock_fetch:
|
| 163 |
-
mock_response =
|
| 164 |
mock_fetch.return_value = mock_response
|
| 165 |
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
assert result.exit_code == 0
|
| 178 |
|
| 179 |
def test_invalid_arguments(self, runner, html_url):
|
| 180 |
"""Test invalid arguments handling"""
|
|
@@ -182,12 +186,8 @@ class TestCLI:
|
|
| 182 |
result = runner.invoke(get)
|
| 183 |
assert result.exit_code != 0
|
| 184 |
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
get,
|
| 191 |
-
[html_url, 'output.invalid']
|
| 192 |
-
)
|
| 193 |
-
# Should handle the error gracefully
|
|
|
|
| 3 |
from unittest.mock import patch, MagicMock
|
| 4 |
import pytest_httpbin
|
| 5 |
|
| 6 |
+
from scrapling.parser import Selector
|
| 7 |
from scrapling.cli import (
|
| 8 |
shell, mcp, get, post, put, delete, fetch, stealthy_fetch
|
| 9 |
)
|
| 10 |
|
| 11 |
|
| 12 |
@pytest_httpbin.use_class_based_httpbin
|
| 13 |
+
def configure_selector_mock():
|
| 14 |
+
"""Helper function to create a properly configured Selector mock"""
|
| 15 |
+
mock_response = MagicMock(spec=Selector)
|
| 16 |
+
mock_response.body = "<html><body>Test content</body></html>"
|
| 17 |
+
mock_response.get_all_text.return_value = "Test content"
|
| 18 |
+
mock_response.css_first.return_value = mock_response
|
| 19 |
+
mock_response.css.return_value = [mock_response]
|
| 20 |
+
return mock_response
|
| 21 |
+
|
| 22 |
+
|
| 23 |
class TestCLI:
|
| 24 |
"""Test CLI functionality"""
|
| 25 |
|
|
|
|
| 56 |
output_file = tmp_path / "output.md"
|
| 57 |
|
| 58 |
with patch('scrapling.fetchers.Fetcher.get') as mock_get:
|
| 59 |
+
mock_response = configure_selector_mock()
|
| 60 |
mock_response.status = 200
|
| 61 |
mock_get.return_value = mock_response
|
| 62 |
|
| 63 |
+
result = runner.invoke(
|
| 64 |
+
get,
|
| 65 |
+
[html_url, str(output_file)]
|
| 66 |
+
)
|
| 67 |
+
assert result.exit_code == 0
|
|
|
|
| 68 |
|
| 69 |
# Test with various options
|
| 70 |
with patch('scrapling.fetchers.Fetcher.get') as mock_get:
|
| 71 |
mock_get.return_value = mock_response
|
| 72 |
|
| 73 |
+
result = runner.invoke(
|
| 74 |
+
get,
|
| 75 |
+
[
|
| 76 |
+
html_url,
|
| 77 |
+
str(output_file),
|
| 78 |
+
'-H', 'User-Agent: Test',
|
| 79 |
+
'--cookies', 'session=abc123',
|
| 80 |
+
'--timeout', '60',
|
| 81 |
+
'--proxy', 'http://proxy:8080',
|
| 82 |
+
'-s', '.content',
|
| 83 |
+
'-p', 'page=1'
|
| 84 |
+
]
|
| 85 |
+
)
|
| 86 |
+
assert result.exit_code == 0
|
|
|
|
| 87 |
|
| 88 |
def test_extract_post_command(self, runner, tmp_path, html_url):
|
| 89 |
"""Test extract `post` command"""
|
| 90 |
output_file = tmp_path / "output.html"
|
| 91 |
|
| 92 |
with patch('scrapling.fetchers.Fetcher.post') as mock_post:
|
| 93 |
+
mock_response = configure_selector_mock()
|
| 94 |
mock_post.return_value = mock_response
|
| 95 |
|
| 96 |
+
result = runner.invoke(
|
| 97 |
+
post,
|
| 98 |
+
[
|
| 99 |
+
html_url,
|
| 100 |
+
str(output_file),
|
| 101 |
+
'-d', 'key=value',
|
| 102 |
+
'-j', '{"data": "test"}'
|
| 103 |
+
]
|
| 104 |
+
)
|
| 105 |
+
assert result.exit_code == 0
|
|
|
|
| 106 |
|
| 107 |
def test_extract_put_command(self, runner, tmp_path, html_url):
|
| 108 |
"""Test extract `put` command"""
|
| 109 |
output_file = tmp_path / "output.html"
|
| 110 |
|
| 111 |
with patch('scrapling.fetchers.Fetcher.put') as mock_put:
|
| 112 |
+
mock_response = configure_selector_mock()
|
| 113 |
mock_put.return_value = mock_response
|
| 114 |
|
| 115 |
+
result = runner.invoke(
|
| 116 |
+
put,
|
| 117 |
+
[
|
| 118 |
+
html_url,
|
| 119 |
+
str(output_file),
|
| 120 |
+
'-d', 'key=value',
|
| 121 |
+
'-j', '{"data": "test"}'
|
| 122 |
+
]
|
| 123 |
+
)
|
| 124 |
+
assert result.exit_code == 0
|
|
|
|
| 125 |
|
| 126 |
def test_extract_delete_command(self, runner, tmp_path, html_url):
|
| 127 |
"""Test extract `delete` command"""
|
| 128 |
output_file = tmp_path / "output.html"
|
| 129 |
|
| 130 |
with patch('scrapling.fetchers.Fetcher.delete') as mock_delete:
|
| 131 |
+
mock_response = configure_selector_mock()
|
| 132 |
mock_delete.return_value = mock_response
|
| 133 |
|
| 134 |
+
result = runner.invoke(
|
| 135 |
+
delete,
|
| 136 |
+
[
|
| 137 |
+
html_url,
|
| 138 |
+
str(output_file)
|
| 139 |
+
]
|
| 140 |
+
)
|
| 141 |
+
assert result.exit_code == 0
|
|
|
|
| 142 |
|
| 143 |
def test_extract_fetch_command(self, runner, tmp_path, html_url):
|
| 144 |
"""Test extract fetch command"""
|
| 145 |
output_file = tmp_path / "output.txt"
|
| 146 |
|
| 147 |
with patch('scrapling.fetchers.DynamicFetcher.fetch') as mock_fetch:
|
| 148 |
+
mock_response = configure_selector_mock()
|
| 149 |
mock_fetch.return_value = mock_response
|
| 150 |
|
| 151 |
+
result = runner.invoke(
|
| 152 |
+
fetch,
|
| 153 |
+
[
|
| 154 |
+
html_url,
|
| 155 |
+
str(output_file),
|
| 156 |
+
'--headless',
|
| 157 |
+
'--stealth',
|
| 158 |
+
'--timeout', '60000'
|
| 159 |
+
]
|
| 160 |
+
)
|
| 161 |
+
assert result.exit_code == 0
|
|
|
|
| 162 |
|
| 163 |
def test_extract_stealthy_fetch_command(self, runner, tmp_path, html_url):
|
| 164 |
"""Test extract fetch command"""
|
| 165 |
output_file = tmp_path / "output.md"
|
| 166 |
|
| 167 |
with patch('scrapling.fetchers.StealthyFetcher.fetch') as mock_fetch:
|
| 168 |
+
mock_response = configure_selector_mock()
|
| 169 |
mock_fetch.return_value = mock_response
|
| 170 |
|
| 171 |
+
result = runner.invoke(
|
| 172 |
+
stealthy_fetch,
|
| 173 |
+
[
|
| 174 |
+
html_url,
|
| 175 |
+
str(output_file),
|
| 176 |
+
'--headless',
|
| 177 |
+
'--css-selector', 'body',
|
| 178 |
+
'--timeout', '60000'
|
| 179 |
+
]
|
| 180 |
+
)
|
| 181 |
+
assert result.exit_code == 0
|
|
|
|
| 182 |
|
| 183 |
def test_invalid_arguments(self, runner, html_url):
|
| 184 |
"""Test invalid arguments handling"""
|
|
|
|
| 186 |
result = runner.invoke(get)
|
| 187 |
assert result.exit_code != 0
|
| 188 |
|
| 189 |
+
_ = runner.invoke(
|
| 190 |
+
get,
|
| 191 |
+
[html_url, 'output.invalid']
|
| 192 |
+
)
|
| 193 |
+
# Should handle the error gracefully
|
|
|
|
|
|
|
|
|
|
|
|