From e8e6d28479a0479361cd3de0fb854c243ed684b1 Mon Sep 17 00:00:00 2001 From: Elias Ram Date: Tue, 20 Feb 2024 20:41:18 +0100 Subject: [PATCH 1/2] test #8 added tests for LxmlLinkExtractor --- tests/test_linkextractors.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/tests/test_linkextractors.py b/tests/test_linkextractors.py index 18e9608c1..66a30c635 100644 --- a/tests/test_linkextractors.py +++ b/tests/test_linkextractors.py @@ -2,6 +2,7 @@ import pickle import re import unittest from typing import Optional +from unittest.mock import Mock from packaging.version import Version from pytest import mark @@ -851,3 +852,36 @@ class LxmlLinkExtractorTestCase(Base.LinkExtractorTestCase): ), ], ) + + def test_link_allowed_is_false_with_empty_url(self): + mock_link = Mock() + mock_link.url = "" + expected = False + + actual = LxmlLinkExtractor()._link_allowed(mock_link) + + self.assertEqual(expected, actual) + + def test_link_allowed_is_false_with_bad_url_prefix(self): + mock_link = Mock() + mock_link.url = "htp://should_be_http.com" + expected = False + + actual = LxmlLinkExtractor()._link_allowed(mock_link) + + self.assertEqual(expected, actual) + + def test_link_allowed_is_false_with_missing_url_prefix(self): + mock_link = Mock() + mock_link.url = "should_have_prefix.com" + expected = False + + actual = LxmlLinkExtractor()._link_allowed(mock_link) + + self.assertEqual(expected, actual) + + def test_link_allowed_raises_with_none_url(self): + mock_link = Mock() + mock_link.url = None + + self.assertRaises(AttributeError, LxmlLinkExtractor()._link_allowed, mock_link) From f19045403a44011a62162d73fedacf5038af098f Mon Sep 17 00:00:00 2001 From: Elias Ram Date: Wed, 21 Feb 2024 16:03:51 +0100 Subject: [PATCH 2/2] test #8 made tests cleaner --- tests/test_linkextractors.py | 34 ++++++---------------------------- 1 file changed, 6 insertions(+), 28 deletions(-) diff --git a/tests/test_linkextractors.py b/tests/test_linkextractors.py index 66a30c635..55ea9eed2 100644 --- a/tests/test_linkextractors.py +++ b/tests/test_linkextractors.py @@ -2,7 +2,6 @@ import pickle import re import unittest from typing import Optional -from unittest.mock import Mock from packaging.version import Version from pytest import mark @@ -854,34 +853,13 @@ class LxmlLinkExtractorTestCase(Base.LinkExtractorTestCase): ) def test_link_allowed_is_false_with_empty_url(self): - mock_link = Mock() - mock_link.url = "" - expected = False - - actual = LxmlLinkExtractor()._link_allowed(mock_link) - - self.assertEqual(expected, actual) + bad_link = Link("") + self.assertFalse(LxmlLinkExtractor()._link_allowed(bad_link)) def test_link_allowed_is_false_with_bad_url_prefix(self): - mock_link = Mock() - mock_link.url = "htp://should_be_http.com" - expected = False - - actual = LxmlLinkExtractor()._link_allowed(mock_link) - - self.assertEqual(expected, actual) + bad_link = Link("htp://should_be_http.example") + self.assertFalse(LxmlLinkExtractor()._link_allowed(bad_link)) def test_link_allowed_is_false_with_missing_url_prefix(self): - mock_link = Mock() - mock_link.url = "should_have_prefix.com" - expected = False - - actual = LxmlLinkExtractor()._link_allowed(mock_link) - - self.assertEqual(expected, actual) - - def test_link_allowed_raises_with_none_url(self): - mock_link = Mock() - mock_link.url = None - - self.assertRaises(AttributeError, LxmlLinkExtractor()._link_allowed, mock_link) + bad_link = Link("should_have_prefix.example") + self.assertFalse(LxmlLinkExtractor()._link_allowed(bad_link))