mirror of
https://github.com/logto-io/logto.git
synced 2025-03-10 22:22:45 -05:00
test(core): add unit tests for database utils (#95)
* test(core): add unit tests for database utils * test(core): add tests for `conditionalSql()`
This commit is contained in:
parent
9546aee496
commit
1352cce911
3 changed files with 164 additions and 2 deletions
26
.github/workflows/add-labels.yml
vendored
26
.github/workflows/add-labels.yml
vendored
|
@ -10,7 +10,33 @@ jobs:
|
|||
add_labels:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
<<<<<<< HEAD
|
||||
- uses: logto-io/actions-add-labels-run-steps@v1.0.1
|
||||
=======
|
||||
- uses: actions/checkout@v2
|
||||
|
||||
- name: Extract Label
|
||||
id: extract-label
|
||||
run: |
|
||||
echo "::set-output name=labels_to_add::$(
|
||||
node -e "
|
||||
const title = process.env.TITLE;
|
||||
const labelMapping = {
|
||||
chore: 'chore',
|
||||
feat: 'feature',
|
||||
fix: 'bugfix',
|
||||
refactor: 'enhancement',
|
||||
test: 'testing',
|
||||
};
|
||||
const foundKey = Object.keys(labelMapping).find((label) => title && title.startsWith(label));
|
||||
console.log(foundKey ? labelMapping[foundKey] : '');
|
||||
"
|
||||
)"
|
||||
env:
|
||||
TITLE: ${{ github.event.pull_request.title || github.event.issue.title }}
|
||||
|
||||
- uses: actions-ecosystem/action-add-labels@v1.1.0
|
||||
>>>>>>> 5dc8d4c (test(core): add unit tests for database utils)
|
||||
with:
|
||||
title: ${{ github.event.pull_request.title || github.event.issue.title }}
|
||||
github-token: ${{ github.token }}
|
||||
|
|
136
packages/core/src/database/utils.test.ts
Normal file
136
packages/core/src/database/utils.test.ts
Normal file
|
@ -0,0 +1,136 @@
|
|||
import { sql } from 'slonik';
|
||||
import { SqlToken } from 'slonik/dist/src/tokens.js';
|
||||
import dayjs from 'dayjs';
|
||||
import {
|
||||
excludeAutoSetFields,
|
||||
autoSetFields,
|
||||
convertToPrimitiveOrSql,
|
||||
convertToIdentifiers,
|
||||
convertToTimestamp,
|
||||
conditionalSql,
|
||||
} from './utils';
|
||||
import { Table } from './types';
|
||||
|
||||
describe('conditionalSql()', () => {
|
||||
it('returns empty sql when value is falsy', () => {
|
||||
expect(conditionalSql(false, () => sql`select 1`)).toEqual({
|
||||
sql: '',
|
||||
type: SqlToken,
|
||||
values: [],
|
||||
});
|
||||
});
|
||||
|
||||
it('builds sql when value is truthy', () => {
|
||||
expect(conditionalSql(true, () => sql`select 1`)).toEqual({
|
||||
sql: 'select 1',
|
||||
type: SqlToken,
|
||||
values: [],
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('excludeAutoSetFields()', () => {
|
||||
it('excludes auto set fields when needed', () => {
|
||||
expect(excludeAutoSetFields(['foo', autoSetFields[0]])).toEqual(['foo']);
|
||||
});
|
||||
|
||||
it('keeps the original value when no auto-set field is found', () => {
|
||||
expect(excludeAutoSetFields(['foo'])).toEqual(['foo']);
|
||||
});
|
||||
});
|
||||
|
||||
describe('convertToPrimitiveOrSql()', () => {
|
||||
const normalKey = 'foo';
|
||||
const timestampKeyEndings = ['_at', 'At'];
|
||||
|
||||
it('does not transform normal values with normal key', () => {
|
||||
expect(convertToPrimitiveOrSql(normalKey, null)).toEqual(null);
|
||||
expect(convertToPrimitiveOrSql(normalKey, 'bar')).toEqual('bar');
|
||||
expect(convertToPrimitiveOrSql(normalKey, 123)).toEqual(123);
|
||||
expect(convertToPrimitiveOrSql(normalKey, true)).toEqual(true);
|
||||
expect(convertToPrimitiveOrSql(normalKey, { foo: 'bar' })).toEqual('{"foo":"bar"}');
|
||||
});
|
||||
|
||||
it('converts value to sql when key ends with special set and value is number', () => {
|
||||
for (const value of timestampKeyEndings) {
|
||||
expect(convertToPrimitiveOrSql(`${normalKey}${value}`, 12_341_234)).toEqual({
|
||||
sql: 'to_timestamp($1)',
|
||||
type: SqlToken,
|
||||
values: [12_341.234],
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
it('does not transform value to timestamp when value is not number', () => {
|
||||
for (const value of timestampKeyEndings) {
|
||||
expect(convertToPrimitiveOrSql(`${normalKey}${value}`, '123')).toEqual('123');
|
||||
}
|
||||
});
|
||||
|
||||
it('throws an error when value is not primitive', () => {
|
||||
// @ts-expect-error
|
||||
expect(() => convertToPrimitiveOrSql(normalKey, [123, 456])).toThrow(
|
||||
'Cannot convert foo with 123,456 to primitive'
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('convertToIdentifiers()', () => {
|
||||
const table = 'foo';
|
||||
const fields = {
|
||||
fooBar: 'foo_bar',
|
||||
baz: 'baz',
|
||||
};
|
||||
const data: Table = { table, fields };
|
||||
|
||||
it('converts table to correct identifiers', () => {
|
||||
expect(convertToIdentifiers(data)).toEqual({
|
||||
table: sql.identifier([table]),
|
||||
fields: {
|
||||
fooBar: sql.identifier([fields.fooBar]),
|
||||
baz: sql.identifier([fields.baz]),
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it('converts table to correct identifiers with prefix', () => {
|
||||
expect(convertToIdentifiers(data, true)).toEqual({
|
||||
table: sql.identifier([table]),
|
||||
fields: {
|
||||
fooBar: sql.identifier([table, fields.fooBar]),
|
||||
baz: sql.identifier([table, fields.baz]),
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('convertToTimestamp()', () => {
|
||||
const fakeTime = new Date();
|
||||
|
||||
beforeAll(() => {
|
||||
jest.useFakeTimers('modern');
|
||||
jest.setSystemTime(fakeTime);
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
jest.useRealTimers();
|
||||
});
|
||||
|
||||
it('converts to sql with current time by default', () => {
|
||||
expect(convertToTimestamp()).toEqual({
|
||||
sql: 'to_timestamp($1)',
|
||||
type: SqlToken,
|
||||
values: [fakeTime.valueOf() / 1000],
|
||||
});
|
||||
});
|
||||
|
||||
it('converts to sql per time parameter', () => {
|
||||
const time = dayjs(123_123_123);
|
||||
|
||||
expect(convertToTimestamp(time)).toEqual({
|
||||
sql: 'to_timestamp($1)',
|
||||
type: SqlToken,
|
||||
values: [time.valueOf() / 1000],
|
||||
});
|
||||
});
|
||||
});
|
|
@ -38,7 +38,7 @@ export const convertToPrimitiveOrSql = (
|
|||
return null;
|
||||
}
|
||||
|
||||
if (typeof value === 'object') {
|
||||
if (typeof value === 'object' && !Array.isArray(value)) {
|
||||
return JSON.stringify(value);
|
||||
}
|
||||
|
||||
|
@ -50,7 +50,7 @@ export const convertToPrimitiveOrSql = (
|
|||
return value;
|
||||
}
|
||||
|
||||
throw new Error(`Cannot convert to primitive from ${typeof value}`);
|
||||
throw new Error(`Cannot convert ${key} with ${value.toString()} to primitive`);
|
||||
};
|
||||
|
||||
export const convertToIdentifiers = <T extends Table>(
|
||||
|
|
Loading…
Add table
Reference in a new issue