Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 122 additions & 0 deletions test/node/src/camel-case-plugin.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import {
CamelCasePlugin,
CamelCasePluginOptions,
createQueryId,
CreateTableNode,
DummyDriver,
Kysely,
PostgresAdapter,
PostgresIntrospector,
PostgresQueryCompiler,
} from '../../..'
import { expect } from './test-setup'

describe('CamelCasePlugin', () => {
const db = new Kysely({
dialect: {
createAdapter: () => new PostgresAdapter(),
createDriver: () => new DummyDriver(),
createIntrospector: (db) => new PostgresIntrospector(db),
createQueryCompiler: () => new PostgresQueryCompiler(),
},
})
type TestCaseWithOptions = {
name: string
options?: CamelCasePluginOptions
schema: [source: string, expected: string][]
result: [source: string, expected: string][]
}
const testCaseWithOptions: TestCaseWithOptions[] = [
{
name: 'undefined',
options: undefined,
schema: [
['asis', 'asis'],
['fooBar', 'foo_bar'],
['aFOOBar', 'a_foobar'],
['_fooBar', '_foo_bar'],
],
result: [
['asis', 'asis'],
['foo_bar', 'fooBar'],
['a_f_o_o_bar', 'aFOOBar'],
['_id', '_Id'],
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently, this test case is passing. I think it should modified like ['_id', 'id']

],
},
{
name: 'upperCase',
options: { upperCase: true },
schema: [
['foo', 'FOO'],
['fooBar', 'FOO_BAR'],
],
result: [
['asis', 'asis'],
['FOO', 'foo'],
['foo_bar', 'fooBar'],
['FOO_BAR', 'fooBar'],
],
},
{
name: 'underscoreBetweenUppercaseLetters',
options: { underscoreBetweenUppercaseLetters: true },
schema: [
['asis', 'asis'],
['fooBar', 'foo_bar'],
['aFOOBar', 'a_f_o_o_bar'],
['_fooBar', '_foo_bar'],
],
result: [
['asis', 'asis'],
['foo_bar', 'fooBar'],
['a_f_o_o_bar', 'aFOOBar'],
],
},
{
name: 'underscoreBeforeDigits',
options: { underscoreBeforeDigits: true },
schema: [
['asis', 'asis'],
['fooBar', 'foo_bar'],
['foo12Bar', 'foo_12_bar'],
],
result: [
['asis', 'asis'],
['foo_bar', 'fooBar'],
['a_f_o_o_bar', 'aFOOBar'],
],
},
]

for (const {
name,
options,
schema: schemaTestCases,
result: resultTestCases,
} of testCaseWithOptions) {
describe(`with "${name}" options`, () => {
const plugin = new CamelCasePlugin(options)

for (const [source, expected] of schemaTestCases) {
it(`should convert schema "${source}"`, () => {
const result = plugin.transformQuery({
queryId: createQueryId(),
node: db.schema.createTable(source).toOperationNode(),
}) as CreateTableNode
expect(result.table.table.identifier.name).to.equal(expected)
})
}

for (const [source, expected] of resultTestCases) {
it(`should convert result "${source}"`, async () => {
const { rows } = await plugin.transformResult({
queryId: createQueryId(),
result: { rows: [{ [source]: 123456789 }] },
})
expect(rows[0]).has.key(expected)
expect(rows[0][expected]).to.equal(123456789)
})
}
})
}
})