1
0
mirror of https://github.com/scrapy/scrapy.git synced 2025-02-26 12:43:48 +00:00
scrapy/tests/test_link.py

60 lines
2.2 KiB
Python
Raw Normal View History

2011-01-25 19:51:17 -02:00
import unittest
import warnings
import six
2011-01-25 19:51:17 -02:00
from scrapy.link import Link
2011-01-25 19:51:17 -02:00
class LinkTest(unittest.TestCase):
def _assert_same_links(self, link1, link2):
self.assertEqual(link1, link2)
self.assertEqual(hash(link1), hash(link2))
def _assert_different_links(self, link1, link2):
self.assertNotEqual(link1, link2)
self.assertNotEqual(hash(link1), hash(link2))
2011-01-25 19:51:17 -02:00
def test_eq_and_hash(self):
2014-08-02 00:16:01 +06:00
l1 = Link(b"http://www.example.com")
l2 = Link(b"http://www.example.com/other")
l3 = Link(b"http://www.example.com")
2011-01-25 19:51:17 -02:00
self._assert_same_links(l1, l1)
self._assert_different_links(l1, l2)
self._assert_same_links(l1, l3)
2011-01-25 19:51:17 -02:00
2014-08-02 00:16:01 +06:00
l4 = Link(b"http://www.example.com", text="test")
l5 = Link(b"http://www.example.com", text="test2")
l6 = Link(b"http://www.example.com", text="test")
2011-01-25 19:51:17 -02:00
self._assert_same_links(l4, l4)
self._assert_different_links(l4, l5)
self._assert_same_links(l4, l6)
2014-08-02 00:16:01 +06:00
l7 = Link(b"http://www.example.com", text="test", fragment='something', nofollow=False)
l8 = Link(b"http://www.example.com", text="test", fragment='something', nofollow=False)
l9 = Link(b"http://www.example.com", text="test", fragment='something', nofollow=True)
l10 = Link(b"http://www.example.com", text="test", fragment='other', nofollow=False)
self._assert_same_links(l7, l8)
self._assert_different_links(l7, l9)
self._assert_different_links(l7, l10)
def test_repr(self):
2014-08-02 00:16:01 +06:00
l1 = Link(b"http://www.example.com", text="test", fragment='something', nofollow=True)
l2 = eval(repr(l1))
self._assert_same_links(l1, l2)
def test_non_str_url(self):
with warnings.catch_warnings(record=True) as w:
if six.PY2:
link = Link(u"http://www.example.com/\xa3")
self.assertIsInstance(link.url, str)
self.assertEqual(link.url, b'http://www.example.com/\xc2\xa3')
else:
link = Link(b"http://www.example.com/\xc2\xa3")
self.assertIsInstance(link.url, str)
self.assertEqual(link.url, u'http://www.example.com/\xa3')
assert len(w) == 1, "warning not issued"