-
Notifications
You must be signed in to change notification settings - Fork 13.7k
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
fix: save dataset and repopulate state #20965
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* 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. | ||
*/ | ||
|
||
// /superset/sqllab_viz | ||
interface SqlLabPostRequest { | ||
data: { | ||
schema: string; | ||
sql: string; | ||
dbId: number; | ||
templateParams?: string | undefined; | ||
datasourceName: string; | ||
metrics?: string[]; | ||
columns?: string[]; | ||
}; | ||
} | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,10 +17,14 @@ | |
* under the License. | ||
*/ | ||
import { DatasourceType } from '@superset-ui/core'; | ||
import fetchMock from 'fetch-mock'; | ||
import { | ||
setDatasource, | ||
changeDatasource, | ||
saveDataset, | ||
} from 'src/explore/actions/datasourcesActions'; | ||
import sinon from 'sinon'; | ||
import * as ClientError from 'src/utils/getClientErrorObject'; | ||
import datasourcesReducer from '../reducers/datasourcesReducer'; | ||
import { updateFormDataByDatasource } from './exploreActions'; | ||
|
||
|
@@ -51,10 +55,21 @@ const NEW_DATASOURCE = { | |
description: null, | ||
}; | ||
|
||
const SAVE_DATASET_POST_ARGS = { | ||
schema: 'foo', | ||
sql: 'select * from bar', | ||
database: { id: 1 }, | ||
templateParams: undefined, | ||
datasourceName: 'new dataset', | ||
columns: [], | ||
}; | ||
|
||
const defaultDatasourcesReducerState = { | ||
[CURRENT_DATASOURCE.uid]: CURRENT_DATASOURCE, | ||
}; | ||
|
||
const saveDatasetEndpoint = `glob:*/superset/sqllab_viz/`; | ||
|
||
test('sets new datasource', () => { | ||
const newState = datasourcesReducer( | ||
defaultDatasourcesReducerState, | ||
|
@@ -83,3 +98,42 @@ test('change datasource action', () => { | |
updateFormDataByDatasource(CURRENT_DATASOURCE, NEW_DATASOURCE), | ||
); | ||
}); | ||
|
||
test('saveDataset handles success', async () => { | ||
const datasource = { id: 1 }; | ||
const saveDatasetResponse = { | ||
data: datasource, | ||
}; | ||
fetchMock.reset(); | ||
fetchMock.post(saveDatasetEndpoint, saveDatasetResponse); | ||
const dispatch = sinon.spy(); | ||
const getState = sinon.spy(() => ({ explore: { datasource } })); | ||
const dataset = await saveDataset(SAVE_DATASET_POST_ARGS)(dispatch); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice way to test this! |
||
|
||
expect(fetchMock.calls(saveDatasetEndpoint)).toHaveLength(1); | ||
expect(dispatch.callCount).toBe(1); | ||
const thunk = dispatch.getCall(0).args[0]; | ||
thunk(dispatch, getState); | ||
expect(dispatch.getCall(1).args[0].type).toEqual('SET_DATASOURCE'); | ||
|
||
expect(dataset).toEqual(datasource); | ||
}); | ||
|
||
test('updateSlice with add to existing dashboard handles failure', async () => { | ||
fetchMock.reset(); | ||
const sampleError = new Error('sampleError'); | ||
fetchMock.post(saveDatasetEndpoint, { throws: sampleError }); | ||
const dispatch = sinon.spy(); | ||
const errorSpy = jest.spyOn(ClientError, 'getClientErrorObject'); | ||
|
||
let caughtError; | ||
try { | ||
await saveDataset(SAVE_DATASET_POST_ARGS)(dispatch); | ||
} catch (error) { | ||
caughtError = error; | ||
} | ||
|
||
expect(caughtError).toEqual(sampleError); | ||
expect(fetchMock.calls(saveDatasetEndpoint)).toHaveLength(4); | ||
expect(errorSpy).toHaveBeenCalledWith(sampleError); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,7 +49,8 @@ export const HYDRATE_EXPLORE = 'HYDRATE_EXPLORE'; | |
export const hydrateExplore = | ||
({ form_data, slice, dataset }: ExplorePageInitialData) => | ||
(dispatch: Dispatch, getState: () => ExplorePageState) => { | ||
const { user, datasources, charts, sliceEntities, common } = getState(); | ||
const { user, datasources, charts, sliceEntities, common, explore } = | ||
getState(); | ||
|
||
const sliceId = getUrlParam(URL_PARAMS.sliceId); | ||
const dashboardId = getUrlParam(URL_PARAMS.dashboardId); | ||
|
@@ -119,7 +120,7 @@ export const hydrateExplore = | |
controls: initialControls, | ||
form_data: initialFormData, | ||
slice: initialSlice, | ||
controlsTransferred: [], | ||
controlsTransferred: explore.controlsTransferred, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @michael-s-molina do you see any problems with us pulling in state here on hydrate? It's not a property that exists in the persisted db. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. The Ping me or @kgabryje if you need more assistance on this. |
||
standalone: getUrlParam(URL_PARAMS.standalone), | ||
force: getUrlParam(URL_PARAMS.force), | ||
sliceDashboards: initialFormData.dashboards, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a first attempt at creating a set of types of that map to the api. cc @hughhhh cc @eric-briscoe. We talked about auto-generating this with Marshmallow, but this seemed like a decent start. Thoughts?