Schemas.utils.test.ts176 lines · main
| 1 | import { describe, expect, test } from 'vitest' |
| 2 | |
| 3 | import { getSchemaAsMarkdown, getTableDefinitionAsMarkdown } from './Schemas.utils' |
| 4 | |
| 5 | describe('Schemas.utils', () => { |
| 6 | test('getSchemaAsMarkdown returns properly formatted markdown', () => { |
| 7 | const schema = 'public' |
| 8 | const tables = [ |
| 9 | { |
| 10 | ref: 'default', |
| 11 | id: 20999, |
| 12 | name: 'test', |
| 13 | description: 'An excellent description', |
| 14 | schema: 'public', |
| 15 | isForeign: false, |
| 16 | columns: [ |
| 17 | { |
| 18 | id: '20999.1', |
| 19 | isPrimary: true, |
| 20 | name: 'id', |
| 21 | format: 'int8', |
| 22 | isNullable: false, |
| 23 | isUnique: false, |
| 24 | isUpdateable: true, |
| 25 | isIdentity: true, |
| 26 | description: '', |
| 27 | }, |
| 28 | { |
| 29 | id: '20999.2', |
| 30 | isPrimary: false, |
| 31 | name: 'created_at', |
| 32 | format: 'timestamptz', |
| 33 | isNullable: false, |
| 34 | isUnique: false, |
| 35 | isUpdateable: true, |
| 36 | isIdentity: false, |
| 37 | description: '', |
| 38 | }, |
| 39 | { |
| 40 | id: '20999.3', |
| 41 | isPrimary: false, |
| 42 | name: 'user_id', |
| 43 | format: 'uuid', |
| 44 | isNullable: true, |
| 45 | isUnique: false, |
| 46 | isUpdateable: true, |
| 47 | isIdentity: false, |
| 48 | description: '', |
| 49 | }, |
| 50 | ], |
| 51 | }, |
| 52 | { |
| 53 | ref: 'default', |
| 54 | id: 21049, |
| 55 | name: 'test2', |
| 56 | description: '', |
| 57 | schema: 'public', |
| 58 | isForeign: false, |
| 59 | columns: [ |
| 60 | { |
| 61 | id: '21049.1', |
| 62 | isPrimary: true, |
| 63 | name: 'id', |
| 64 | format: 'int8', |
| 65 | isNullable: false, |
| 66 | isUnique: false, |
| 67 | isUpdateable: true, |
| 68 | isIdentity: true, |
| 69 | description: '', |
| 70 | }, |
| 71 | { |
| 72 | id: '21049.2', |
| 73 | isPrimary: false, |
| 74 | name: 'created_at', |
| 75 | format: 'timestamptz', |
| 76 | isNullable: false, |
| 77 | isUnique: false, |
| 78 | isUpdateable: true, |
| 79 | isIdentity: false, |
| 80 | description: '', |
| 81 | }, |
| 82 | ], |
| 83 | }, |
| 84 | { |
| 85 | id: 21009, |
| 86 | ref: 'default', |
| 87 | schema: 'auth', |
| 88 | name: 'auth.users.id', |
| 89 | description: '', |
| 90 | isForeign: true, |
| 91 | columns: [], |
| 92 | }, |
| 93 | ] |
| 94 | const result = getSchemaAsMarkdown(schema, tables) |
| 95 | expect(result).toBe(`## Table \`test\` |
| 96 | |
| 97 | An excellent description |
| 98 | |
| 99 | ### Columns |
| 100 | |
| 101 | | Name | Type | Constraints | |
| 102 | |------|------|-------------| |
| 103 | | \`id\` | \`int8\` | Primary Identity | |
| 104 | | \`created_at\` | \`timestamptz\` | | |
| 105 | | \`user_id\` | \`uuid\` | Nullable | |
| 106 | |
| 107 | ## Table \`test2\` |
| 108 | |
| 109 | ### Columns |
| 110 | |
| 111 | | Name | Type | Constraints | |
| 112 | |------|------|-------------| |
| 113 | | \`id\` | \`int8\` | Primary Identity | |
| 114 | | \`created_at\` | \`timestamptz\` | | |
| 115 | |
| 116 | `) |
| 117 | }) |
| 118 | test('getTableDefinitionAsMarkdown returns properly formatted markdown', () => { |
| 119 | const table = { |
| 120 | ref: 'default', |
| 121 | id: 20999, |
| 122 | name: 'test', |
| 123 | description: 'An excellent description', |
| 124 | schema: 'public', |
| 125 | isForeign: false, |
| 126 | columns: [ |
| 127 | { |
| 128 | id: '20999.1', |
| 129 | isPrimary: true, |
| 130 | name: 'id', |
| 131 | format: 'int8', |
| 132 | isNullable: false, |
| 133 | isUnique: false, |
| 134 | isUpdateable: true, |
| 135 | isIdentity: true, |
| 136 | description: '', |
| 137 | }, |
| 138 | { |
| 139 | id: '20999.2', |
| 140 | isPrimary: false, |
| 141 | name: 'created_at', |
| 142 | format: 'timestamptz', |
| 143 | isNullable: false, |
| 144 | isUnique: false, |
| 145 | isUpdateable: true, |
| 146 | isIdentity: false, |
| 147 | description: '', |
| 148 | }, |
| 149 | { |
| 150 | id: '20999.3', |
| 151 | isPrimary: false, |
| 152 | name: 'user_id', |
| 153 | format: 'uuid', |
| 154 | isNullable: true, |
| 155 | isUnique: false, |
| 156 | isUpdateable: true, |
| 157 | isIdentity: false, |
| 158 | description: '', |
| 159 | }, |
| 160 | ], |
| 161 | } |
| 162 | const result = getTableDefinitionAsMarkdown(table) |
| 163 | expect(result).toBe(`## Table \`test\` |
| 164 | |
| 165 | An excellent description |
| 166 | |
| 167 | ### Columns |
| 168 | |
| 169 | | Name | Type | Constraints | |
| 170 | |------|------|-------------| |
| 171 | | \`id\` | \`int8\` | Primary Identity | |
| 172 | | \`created_at\` | \`timestamptz\` | | |
| 173 | | \`user_id\` | \`uuid\` | Nullable | |
| 174 | `) |
| 175 | }) |
| 176 | }) |