Skip to content
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: Additional ResultSet tests #14741

Merged
merged 8 commits into from
May 24, 2021
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 51 additions & 38 deletions superset-frontend/spec/javascripts/sqllab/ResultSet_spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import React from 'react';
import { shallow } from 'enzyme';
import { styledMount } from 'spec/helpers/theming';
import { render, screen } from 'spec/helpers/testing-library';
import { Provider } from 'react-redux';
import sinon from 'sinon';
import Alert from 'src/components/Alert';
Expand All @@ -38,49 +39,48 @@ import {
stoppedQuery,
initialState,
user,
queryWithNoQueryLimit,
} from './fixtures';

const mockStore = configureStore([thunk]);
const store = mockStore(initialState);

describe('ResultSet', () => {
const clearQuerySpy = sinon.spy();
const fetchQuerySpy = sinon.spy();
const reRunQuerySpy = sinon.spy();
const mockedProps = {
actions: {
clearQueryResults: clearQuerySpy,
fetchQueryResults: fetchQuerySpy,
reRunQuery: reRunQuerySpy,
},
cache: true,
query: queries[0],
height: 140,
database: { allows_virtual_table_explore: true },
user,
defaultQueryLimit: 1000,
};
const stoppedQueryProps = { ...mockedProps, query: stoppedQuery };
const runningQueryProps = { ...mockedProps, query: runningQuery };
const cachedQueryProps = { ...mockedProps, query: cachedQuery };
const failedQueryWithErrorMessageProps = {
...mockedProps,
query: failedQueryWithErrorMessage,
};
const failedQueryWithErrorsProps = {
...mockedProps,
query: failedQueryWithErrors,
};
const newProps = {
query: {
cached: false,
resultsKey: 'new key',
results: {
data: [{ a: 1 }],
},
const clearQuerySpy = sinon.spy();
const fetchQuerySpy = sinon.spy();
const reRunQuerySpy = sinon.spy();
const mockedProps = {
actions: {
clearQueryResults: clearQuerySpy,
fetchQueryResults: fetchQuerySpy,
reRunQuery: reRunQuerySpy,
},
cache: true,
query: queries[0],
height: 140,
database: { allows_virtual_table_explore: true },
user,
defaultQueryLimit: 1000,
};
const stoppedQueryProps = { ...mockedProps, query: stoppedQuery };
const runningQueryProps = { ...mockedProps, query: runningQuery };
const cachedQueryProps = { ...mockedProps, query: cachedQuery };
const failedQueryWithErrorMessageProps = {
...mockedProps,
query: failedQueryWithErrorMessage,
};
const failedQueryWithErrorsProps = {
...mockedProps,
query: failedQueryWithErrors,
};
const newProps = {
query: {
cached: false,
resultsKey: 'new key',
results: {
data: [{ a: 1 }],
},
};

},
};
describe('ResultSet', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's go straight to the test without nesting in describe. That's the approach we have been trying to use lately

it('is valid', () => {
expect(React.isValidElement(<ResultSet {...mockedProps} />)).toBe(true);
});
Expand Down Expand Up @@ -182,3 +182,16 @@ describe('ResultSet', () => {
});
});
});

describe('RTL ResultSet tests', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as my other comment concerning the usage of describe

it('renders if there is no limit in query.results but has queryLimit', () => {
render(<ResultSet {...mockedProps} />, { useRedux: true });
expect(screen.getByRole('grid')).toBeInTheDocument();
});

it('renders if there is a limit in query.results but not queryLimit', () => {
const props = { ...mockedProps, query: queryWithNoQueryLimit };
render(<ResultSet {...props} />, { useRedux: true });
expect(screen.getByRole('grid')).toBeInTheDocument();
});
});
61 changes: 61 additions & 0 deletions superset-frontend/spec/javascripts/sqllab/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,67 @@ export const queries = [
results: null,
},
];
export const queryWithNoQueryLimit = {
dbId: 1,
sql: 'SELECT * FROM superset.slices',
sqlEditorId: 'SJ8YO72R',
tab: 'Demo',
runAsync: false,
ctas: false,
cached: false,
id: 'BkA1CLrJg',
progress: 100,
startDttm: 1476910566092.96,
state: 'success',
changedOn: 1476910566000,
tempTable: null,
userId: 1,
executedSql: null,
changed_on: '2016-10-19T20:56:06',
rows: 42,
endDttm: 1476910566798,
limit_reached: false,
schema: 'test_schema',
errorMessage: null,
db: 'main',
user: 'admin',
limit: 1000,
serverId: 141,
resultsKey: null,
results: {
columns: [
{
is_date: true,
name: 'ds',
type: 'STRING',
},
{
is_date: false,
name: 'gender',
type: 'STRING',
},
],
selected_columns: [
{
is_date: true,
name: 'ds',
type: 'STRING',
},
{
is_date: false,
name: 'gender',
type: 'STRING',
},
],
data: [
{ col1: 0, col2: 1 },
{ col1: 2, col2: 3 },
],
query: {
limit: 100,
},
},
};
export const queryWithBadColumns = {
...queries[0],
results: {
Expand Down
14 changes: 6 additions & 8 deletions superset-frontend/src/SqlLab/components/ResultSet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -526,34 +526,32 @@ export default class ResultSet extends React.PureComponent<
const { results, rows, queryLimit, limitingFactor } = this.props.query;
let limitMessage;
const limitReached = results?.displayLimitReached;
const isAdmin = !!this.props.user?.roles.Admin;
const limit = queryLimit || results.query.limit;
const isAdmin = !!this.props.user?.roles.Admin;
Copy link

@sujeetpillai sujeetpillai Jun 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm getting an error in SQLLab here if the this.props.user.roles is undefined. Changing this code to the below fixes the issue. If roles is undefined assume it's not admin. I'm not sure why roles wouldn't be populated though. I was logged in as an Administrator.

const isAdmin = !!this.props.user?.roles?.Admin;

Since this is only used to change the display message, I don't think the above fix will break any functionality.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that makes sense to me, @eschutho what do you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That looks good. @suddjian did make a fix whereby localstorage was overwriting user roles, but I think this works as a guard against any other issues that may be creeping up. At some point maybe we can take another look into why or when a user wouldn't have any roles.

const displayMaxRowsReachedMessage = {
withAdmin: t(
`The number of results displayed is limited to %(rows)d by the configuration DISPLAY_MAX_ROWS. `,
{ rows },
).concat(
t(
`Please add additional limits/filters or download to csv to see more rows up to the`,
`Please add additional limits/filters or download to csv to see more rows up to `,
),
t(`the %(limit)d limit.`, {
limit,
}),
t(`the %(limit)d limit.`, { limit }),
),
withoutAdmin: t(
`The number of results displayed is limited to %(rows)d. `,
{ rows },
).concat(
t(
`Please add additional limits/filters, download to csv, or contact an admin`,
`Please add additional limits/filters, download to csv, or contact an admin `,
),
t(`to see more rows up to the the %(limit)d limit.`, {
t(`to see more rows up to the %(limit)d limit.`, {
limit,
}),
),
};
const shouldUseDefaultDropdownAlert =
queryLimit === this.props.defaultQueryLimit &&
limit === this.props.defaultQueryLimit &&
limitingFactor === LIMITING_FACTOR.DROPDOWN;

if (limitingFactor === LIMITING_FACTOR.QUERY && this.props.csv) {
Expand Down