Debian Codegraph @

tests/test_short_url.py

raw
1 """Shortening url tests."""
2
3 from urllib.parse import urlparse
4
5 import pytest
6
7 from pydata_sphinx_theme.short_link import ShortenLinkTransform
8
9
10 class Mock:
11 """mock object."""
12
13 pass
14
15
16 @pytest.mark.parametrize(
17 "platform,url,expected",
18 [
19 # TODO, I belive this is wrong as both github.com and github.com/github
20 # shorten to just github.
21 ("github", "https://github.com", "github"),
22 ("github", "https://github.com/github", "github"),
23 ("github", "https://github.com/pydata", "pydata"),
24 (
25 "github",
26 "https://github.com/pydata/pydata-sphinx-theme",
27 "pydata/pydata-sphinx-theme",
28 ),
29 (
30 "github",
31 "https://github.com/pydata/pydata-sphinx-theme/pull/1012",
32 "pydata/pydata-sphinx-theme#1012",
33 ),
34 # TODO, I belive this is wrong as both orgs/pydata/projects/2 and
35 # pydata/projects/issue/2 shorten to the same
36 ("github", "https://github.com/orgs/pydata/projects/2", "pydata/projects#2"),
37 ("github", "https://github.com/pydata/projects/pull/2", "pydata/projects#2"),
38 # issues and pulls are athe same, so it's ok to normalise to the same here
39 ("github", "https://github.com/pydata/projects/issues/2", "pydata/projects#2"),
40 # Gitlab
41 ("gitlab", "https://gitlab.com/tezos/tezos/-/issues", "tezos/tezos/issues"),
42 ("gitlab", "https://gitlab.com/tezos/tezos/issues", "tezos/tezos/issues"),
43 (
44 "gitlab",
45 "https://gitlab.com/gitlab-org/gitlab/-/issues/375583",
46 "gitlab-org/gitlab#375583",
47 ),
48 (
49 # TODO, non canonical url, discuss if should maybe be shortened to
50 # gitlab-org/gitlab#375583
51 "gitlab",
52 "https://gitlab.com/gitlab-org/gitlab/issues/375583",
53 "gitlab-org/gitlab/issues/375583",
54 ),
55 (
56 "gitlab",
57 "https://gitlab.com/gitlab-org/gitlab/-/issues/",
58 "gitlab-org/gitlab/issues",
59 ),
60 (
61 # TODO, non canonical url, discuss if should maybe be shortened to
62 # gitlab-org/gitlab/issues (no trailing slash)
63 "gitlab",
64 "https://gitlab.com/gitlab-org/gitlab/issues/",
65 "gitlab-org/gitlab/issues/",
66 ),
67 (
68 "gitlab",
69 "https://gitlab.com/gitlab-org/gitlab/-/issues",
70 "gitlab-org/gitlab/issues",
71 ),
72 (
73 "gitlab",
74 "https://gitlab.com/gitlab-org/gitlab/issues",
75 "gitlab-org/gitlab/issues",
76 ),
77 (
78 "gitlab",
79 "https://gitlab.com/gitlab-org/gitlab/-/merge_requests/84669",
80 "gitlab-org/gitlab!84669",
81 ),
82 (
83 "gitlab",
84 "https://gitlab.com/gitlab-org/gitlab/-/pipelines/511894707",
85 "gitlab-org/gitlab/-/pipelines/511894707",
86 ),
87 (
88 "gitlab",
89 "https://gitlab.com/gitlab-com/gl-infra/production/-/issues/6788",
90 "gitlab-com/gl-infra/production#6788",
91 ),
92 ],
93 )
94 def test_shorten(platform, url, expected):
95 """Unit test for url shortening.
96
97 Usually you also want a build test in `test_build.py`
98 """
99 document = Mock()
100 document.settings = Mock()
101 document.settings.language_code = "en"
102 document.reporter = None
103
104 sl = ShortenLinkTransform(document)
105 sl.platform = platform
106
107 URI = urlparse(url)
108
109 assert sl.parse_url(URI) == expected