|
12 | 12 | get_combined_config, |
13 | 13 | load_dash_env_vars, |
14 | 14 | ) |
15 | | -from dash._utils import get_asset_path |
| 15 | +from dash._utils import ( |
| 16 | + get_asset_path, |
| 17 | + get_relative_path, |
| 18 | + strip_relative_path, |
| 19 | +) |
16 | 20 |
|
17 | 21 |
|
18 | 22 | @pytest.fixture |
@@ -156,3 +160,81 @@ def test_load_dash_env_vars_refects_to_os_environ(empty_environ): |
156 | 160 | def test_app_name_server(empty_environ, name, server, expected): |
157 | 161 | app = Dash(name=name, server=server) |
158 | 162 | assert app.config.name == expected |
| 163 | + |
| 164 | + |
| 165 | +@pytest.mark.parametrize( |
| 166 | + "prefix, partial_path, expected", |
| 167 | + [ |
| 168 | + ("/", "", "/"), |
| 169 | + ("/my-dash-app/", "", "/my-dash-app/"), |
| 170 | +
|
| 171 | + ("/", "/", "/"), |
| 172 | + ("/my-dash-app/", "/", "/my-dash-app/"), |
| 173 | +
|
| 174 | + ("/", "/page-1", "/page-1"), |
| 175 | + ("/my-dash-app/", "/page-1", "/my-dash-app/page-1"), |
| 176 | +
|
| 177 | + ("/", "/page-1/", "/page-1/"), |
| 178 | + ("/my-dash-app/", "/page-1/", "/my-dash-app/page-1/"), |
| 179 | +
|
| 180 | + ("/", "/page-1/sub-page-1", "/page-1/sub-page-1"), |
| 181 | + ("/my-dash-app/", "/page-1/sub-page-1", "/my-dash-app/page-1/sub-page-1"), |
| 182 | + ] |
| 183 | +) |
| 184 | +def test_pathname_prefix_relative_url(prefix, partial_path, expected): |
| 185 | + path = get_relative_path(prefix, partial_path) |
| 186 | + assert path == expected |
| 187 | + |
| 188 | +@pytest.mark.parametrize( |
| 189 | + "prefix, partial_path", |
| 190 | + [ |
| 191 | + ("/", "relative-page-1"), |
| 192 | + ("/my-dash-app/", "relative-page-1"), |
| 193 | + ] |
| 194 | +) |
| 195 | +def test_invalid_get_relative_path(prefix, partial_path): |
| 196 | + with pytest.raises(_exc.UnsupportedRelativePath): |
| 197 | + get_relative_path(prefix, partial_path) |
| 198 | + |
| 199 | +@pytest.mark.parametrize( |
| 200 | + "prefix, partial_path, expected", |
| 201 | + [ |
| 202 | + ("/", None, None), |
| 203 | + ("/my-dash-app/", None, None), |
| 204 | +
|
| 205 | + ("/", "/", ""), |
| 206 | + ("/my-dash-app/", "/my-dash-app", ""), |
| 207 | + ("/my-dash-app/", "/my-dash-app/", ""), |
| 208 | +
|
| 209 | + ("/", "/page-1", "page-1"), |
| 210 | + ("/my-dash-app/", "/my-dash-app/page-1", "page-1"), |
| 211 | +
|
| 212 | + ("/", "/page-1/", "page-1"), |
| 213 | + ("/my-dash-app/", "/my-dash-app/page-1/", "page-1"), |
| 214 | +
|
| 215 | + ("/", "/page-1/sub-page-1", "page-1/sub-page-1"), |
| 216 | + ("/my-dash-app/", "/my-dash-app/page-1/sub-page-1", "page-1/sub-page-1"), |
| 217 | +
|
| 218 | + ("/", "/page-1/sub-page-1/", "page-1/sub-page-1"), |
| 219 | + ("/my-dash-app/", "/my-dash-app/page-1/sub-page-1/", "page-1/sub-page-1"), |
| 220 | +
|
| 221 | + ("/my-dash-app/", "/my-dash-app/my-dash-app/", "my-dash-app"), |
| 222 | + ("/my-dash-app/", "/my-dash-app/something-else/my-dash-app/", "something-else/my-dash-app"), |
| 223 | + ] |
| 224 | +) |
| 225 | +def test_strip_relative_path(prefix, partial_path, expected): |
| 226 | + path = strip_relative_path(prefix, partial_path) |
| 227 | + assert path == expected |
| 228 | + |
| 229 | + |
| 230 | +@pytest.mark.parametrize( |
| 231 | + "prefix, partial_path", |
| 232 | + [ |
| 233 | + ("/", "relative-page-1"), |
| 234 | + ("/my-dash-app", "relative-page-1"), |
| 235 | + ("/my-dash-app", "/some-other-path") |
| 236 | + ] |
| 237 | +) |
| 238 | +def test_invalid_strip_relative_path(prefix, partial_path): |
| 239 | + with pytest.raises(_exc.UnsupportedRelativePath): |
| 240 | + strip_relative_path(prefix, partial_path) |
0 commit comments