Skip to content

Commit ee6053f

Browse files
committed
🔧 fix test cases by mentioning their own expected objects
1 parent ecd3ace commit ee6053f

File tree

3 files changed

+131
-17
lines changed

3 files changed

+131
-17
lines changed

frontend/packages/db-structure/src/parser/schemarb/index.test.ts

Lines changed: 109 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
import { describe, expect, it } from 'vitest'
22
import type { Table } from '../../schema/index.js'
3-
import { aColumn, aSchema, aTable } from '../../schema/index.js'
3+
import {
4+
aCheckConstraint,
5+
aColumn,
6+
aForeignKeyConstraint,
7+
aPrimaryKeyConstraint,
8+
aRelationship,
9+
aSchema,
10+
aTable,
11+
aUniqueConstraint,
12+
anIndex,
13+
} from '../../schema/index.js'
414
import { UnsupportedTokenError, processor } from './index.js'
515

616
import { createParserTestCases } from '../__tests__/index.js'
@@ -218,7 +228,45 @@ describe(processor, () => {
218228
end
219229
`)
220230

221-
expect(value).toEqual(parserTestCases['index (unique: true)'](''))
231+
const expected = aSchema({
232+
tables: {
233+
users: aTable({
234+
name: 'users',
235+
columns: {
236+
id: aColumn({
237+
name: 'id',
238+
type: 'bigserial',
239+
primary: true,
240+
notNull: true,
241+
unique: true,
242+
}),
243+
email: aColumn({
244+
name: 'email',
245+
type: 'varchar',
246+
}),
247+
},
248+
indexes: {
249+
index_users_on_email: anIndex({
250+
name: 'index_users_on_email',
251+
columns: ['email'],
252+
unique: true,
253+
}),
254+
},
255+
constraints: {
256+
PRIMARY_id: aPrimaryKeyConstraint({
257+
name: 'PRIMARY_id',
258+
columnName: 'id',
259+
}),
260+
UNIQUE_email: aUniqueConstraint({
261+
name: 'UNIQUE_email',
262+
columnName: 'email',
263+
}),
264+
},
265+
}),
266+
},
267+
})
268+
269+
expect(value).toEqual(expected)
222270
})
223271

224272
it('foreign key', async () => {
@@ -231,12 +279,27 @@ describe(processor, () => {
231279
add_foreign_key "posts", "users", column: "user_id", name: "${keyName}"
232280
`)
233281

234-
expect(value.relationships).toEqual(
235-
parserTestCases['foreign key'](keyName).relationships,
236-
)
237-
expect(value.tables['posts']?.constraints).toEqual(
238-
parserTestCases['foreign key'](keyName).constraints,
239-
)
282+
expect(value.relationships).toEqual({
283+
fk_posts_user_id: aRelationship({
284+
name: 'fk_posts_user_id',
285+
foreignTableName: 'posts',
286+
foreignColumnName: 'user_id',
287+
primaryTableName: 'users',
288+
primaryColumnName: 'id',
289+
}),
290+
})
291+
expect(value.tables['posts']?.constraints).toEqual({
292+
PRIMARY_id: aPrimaryKeyConstraint({
293+
name: 'PRIMARY_id',
294+
columnName: 'id',
295+
}),
296+
fk_posts_user_id: aForeignKeyConstraint({
297+
name: 'fk_posts_user_id',
298+
columnName: 'user_id',
299+
targetTableName: 'users',
300+
targetColumnName: 'id',
301+
}),
302+
})
240303
})
241304

