-
Notifications
You must be signed in to change notification settings - Fork 180
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(utils): improve createSchemalize (#1058)
- Loading branch information
1 parent
9b4ec81
commit fd7dba7
Showing
6 changed files
with
344 additions
and
25 deletions.
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
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
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,271 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { createSchemalize } from '../../src/utils'; | ||
|
||
describe('utils', () => { | ||
describe('createSchemalize', () => { | ||
it.each([ | ||
[true, true], | ||
[true, false], | ||
[false, true], | ||
[false, false], | ||
])('should return a function', (shouldDecamelize, shouldQuote) => { | ||
const actual = createSchemalize({ | ||
shouldDecamelize, | ||
shouldQuote, | ||
}); | ||
|
||
expect(actual).toBeTypeOf('function'); | ||
}); | ||
|
||
it('should decamelize and quote for string', () => { | ||
const schemalize = createSchemalize({ | ||
shouldDecamelize: true, | ||
shouldQuote: true, | ||
}); | ||
|
||
const actual = schemalize('myTable'); | ||
|
||
expect(actual).toBeTypeOf('string'); | ||
expect(actual).toBe('"my_table"'); | ||
}); | ||
|
||
it('should decamelize and quote for schema', () => { | ||
const schemalize = createSchemalize({ | ||
shouldDecamelize: true, | ||
shouldQuote: true, | ||
}); | ||
|
||
const actual = schemalize({ schema: 'mySchema', name: 'myTable' }); | ||
|
||
expect(actual).toBeTypeOf('string'); | ||
expect(actual).toBe('"my_schema"."my_table"'); | ||
}); | ||
|
||
it('should only decamelize for string', () => { | ||
const schemalize = createSchemalize({ | ||
shouldDecamelize: true, | ||
shouldQuote: false, | ||
}); | ||
|
||
const actual = schemalize('myTable'); | ||
|
||
expect(actual).toBeTypeOf('string'); | ||
expect(actual).toBe('my_table'); | ||
}); | ||
|
||
it('should only decamelize for schema', () => { | ||
const schemalize = createSchemalize({ | ||
shouldDecamelize: true, | ||
shouldQuote: false, | ||
}); | ||
|
||
const actual = schemalize({ schema: 'mySchema', name: 'myTable' }); | ||
|
||
expect(actual).toBeTypeOf('string'); | ||
expect(actual).toBe('my_schema.my_table'); | ||
}); | ||
|
||
it('should only quote for string', () => { | ||
const schemalize = createSchemalize({ | ||
shouldDecamelize: false, | ||
shouldQuote: true, | ||
}); | ||
|
||
const actual = schemalize('myTable'); | ||
|
||
expect(actual).toBeTypeOf('string'); | ||
expect(actual).toBe('"myTable"'); | ||
}); | ||
|
||
it('should only quote for schema', () => { | ||
const schemalize = createSchemalize({ | ||
shouldDecamelize: false, | ||
shouldQuote: true, | ||
}); | ||
|
||
const actual = schemalize({ schema: 'mySchema', name: 'myTable' }); | ||
|
||
expect(actual).toBeTypeOf('string'); | ||
expect(actual).toBe('"mySchema"."myTable"'); | ||
}); | ||
|
||
it('should not decamelize and quote for string', () => { | ||
const schemalize = createSchemalize({ | ||
shouldDecamelize: false, | ||
shouldQuote: false, | ||
}); | ||
|
||
const actual = schemalize('myTable'); | ||
|
||
expect(actual).toBeTypeOf('string'); | ||
expect(actual).toBe('myTable'); | ||
}); | ||
|
||
it('should not decamelize and quote for schema', () => { | ||
const schemalize = createSchemalize({ | ||
shouldDecamelize: false, | ||
shouldQuote: false, | ||
}); | ||
|
||
const actual = schemalize({ schema: 'mySchema', name: 'myTable' }); | ||
|
||
expect(actual).toBeTypeOf('string'); | ||
expect(actual).toBe('mySchema.myTable'); | ||
}); | ||
|
||
// TODO @Shinigami92 2024-04-03: Should this throw an error? | ||
it.each([ | ||
[true, true, '""'], | ||
[true, false, ''], | ||
[false, true, '""'], | ||
[false, false, ''], | ||
])( | ||
'should return empty string', | ||
(shouldDecamelize, shouldQuote, expected) => { | ||
const schemalize = createSchemalize({ | ||
shouldDecamelize, | ||
shouldQuote, | ||
}); | ||
|
||
const actual = schemalize(''); | ||
|
||
expect(actual).toBeTypeOf('string'); | ||
expect(actual).toBe(expected); | ||
} | ||
); | ||
|
||
it.each([ | ||
[true, true, '"my_table"'], | ||
[true, false, 'my_table'], | ||
[false, true, '"myTable"'], | ||
[false, false, 'myTable'], | ||
])('should accept only name', (shouldDecamelize, shouldQuote, expected) => { | ||
const schemalize = createSchemalize({ | ||
shouldDecamelize, | ||
shouldQuote, | ||
}); | ||
|
||
const actual = schemalize({ name: 'myTable' }); | ||
|
||
expect(actual).toBeTypeOf('string'); | ||
expect(actual).toBe(expected); | ||
}); | ||
}); | ||
|
||
describe('createSchemalize (deprecated)', () => { | ||
it.each([ | ||
[true, true], | ||
[true, false], | ||
[false, true], | ||
[false, false], | ||
])('should return a function', (shouldDecamelize, shouldQuote) => { | ||
const actual = createSchemalize(shouldDecamelize, shouldQuote); | ||
|
||
expect(actual).toBeTypeOf('function'); | ||
}); | ||
|
||
it('should decamelize and quote for string', () => { | ||
const schemalize = createSchemalize(true, true); | ||
|
||
const actual = schemalize('myTable'); | ||
|
||
expect(actual).toBeTypeOf('string'); | ||
expect(actual).toBe('"my_table"'); | ||
}); | ||
|
||
it('should decamelize and quote for schema', () => { | ||
const schemalize = createSchemalize(true, true); | ||
|
||
const actual = schemalize({ schema: 'mySchema', name: 'myTable' }); | ||
|
||
expect(actual).toBeTypeOf('string'); | ||
expect(actual).toBe('"my_schema"."my_table"'); | ||
}); | ||
|
||
it('should only decamelize for string', () => { | ||
const schemalize = createSchemalize(true, false); | ||
|
||
const actual = schemalize('myTable'); | ||
|
||
expect(actual).toBeTypeOf('string'); | ||
expect(actual).toBe('my_table'); | ||
}); | ||
|
||
it('should only decamelize for schema', () => { | ||
const schemalize = createSchemalize(true, false); | ||
|
||
const actual = schemalize({ schema: 'mySchema', name: 'myTable' }); | ||
|
||
expect(actual).toBeTypeOf('string'); | ||
expect(actual).toBe('my_schema.my_table'); | ||
}); | ||
|
||
it('should only quote for string', () => { | ||
const schemalize = createSchemalize(false, true); | ||
|
||
const actual = schemalize('myTable'); | ||
|
||
expect(actual).toBeTypeOf('string'); | ||
expect(actual).toBe('"myTable"'); | ||
}); | ||
|
||
it('should only quote for schema', () => { | ||
const schemalize = createSchemalize(false, true); | ||
|
||
const actual = schemalize({ schema: 'mySchema', name: 'myTable' }); | ||
|
||
expect(actual).toBeTypeOf('string'); | ||
expect(actual).toBe('"mySchema"."myTable"'); | ||
}); | ||
|
||
it('should not decamelize and quote for string', () => { | ||
const schemalize = createSchemalize(false, false); | ||
|
||
const actual = schemalize('myTable'); | ||
|
||
expect(actual).toBeTypeOf('string'); | ||
expect(actual).toBe('myTable'); | ||
}); | ||
|
||
it('should not decamelize and quote for schema', () => { | ||
const schemalize = createSchemalize(false, false); | ||
|
||
const actual = schemalize({ schema: 'mySchema', name: 'myTable' }); | ||
|
||
expect(actual).toBeTypeOf('string'); | ||
expect(actual).toBe('mySchema.myTable'); | ||
}); | ||
|
||
// TODO @Shinigami92 2024-04-03: Should this throw an error? | ||
it.each([ | ||
[true, true, '""'], | ||
[true, false, ''], | ||
[false, true, '""'], | ||
[false, false, ''], | ||
])( | ||
'should return empty string', | ||
(shouldDecamelize, shouldQuote, expected) => { | ||
const schemalize = createSchemalize(shouldDecamelize, shouldQuote); | ||
|
||
const actual = schemalize(''); | ||
|
||
expect(actual).toBeTypeOf('string'); | ||
expect(actual).toBe(expected); | ||
} | ||
); | ||
|
||
it.each([ | ||
[true, true, '"my_table"'], | ||
[true, false, 'my_table'], | ||
[false, true, '"myTable"'], | ||
[false, false, 'myTable'], | ||
])('should accept only name', (shouldDecamelize, shouldQuote, expected) => { | ||
const schemalize = createSchemalize(shouldDecamelize, shouldQuote); | ||
|
||
const actual = schemalize({ name: 'myTable' }); | ||
|
||
expect(actual).toBeTypeOf('string'); | ||
expect(actual).toBe(expected); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.