Skip to content

Commit

Permalink
Fix: unittest for Google and Bing requests
Browse files Browse the repository at this point in the history
  • Loading branch information
Zepolimer committed Aug 7, 2024
1 parent e7d11f0 commit 097ee38
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 39 deletions.
39 changes: 0 additions & 39 deletions tests/__init__.py
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>'
)
76 changes: 76 additions & 0 deletions tests/crawler/__init__.py
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>'
)

0 comments on commit 097ee38

Please sign in to comment.