242305
describe('foreign key cardinality', () => {
@@ -299,12 +362,34 @@ describe(processor, () => {
299362
add_foreign_key "posts", "users", column: "user_id", name: "fk_posts_user_id", on_update: :restrict, on_delete: :cascade
300363
`)
301364

302-
expect(value.relationships).toEqual(
303-
parserTestCases['foreign key with action'].relationships,
304-
)
305-
expect(value.tables['posts']?.constraints).toEqual(
306-
parserTestCases['foreign key with action'].constraints,
307-
)
365+
expect(value.relationships).toEqual({
366+
fk_posts_user_id: aRelationship({
367+
name: 'fk_posts_user_id',
368+
foreignTableName: 'posts',
369+
foreignColumnName: 'user_id',
370+
primaryTableName: 'users',
371+
primaryColumnName: 'id',
372+
cardinality: 'ONE_TO_MANY',
373+
updateConstraint: 'RESTRICT',
374+
deleteConstraint: 'CASCADE',
375+
}),
376+
})
377+
expect(value.tables['posts']?.constraints).toEqual({
378+
PRIMARY_id: aPrimaryKeyConstraint({
379+
type: 'PRIMARY KEY',
380+
name: 'PRIMARY_id',
381+
columnName: 'id',
382+
}),
383+
fk_posts_user_id: aForeignKeyConstraint({
384+
type: 'FOREIGN KEY',
385+
name: 'fk_posts_user_id',
386+
columnName: 'user_id',
387+
targetTableName: 'users',
388+
targetColumnName: 'id',
389+
updateConstraint: 'RESTRICT',
390+
deleteConstraint: 'CASCADE',
391+
}),
392+
})
308393
})
309394

310395
it('check constraint', async () => {
@@ -316,9 +401,16 @@ describe(processor, () => {
316401
add_check_constraint "users", "age >= 20 and age < 20", name: "age_range_check"
317402
`)
318403

319-
expect(value.tables['users']?.constraints).toEqual(
320-
parserTestCases['check constraint'],
321-
)
404+
expect(value.tables['users']?.constraints).toEqual({
405+
PRIMARY_id: aPrimaryKeyConstraint({
406+
name: 'PRIMARY_id',
407+
columnName: 'id',
408+
}),
409+
age_range_check: aCheckConstraint({
410+
name: 'age_range_check',
411+
detail: 'age >= 20 and age < 20',
412+
}),
413+
})
322414
})
323415
})
324416

frontend/packages/db-structure/src/schema/factories.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ import type {
33
Column,
44
ForeignKeyConstraint,
55
Index,
6+
PrimaryKeyConstraint,
67
Relationship,
78
Schema,
89
Table,
910
Tables,
11+
UniqueConstraint,
1012
} from './schema.js'
1113

1214
export const aColumn = (override?: Partial<Column>): Column => ({
@@ -44,6 +46,15 @@ export const anIndex = (override?: Partial<Index>): Index => ({
4446
...override,
4547
})
4648

49+
export const aPrimaryKeyConstraint = (
50+
override?: Partial<PrimaryKeyConstraint>,
51+
): PrimaryKeyConstraint => ({
52+
type: 'PRIMARY KEY',
53+
name: '',
54+
columnName: '',
55+
...override,
56+
})
57+
4758
export const aForeignKeyConstraint = (
4859
override?: Partial<ForeignKeyConstraint>,
4960
): ForeignKeyConstraint => ({
@@ -57,6 +68,15 @@ export const aForeignKeyConstraint = (
5768
...override,
5869
})
5970

71+
export const aUniqueConstraint = (
72+
override?: Partial<UniqueConstraint>,
73+
): UniqueConstraint => ({
74+
type: 'UNIQUE',
75+
name: '',
76+
columnName: '',
77+
...override,
78+
})
79+
6080
export const aCheckConstraint = (
6181
override?: Partial<CheckConstraint>,
6282
): CheckConstraint => ({

frontend/packages/db-structure/src/schema/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ export {
2828
aTable,
2929
aSchema,
3030
anIndex,
31+
aPrimaryKeyConstraint,
3132
aRelationship,
33+
aUniqueConstraint,
3234
aForeignKeyConstraint,
3335
aCheckConstraint,
3436
} from './factories.js'

0 commit comments

Comments
 (0)