File: test_link.py

package info (click to toggle)
python-scrapy 2.13.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,664 kB
  • sloc: python: 52,028; xml: 199; makefile: 25; sh: 7
file content (57 lines) | stat: -rw-r--r-- 1,862 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import pytest

from scrapy.link import Link


class TestLink:
    def _assert_same_links(self, link1, link2):
        assert link1 == link2
        assert hash(link1) == hash(link2)

    def _assert_different_links(self, link1, link2):
        assert link1 != link2
        assert hash(link1) != hash(link2)

    def test_eq_and_hash(self):
        l1 = Link("http://www.example.com")
        l2 = Link("http://www.example.com/other")
        l3 = Link("http://www.example.com")

        self._assert_same_links(l1, l1)
        self._assert_different_links(l1, l2)
        self._assert_same_links(l1, l3)

        l4 = Link("http://www.example.com", text="test")
        l5 = Link("http://www.example.com", text="test2")
        l6 = Link("http://www.example.com", text="test")

        self._assert_same_links(l4, l4)
        self._assert_different_links(l4, l5)
        self._assert_same_links(l4, l6)

        l7 = Link(
            "http://www.example.com", text="test", fragment="something", nofollow=False
        )
        l8 = Link(
            "http://www.example.com", text="test", fragment="something", nofollow=False
        )
        l9 = Link(
            "http://www.example.com", text="test", fragment="something", nofollow=True
        )
        l10 = Link(
            "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):
        l1 = Link(
            "http://www.example.com", text="test", fragment="something", nofollow=True
        )
        l2 = eval(repr(l1))  # pylint: disable=eval-used
        self._assert_same_links(l1, l2)

    def test_bytes_url(self):
        with pytest.raises(TypeError):
            Link(b"http://www.example.com/\xc2\xa3")