Using conservative unittest asserts

This commit is contained in:
czaky 2024-05-17 12:04:00 +00:00
parent 122a9568de
commit 8fe19d54ef
2 changed files with 41 additions and 35 deletions

View file

@ -14,19 +14,19 @@ class TestClient(SearxTestCase):
def test_get_single_transport(self): def test_get_single_transport(self):
t = client.get_single_transport(proxy_url="socks4://local:1080") t = client.get_single_transport(proxy_url="socks4://local:1080")
assert isinstance(t, client.AsyncProxyTransportFixed) self.assertTrue(isinstance(t, client.AsyncProxyTransportFixed))
t = client.get_single_transport(proxy_url="socks5://local:1080") t = client.get_single_transport(proxy_url="socks5://local:1080")
assert isinstance(t, client.AsyncProxyTransportFixed) self.assertTrue(isinstance(t, client.AsyncProxyTransportFixed))
t = client.get_single_transport(proxy_url="socks5h://local:1080") t = client.get_single_transport(proxy_url="socks5h://local:1080")
assert isinstance(t, client.AsyncProxyTransportFixed) self.assertTrue(isinstance(t, client.AsyncProxyTransportFixed))
t = client.get_single_transport(proxy_url="https://local:8080") t = client.get_single_transport(proxy_url="https://local:8080")
assert isinstance(t, httpx.AsyncHTTPTransport) self.assertTrue(isinstance(t, httpx.AsyncHTTPTransport))
def test_get_parallel_transport(self): def test_get_parallel_transport(self):
t = client.get_transport( t = client.get_transport(
proxy_urls=["socks5h://local:1080", "socks5h://local:1180"], proxy_urls=["socks5h://local:1080", "socks5h://local:1180"],
) )
assert isinstance(t, client.AsyncParallelTransport) self.assertTrue(isinstance(t, client.AsyncParallelTransport))
@patch( @patch(
'searx.network.client.AsyncProxyTransportFixed.handle_async_request', 'searx.network.client.AsyncProxyTransportFixed.handle_async_request',
@ -38,12 +38,12 @@ class TestClient(SearxTestCase):
) )
request = httpx.Request(url="http://wiki.com", method="GET") request = httpx.Request(url="http://wiki.com", method="GET")
response = await t.handle_async_request(request) response = await t.handle_async_request(request)
assert response.status_code == 200 self.assertEqual(response.status_code, 200)
handler_mock.assert_called_once_with(request) handler_mock.assert_called_once_with(request)
response = await t.handle_async_request(request) response = await t.handle_async_request(request)
assert response.status_code == 301 self.assertEqual(handler_mock.call_count, 2)
assert handler_mock.call_count == 2 self.assertEqual(response.status_code, 301)
@patch( @patch(
'searx.network.client.AsyncProxyTransportFixed.handle_async_request', 'searx.network.client.AsyncProxyTransportFixed.handle_async_request',
@ -54,12 +54,12 @@ class TestClient(SearxTestCase):
proxy_urls=["socks5h://local:1080", "socks5h://local:1180"], proxy_urls=["socks5h://local:1080", "socks5h://local:1180"],
proxy_request_redundancy=2, proxy_request_redundancy=2,
) )
assert isinstance(t, client.AsyncParallelTransport) self.assertTrue(isinstance(t, client.AsyncParallelTransport))
request = httpx.Request(url="http://wiki.com", method="GET") request = httpx.Request(url="http://wiki.com", method="GET")
response = await t.handle_async_request(request) response = await t.handle_async_request(request)
handler_mock.assert_called_with(request) handler_mock.assert_called_with(request)
assert response.status_code == 200 self.assertEqual(handler_mock.call_count, 2)
assert handler_mock.call_count == 2 self.assertEqual(response.status_code, 200)
@patch( @patch(
'searx.network.client.AsyncProxyTransportFixed.handle_async_request', 'searx.network.client.AsyncProxyTransportFixed.handle_async_request',
@ -70,12 +70,12 @@ class TestClient(SearxTestCase):
proxy_urls=["socks5h://local:1080", "socks5h://local:1180"], proxy_urls=["socks5h://local:1080", "socks5h://local:1180"],
proxy_request_redundancy=2, proxy_request_redundancy=2,
) )
assert isinstance(t, client.AsyncParallelTransport) self.assertTrue(isinstance(t, client.AsyncParallelTransport))
request = httpx.Request(url="http://wiki.com", method="GET") request = httpx.Request(url="http://wiki.com", method="GET")
response = await t.handle_async_request(request) response = await t.handle_async_request(request)
handler_mock.assert_called_with(request) handler_mock.assert_called_with(request)
assert response.status_code == 404 self.assertEqual(handler_mock.call_count, 2)
assert handler_mock.call_count == 2 self.assertEqual(response.status_code, 404)
@patch( @patch(
'searx.network.client.AsyncProxyTransportFixed.handle_async_request', 'searx.network.client.AsyncProxyTransportFixed.handle_async_request',
@ -86,12 +86,12 @@ class TestClient(SearxTestCase):
proxy_urls=["socks5h://local:1080", "socks5h://local:1180"], proxy_urls=["socks5h://local:1080", "socks5h://local:1180"],
proxy_request_redundancy=2, proxy_request_redundancy=2,
) )
assert isinstance(t, client.AsyncParallelTransport) self.assertTrue(isinstance(t, client.AsyncParallelTransport))
request = httpx.Request(url="http://wiki.com", method="GET") request = httpx.Request(url="http://wiki.com", method="GET")
response = await t.handle_async_request(request) response = await t.handle_async_request(request)
handler_mock.assert_called_with(request) handler_mock.assert_called_with(request)
assert response.status_code == 403 self.assertEqual(handler_mock.call_count, 2)
assert handler_mock.call_count == 2 self.assertEqual(response.status_code, 403)
@patch( @patch(
'searx.network.client.AsyncProxyTransportFixed.handle_async_request', 'searx.network.client.AsyncProxyTransportFixed.handle_async_request',
@ -102,12 +102,12 @@ class TestClient(SearxTestCase):
proxy_urls=["socks5h://local:1080", "socks5h://local:1180"], proxy_urls=["socks5h://local:1080", "socks5h://local:1180"],
proxy_request_redundancy=2, proxy_request_redundancy=2,
) )
assert isinstance(t, client.AsyncParallelTransport) self.assertTrue(isinstance(t, client.AsyncParallelTransport))
request = httpx.Request(url="http://wiki.com", method="GET") request = httpx.Request(url="http://wiki.com", method="GET")
response = await t.handle_async_request(request) response = await t.handle_async_request(request)
handler_mock.assert_called_with(request) handler_mock.assert_called_with(request)
assert response.status_code == 200 self.assertEqual(response.status_code, 200)
assert handler_mock.call_count == 2 self.assertEqual(handler_mock.call_count, 2)
@patch( @patch(
'searx.network.client.AsyncProxyTransportFixed.handle_async_request', 'searx.network.client.AsyncProxyTransportFixed.handle_async_request',
@ -118,11 +118,11 @@ class TestClient(SearxTestCase):
proxy_urls=["socks5h://local:1080", "socks5h://local:1180"], proxy_urls=["socks5h://local:1080", "socks5h://local:1180"],
proxy_request_redundancy=2, proxy_request_redundancy=2,
) )
assert isinstance(t, client.AsyncParallelTransport) self.assertTrue(isinstance(t, client.AsyncParallelTransport))
request = httpx.Request(url="http://wiki.com", method="GET") request = httpx.Request(url="http://wiki.com", method="GET")
response = None response = None
with self.assertRaises(httpx.RequestError): with self.assertRaises(httpx.RequestError):
response = await t.handle_async_request(request) response = await t.handle_async_request(request)
handler_mock.assert_called_with(request) handler_mock.assert_called_with(request)
assert not response self.assertFalse(response)
assert handler_mock.call_count == 2 self.assertEqual(handler_mock.call_count, 2)

