ColumnList.utils.test.ts 1.9 KB

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