Skip to content

Commit

Permalink
More unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mistercrunch committed Apr 19, 2016
1 parent 25ae91e commit 659350d
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 13 deletions.
1 change: 1 addition & 0 deletions caravel/viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ def __init__(self, datasource, form_data, slice_=None):
if not form.validate():
for k, v in form.errors.items():
if not data.get('json') and not data.get('async'):
logging.error("{}: {}".format(k, " ".join(v)))
flash("{}: {}".format(k, " ".join(v)), 'danger')
if previous_viz_type != self.viz_type:
data = {
Expand Down
69 changes: 56 additions & 13 deletions tests/core_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@

import caravel
from caravel import app, db, models, utils, appbuilder
from caravel.models import DruidCluster

#os.environ['CARAVEL_CONFIG'] = 'tests.caravel_test_config'
os.environ['CARAVEL_CONFIG'] = 'tests.caravel_test_config'

app.config['TESTING'] = True
app.config['CSRF_ENABLED'] = False
Expand All @@ -26,17 +27,34 @@
cli = imp.load_source('cli', BASE_DIR + "/bin/caravel")


class CoreTests(unittest.TestCase):
class CaravelTestCase(unittest.TestCase):

def __init__(self, *args, **kwargs):
super(CoreTests, self).__init__(*args, **kwargs)
super(CaravelTestCase, self).__init__(*args, **kwargs)
self.client = app.test_client()
role_admin = appbuilder.sm.find_role('Admin')
user = appbuilder.sm.find_user('admin')
if not user:
appbuilder.sm.add_user(
'admin', 'admin',' user', 'admin@fab.org',
role_admin, 'general')

def login(self):
self.client.post(
'/login/',
data=dict(username='admin', password='general'),
follow_redirects=True)


class CoreTests(CaravelTestCase):

def __init__(self, *args, **kwargs):
super(CoreTests, self).__init__(*args, **kwargs)
self.table_ids = {tbl.table_name: tbl.id for tbl in (
db.session
.query(models.SqlaTable)
.all()
)}
utils.init(caravel)
self.load_examples()

Expand All @@ -46,15 +64,27 @@ def setUp(self):
def tearDown(self):
pass

def login(self):
self.client.post(
'/login/',
data=dict(username='admin', password='general'),
follow_redirects=True)

def load_examples(self):
cli.load_examples(sample=True)

def test_save_slice(self):
self.login()

slice_id = db.session.query(models.Slice.id).filter_by(slice_name="Energy Sankey").scalar()
copy_name = "Test Sankey Save"
tbl_id = self.table_ids.get('energy_usage')
url = "/caravel/explore/table/{}/?viz_type=sankey&groupby=source&groupby=target&metric=sum__value&row_limit=5000&where=&having=&flt_col_0=source&flt_op_0=in&flt_eq_0=&slice_id={}&slice_name={}&collapsed_fieldsets=&action={}&datasource_name=energy_usage&datasource_id=1&datasource_type=table&previous_viz_type=sankey"

db.session.commit()
resp = self.client.get(
url.format(tbl_id, slice_id, copy_name, 'save'),
follow_redirects=True)
assert copy_name in resp.data.decode('utf-8')
resp = self.client.get(
url.format(tbl_id, slice_id, copy_name, 'overwrite'),
follow_redirects=True)
assert 'Energy' in resp.data.decode('utf-8')

def test_slices(self):
# Testing by running all the examples
self.login()
Expand All @@ -69,11 +99,12 @@ def test_slices(self):
self.client.get(url)

def test_csv(self):
self.client.get('/caravel/explore/table/1/?viz_type=table&granularity=ds&since=100+years&until=now&metrics=count&groupby=name&limit=50&show_brush=y&show_brush=false&show_legend=y&show_brush=false&rich_tooltip=y&show_brush=false&show_brush=false&show_brush=false&show_brush=false&y_axis_format=&x_axis_showminmax=y&show_brush=false&line_interpolation=linear&rolling_type=None&rolling_periods=&time_compare=&num_period_compare=&where=&having=&flt_col_0=gender&flt_op_0=in&flt_eq_0=&flt_col_0=gender&flt_op_0=in&flt_eq_0=&slice_id=14&slice_name=Boys&collapsed_fieldsets=&action=&datasource_name=birth_names&datasource_id=1&datasource_type=table&previous_viz_type=line&csv=true')
self.login()
self.client.get('/caravel/explore/table/{}/?viz_type=table&granularity=ds&since=100+years&until=now&metrics=count&groupby=name&limit=50&show_brush=y&show_brush=false&show_legend=y&show_brush=false&rich_tooltip=y&show_brush=false&show_brush=false&show_brush=false&show_brush=false&y_axis_format=&x_axis_showminmax=y&show_brush=false&line_interpolation=linear&rolling_type=None&rolling_periods=&time_compare=&num_period_compare=&where=&having=&flt_col_0=gender&flt_op_0=in&flt_eq_0=&flt_col_0=gender&flt_op_0=in&flt_eq_0=&slice_id=14&slice_name=Boys&collapsed_fieldsets=&action=&datasource_name=birth_names&datasource_id=1&datasource_type=table&previous_viz_type=line&csv=true'.format(self.table_ids.get('birth_names')))

def test_bubble_chart_no_time(self):
self.login()
response = self.client.get('/caravel/explore/table/1/?viz_type=bubble&series=source&entity=source&x=count&y=count&size=count&limit=50&x_log_scale=false&y_log_scale=false&show_legend=y&show_legend=false&max_bubble_size=25&where=&having=&flt_col_0=source&flt_op_0=in&flt_eq_0=&slice_id=&slice_name=&collapsed_fieldsets=&action=&datasource_name=energy_usage&datasource_id=1&datasource_type=table&previous_viz_type=bubble&json=true&force=false')
response = self.client.get('/caravel/explore/table/{}/?viz_type=bubble&series=source&entity=source&x=count&y=count&size=count&limit=50&x_log_scale=false&y_log_scale=false&show_legend=y&show_legend=false&max_bubble_size=25&where=&having=&flt_col_0=source&flt_op_0=in&flt_eq_0=&slice_id=&slice_name=&collapsed_fieldsets=&action=&datasource_name=energy_usage&datasource_id=1&datasource_type=table&previous_viz_type=bubble&json=true&force=false'.format(self.table_ids.get('energy_usage')))
self.assertEqual(response.status_code, 200)

def test_dashboard(self):
Expand All @@ -82,7 +113,6 @@ def test_dashboard(self):
for dash in db.session.query(models.Dashboard).all():
urls[dash.dashboard_title] = dash.url
for title, url in urls.items():
print(url)
assert escape(title) in self.client.get(url).data.decode('utf-8')

def test_doctests(self):
Expand All @@ -96,6 +126,16 @@ def test_misc(self):
assert self.client.get('/health').data.decode('utf-8') == "OK"
assert self.client.get('/ping').data.decode('utf-8') == "OK"

def test_shortner(self):
self.login()
data = "//caravel/explore/table/1/?viz_type=sankey&groupby=source&groupby=target&metric=sum__value&row_limit=5000&where=&having=&flt_col_0=source&flt_op_0=in&flt_eq_0=&slice_id=78&slice_name=Energy+Sankey&collapsed_fieldsets=&action=&datasource_name=energy_usage&datasource_id=1&datasource_type=table&previous_viz_type=sankey"
resp = self.client.post('/r/shortner/', data=data)
assert '/r/' in resp.data.decode('utf-8')

def test_save_dash(self):
self.login()



SEGMENT_METADATA = [{
"id" : "some_id",
Expand Down Expand Up @@ -129,9 +169,11 @@ class DruidTests(unittest.TestCase):

"""Testing interactions with Druid"""

def __init__(self, *args, **kwargs):
super(DruidTests, self).__init__(*args, **kwargs)

@patch('caravel.models.PyDruid')
def test_client(self, PyDruid):
from caravel.models import DruidCluster
instance = PyDruid.return_value
instance.time_boundary.return_value = [
{'result': {'maxTime': '2016-01-01'}}]
Expand All @@ -154,6 +196,7 @@ def test_client(self, PyDruid):
broker_host='localhost',
broker_port=7980,
metadata_last_refreshed=datetime.now())

db.session.add(cluster)
cluster.get_datasources = Mock(return_value=['test_datasource'])
cluster.refresh_datasources()
Expand Down

0 comments on commit 659350d

Please sign in to comment.