Skip to content

Commit

Permalink
Fix tests for deprecate v1 (#2140)
Browse files Browse the repository at this point in the history
* Fixed tests for control panels container and filters

* Fixed python tests for explorev2

* Fix linting errors
  • Loading branch information
vera-liu authored and mistercrunch committed Feb 11, 2017
1 parent f6f15f6 commit 9017ecf
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 66 deletions.
12 changes: 12 additions & 0 deletions superset/assets/javascripts/explorev2/stores/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,15 @@ export const autoQueryFields = [
'datasource',
'viz_type',
];

const defaultFields = Object.assign({}, fields);
Object.keys(fields).forEach((f) => {
defaultFields[f].value = fields[f].default;
});

const defaultState = {
fields: defaultFields,
form_data: getFormDataFromFields(defaultFields),
};

export { defaultFields, defaultState };
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import React from 'react';
import { expect } from 'chai';
import { describe, it } from 'mocha';
import { shallow } from 'enzyme';

import ExploreActionButtons from
'../../../../javascripts/explorev2/components/ExploreActionButtons';
'../../../../javascripts/explorev2/components/ExploreActionButtons';

describe('ExploreActionButtons', () => {
const defaultProps = {
Expand Down
12 changes: 6 additions & 6 deletions superset/assets/spec/javascripts/explorev2/actions_spec.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { it, describe } from 'mocha';
import { expect } from 'chai';
import * as actions from '../../../javascripts/explorev2/actions/exploreActions';
import { initialState } from '../../../javascripts/explorev2/stores/store';
import { defaultState } from '../../../javascripts/explorev2/stores/store';
import { exploreReducer } from '../../../javascripts/explorev2/reducers/exploreReducer';

describe('reducers', () => {
it('sets correct field value given a key and value', () => {
const newState = exploreReducer(
initialState('dist_bar'), actions.setFieldValue('x_axis_label', 'x'));
expect(newState.viz.form_data.x_axis_label).to.equal('x');
defaultState, actions.setFieldValue('x_axis_label', 'x', []));
expect(newState.fields.x_axis_label.value).to.equal('x');
});
it('setFieldValue works as expected with a checkbox', () => {
const newState = exploreReducer(initialState('dist_bar'),
actions.setFieldValue('show_legend', true));
expect(newState.viz.form_data.show_legend).to.equal(true);
const newState = exploreReducer(defaultState,
actions.setFieldValue('show_legend', true, []));
expect(newState.fields.show_legend.value).to.equal(true);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,19 @@ import { expect } from 'chai';
import { describe, it, beforeEach } from 'mocha';
import { shallow } from 'enzyme';
import { Panel } from 'react-bootstrap';
import { defaultFormData, initialState } from '../../../../javascripts/explorev2/stores/store';

import { getFormDataFromFields, defaultFields }
from '../../../../javascripts/explorev2/stores/store';
import {
ControlPanelsContainer,
} from '../../../../javascripts/explorev2/components/ControlPanelsContainer';
import { fields } from '../../../../javascripts/explorev2/stores/fields';

const defaultProps = {
datasource_id: 1,
datasource_type: 'type',
exploreState: initialState(),
form_data: defaultFormData(),
fields,
actions: {
fetchFieldOptions: () => {
// noop
},
},
datasource_type: 'table',
actions: {},
fields: defaultFields,
form_data: getFormDataFromFields(defaultFields),
isDatasourceMetaLoading: false,
exploreState: {},
};

describe('ControlPanelsContainer', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { expect } from 'chai';
import { describe, it, beforeEach } from 'mocha';
import { shallow } from 'enzyme';
import Filter from '../../../../javascripts/explorev2/components/Filter';
import SelectField from '../../../../javascripts/explorev2/components/SelectField';

const defaultProps = {
choices: ['country_name'],
Expand All @@ -16,8 +17,6 @@ const defaultProps = {
// noop
},
filter: {
id: 1,
prefix: 'flt',
col: null,
op: 'in',
value: '',
Expand Down Expand Up @@ -45,16 +44,16 @@ describe('Filter', () => {
it('renders two selects, one button and one input', () => {
expect(wrapper.find(Select)).to.have.lengthOf(2);
expect(wrapper.find(Button)).to.have.lengthOf(1);
expect(wrapper.find('input')).to.have.lengthOf(1);
expect(wrapper.find(SelectField)).to.have.lengthOf(1);
});

it('calls changeFilter when select is changed', () => {
const selectCol = wrapper.find('#select-col');
selectCol.simulate('change', { value: 'col' });
const selectOp = wrapper.find('#select-op');
selectOp.simulate('change', { value: 'in' });
const input = wrapper.find('input');
input.simulate('change', { target: { value: 'x' } });
const selectVal = wrapper.find(SelectField);
selectVal.simulate('change', { value: 'x' });
expect(defaultProps.changeFilter).to.have.property('callCount', 3);
});
});
12 changes: 11 additions & 1 deletion superset/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1478,7 +1478,17 @@ def get_viz(
@expose("/slice/<slice_id>/")
def slice(self, slice_id):
viz_obj = self.get_viz(slice_id)
return redirect(viz_obj.get_url(**request.args))
endpoint = (
'/superset/explore/{}/{}?form_data={}'
.format(
viz_obj.datasource.type,
viz_obj.datasource.id,
json.dumps(viz_obj.form_data)
)
)
if request.args.get("standalone") == "true":
endpoint += '&standalone=true'
return redirect(endpoint)

@log_this
@has_access_api
Expand Down
80 changes: 58 additions & 22 deletions tests/core_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,22 @@ def test_slice_json_endpoint(self):
self.login(username='admin')
slc = self.get_slice("Girls", db.session)

resp = self.get_resp(slc.viz.json_endpoint)
json_endpoint = (
'/superset/explore_json/{}/{}?form_data={}'
.format(slc.datasource_type, slc.datasource_id, json.dumps(slc.viz.form_data))
)
resp = self.get_resp(json_endpoint)
assert '"Jennifer"' in resp

def test_slice_csv_endpoint(self):
self.login(username='admin')
slc = self.get_slice("Girls", db.session)

resp = self.get_resp(slc.viz.csv_endpoint)
csv_endpoint = (
'/superset/explore_json/{}/{}?csv=true&form_data={}'
.format(slc.datasource_type, slc.datasource_id, json.dumps(slc.viz.form_data))
)
resp = self.get_resp(csv_endpoint)
assert 'Jennifer,' in resp

def test_admin_only_permissions(self):
Expand Down Expand Up @@ -122,24 +130,55 @@ def test_save_slice(self):
db.session.commit()
copy_name = "Test Sankey Save"
tbl_id = self.table_ids.get('energy_usage')
url = (
"/superset/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")
new_slice_name = "Test Sankey Overwirte"

# Changing name
resp = self.get_resp(url.format(tbl_id, slice_id, copy_name, 'save'))
assert copy_name in resp

# Setting the name back to its original name
resp = self.get_resp(url.format(tbl_id, slice_id, slice_name, 'save'))
assert slice_name in resp
url = (
"/superset/explore/table/{}/?slice_name={}&"
"action={}&datasource_name=energy_usage&form_data={}")

form_data = {
'viz_type': 'sankey',
'groupby': 'source',
'groupby': 'target',
'metric': 'sum__value',
'row_limit': 5000,
'slice_id': slice_id,
}
# Changing name and save as a new slice
resp = self.get_resp(
url.format(
tbl_id,
copy_name,
'saveas',
json.dumps(form_data)
)
)
slices = db.session.query(models.Slice) \
.filter_by(slice_name=copy_name).all()
assert len(slices) == 1
new_slice_id = slices[0].id

form_data = {
'viz_type': 'sankey',
'groupby': 'source',
'groupby': 'target',
'metric': 'sum__value',
'row_limit': 5000,
'slice_id': new_slice_id,
}
# Setting the name back to its original name by overwriting new slice
resp = self.get_resp(
url.format(
tbl_id,
new_slice_name,
'overwrite',
json.dumps(form_data)
)
)
slc = db.session.query(models.Slice).filter_by(id=new_slice_id).first()
assert slc.slice_name == new_slice_name
db.session.delete(slc)

# Doing a basic overwrite
assert 'Energy' in self.get_resp(
url.format(tbl_id, slice_id, copy_name, 'overwrite'))

def test_filter_endpoint(self):
self.login(username='admin')
Expand Down Expand Up @@ -168,8 +207,6 @@ def test_slices(self):
for slc in db.session.query(Slc).all():
urls += [
(slc.slice_name, 'slice_url', slc.slice_url),
(slc.slice_name, 'json_endpoint', slc.viz.json_endpoint),
(slc.slice_name, 'csv_endpoint', slc.viz.csv_endpoint),
(slc.slice_name, 'slice_id_url', slc.slice_id_url),
]
for name, method, url in urls:
Expand Down Expand Up @@ -544,8 +581,7 @@ def test_fetch_datasource_metadata(self):
self.login(username='admin')
url = (
'/superset/fetch_datasource_metadata?'
'datasource_type=table&'
'datasource_id=1'
+ 'datasourceKey=1__table'
)
resp = self.get_json_resp(url)
keys = [
Expand Down
48 changes: 31 additions & 17 deletions tests/druid_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,30 +116,44 @@ def test_client(self, PyDruid):

resp = self.get_resp('/superset/explore/druid/{}/'.format(
datasource_id))
self.assertIn("[test_cluster].[test_datasource]", resp)

self.assertIn("test_datasource", resp)
form_data = {
'viz_type': 'table',
'granularity': 'one+day',
'druid_time_origin': '',
'since': '7+days+ago',
'until': 'now',
'row_limit': 5000,
'include_search': 'false',
'metrics': ['count'],
'groupby': ['dim1'],
'force': 'true',
}
# One groupby
url = (
'/superset/explore_json/druid/{}/?viz_type=table&granularity=one+day&'
'druid_time_origin=&since=7+days+ago&until=now&row_limit=5000&'
'include_search=false&metrics=count&groupby=dim1&flt_col_0=dim1&'
'flt_op_0=in&flt_eq_0=&slice_id=&slice_name=&collapsed_fieldsets=&'
'action=&datasource_name=test_datasource&datasource_id={}&'
'datasource_type=druid&previous_viz_type=table&'
'force=true'.format(datasource_id, datasource_id))
'/superset/explore_json/druid/{}/?form_data={}'.format(
datasource_id, json.dumps(form_data))
)
resp = self.get_json_resp(url)
self.assertEqual("Canada", resp['data']['records'][0]['dim1'])

form_data = {
'viz_type': 'table',
'granularity': 'one+day',
'druid_time_origin': '',
'since': '7+days+ago',
'until': 'now',
'row_limit': 5000,
'include_search': 'false',
'metrics': ['count'],
'groupby': ['dim1', 'dim2d'],
'force': 'true',
}
# two groupby
url = (
'/superset/explore_json/druid/{}/?viz_type=table&granularity=one+day&'
'druid_time_origin=&since=7+days+ago&until=now&row_limit=5000&'
'include_search=false&metrics=count&groupby=dim1&'
'flt_col_0=dim1&groupby=dim2d&'
'flt_op_0=in&flt_eq_0=&slice_id=&slice_name=&collapsed_fieldsets=&'
'action=&datasource_name=test_datasource&datasource_id={}&'
'datasource_type=druid&previous_viz_type=table&'
'force=true'.format(datasource_id, datasource_id))
'/superset/explore_json/druid/{}/?form_data={}'.format(
datasource_id, json.dumps(form_data))
)
resp = self.get_json_resp(url)
self.assertEqual("Canada", resp['data']['records'][0]['dim1'])

Expand Down

0 comments on commit 9017ecf

Please sign in to comment.