-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add separate limit setting for SqlLab (#4941)
* Add separate limit setting for SqlLab Use separate param for wrap sql Get query limit from config unit tests for limit control rendering in sql editor py unit test pg tests Add max rows limit Remove concept of infinity, always require defined limits consistency Assert on validation errors instead of tooltip fix unit tests attempt persist state pr comments and linting * load configs in via common param * default to 1k
- Loading branch information
1 parent
aed774e
commit 0584e36
Showing
17 changed files
with
303 additions
and
14 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
superset/assets/spec/javascripts/sqllab/LimitControl_spec.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import React from 'react'; | ||
import { shallow } from 'enzyme'; | ||
|
||
import { Label } from 'react-bootstrap'; | ||
import LimitControl from '../../../src/SqlLab/components/LimitControl'; | ||
import ControlHeader from '../../../src/explore/components/ControlHeader'; | ||
|
||
describe('LimitControl', () => { | ||
const defaultProps = { | ||
value: 0, | ||
defaultQueryLimit: 1000, | ||
maxRow: 100000, | ||
onChange: () => {}, | ||
}; | ||
let wrapper; | ||
const factory = o => <LimitControl {...o} />; | ||
beforeEach(() => { | ||
wrapper = shallow(factory(defaultProps)); | ||
}); | ||
it('is a valid element', () => { | ||
expect(React.isValidElement(<LimitControl {...defaultProps} />)).toEqual(true); | ||
}); | ||
it('renders a Label', () => { | ||
expect(wrapper.find(Label)).toHaveLength(1); | ||
}); | ||
it('loads the correct state', () => { | ||
const value = 100; | ||
wrapper = shallow(factory({ ...defaultProps, value })); | ||
expect(wrapper.state().textValue).toEqual(value.toString()); | ||
wrapper.find(Label).first().simulate('click'); | ||
expect(wrapper.state().showOverlay).toBe(true); | ||
expect(wrapper.find(ControlHeader).props().validationErrors).toHaveLength(0); | ||
}); | ||
it('handles invalid value', () => { | ||
wrapper.find(Label).first().simulate('click'); | ||
wrapper.setState({ textValue: 'invalid' }); | ||
expect(wrapper.find(ControlHeader).props().validationErrors).toHaveLength(1); | ||
}); | ||
it('handles negative value', () => { | ||
wrapper.find(Label).first().simulate('click'); | ||
wrapper.setState({ textValue: '-1' }); | ||
expect(wrapper.find(ControlHeader).props().validationErrors).toHaveLength(1); | ||
}); | ||
it('handles value above max row', () => { | ||
wrapper.find(Label).first().simulate('click'); | ||
wrapper.setState({ textValue: (defaultProps.maxRow + 1).toString() }); | ||
expect(wrapper.find(ControlHeader).props().validationErrors).toHaveLength(1); | ||
}); | ||
it('opens and closes', () => { | ||
wrapper.find(Label).first().simulate('click'); | ||
expect(wrapper.state().showOverlay).toBe(true); | ||
wrapper.find('.ok').first().simulate('click'); | ||
expect(wrapper.state().showOverlay).toBe(false); | ||
}); | ||
it('resets and closes', () => { | ||
const value = 100; | ||
wrapper = shallow(factory({ ...defaultProps, value })); | ||
wrapper.find(Label).first().simulate('click'); | ||
expect(wrapper.state().textValue).toEqual(value.toString()); | ||
wrapper.find('.reset').simulate('click'); | ||
expect(wrapper.state().textValue).toEqual(defaultProps.defaultQueryLimit.toString()); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { | ||
Button, | ||
Label, | ||
FormGroup, | ||
FormControl, | ||
Overlay, | ||
Popover, | ||
} from 'react-bootstrap'; | ||
import { t } from '@superset-ui/translation'; | ||
|
||
import ControlHeader from '../../explore/components/ControlHeader'; | ||
|
||
const propTypes = { | ||
value: PropTypes.number, | ||
defaultQueryLimit: PropTypes.number.isRequired, | ||
maxRow: PropTypes.number.isRequired, | ||
onChange: PropTypes.func.isRequired, | ||
}; | ||
|
||
export default class LimitControl extends React.PureComponent { | ||
constructor(props) { | ||
super(props); | ||
const { value, defaultQueryLimit } = props; | ||
this.state = { | ||
textValue: value.toString() || defaultQueryLimit.toString(), | ||
showOverlay: false, | ||
}; | ||
this.handleHide = this.handleHide.bind(this); | ||
this.handleToggle = this.handleToggle.bind(this); | ||
this.submitAndClose = this.submitAndClose.bind(this); | ||
} | ||
|
||
setValueAndClose(val) { | ||
this.setState({ textValue: val }, this.submitAndClose); | ||
} | ||
|
||
submitAndClose() { | ||
const value = parseInt(this.state.textValue, 10) || this.props.defaultQueryLimit; | ||
this.props.onChange(value); | ||
this.setState({ showOverlay: false }); | ||
} | ||
|
||
isValidLimit(limit) { | ||
const value = parseInt(limit, 10); | ||
return !(Number.isNaN(value) || value <= 0 || (this.props.maxRow && value > this.props.maxRow)); | ||
} | ||
|
||
handleToggle() { | ||
this.setState({ showOverlay: !this.state.showOverlay }); | ||
} | ||
|
||
handleHide() { | ||
this.setState({ showOverlay: false }); | ||
} | ||
|
||
renderPopover() { | ||
const textValue = this.state.textValue; | ||
const isValid = this.isValidLimit(textValue); | ||
const errorMsg = 'Row limit must be positive integer' + | ||
(this.props.maxRow ? ` and not greater than ${this.props.maxRow}` : ''); | ||
return ( | ||
<Popover id="sqllab-limit-results"> | ||
<div style={{ width: '100px' }}> | ||
<ControlHeader | ||
label={t('Row limit')} | ||
validationErrors={!isValid ? [t(errorMsg)] : []} | ||
/> | ||
<FormGroup> | ||
<FormControl | ||
type="text" | ||
value={textValue} | ||
placeholder={t(`Max: ${this.props.maxRow}`)} | ||
bsSize="small" | ||
onChange={e => this.setState({ textValue: e.target.value })} | ||
/> | ||
</FormGroup> | ||
<div className="clearfix"> | ||
<Button | ||
bsSize="small" | ||
bsStyle="primary" | ||
className="float-left ok" | ||
disabled={!isValid} | ||
onClick={this.submitAndClose} | ||
> | ||
Ok | ||
</Button> | ||
<Button | ||
bsSize="small" | ||
className="float-right reset" | ||
onClick={this.setValueAndClose.bind(this, this.props.defaultQueryLimit.toString())} | ||
> | ||
Reset | ||
</Button> | ||
</div> | ||
</div> | ||
</Popover> | ||
); | ||
} | ||
|
||
render() { | ||
return ( | ||
<div> | ||
<Label | ||
style={{ cursor: 'pointer' }} | ||
onClick={this.handleToggle} | ||
> | ||
LIMIT {this.props.value || this.props.maxRow} | ||
</Label> | ||
<Overlay | ||
rootClose | ||
show={this.state.showOverlay} | ||
onHide={this.handleHide} | ||
trigger="click" | ||
placement="right" | ||
target={this} | ||
> | ||
{this.renderPopover()} | ||
</Overlay> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
LimitControl.propTypes = propTypes; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.