-
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.
Fix: unittest for Google and Bing requests
- Loading branch information
Showing
2 changed files
with
76 additions
and
39 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 |
---|---|---|
@@ -1,39 +0,0 @@ | ||
import unittest | ||
from unittest.mock import Mock, patch | ||
|
||
from python_advanced_search.models.location import Location | ||
from python_advanced_search.services.crawler import GoogleRequest | ||
from python_advanced_search.models.query import Query | ||
|
||
|
||
class CrawlerTestCase(unittest.TestCase): | ||
def test_request(self): | ||
mock_content = Mock() | ||
mock_content.return_value = '<html></html>' | ||
|
||
mock_page = Mock() | ||
mock_page.return_value = Mock( | ||
content=mock_content | ||
) | ||
|
||
mock_playwright = Mock() | ||
mock_playwright.start().chromium.launch().new_context().new_page = mock_page | ||
|
||
with patch( | ||
'python_advanced_search.services.crawler.sync_playwright', | ||
return_value=mock_playwright | ||
): | ||
request = Query().include( | ||
expression='python unittest' | ||
).to(GoogleRequest, Location.FRANCE) | ||
|
||
self.assertEqual( | ||
request.url, | ||
'https://google.fr/search?q=python+unittest' | ||
) | ||
|
||
response = request.get() | ||
self.assertEqual( | ||
response.html, | ||
'<html></html>' | ||
) | ||
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,76 @@ | ||
import unittest | ||
from unittest.mock import Mock, patch | ||
|
||
from python_crawler.crawler import GoogleRequest, BingRequest | ||
|
||
|
||
class CrawlerTestCase(unittest.TestCase): | ||
def test_google_request(self): | ||
class Query: | ||
def __init__(self, encoded_str): | ||
self.encoded_str = encoded_str | ||
|
||
|
||
mock_content = Mock() | ||
mock_content.return_value = '<html></html>' | ||
|
||
mock_page = Mock() | ||
mock_page.return_value = Mock( | ||
content=mock_content | ||
) | ||
|
||
mock_playwright = Mock() | ||
mock_playwright.start().chromium.launch().new_context().new_page = mock_page | ||
|
||
with patch( | ||
'python_crawler.crawler.sync_playwright', | ||
return_value=mock_playwright | ||
): | ||
request = GoogleRequest( | ||
query=Query(encoded_str='q=google_test') | ||
) | ||
self.assertEqual( | ||
request.url, | ||
'https://google.com/search?q=google_test' | ||
) | ||
|
||
response = request.get() | ||
self.assertEqual( | ||
response.html, | ||
'<html></html>' | ||
) | ||
|
||
|
||
def test_bing_request(self): | ||
class Query: | ||
def __init__(self, encoded_str): | ||
self.encoded_str = encoded_str | ||
|
||
mock_content = Mock() | ||
mock_content.return_value = '<html></html>' | ||
|
||
mock_page = Mock() | ||
mock_page.return_value = Mock( | ||
content=mock_content | ||
) | ||
|
||
mock_playwright = Mock() | ||
mock_playwright.start().firefox.launch().new_context().new_page = mock_page | ||
|
||
with patch( | ||
'python_crawler.crawler.sync_playwright', | ||
return_value=mock_playwright | ||
): | ||
request = BingRequest( | ||
query=Query(encoded_str='q=bing_test') | ||
) | ||
self.assertEqual( | ||
request.url, | ||
'https://bing.com/search?q=bing_test' | ||
) | ||
|
||
response = request.get() | ||
self.assertEqual( | ||
response.html, | ||
'<html></html>' | ||
) |