Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
betodealmeida committed May 8, 2024
1 parent 073767f commit 65689a6
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 12 deletions.
13 changes: 12 additions & 1 deletion superset-frontend/src/SqlLab/actions/sqlLab.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,7 @@ describe('async actions', () => {
queryEditor: {
name: 'Copy of Dummy query editor',
dbId: 1,
catalog: query.catalog,
schema: query.schema,
autorun: true,
sql: 'SELECT * FROM something',
Expand Down Expand Up @@ -481,6 +482,7 @@ describe('async actions', () => {
sql: expect.stringContaining('SELECT ...'),
name: `Untitled Query 7`,
dbId: defaultQueryEditor.dbId,
catalog: defaultQueryEditor.catalog,
schema: defaultQueryEditor.schema,
autorun: false,
queryLimit:
Expand Down Expand Up @@ -747,6 +749,7 @@ describe('async actions', () => {
describe('addTable', () => {
it('dispatches table state from unsaved change', () => {
const tableName = 'table';
const catalogName = null;
const schemaName = 'schema';
const expectedDbId = 473892;
const store = mockStore({
Expand All @@ -759,12 +762,18 @@ describe('async actions', () => {
},
},
});
const request = actions.addTable(query, tableName, schemaName);
const request = actions.addTable(
query,
tableName,
catalogName,
schemaName,
);
request(store.dispatch, store.getState);
expect(store.getActions()[0]).toEqual(
expect.objectContaining({
table: expect.objectContaining({
name: tableName,
catalog: catalogName,
schema: schemaName,
dbId: expectedDbId,
}),
Expand Down Expand Up @@ -811,6 +820,7 @@ describe('async actions', () => {
});

const tableName = 'table';
const catalogName = null;
const schemaName = 'schema';
const store = mockStore({
...initialState,
Expand All @@ -829,6 +839,7 @@ describe('async actions', () => {
const request = actions.runTablePreviewQuery({
dbId: 1,
name: tableName,
catalog: catalogName,
schema: schemaName,
});
return request(store.dispatch, store.getState).then(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ test('returns column keywords among selected tables', async () => {
useKeywords({
queryEditorId: expectQueryEditorId,
dbId: expectDbId,
catalog: null,
schema: expectSchema,
}),
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ describe('SaveDatasetModal', () => {
expect(createDatasource).toHaveBeenCalledWith({
datasourceName: 'my dataset',
dbId: 1,
catalog: null,
schema: 'main',
sql: 'SELECT *',
templateParams: undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ const mockedProps = {

beforeEach(() => {
fetchMock.get('glob:*/api/v1/database/?*', { result: [] });
fetchMock.get('glob:*/api/v1/database/*/catalogs/?*', {
count: 0,
result: [],
});
fetchMock.get('glob:*/api/v1/database/*/schemas/?*', {
count: 2,
result: ['main', 'new_schema'],
Expand Down Expand Up @@ -103,26 +107,29 @@ test('renders a TableElement', async () => {
});

test('table should be visible when expanded is true', async () => {
const { container, getByText, getByRole, queryAllByText } =
await renderAndWait(mockedProps, undefined, {
const { container, getByText, getByRole } = await renderAndWait(
mockedProps,
undefined,
{
...initialState,
sqlLab: { ...initialState.sqlLab, tables: [table] },
});
},
);

const dbSelect = getByRole('combobox', {
name: 'Select database or type to search databases',
});
const schemaSelect = getByRole('combobox', {
name: 'Select schema or type to search schemas',
});
const dropdown = getByText(/Table/i);
const abUser = queryAllByText(/ab_user/i);
const dropdown = getByText(/Select table/i);
const abUser = getByText(/ab_user/i);

expect(getByText(/Database/i)).toBeInTheDocument();
expect(dbSelect).toBeInTheDocument();
expect(schemaSelect).toBeInTheDocument();
expect(dropdown).toBeInTheDocument();
expect(abUser).toHaveLength(2);
expect(abUser).toBeInTheDocument();
expect(
container.querySelector('.ant-collapse-content-active'),
).toBeInTheDocument();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const createProps = (): DatabaseSelectorProps => ({
formMode: false,
isDatabaseSelectEnabled: true,
readOnly: false,
catalog: null,
schema: 'public',
sqlLabMode: true,
getDbList: jest.fn(),
Expand Down Expand Up @@ -158,16 +159,23 @@ const fakeSchemaApiResult = {
result: ['information_schema', 'public'],
};

const fakeCatalogApiResult = {
count: 0,
result: [],
};

const fakeFunctionNamesApiResult = {
function_names: [],
};

const databaseApiRoute = 'glob:*/api/v1/database/?*';
const catalogApiRoute = 'glob:*/api/v1/database/*/catalogs/?*';
const schemaApiRoute = 'glob:*/api/v1/database/*/schemas/?*';
const tablesApiRoute = 'glob:*/api/v1/database/*/tables/*';

function setupFetchMock() {
fetchMock.get(databaseApiRoute, fakeDatabaseApiResult);
fetchMock.get(catalogApiRoute, fakeCatalogApiResult);
fetchMock.get(schemaApiRoute, fakeSchemaApiResult);
fetchMock.get(tablesApiRoute, fakeFunctionNamesApiResult);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const getTableMockFunction = () =>
}) as any;

const databaseApiRoute = 'glob:*/api/v1/database/?*';
const catalogApiRoute = 'glob:*/api/v1/database/*/catalogs/?*';
const schemaApiRoute = 'glob:*/api/v1/database/*/schemas/?*';
const tablesApiRoute = 'glob:*/api/v1/database/*/tables/*';

Expand All @@ -74,6 +75,7 @@ afterEach(() => {
});

test('renders with default props', async () => {
fetchMock.get(catalogApiRoute, { result: [] });
fetchMock.get(schemaApiRoute, { result: [] });
fetchMock.get(tablesApiRoute, getTableMockFunction());

Expand All @@ -96,6 +98,7 @@ test('renders with default props', async () => {
});

test('skips select all options', async () => {
fetchMock.get(catalogApiRoute, { result: [] });
fetchMock.get(schemaApiRoute, { result: ['test_schema'] });
fetchMock.get(tablesApiRoute, getTableMockFunction());

Expand All @@ -115,6 +118,7 @@ test('skips select all options', async () => {
});

test('renders table options without Select All option', async () => {
fetchMock.get(catalogApiRoute, { result: [] });
fetchMock.get(schemaApiRoute, { result: ['test_schema'] });
fetchMock.get(tablesApiRoute, getTableMockFunction());

Expand All @@ -133,6 +137,7 @@ test('renders table options without Select All option', async () => {
});

test('renders disabled without schema', async () => {
fetchMock.get(catalogApiRoute, { result: [] });
fetchMock.get(schemaApiRoute, { result: [] });
fetchMock.get(tablesApiRoute, getTableMockFunction());

Expand All @@ -150,6 +155,7 @@ test('renders disabled without schema', async () => {
});

test('table select retain value if not in SQL Lab mode', async () => {
fetchMock.get(catalogApiRoute, { result: [] });
fetchMock.get(schemaApiRoute, { result: ['test_schema'] });
fetchMock.get(tablesApiRoute, getTableMockFunction());

Expand Down Expand Up @@ -191,6 +197,7 @@ test('table select retain value if not in SQL Lab mode', async () => {
});

test('table multi select retain all the values selected', async () => {
fetchMock.get(catalogApiRoute, { result: [] });
fetchMock.get(schemaApiRoute, { result: ['test_schema'] });
fetchMock.get(tablesApiRoute, getTableMockFunction());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ fetchMock.get(
},
);

fetchMock.get('glob:*api/v1/database/*/schemas/', {
result: [],
});

fetchMock.get('glob:*api/v1/database/1/schemas/', {
result: ['information_schema', 'public'],
});
Expand Down
10 changes: 5 additions & 5 deletions superset-frontend/src/hooks/apiResources/schemas.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ describe('useSchemas hook', () => {
})}`,
).length,
).toBe(1);
expect(onSuccess).toHaveBeenCalledTimes(1);
expect(onSuccess).toHaveBeenCalledTimes(2);
act(() => {
result.current.refetch();
});
Expand All @@ -92,7 +92,7 @@ describe('useSchemas hook', () => {
})}`,
).length,
).toBe(1);
expect(onSuccess).toHaveBeenCalledTimes(2);
expect(onSuccess).toHaveBeenCalledTimes(3);
expect(result.current.data).toEqual(expectedResult);
});

Expand Down Expand Up @@ -143,17 +143,17 @@ describe('useSchemas hook', () => {

await waitFor(() => expect(result.current.data).toEqual(expectedResult));
expect(fetchMock.calls(schemaApiRoute).length).toBe(1);
expect(onSuccess).toHaveBeenCalledTimes(1);
expect(onSuccess).toHaveBeenCalledTimes(2);

rerender({ dbId: 'db2' });
await waitFor(() => expect(result.current.data).toEqual(expectedResult2));
expect(fetchMock.calls(schemaApiRoute).length).toBe(2);
expect(onSuccess).toHaveBeenCalledTimes(2);
expect(onSuccess).toHaveBeenCalledTimes(4);

rerender({ dbId: expectDbId });
await waitFor(() => expect(result.current.data).toEqual(expectedResult));
expect(fetchMock.calls(schemaApiRoute).length).toBe(2);
expect(onSuccess).toHaveBeenCalledTimes(3);
expect(onSuccess).toHaveBeenCalledTimes(5);

// clean up cache
act(() => {
Expand Down
16 changes: 16 additions & 0 deletions superset-frontend/src/hooks/apiResources/tables.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,11 @@ describe('useTables hook', () => {
test('returns api response mapping json options', async () => {
const expectDbId = 'db1';
const expectedSchema = 'schema1';
const catalogApiRoute = `glob:*/api/v1/database/${expectDbId}/catalogs/*`;
const schemaApiRoute = `glob:*/api/v1/database/${expectDbId}/schemas/*`;
const tableApiRoute = `glob:*/api/v1/database/${expectDbId}/tables/?q=*`;
fetchMock.get(tableApiRoute, fakeApiResult);
fetchMock.get(catalogApiRoute, { count: 0, result: [] });
fetchMock.get(schemaApiRoute, {
result: fakeSchemaApiResult,
});
Expand Down Expand Up @@ -130,9 +132,11 @@ describe('useTables hook', () => {
test('skips the deprecated schema option', async () => {
const expectDbId = 'db1';
const unexpectedSchema = 'invalid schema';
const catalogApiRoute = `glob:*/api/v1/database/${expectDbId}/catalogs/*`;
const schemaApiRoute = `glob:*/api/v1/database/${expectDbId}/schemas/*`;
const tableApiRoute = `glob:*/api/v1/database/${expectDbId}/tables/?q=*`;
fetchMock.get(tableApiRoute, fakeApiResult);
fetchMock.get(catalogApiRoute, { count: 0, result: [] });
fetchMock.get(schemaApiRoute, {
result: fakeSchemaApiResult,
});
Expand Down Expand Up @@ -166,6 +170,10 @@ describe('useTables hook', () => {
const expectedSchema = 'schema2';
const tableApiRoute = `glob:*/api/v1/database/${expectDbId}/tables/?q=*`;
fetchMock.get(tableApiRoute, fakeHasMoreApiResult);
fetchMock.get(`glob:*/api/v1/database/${expectDbId}/catalogs/*`, {
count: 0,
result: [],
});
fetchMock.get(`glob:*/api/v1/database/${expectDbId}/schemas/*`, {
result: fakeSchemaApiResult,
});
Expand All @@ -191,6 +199,10 @@ describe('useTables hook', () => {
const expectedSchema = 'schema1';
const tableApiRoute = `glob:*/api/v1/database/${expectDbId}/tables/?q=*`;
fetchMock.get(tableApiRoute, fakeApiResult);
fetchMock.get(`glob:*/api/v1/database/${expectDbId}/catalogs/*`, {
count: 0,
result: [],
});
fetchMock.get(`glob:*/api/v1/database/${expectDbId}/schemas/*`, {
result: fakeSchemaApiResult,
});
Expand Down Expand Up @@ -220,6 +232,10 @@ describe('useTables hook', () => {
fetchMock.get(tableApiRoute, url =>
url.includes(expectedSchema) ? fakeApiResult : fakeHasMoreApiResult,
);
fetchMock.get(`glob:*/api/v1/database/${expectDbId}/catalogs/*`, {
count: 0,
result: [],
});
fetchMock.get(`glob:*/api/v1/database/${expectDbId}/schemas/*`, {
result: fakeSchemaApiResult,
});
Expand Down

0 comments on commit 65689a6

Please sign in to comment.