mirror of
https://github.com/hasura/graphql-engine.git
synced 2024-12-14 17:02:49 +03:00
tests (console): add tests for getTableName
util function
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/6375 GitOrigin-RevId: 82acd22d60a878c6619158637290c923cd8b43f0
This commit is contained in:
parent
c274cb8a44
commit
a9c887cd16
@ -1,6 +1,5 @@
|
||||
import { useIsUnmounted } from '@/components/Services/Data';
|
||||
import { getTableName } from '@/features/Data';
|
||||
import { DataSource } from '@/features/DataSource';
|
||||
import { DataSource, getTableName } from '@/features/DataSource';
|
||||
import { useHttpClient } from '@/features/Network';
|
||||
import { useState, useEffect } from 'react';
|
||||
|
||||
|
@ -1,11 +1,11 @@
|
||||
import { BrowseRowsContainer } from '@/features/BrowseRows';
|
||||
import { getTableName } from '@/features/DataSource';
|
||||
import { Table } from '@/features/MetadataAPI';
|
||||
import { IndicatorCard } from '@/new-components/IndicatorCard';
|
||||
import { Tabs } from '@/new-components/Tabs';
|
||||
import React, { useState } from 'react';
|
||||
import { useDatabaseHierarchy } from '../hooks';
|
||||
import { ModifyTable } from '../ModifyTable/ModifyTable';
|
||||
import { getTableName } from '../TrackTables/hooks/useTables';
|
||||
import { Breadcrumbs, TableName } from './parts';
|
||||
|
||||
export interface ManageTableProps {
|
||||
|
@ -1,6 +1,11 @@
|
||||
import type { IntrospectedTable } from '@/features/DataSource';
|
||||
import { DataSource, exportMetadata, Feature } from '@/features/DataSource';
|
||||
import { MetadataTable, Table } from '@/features/MetadataAPI';
|
||||
import {
|
||||
getTableName,
|
||||
IntrospectedTable,
|
||||
DataSource,
|
||||
exportMetadata,
|
||||
Feature,
|
||||
} from '@/features/DataSource';
|
||||
import { MetadataTable } from '@/features/MetadataAPI';
|
||||
import { useHttpClient } from '@/features/Network';
|
||||
import { useQuery } from 'react-query';
|
||||
import type { TrackableTable } from '../types';
|
||||
@ -9,42 +14,6 @@ export type UseTablesProps = {
|
||||
dataSourceName: string;
|
||||
};
|
||||
|
||||
export const getTableName = (
|
||||
table: Table,
|
||||
databaseHierarchy: string[]
|
||||
): string => {
|
||||
if (databaseHierarchy.length === 0) {
|
||||
if (!Array.isArray(table)) return '';
|
||||
|
||||
const result = table.reduce<string[]>((acc, item) => {
|
||||
if (typeof item === 'string') acc.push(item);
|
||||
return acc;
|
||||
}, []);
|
||||
|
||||
return result.join('.');
|
||||
}
|
||||
|
||||
if (table && typeof table === 'object') {
|
||||
const flatJsonTableDefinition = Object.entries(table).reduce<
|
||||
Record<string, string>
|
||||
>((acc, item) => {
|
||||
const [key, value] = item;
|
||||
if (typeof key === 'string' && typeof value === 'string')
|
||||
acc[key] = value;
|
||||
return acc;
|
||||
}, {});
|
||||
|
||||
const tableName = databaseHierarchy
|
||||
.map(key => {
|
||||
return flatJsonTableDefinition[key];
|
||||
})
|
||||
.join('.');
|
||||
return tableName;
|
||||
}
|
||||
|
||||
return JSON.stringify(table);
|
||||
};
|
||||
|
||||
const getTrackableTables = (
|
||||
trackedTables: MetadataTable[],
|
||||
introspectedTables: IntrospectedTable[],
|
||||
|
@ -1,6 +1,6 @@
|
||||
export * from './ManageContainer';
|
||||
export * from './components';
|
||||
export * from './hooks';
|
||||
export { getTableName, tablesQueryKey } from './TrackTables/hooks/useTables';
|
||||
export { tablesQueryKey } from './TrackTables/hooks/useTables';
|
||||
export { useTrackTable } from './TrackTables/hooks/useTrackTable';
|
||||
export { useMetadataSource } from './TrackTables/hooks/useMetadataSource';
|
||||
|
@ -1,4 +1,5 @@
|
||||
import { useDatabaseHierarchy, getTableName } from '@/features/Data';
|
||||
import { useDatabaseHierarchy } from '@/features/Data';
|
||||
import { getTableName } from '@/features/DataSource';
|
||||
import { Table } from '@/features/MetadataAPI';
|
||||
import { InputField, Select } from '@/new-components/Form';
|
||||
import React from 'react';
|
||||
|
@ -1,4 +1,5 @@
|
||||
import { useDatabaseHierarchy, getTableName } from '@/features/Data';
|
||||
import { useDatabaseHierarchy } from '@/features/Data';
|
||||
import { getTableName } from '@/features/DataSource';
|
||||
import { Table } from '@/features/MetadataAPI';
|
||||
import { InputField, Select } from '@/new-components/Form';
|
||||
import React from 'react';
|
||||
|
@ -0,0 +1,78 @@
|
||||
import { getTableName } from './getTableName';
|
||||
|
||||
describe('getTableName', () => {
|
||||
test.each`
|
||||
table | databaseHierarchy | expectedName
|
||||
${{ name: 'Album', schema: 'public' }} | ${['schema', 'name']} | ${'public.Album'}
|
||||
${{ name: 'Album', dataset: 'public' }} | ${['dataset', 'name']} | ${'public.Album'}
|
||||
${['public', 'Album']} | ${[]} | ${'public.Album'}
|
||||
`(
|
||||
'when invoked for $table & $hierarchy, should return $expectedName',
|
||||
({ table, databaseHierarchy, expectedName }) => {
|
||||
console.log(table, databaseHierarchy);
|
||||
const result = getTableName(table, databaseHierarchy);
|
||||
expect(result).toBe(expectedName);
|
||||
}
|
||||
);
|
||||
|
||||
it('should throw error, if hierachy is wrong', () => {
|
||||
try {
|
||||
// TS error is hidden because we're checking for an error
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
const result = getTableName({ name: 'Album', schema: 'public' }, [
|
||||
'dataset',
|
||||
'name',
|
||||
]);
|
||||
} catch (err: unknown) {
|
||||
expect((err as Error).message).toEqual('unable to find hierachy value');
|
||||
}
|
||||
});
|
||||
|
||||
it('should throw error, if hierachy is empty and the table is an object', () => {
|
||||
try {
|
||||
// TS error is hidden because we're checking for an error
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
const result = getTableName({ name: 'Album', schema: 'public' }, []);
|
||||
} catch (err: unknown) {
|
||||
expect((err as Error).message).toEqual('No database hierarchy found');
|
||||
}
|
||||
});
|
||||
|
||||
test.each`
|
||||
tableObject | databaseHierarchy
|
||||
${null} | ${['schema', 'name']}
|
||||
${undefined} | ${['dataset', 'name']}
|
||||
`(
|
||||
'should throw error, if table is %s',
|
||||
({ tableObject, databaseHierarchy }) => {
|
||||
try {
|
||||
// TS error is hidden because we're checking for an error
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
const result = getTableName(tableObject, databaseHierarchy);
|
||||
} catch (err: unknown) {
|
||||
expect((err as Error).message).toEqual(
|
||||
'Table cannot be null or undefined'
|
||||
);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
it.each`
|
||||
tableObject | databaseHierarchy
|
||||
${1} | ${[]}
|
||||
${123.5} | ${[]}
|
||||
`(
|
||||
'should throw error, if table is %s',
|
||||
({ tableObject, databaseHierarchy }) => {
|
||||
try {
|
||||
// TS error is hidden because we're checking for an error
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
const result = getTableName(tableObject, databaseHierarchy);
|
||||
} catch (err: unknown) {
|
||||
expect((err as Error).message).toEqual(
|
||||
'Table name could be generated for the given table type'
|
||||
);
|
||||
}
|
||||
}
|
||||
);
|
||||
});
|
@ -0,0 +1,45 @@
|
||||
import { Table } from '@/features/MetadataAPI';
|
||||
|
||||
export const getTableName = (
|
||||
table: Table,
|
||||
databaseHierarchy: string[]
|
||||
): string => {
|
||||
if (table === null || table === undefined)
|
||||
throw Error('Table cannot be null or undefined');
|
||||
|
||||
if (typeof table === 'string') return table;
|
||||
|
||||
if (Array.isArray(table)) {
|
||||
// verify if every entry in the array is a string
|
||||
const result = table.map<string>(item => {
|
||||
if (typeof item === 'string') return item;
|
||||
throw Error('Non string values found in table');
|
||||
});
|
||||
|
||||
return result.join('.');
|
||||
}
|
||||
|
||||
if (typeof table === 'object') {
|
||||
if (!databaseHierarchy || !databaseHierarchy.length)
|
||||
throw Error('No database hierarchy found');
|
||||
|
||||
const flatJsonTableDefinition = Object.entries(table).reduce<
|
||||
Record<string, string>
|
||||
>((acc, item) => {
|
||||
const [key, value] = item;
|
||||
if (typeof key === 'string' && typeof value === 'string')
|
||||
acc[key] = value;
|
||||
return acc;
|
||||
}, {});
|
||||
|
||||
const tableName = databaseHierarchy
|
||||
.map(key => {
|
||||
if (flatJsonTableDefinition[key]) return flatJsonTableDefinition[key];
|
||||
throw Error('unable to find hierachy value');
|
||||
})
|
||||
.join('.');
|
||||
return tableName;
|
||||
}
|
||||
|
||||
throw Error('Table name could be generated for the given table type');
|
||||
};
|
@ -0,0 +1 @@
|
||||
export { getTableName } from './getTableName';
|
@ -6,7 +6,6 @@ import { z } from 'zod';
|
||||
import { bigquery } from './bigquery';
|
||||
import { citus } from './citus';
|
||||
import { cockroach } from './cockroach';
|
||||
import * as utils from './common/utils';
|
||||
import { gdc } from './gdc';
|
||||
import { mssql } from './mssql';
|
||||
import { postgres, PostgresTable } from './postgres';
|
||||
@ -37,6 +36,7 @@ import {
|
||||
RunSQLResponse,
|
||||
} from './api';
|
||||
import { getAllSourceKinds } from './common/getAllSourceKinds';
|
||||
import { getTableName } from './common/getTableName';
|
||||
|
||||
export enum Feature {
|
||||
NotImplemented = 'Not Implemented',
|
||||
@ -407,7 +407,7 @@ export * from './types';
|
||||
export {
|
||||
PostgresTable,
|
||||
exportMetadata,
|
||||
utils,
|
||||
getTableName,
|
||||
RunSQLResponse,
|
||||
getDriverPrefix,
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user