redash/tests/handlers/test_query_snippets.py

61 lines
2.0 KiB
Python
Raw Normal View History

2016-12-06 10:26:48 +00:00
from tests import BaseTestCase
from redash.models import QuerySnippet
class TestQuerySnippetResource(BaseTestCase):
def test_get_snippet(self):
snippet = self.factory.create_query_snippet()
rv = self.make_request("get", "/api/query_snippets/{}".format(snippet.id))
2016-12-06 10:26:48 +00:00
for field in ("snippet", "description", "trigger"):
2016-12-06 10:26:48 +00:00
self.assertEqual(rv.json[field], getattr(snippet, field))
def test_update_snippet(self):
snippet = self.factory.create_query_snippet()
data = {
"snippet": "updated",
"trigger": "updated trigger",
"description": "updated description",
2016-12-06 10:26:48 +00:00
}
rv = self.make_request(
"post", "/api/query_snippets/{}".format(snippet.id), data=data
)
2016-12-06 10:26:48 +00:00
for field in ("snippet", "description", "trigger"):
2016-12-06 10:26:48 +00:00
self.assertEqual(rv.json[field], data[field])
def test_delete_snippet(self):
snippet = self.factory.create_query_snippet()
rv = self.make_request("delete", "/api/query_snippets/{}".format(snippet.id))
2016-12-06 10:26:48 +00:00
self.assertIsNone(QuerySnippet.query.get(snippet.id))
class TestQuerySnippetListResource(BaseTestCase):
def test_create_snippet(self):
data = {
"snippet": "updated",
"trigger": "updated trigger",
"description": "updated description",
2016-12-06 10:26:48 +00:00
}
rv = self.make_request("post", "/api/query_snippets", data=data)
2016-12-06 10:26:48 +00:00
self.assertEqual(rv.status_code, 200)
def test_list_all_snippets(self):
snippet1 = self.factory.create_query_snippet()
snippet2 = self.factory.create_query_snippet()
snippet_diff_org = self.factory.create_query_snippet(
org=self.factory.create_org()
)
2016-12-06 10:26:48 +00:00
rv = self.make_request("get", "/api/query_snippets")
ids = [s["id"] for s in rv.json]
2016-12-06 10:26:48 +00:00
self.assertIn(snippet1.id, ids)
self.assertIn(snippet2.id, ids)
self.assertNotIn(snippet_diff_org.id, ids)