-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest_dashboard.py
More file actions
235 lines (196 loc) · 8.89 KB
/
test_dashboard.py
File metadata and controls
235 lines (196 loc) · 8.89 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# Copyright (c) 2022 The Regents of the University of Michigan
# All rights reserved.
# This software is licensed under the BSD 3-Clause License.
import json
import re
import shutil
import tempfile
import unittest
from urllib.parse import quote as urlquote
import pytest
from signac import init_project
import signac_dashboard.modules
from signac_dashboard import Dashboard
class DashboardTestCase(unittest.TestCase):
def get_response(self, query):
rv = self.test_client.get(query, follow_redirects=True)
return str(rv.get_data())
def setUp(self):
self._tmp_dir = tempfile.mkdtemp()
self.project = init_project(self._tmp_dir)
# Set up some fake jobs
for a in range(3):
for b in range(2):
job = self.project.open_job({"a": a, "b": b})
with job:
job.document["sum"] = a + b
self.config = {"ACCESS_TOKEN": "test"}
self.modules = []
self.dashboard = Dashboard(
config=self.config, project=self.project, modules=self.modules
)
self.test_client = self.dashboard.app.test_client()
self.addCleanup(shutil.rmtree, self._tmp_dir)
# Test logged out content
response = self.get_response("/")
assert "Login required" in response
response = self.get_response("/jobs/7f9fb369851609ce9cb91404549393f3")
assert "Login required" in response
response = self.get_response("/login?token=error")
assert "Login required" in response
assert "Incorrect token" in response
# login
self.test_client.get("/login?token=test", follow_redirects=True)
def test_get_project(self):
rv = self.test_client.get("/project/", follow_redirects=True)
response = str(rv.get_data())
assert "signac-dashboard" in response
def test_get_jobs(self):
rv = self.test_client.get("/jobs/", follow_redirects=True)
response = str(rv.get_data())
assert "signac-dashboard: Jobs" in response
def test_job_count(self):
rv = self.test_client.get("/jobs/", follow_redirects=True)
response = str(rv.get_data())
assert f"{len(self.project)} jobs" in response
def test_sp_search(self):
dictquery = {"a": 0}
true_num_jobs = len(list(self.project.find_jobs(dictquery)))
query = urlquote(json.dumps(dictquery))
rv = self.test_client.get(f"/search?q={query}", follow_redirects=True)
response = str(rv.get_data())
assert f"{true_num_jobs} jobs" in response
def test_doc_search(self):
dictquery = {"doc.sum": 1}
true_num_jobs = len(list(self.project.find_jobs(dictquery)))
query = urlquote(json.dumps(dictquery))
rv = self.test_client.get(f"/search?q={query}", follow_redirects=True)
response = str(rv.get_data())
assert f"{true_num_jobs} jobs" in response
def test_allow_where_search(self):
dictquery = {"doc.sum": 1}
true_num_jobs = len(list(self.project.find_jobs(dictquery)))
query = urlquote('doc.sum.$where "lambda x: x == 1"')
self.dashboard.config["ALLOW_WHERE"] = False
rv = self.test_client.get(f"/search?q={query}", follow_redirects=True)
response = str(rv.get_data())
assert "ALLOW_WHERE must be enabled for this query." in response
self.dashboard.config["ALLOW_WHERE"] = True
rv = self.test_client.get(f"/search?q={query}", follow_redirects=True)
response = str(rv.get_data())
assert f"{true_num_jobs} jobs" in response
def test_update_cache(self):
rv = self.test_client.get("/jobs", follow_redirects=True)
response = str(rv.get_data())
assert f"{len(self.project)} jobs" in response
# Create a new job. Because the project has been cached, the response
# will be wrong until the cache is cleared.
self.project.open_job({"a": "test-cache"}).init()
rv = self.test_client.get("/jobs", follow_redirects=True)
response = str(rv.get_data())
assert f"{len(self.project)} jobs" not in response
# Clear cache and try again.
self.dashboard.update_cache()
rv = self.test_client.get("/jobs", follow_redirects=True)
response = str(rv.get_data())
assert f"{len(self.project)} jobs" in response
def test_no_view_single_job(self):
"""Make sure View panel is not shown when on a single job page."""
response = self.get_response("/jobs/7f9fb369851609ce9cb91404549393f3")
assert "Views" not in response
def test_logout(self):
response = self.get_response("/logout")
if self.dashboard.config.get("ACCESS_TOKEN") is not None:
assert "Login required" in response
class NoModulesTestCase(DashboardTestCase):
"""Test the inherited tests and cases without any modules."""
def test_job_sidebar(self):
response = self.get_response("/jobs/?view=grid")
assert "No modules." in response
def test_project_sidebar(self):
response = self.get_response("/project/")
assert "No modules." in response
assert "Views" not in response
class AllModulesTestCase(DashboardTestCase):
"""Add all modules and contexts and test again."""
def setUp(self):
self._tmp_dir = tempfile.mkdtemp()
self.project = init_project(self._tmp_dir)
# Set up some fake jobs
for a in range(3):
for b in range(2):
job = self.project.open_job({"a": a, "b": b})
with job:
job.document["sum"] = a + b
self.config = {"ACCESS_TOKEN": None}
modules = []
for m in signac_dashboard.modules.__all__:
module = getattr(signac_dashboard.modules, m)
for c in module._supported_contexts:
modules.append(module(context=c))
with self.assertRaises(RuntimeError):
module(context="BadContext")
self.modules = modules
self.dashboard = Dashboard(
config=self.config, project=self.project, modules=self.modules
)
self.test_client = self.dashboard.app.test_client()
self.addCleanup(shutil.rmtree, self._tmp_dir)
def test_login_with_None_token(self):
rv = self.test_client.get("/login", follow_redirects=True)
response = str(rv.get_data())
assert "signac-dashboard" in response
def test_module_visible_mobile(self):
response = self.get_response("/jobs/?view=grid")
# Check for two instances of Modules header
pattern = re.compile("Modules</h")
module_headers = re.findall(pattern, response)
assert len(module_headers) == 2
def test_module_selector(self):
project_response = self.get_response("/project/")
job_response = self.get_response("/jobs/?view=grid")
for m in self.modules:
print(f"Checking for {m.name} in {m.context}.")
if m.context == "ProjectContext":
assert m.name in project_response
elif m.context == "JobContext":
assert m.name in job_response
def test_enabled_module_indices_project_session(self):
"""Ensure that the message is not displayed when modules are actually enabled."""
project_response = self.get_response("/project/")
assert "No modules for the ProjectContext are enabled." not in project_response
def test_enabled_module_indices_job_session(self):
"""Ensure that the message is not displayed when modules are actually enabled."""
job_response = self.get_response("/jobs/?view=grid")
assert "No modules for the JobContext are enabled." not in job_response
def test_navigator_module(self):
"""Look for the next and previous values in a table on the page."""
response = self.get_response("/jobs/017d53deb17a290d8b0d2ae02fa8bd9d")
# next job for a = 2
assert '<a href="/jobs/fb4e5868559e719f0c5826de08023281"' in response
# next job for b = 1
assert '<a href="/jobs/386b19932c82f3f9749dd6611e846293"' in response
assert "disabled>min</div>" in response # no previous job for b
@pytest.mark.parametrize(
"filename,expected",
[
("test.pdf", "fa-file-pdf"),
("archive.zip", "fa-file-archive"),
("image.png", "fa-file-image"),
("audio.mp3", "fa-file-audio"),
("video.mp4", "fa-file-video"),
("text.txt", "fa-file-alt"),
("text.csv", "fa-file-excel"),
("code.sh", "fa-file-code"),
("code.py", "fa-file-code"),
("code.h", "fa-file-code"),
("code.c", "fa-file-code"),
("code.json", "fa-file-code"),
],
)
def test_file_list_icon(filename, expected):
"""Test that FileList._get_icon returns correct icon classes."""
file_list = signac_dashboard.modules.FileList()
assert file_list._get_icon(filename) == expected
if __name__ == "__main__":
unittest.main()