View file

@ -49,24 +49,30 @@ class TestNetwork(SearxTestCase): # pylint: disable=missing-class-docstring
def test_proxies_by_patterns(self): def test_proxies_by_patterns(self):
network = Network(proxies='http://localhost:1337') network = Network(proxies='http://localhost:1337')
assert network._proxies_by_pattern == {'all://': ('http://localhost:1337',)} self.assertEqual(network._proxies_by_pattern, {'all://': ('http://localhost:1337',)})
network = Network(proxies={'https': 'http://localhost:1337', 'http': 'http://localhost:1338'}) network = Network(proxies={'https': 'http://localhost:1337', 'http': 'http://localhost:1338'})
assert network._proxies_by_pattern == { self.assertEqual(
'https://': ('http://localhost:1337',), network._proxies_by_pattern,
'http://': ('http://localhost:1338',), {
} 'https://': ('http://localhost:1337',),
'http://': ('http://localhost:1338',),
},
)
network = Network( network = Network(
proxies={'https': ['http://localhost:1337', 'http://localhost:1339'], 'http': 'http://localhost:1338'} proxies={'https': ['http://localhost:1337', 'http://localhost:1339'], 'http': 'http://localhost:1338'}
) )
assert network._proxies_by_pattern == { self.assertEqual(
'https://': ( network._proxies_by_pattern,
'http://localhost:1337', {
'http://localhost:1339', 'https://': (
), 'http://localhost:1337',
'http://': ('http://localhost:1338',), 'http://localhost:1339',
} ),
'http://': ('http://localhost:1338',),
},
)
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
Network(proxies=1) Network(proxies=1)