Debian Codegraph @

tests/test_utils.py

raw
1 from __future__ import annotations
2
3 import ntpath
4 import posixpath
5 from types import ModuleType
6
7 import pytest
8 from markupsafe import escape
9 from markupsafe import Markup
10
11 from flask_debugtoolbar.utils import _relative_paths
12 from flask_debugtoolbar.utils import _shortest_relative_path
13 from flask_debugtoolbar.utils import decode_text
14 from flask_debugtoolbar.utils import format_sql
15 from flask_debugtoolbar.utils import HAVE_PYGMENTS
16
17
18 @pytest.mark.parametrize(
19 "value,paths,expected,path_module",
20 [
21 # should yield relative path to the parent directory
22 ("/foo/bar", ["/foo"], ["bar"], posixpath),
23 ("c:\\foo\\bar", ["c:\\foo"], ["bar"], ntpath),
24 # should not yield result if no path is a parent directory
25 ("/foo/bar", ["/baz"], [], posixpath),
26 ("c:\\foo\\bar", ["c:\\baz"], [], ntpath),
27 # should only yield relative paths for parent directories
28 ("/foo/bar", ["/foo", "/baz"], ["bar"], posixpath),
29 ("c:\\foo\\bar", ["c:\\foo", "c:\\baz"], ["bar"], ntpath),
30 # should yield all results when multiple parents match
31 ("/foo/bar/baz", ["/foo", "/foo/bar"], ["bar/baz", "baz"], posixpath),
32 ("c:\\foo\\bar\\baz", ["c:\\foo", "c:\\foo\\bar"], ["bar\\baz", "baz"], ntpath),
33 # should ignore case differences on windows
34 ("c:\\Foo\\bar", ["c:\\foo"], ["bar"], ntpath),
35 # should preserve original case
36 ("/Foo/Bar", ["/Foo"], ["Bar"], posixpath),
37 ("c:\\Foo\\Bar", ["c:\\foo"], ["Bar"], ntpath),
38 ],
39 )
40 def test_relative_paths(
41 value: str, paths: list[str], expected: list[str], path_module: ModuleType
42 ) -> None:
43 assert list(_relative_paths(value, paths, path_module)) == expected
44
45
46 @pytest.mark.parametrize(
47 "value,paths,expected,path_module",
48 [
49 # should yield relative path to the parent directory
50 ("/foo/bar", ["/foo"], "bar", posixpath),
51 ("c:\\foo\\bar", ["c:\\foo"], "bar", ntpath),
52 # should return the original value if no path is a parent directory
53 ("/foo/bar", ["/baz"], "/foo/bar", posixpath),
54 ("c:\\foo\\bar", ["c:\\baz"], "c:\\foo\\bar", ntpath),
55 # should yield shortest result when multiple parents match
56 ("/foo/bar/baz", ["/foo", "/foo/bar"], "baz", posixpath),
57 ("c:\\foo\\bar\\baz", ["c:\\foo", "c:\\foo\\bar"], "baz", ntpath),
58 ],
59 )
60 def test_shortest_relative_path(
61 value: str, paths: list[str], expected: str, path_module: ModuleType
62 ) -> None:
63 assert _shortest_relative_path(value, paths, path_module) == expected
64
65
66 def test_decode_text_unicode() -> None:
67 value = "\uffff"
68 decoded = decode_text(value)
69 assert decoded == value
70
71
72 def test_decode_text_ascii() -> None:
73 value = "abc"
74 assert decode_text(value.encode("ascii")) == value
75
76
77 def test_decode_text_non_ascii() -> None:
78 value = b"abc \xff xyz"
79 assert isinstance(value, bytes)
80
81 decoded = decode_text(value)
82 assert not isinstance(decoded, bytes)
83
84 assert decoded.startswith("abc")
85 assert decoded.endswith("xyz")
86
87
88 @pytest.fixture()
89 def no_pygments(monkeypatch: pytest.MonkeyPatch) -> None:
90 monkeypatch.setattr("flask_debugtoolbar.utils.HAVE_PYGMENTS", False)
91
92
93 @pytest.mark.usefixtures("no_pygments")
94 def test_format_sql_no_pygments() -> None:
95 sql = "select 1"
96 assert format_sql(sql, {}) == sql
97
98
99 @pytest.mark.usefixtures("no_pygments")
100 def test_format_sql_no_pygments_non_ascii() -> None:
101 sql = b"select '\xff'"
102 formatted = format_sql(sql, {})
103 assert formatted.startswith("select '")
104
105
106 @pytest.mark.usefixtures("no_pygments")
107 def test_format_sql_no_pygments_escape_html() -> None:
108 sql = "select x < 1"
109 formatted = format_sql(sql, {})
110 assert not isinstance(formatted, Markup)
111 assert escape(formatted) == "select x &lt; 1"
112
113
114 @pytest.mark.skipif(not HAVE_PYGMENTS, reason='test requires the "Pygments" library')
115 def test_format_sql_pygments() -> None:
116 sql = "select 1"
117 html = format_sql(sql, {})
118 assert isinstance(html, Markup)
119 assert html.startswith("<div")
120 assert "select" in html
121 assert "1" in html
122
123
124 @pytest.mark.skipif(not HAVE_PYGMENTS, reason='test requires the "Pygments" library')
125 def test_format_sql_pygments_non_ascii() -> None:
126 sql = b"select 'abc \xff xyz'"
127 html = format_sql(sql, {})
128 assert isinstance(html, Markup)
129 assert html.startswith("<div")
130 assert "select" in html
131 assert "abc" in html
132 assert "xyz" in html