diff --git a/test/node/src/camel-case-plugin.test.ts b/test/node/src/camel-case-plugin.test.ts new file mode 100644 index 000000000..f7875be85 --- /dev/null +++ b/test/node/src/camel-case-plugin.test.ts @@ -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'], + ], + }, + { + 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) + }) + } + }) + } +})