Skip to content

Commit

Permalink
Fix: Change datasource table from explore view (#273)
Browse files Browse the repository at this point in the history
* Change datasource from explore view

* Revert package-lock.json (master checkout)

* Revert tests/datasource_tests.py
  • Loading branch information
shmsr authored Mar 9, 2020
1 parent b06b2b7 commit 68ba561
Show file tree
Hide file tree
Showing 14 changed files with 556 additions and 96 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import React from 'react';
import { Modal } from 'react-bootstrap';
import configureStore from 'redux-mock-store';
import { shallow } from 'enzyme';
import fetchMock from 'fetch-mock';
import thunk from 'redux-thunk';
import sinon from 'sinon';

import ChangeDatasourceModal from '../../../src/datasource/ChangeDatasourceModal';
import mockDatasource from '../../fixtures/mockDatasource';

const props = {
addDangerToast: () => {},
onDatasourceSave: sinon.spy(),
onChange: () => {},
onHide: () => {},
show: true,
};

const datasource = mockDatasource['7__table'];
const datasourceData = {
id: datasource.name,
type: datasource.type,
uid: datasource.id,
};

const DATASOURCES_ENDPOINT = 'glob:*/superset/datasources/';
const DATASOURCE_ENDPOINT = `glob:*/datasource/get/${datasourceData.type}/${datasourceData.id}`;
const DATASOURCES_PAYLOAD = { json: 'data' };
const DATASOURCE_PAYLOAD = { new: 'data' };

describe('ChangeDatasourceModal', () => {
const mockStore = configureStore([thunk]);
const store = mockStore({});
fetchMock.get(DATASOURCES_ENDPOINT, DATASOURCES_PAYLOAD);

let wrapper;
let el;
let inst;

beforeEach(() => {
el = <ChangeDatasourceModal {...props} />;
wrapper = shallow(el, { context: { store } }).dive();
inst = wrapper.instance();
});

it('is valid', () => {
expect(React.isValidElement(el)).toBe(true);
});

it('renders a Modal', () => {
expect(wrapper.find(Modal)).toHaveLength(1);
});

it('fetches datasources', (done) => {
inst.onEnterModal();
setTimeout(() => {
expect(fetchMock.calls(DATASOURCES_ENDPOINT)).toHaveLength(1);
fetchMock.reset();
done();
}, 0);
});

it('changes the datasource', (done) => {
fetchMock.get(DATASOURCE_ENDPOINT, DATASOURCE_PAYLOAD);
inst.selectDatasource(datasourceData);
setTimeout(() => {
expect(fetchMock.calls(DATASOURCE_ENDPOINT)).toHaveLength(1);
expect(props.onDatasourceSave.getCall(0).args[0]).toEqual(DATASOURCE_PAYLOAD);
fetchMock.reset();
done();
}, 0);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ describe('DatasourceEditor', () => {
});

it('renders isSqla fields', () => {
wrapper.setState({ activeTabKey: 4 });
expect(wrapper.state('isSqla')).toBe(true);
expect(wrapper.find(Field).find({ fieldKey: 'fetch_values_predicate' }).exists()).toBe(true);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import sinon from 'sinon';
import configureStore from 'redux-mock-store';
import { shallow } from 'enzyme';
import DatasourceModal from '../../../../src/datasource/DatasourceModal';
import ChangeDatasourceModal from '../../../../src/datasource/ChangeDatasourceModal';
import DatasourceControl from '../../../../src/explore/components/controls/DatasourceControl';

const defaultProps = {
Expand Down Expand Up @@ -53,4 +54,9 @@ describe('DatasourceControl', () => {
const wrapper = setup();
expect(wrapper.find(DatasourceModal)).toHaveLength(1);
});

it('renders a ChangeDatasourceModal', () => {
const wrapper = setup();
expect(wrapper.find(ChangeDatasourceModal)).toHaveLength(1);
});
});
173 changes: 173 additions & 0 deletions superset/assets/src/datasource/ChangeDatasourceModal.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import React from 'react';
import PropTypes from 'prop-types';
import { Table } from 'reactable-arc';
import {
FormControl,
Modal,
} from 'react-bootstrap';
import { SupersetClient } from '@superset-ui/connection';
import { t } from '@superset-ui/translation';

import getClientErrorObject from '../utils/getClientErrorObject';
import Loading from '../components/Loading';
import withToasts from '../messageToasts/enhancers/withToasts';

const propTypes = {
addDangerToast: PropTypes.func.isRequired,
onChange: PropTypes.func,
onDatasourceSave: PropTypes.func,
onHide: PropTypes.func,
show: PropTypes.bool.isRequired,
};

const defaultProps = {
onChange: () => {},
onDatasourceSave: () => {},
onHide: () => {},
};

const TABLE_COLUMNS = ['name', 'type', 'schema', 'connection', 'creator'];
const TABLE_FILTERABLE = ['rawName', 'type', 'schema', 'connection', 'creator'];

class ChangeDatasourceModal extends React.PureComponent {
constructor(props) {
super(props);
this.state = {
loading: true,
datasources: null,
};
this.setSearchRef = this.setSearchRef.bind(this);
this.onEnterModal = this.onEnterModal.bind(this);
this.selectDatasource = this.selectDatasource.bind(this);
this.changeSearch = this.changeSearch.bind(this);
}

onEnterModal() {
if (this.searchRef) {
this.searchRef.focus();
}
if (!this.state.datasources) {
SupersetClient.get({
endpoint: '/superset/datasources/',
})
.then(({ json }) => {
const datasources = json.map(ds => ({
rawName: ds.name,
connection: ds.connection,
schema: ds.schema,
name: (
<a
href="#"
onClick={this.selectDatasource.bind(this, ds)}
className="datasource-link"
>
{ds.name}
</a>
),
type: ds.type,
}));

this.setState({ loading: false, datasources });
})
.catch((response) => {
this.setState({ loading: false });
getClientErrorObject(response).then(({ error }) => {
this.props.addDangerToast(error.error || error.statusText || error);
});
});
}
}

setSearchRef(searchRef) {
this.searchRef = searchRef;
}

changeSearch(event) {
this.setState({ filter: event.target.value });
}

selectDatasource(datasource) {
SupersetClient.get({
endpoint: `/datasource/get/${datasource.type}/${datasource.id}`,
})
.then(({ json }) => {
this.props.onDatasourceSave(json);
this.props.onChange(datasource.uid);
})
.catch((response) => {
getClientErrorObject(response).then(({ error, message }) => {
const errorMessage = error ? error.error || error.statusText || error : message;
this.props.addDangerToast(errorMessage);
});
});
this.props.onHide();
}

render() {
const { datasources, filter, loading } = this.state;
const { show, onHide } = this.props;

return (
<Modal
show={show}
onHide={onHide}
onEnter={this.onEnterModal}
onExit={this.setSearchRef}
bsSize="lg"
>
<Modal.Header closeButton>
<Modal.Title>{t('Select a datasource')}</Modal.Title>
</Modal.Header>
<Modal.Body>
<div>
<FormControl
inputRef={(ref) => {
this.setSearchRef(ref);
}}
type="text"
bsSize="sm"
value={filter}
placeholder={t('Search / Filter')}
onChange={this.changeSearch}
/>
</div>
{loading && <Loading />}
{datasources && (
<Table
columns={TABLE_COLUMNS}
className="table table-condensed"
data={datasources}
itemsPerPage={20}
filterable={TABLE_FILTERABLE}
filterBy={filter}
hideFilterInput
/>
)}
</Modal.Body>
</Modal>
);
}
}

ChangeDatasourceModal.propTypes = propTypes;
ChangeDatasourceModal.defaultProps = defaultProps;

export default withToasts(ChangeDatasourceModal);
Loading

0 comments on commit 68ba561

Please sign in to comment.