ColumnList.utils.test.ts66 lines · main
1import { describe, expect, it } from 'vitest'
2
3import {
4 getColumnTypeAffordance,
5 getForeignKeyColumnNames,
6 getPrimaryKeyColumnNames,
7 getUniqueIndexColumnNames,
8} from './ColumnList.utils'
9
10describe('ColumnList.utils', () => {
11 it('normalises quoted array formats before resolving the affordance kind', () => {
12 expect(getColumnTypeAffordance('"uuid"[]')).toEqual({
13 kind: 'text',
14 label: 'Text',
15 })
16 })
17
18 it('maps recognised Postgres formats to their affordance labels', () => {
19 expect(getColumnTypeAffordance('timestamptz')).toEqual({
20 kind: 'time',
21 label: 'Date / time',
22 })
23 expect(getColumnTypeAffordance('jsonb')).toEqual({
24 kind: 'json',
25 label: 'JSON',
26 })
27 })
28
29 it('falls back to the other affordance for unrecognised formats', () => {
30 expect(getColumnTypeAffordance('citext')).toEqual({
31 kind: 'other',
32 label: 'Other',
33 })
34 })
35
36 it('derives only source-table foreign key column names', () => {
37 const table = {
38 schema: 'public',
39 name: 'orders',
40 primary_keys: [{ name: 'id' }],
41 relationships: [
42 {
43 source_schema: 'public',
44 source_table_name: 'orders',
45 source_column_name: 'customer_id',
46 target_table_schema: 'public',
47 target_table_name: 'customers',
48 target_column_name: 'id',
49 },
50 {
51 source_schema: 'public',
52 source_table_name: 'customers',
53 source_column_name: 'account_id',
54 target_table_schema: 'public',
55 target_table_name: 'accounts',
56 target_column_name: 'id',
57 },
58 ],
59 unique_indexes: [{ columns: ['reference'] }, { columns: ['customer_id', 'reference'] }],
60 } as const
61
62 expect([...getPrimaryKeyColumnNames(table)]).toEqual(['id'])
63 expect([...getForeignKeyColumnNames(table)]).toEqual(['customer_id'])
64 expect([...getUniqueIndexColumnNames(table)]).toEqual(['reference'])
65 })
66})