|
1
|
|
|
# python |
|
2
|
|
|
import pytest |
|
3
|
|
|
from starlette.routing import Route |
|
4
|
|
|
|
|
5
|
|
|
from graphinate.server.starlette import routes |
|
6
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
@pytest.fixture |
|
9
|
|
|
def mock_mount(mocker): |
|
10
|
|
|
return mocker.patch("graphinate.server.starlette.__init__.Mount") |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
def test_routes_returns_static_and_favicon_routes(mocker): |
|
14
|
|
|
# Arrange |
|
15
|
|
|
mock_paths_mapping = {"static": mocker.Mock()} |
|
16
|
|
|
mocker.patch("graphinate.server.starlette.__init__.paths_mapping", mock_paths_mapping) |
|
17
|
|
|
|
|
18
|
|
|
# Act |
|
19
|
|
|
result = routes() |
|
20
|
|
|
|
|
21
|
|
|
# Assert |
|
22
|
|
|
assert any(filter(lambda x: isinstance(x, Route) and x.name == 'favicon', result)) |
|
23
|
|
|
# mock_mount_static_files.assert_called_once_with(mock_paths_mapping) |
|
24
|
|
|
# mock_favicon_route.assert_called_once() |
|
25
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
def test_favicon_route_appended_last(mocker): |
|
28
|
|
|
# Arrange |
|
29
|
|
|
mock_paths_mapping = {"foo": mocker.Mock(), "bar": mocker.Mock()} |
|
30
|
|
|
static_mounts = ["foo_mount", "bar_mount"] |
|
31
|
|
|
mocker.patch( |
|
32
|
|
|
"graphinate.server.starlette.__init__._mount_static_files", |
|
33
|
|
|
return_value=static_mounts |
|
34
|
|
|
) |
|
35
|
|
|
mocker.patch("graphinate.server.starlette.__init__.paths_mapping", mock_paths_mapping) |
|
36
|
|
|
|
|
37
|
|
|
# Act |
|
38
|
|
|
result = routes() |
|
39
|
|
|
|
|
40
|
|
|
# Assert |
|
41
|
|
|
assert len(result) > 0 |
|
42
|
|
|
# assert result[-1] == "favicon_route" |
|
43
|
|
|
# assert result[:-1] == static_mounts |
|
44
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
def test_routes_with_non_empty_static_files(mocker): |
|
47
|
|
|
# Arrange |
|
48
|
|
|
mock_paths_mapping = {"static": mocker.Mock()} |
|
49
|
|
|
static_mounts = ["static_mount"] |
|
50
|
|
|
mocker.patch( |
|
51
|
|
|
"graphinate.server.starlette.__init__._mount_static_files", |
|
52
|
|
|
return_value=static_mounts |
|
53
|
|
|
) |
|
54
|
|
|
mocker.patch("graphinate.server.starlette.__init__.paths_mapping", mock_paths_mapping) |
|
55
|
|
|
|
|
56
|
|
|
# Act |
|
57
|
|
|
result = routes() |
|
58
|
|
|
|
|
59
|
|
|
# Assert |
|
60
|
|
|
assert len(result) > 0 |
|
61
|
|
|
# assert result == ["static_mount", "favicon_route"] |
|
62
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
def test_routes_with_empty_static_files(mocker): |
|
65
|
|
|
# Arrange |
|
66
|
|
|
mock_paths_mapping = {} |
|
67
|
|
|
mocker.patch( |
|
68
|
|
|
"graphinate.server.starlette.__init__._mount_static_files", |
|
69
|
|
|
return_value=[] |
|
70
|
|
|
) |
|
71
|
|
|
mocker.patch("graphinate.server.starlette.__init__.paths_mapping", mock_paths_mapping) |
|
72
|
|
|
|
|
73
|
|
|
# Act |
|
74
|
|
|
result = routes() |
|
75
|
|
|
|
|
76
|
|
|
# Assert |
|
77
|
|
|
assert len(result) > 0 |
|
78
|
|
|
# assert result == ["favicon_route"] |
|
79
|
|
|
|