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
Files changed (1) hide show
  1. 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 = MagicMock()
49
  mock_response.status = 200
50
  mock_get.return_value = mock_response
51
 
52
- with patch('scrapling.cli.Convertor.write_content_to_file'):
53
- result = runner.invoke(
54
- get,
55
- [html_url, str(output_file)]
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
- with patch('scrapling.cli.Convertor.write_content_to_file'):
64
- result = runner.invoke(
65
- get,
66
- [
67
- html_url,
68
- str(output_file),
69
- '-H', 'User-Agent: Test',
70
- '--cookies', 'session=abc123',
71
- '--timeout', '60',
72
- '--proxy', 'http://proxy:8080',
73
- '-s', '.content',
74
- '-p', 'page=1'
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 = MagicMock()
85
  mock_post.return_value = mock_response
86
 
87
- with patch('scrapling.cli.Convertor.write_content_to_file'):
88
- result = runner.invoke(
89
- post,
90
- [
91
- html_url,
92
- str(output_file),
93
- '-d', 'key=value',
94
- '-j', '{"data": "test"}'
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 = MagicMock()
105
  mock_put.return_value = mock_response
106
 
107
- with patch('scrapling.cli.Convertor.write_content_to_file'):
108
- result = runner.invoke(
109
- put,
110
- [
111
- html_url,
112
- str(output_file),
113
- '-d', 'key=value',
114
- '-j', '{"data": "test"}'
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 = MagicMock()
125
  mock_delete.return_value = mock_response
126
 
127
- with patch('scrapling.cli.Convertor.write_content_to_file'):
128
- result = runner.invoke(
129
- delete,
130
- [
131
- html_url,
132
- str(output_file)
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 = MagicMock()
143
  mock_fetch.return_value = mock_response
144
 
145
- with patch('scrapling.cli.Convertor.write_content_to_file'):
146
- result = runner.invoke(
147
- fetch,
148
- [
149
- html_url,
150
- str(output_file),
151
- '--headless',
152
- '--stealth',
153
- '--timeout', '60000'
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 = MagicMock()
164
  mock_fetch.return_value = mock_response
165
 
166
- with patch('scrapling.cli.Convertor.write_content_to_file'):
167
- result = runner.invoke(
168
- stealthy_fetch,
169
- [
170
- html_url,
171
- str(output_file),
172
- '--headless',
173
- '--css-selector', 'body',
174
- '--timeout', '60000'
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
- # Invalid output file extension
186
- with patch('scrapling.cli.Convertor.write_content_to_file') as mock_write:
187
- mock_write.side_effect = ValueError("Unknown file type")
188
-
189
- _ = runner.invoke(
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