Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add columns unit tests (#1033)
- Loading branch information
1 parent
84fb4bb
commit e96f85d
Showing
5 changed files
with
246 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { PgType } from '../../../src'; | ||
import { addColumns } from '../../../src/operations/tables'; | ||
import { options1 } from '../../utils'; | ||
|
||
describe('operations', () => { | ||
describe('columns', () => { | ||
describe('addColumns', () => { | ||
const addColumnsFn = addColumns(options1); | ||
|
||
it('should return a function', () => { | ||
expect(addColumnsFn).toBeTypeOf('function'); | ||
}); | ||
|
||
it('should return sql statement', () => { | ||
const statement = addColumnsFn('transactions', { | ||
status: { | ||
type: 'varchar(30)', | ||
default: 'old', | ||
}, | ||
mtime: { | ||
type: PgType.TIMESTAMP_WITH_TIME_ZONE, | ||
default: 'now()', | ||
}, | ||
}); | ||
|
||
expect(statement).toBeTypeOf('string'); | ||
expect(statement).toBe(`ALTER TABLE "transactions" | ||
ADD "status" varchar(30) DEFAULT $pga$old$pga$, | ||
ADD "mtime" timestamp with time zone DEFAULT $pga$now()$pga$;`); | ||
}); | ||
|
||
it('should return sql statement with columnOptions', () => { | ||
const statement = addColumnsFn( | ||
'transactions', | ||
{ | ||
status: { | ||
type: 'varchar(30)', | ||
default: 'old', | ||
}, | ||
}, | ||
{ | ||
ifNotExists: true, | ||
} | ||
); | ||
|
||
expect(statement).toBeTypeOf('string'); | ||
expect(statement).toBe(`ALTER TABLE "transactions" | ||
ADD IF NOT EXISTS "status" varchar(30) DEFAULT $pga$old$pga$;`); | ||
}); | ||
|
||
it('should return sql statement with schema', () => { | ||
const statement = addColumnsFn( | ||
{ schema: 'myschema', name: 'distributors' }, | ||
{ | ||
status: PgType.VARCHAR, | ||
} | ||
); | ||
|
||
expect(statement).toBeTypeOf('string'); | ||
expect(statement).toBe( | ||
`ALTER TABLE "myschema"."distributors" | ||
ADD "status" varchar;` | ||
); | ||
}); | ||
|
||
describe('reverse', () => { | ||
it('should contain a reverse function', () => { | ||
expect(addColumnsFn.reverse).toBeTypeOf('function'); | ||
}); | ||
|
||
it('should return sql statement', () => { | ||
const statement = addColumnsFn.reverse('transactions', { | ||
status: { | ||
type: 'varchar(30)', | ||
default: 'old', | ||
}, | ||
}); | ||
|
||
expect(statement).toBeTypeOf('string'); | ||
expect(statement).toBe(`ALTER TABLE "transactions" | ||
DROP "status";`); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { alterColumn } from '../../../src/operations/tables'; | ||
import { options1 } from '../../utils'; | ||
|
||
describe('operations', () => { | ||
describe('columns', () => { | ||
describe('alterColumn', () => { | ||
const alterColumnFn = alterColumn(options1); | ||
|
||
it('should return a function', () => { | ||
expect(alterColumnFn).toBeTypeOf('function'); | ||
}); | ||
|
||
it('should return sql statement', () => { | ||
const statement = alterColumnFn('distributors', 'address', {}); | ||
|
||
expect(statement).toBeTypeOf('string'); | ||
expect(statement).toHaveLength(0); | ||
}); | ||
|
||
it('should return sql statement with columnOptions', () => { | ||
const statement = alterColumnFn('distributors', 'address', { | ||
default: null, | ||
type: 'varchar(30)', | ||
collation: 'C', | ||
using: 'address::text', | ||
notNull: false, | ||
sequenceGenerated: null, | ||
comment: 'Address of the distributor', | ||
}); | ||
|
||
expect(statement).toBeTypeOf('string'); | ||
expect(statement).toStrictEqual( | ||
`ALTER TABLE "distributors" | ||
ALTER "address" DROP DEFAULT, | ||
ALTER "address" SET DATA TYPE varchar(30) COLLATE C USING address::text, | ||
ALTER "address" DROP NOT NULL, | ||
ALTER "address" DROP IDENTITY; | ||
COMMENT ON COLUMN "distributors"."address" IS $pga$Address of the distributor$pga$;` | ||
); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { dropColumns } from '../../../src/operations/tables'; | ||
import { options1 } from '../../utils'; | ||
|
||
describe('operations', () => { | ||
describe('columns', () => { | ||
describe('dropColumns', () => { | ||
const dropColumnsFn = dropColumns(options1); | ||
|
||
it('should return a function', () => { | ||
expect(dropColumnsFn).toBeTypeOf('function'); | ||
}); | ||
|
||
it('should return sql statement', () => { | ||
const statement = dropColumnsFn('distributors', 'address'); | ||
|
||
expect(statement).toBeTypeOf('string'); | ||
expect(statement).toBe( | ||
`ALTER TABLE "distributors" | ||
DROP "address";` | ||
); | ||
}); | ||
|
||
it('should return sql statement with dropOptions', () => { | ||
const statement = dropColumnsFn( | ||
'distributors', | ||
{ | ||
address: 'varchar(30)', | ||
}, | ||
{ | ||
ifExists: true, | ||
cascade: true, | ||
} | ||
); | ||
|
||
expect(statement).toBeTypeOf('string'); | ||
expect(statement).toBe(`ALTER TABLE "distributors" | ||
DROP IF EXISTS "address";`); | ||
}); | ||
|
||
it('should return sql statement with schema', () => { | ||
const statement = dropColumnsFn( | ||
{ | ||
schema: 'myschema', | ||
name: 'distributors', | ||
}, | ||
{ | ||
address: 'varchar(30)', | ||
} | ||
); | ||
|
||
expect(statement).toBeTypeOf('string'); | ||
expect(statement).toBe(`ALTER TABLE "myschema"."distributors" | ||
DROP "address";`); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { renameColumn } from '../../../src/operations/tables'; | ||
import { options1 } from '../../utils'; | ||
|
||
describe('operations', () => { | ||
describe('columns', () => { | ||
describe('renameColumn', () => { | ||
const renameColumnFn = renameColumn(options1); | ||
|
||
it('should return a function', () => { | ||
expect(renameColumnFn).toBeTypeOf('function'); | ||
}); | ||
|
||
it('should return sql statement', () => { | ||
const statement = renameColumnFn('distributors', 'address', 'city'); | ||
|
||
expect(statement).toBeTypeOf('string'); | ||
expect(statement).toStrictEqual( | ||
'ALTER TABLE "distributors" RENAME "address" TO "city";' | ||
); | ||
}); | ||
|
||
it('should return sql statement with schema', () => { | ||
const statement = renameColumnFn( | ||
{ name: 'distributors', schema: 'myschema' }, | ||
'address', | ||
'city' | ||
); | ||
|
||
expect(statement).toBeTypeOf('string'); | ||
expect(statement).toStrictEqual( | ||
'ALTER TABLE "myschema"."distributors" RENAME "address" TO "city";' | ||
); | ||
}); | ||
|
||
describe('reverse', () => { | ||
it('should contain a reverse function', () => { | ||
expect(renameColumnFn.reverse).toBeTypeOf('function'); | ||
}); | ||
|
||
it('should return sql statement', () => { | ||
const statement = renameColumnFn.reverse( | ||
'distributors', | ||
'address', | ||
'city' | ||
); | ||
|
||
expect(statement).toBeTypeOf('string'); | ||
expect(statement).toBe( | ||
'ALTER TABLE "distributors" RENAME "city" TO "address";' | ||
); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |