Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use more features of unittest #156

Closed
wants to merge 6 commits into from
Closed
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 40 additions & 43 deletions tests/test_dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from signac_dashboard import Dashboard


class DashboardTestCase(unittest.TestCase):
class DashboardBaseTest(unittest.TestCase):
def get_response(self, query):
rv = self.test_client.get(query, follow_redirects=True)
return str(rv.get_data())
Expand All @@ -38,92 +38,85 @@ def setUp(self):
self.test_client = self.dashboard.app.test_client()
self.addCleanup(shutil.rmtree, self._tmp_dir)


class DashboardTestCase(DashboardBaseTest):
def test_get_project(self):
rv = self.test_client.get("/project/", follow_redirects=True)
response = str(rv.get_data())
assert "dashboard-test-project" in response
response = self.get_response("/project/")
self.assertTrue("dashboard-test-project" in response)

def test_get_jobs(self):
rv = self.test_client.get("/jobs/", follow_redirects=True)
response = str(rv.get_data())
assert "dashboard-test-project" in response
response = self.get_response("/jobs/")
self.assertTrue("dashboard-test-project" in response)

def test_job_count(self):
rv = self.test_client.get("/jobs/", follow_redirects=True)
response = str(rv.get_data())
assert f"{self.project.num_jobs()} jobs" in response
response = self.get_response("/jobs/")
self.assertTrue(f"{self.project.num_jobs()} 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
response = self.get_response(f"/search?q={query}")
self.assertTrue(f"{true_num_jobs} jobs" in response)

def test_doc_search(self):
dictquery = {"sum": 1}
true_num_jobs = len(list(self.project.find_jobs(doc_filter=dictquery)))
query = urlquote("doc:" + 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
response = self.get_response(f"/search?q={query}")
self.assertTrue(f"{true_num_jobs} jobs" in response)

def test_allow_where_search(self):
dictquery = {"sum": 1}
true_num_jobs = len(list(self.project.find_jobs(doc_filter=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
response = self.get_response(f"/search?q={query}")
self.assertTrue("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
response = self.get_response(f"/search?q={query}")
self.assertTrue(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"{self.project.num_jobs()} jobs" in response
response = self.get_response("/jobs")
self.assertTrue(f"{self.project.num_jobs()} 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"{self.project.num_jobs()} jobs" not in response
response = self.get_response("/jobs")
self.assertTrue(f"{self.project.num_jobs()} 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"{self.project.num_jobs()} jobs" in response
response = self.get_response("/jobs")
self.assertTrue(f"{self.project.num_jobs()} 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
self.assertTrue("Views" not 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
self.assertTrue("No modules." in response)

def test_project_sidebar(self):
response = self.get_response("/project/")
assert "No modules." in response
assert "Views" not in response
self.assertTrue("No modules." in response)
self.assertTrue("Views" not in response)


class AllModulesTestCase(DashboardTestCase):
"""Add all modules and contexts and test again."""

# todo, keep just the module initialization and move the duplicate code away
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like help refactoring this.

def setUp(self):
self._tmp_dir = tempfile.mkdtemp()
self.project = init_project(
Expand Down Expand Up @@ -155,27 +148,31 @@ def test_module_visible_mobile(self):
# Check for two instances of Modules header
pattern = re.compile("Modules</h")
module_headers = re.findall(pattern, response)
assert len(module_headers) == 2
self.assertTrue(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
with self.subTest(module=f"{m.name} in {m.context}."):
if m.context == "ProjectContext":
self.assertTrue(m.name in project_response)
elif m.context == "JobContext":
self.assertTrue(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
self.assertTrue(
"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
self.assertTrue(
"No modules for the JobContext are enabled." not in job_response
)


if __name__ == "__main__":
Expand Down