forked from fossasia/query-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added unit tests for generalized.py (fossasia#368)
- Loading branch information
Showing
1 changed file
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
from mock import patch | ||
import pytest | ||
|
||
from app.scrapers.generalized import Scraper | ||
|
||
|
||
@patch('requests.models.Response') | ||
@patch('app.scrapers.generalized.requests.get') | ||
def test_get_page(mock_request_get, mock_response): | ||
mock_request_get.return_value = mock_response | ||
mock_response.url = "Mock Url" | ||
response = Scraper().get_page("dummy_query") | ||
assert response == mock_response | ||
expected_payload = {'q': 'dummy_query', '': ''} | ||
expected_headers = { | ||
'User-Agent': ( | ||
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) ' | ||
'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 ' | ||
'Safari/537.36' | ||
) | ||
} | ||
mock_request_get.assert_called_with( | ||
'', headers=expected_headers, params=expected_payload) | ||
|
||
|
||
def test_parse_response(): | ||
with pytest.raises(NotImplementedError): | ||
Scraper().parse_response(None) | ||
|
||
|
||
def test_next_start(): | ||
dummy_prev_results = ['dummy_value'] | ||
if not Scraper().next_start(3, dummy_prev_results) == 4: | ||
raise AssertionError() | ||
|
||
|
||
@patch('app.scrapers.generalized.Scraper.parse_response') | ||
@patch('app.scrapers.generalized.Scraper.get_page') | ||
@patch('requests.models.Response') | ||
def test_search(mock_resp, mock_get_page, mock_parse_resp): | ||
mock_get_page.return_value = mock_resp | ||
mock_resp.text = "Mock response" | ||
expected_resp = [{ | ||
'title': 'mock_title', | ||
'link': 'mock_url' | ||
}] | ||
# assuming parse_response is being implemented by | ||
# classes inheriting Scraper. Thus, returning dummy | ||
# response instead of raising NotImplementedError | ||
mock_parse_resp.return_value = expected_resp | ||
resp = Scraper().search('dummy_query', 1) | ||
assert resp == expected_resp | ||
|
||
|
||
@patch('app.scrapers.generalized.Scraper.get_page') | ||
@patch('requests.models.Response') | ||
def test_search_parsed_response_none(mock_resp, mock_get): | ||
mock_get.return_value = mock_resp | ||
mock_resp.text = "Mock Response" | ||
with patch('app.scrapers.generalized.Scraper.parse_response', | ||
return_value=None): | ||
resp = Scraper().search('dummy_query', 1) | ||
assert resp == [] | ||
|
||
|
||
@patch('app.scrapers.generalized.requests.get') | ||
@patch('app.scrapers.generalized.Scraper.parse_response') | ||
@patch('requests.models.Response') | ||
def test_search_without_count(mock_resp, mock_parse_resp, mock_get): | ||
mock_get.return_value = mock_resp | ||
mock_resp.text = 'mock response' | ||
expected_resp = [{ | ||
'title': 'mock_title', | ||
'link': 'mock_url' | ||
}] | ||
expected_payload = {'q': 'dummy_query'} | ||
expected_headers = { | ||
'User-Agent': ( | ||
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) ' | ||
'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 ' | ||
'Safari/537.36' | ||
) | ||
} | ||
mock_parse_resp.return_value = expected_resp | ||
resp = Scraper().search_without_count('dummy_query') | ||
assert resp == expected_resp | ||
mock_get.assert_called_with( | ||
'', headers=expected_headers, params=expected_payload) |