Skip to content

Commit

Permalink
Add tests for debug logging
Browse files Browse the repository at this point in the history
  • Loading branch information
ScholliYT committed Feb 21, 2025
1 parent 33bbb54 commit 8831aff
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
2 changes: 1 addition & 1 deletion deadseeker/deadseeker.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,5 +103,5 @@ def seek(
responsehandler: Optional[UrlFetchResponseHandler] = None) -> SeekResults:
url_list = [urls] if isinstance(urls, str) else urls
results = asyncio.run(self._main(url_list, responsehandler))
logger.debug(f'Process took {results.elapsed:.2f}')
logger.debug(f'Process took {results.elapsed:.2f} ms')
return results
15 changes: 15 additions & 0 deletions test/test_deadseeker.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
import unittest
from unittest.mock import Mock, patch
from typing import List
Expand Down Expand Up @@ -251,9 +252,23 @@ def get_link_parser_mock(
self.timer_stop = self.timer_stop_patch.start()
self.timer_stop.return_value = 4.0

self.logger = logging.getLogger('deadseeker.deadseeker')

def tearDown(self):
self.timer_stop_patch.stop()

def test_elapsed_time_is_debug_logged(self):
with patch.object(self.logger, 'error') as error_mock, \
patch.object(self.logger, 'info') as info_mock, \
patch.object(self.logger, 'debug') as debug_mock:
results = self.testobj.seek(TEST1_URL_HOME)
error_mock.assert_not_called()
info_mock.assert_not_called()
debug_mock.assert_called_with('Process took 4000.00 ms')

results = self.testobj.seek(TEST1_URL_HOME)
self.assertEqual(4000.0, results.elapsed, 'Elapsed time is not 4.0')

def test_site1_is_fully_crawled(self):
results = self.testobj.seek(TEST1_URL_HOME)
successes = get_urls(results.successes)
Expand Down
48 changes: 47 additions & 1 deletion test/test_linkparser.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import logging
from deadseeker.linkparser import DefaultLinkParser, DefaultLinkParserFactory
from deadseeker.linkacceptor import LinkAcceptor
from deadseeker.common import SeekerConfig, UrlFetchResponse, UrlTarget
import unittest
from unittest.mock import Mock
from unittest.mock import Mock, patch
import os

TESTFILE_LOC = os.path.join(os.path.dirname(__file__), "test_linkparser.html")
Expand Down Expand Up @@ -39,6 +40,51 @@ def setUp(self):
self.testobj: DefaultLinkParser = \
DefaultLinkParser(self.config, self.linkacceptor)

self.logger = logging.getLogger('deadseeker.linkparser')

def test_debug_logs_when_accepting(self):
expectedlinks = [
'/link-manifest-href.webmanifest',
'/link-apple-touch-icon-href.png',
'/link-stylesheet-href.css',
'/script-src.js',
'script-src-relative.js',
'/a-href.html',
'/img-src.jpeg',
'/img-data-src.png',
'//www.google-analytics.com/analytics.js'
]
with patch.object(self.logger, 'error') as error_mock, \
patch.object(self.logger, 'info') as info_mock, \
patch.object(self.logger, 'debug') as debug_mock:
self.testobj.parse(self.resp)
info_mock.assert_not_called()
error_mock.assert_not_called()
for link in expectedlinks:
debug_mock.assert_any_call(f'Accepting url: {link}')

def test_debug_logs_when_skipping(self):
expectedlinks = [
'/link-manifest-href.webmanifest',
'/link-apple-touch-icon-href.png',
'/link-stylesheet-href.css',
'/script-src.js',
'script-src-relative.js',
'/a-href.html',
'/img-src.jpeg',
'/img-data-src.png',
'//www.google-analytics.com/analytics.js'
]
self.linkacceptor.accepts.return_value = False
with patch.object(self.logger, 'error') as error_mock, \
patch.object(self.logger, 'info') as info_mock, \
patch.object(self.logger, 'debug') as debug_mock:
self.testobj.parse(self.resp)
info_mock.assert_not_called()
error_mock.assert_not_called()
for link in expectedlinks:
debug_mock.assert_any_call(f'Skipping url: {link}')

def test_find_links_all_true(self):
actuallinks = self.testobj.parse(self.resp)
expectedlinks = [
Expand Down

0 comments on commit 8831aff

Please sign in to comment.