mirror of
https://github.com/lowdefy/lowdefy.git
synced 2025-04-12 15:40:30 +08:00
fix(graphql): Knex tests, add tablename prop to builder.
This commit is contained in:
parent
d2d8a732d8
commit
ba696f80dd
@ -17,9 +17,44 @@
|
||||
import { validate } from '@lowdefy/ajv';
|
||||
import Knex from './Knex';
|
||||
|
||||
const { schema } = AxiosHttp;
|
||||
const { schema } = Knex;
|
||||
|
||||
test('All requests are present', () => {
|
||||
expect(Knex.requests.KnexRaw).toBeDefined();
|
||||
expect(Knex.requests.KnexBuilder).toBeDefined();
|
||||
});
|
||||
|
||||
test('valid connection schema', () => {
|
||||
const connection = {
|
||||
client: 'pg',
|
||||
};
|
||||
expect(validate({ schema, data: connection })).toEqual({ valid: true });
|
||||
});
|
||||
|
||||
test('valid connection schema, string connection', () => {
|
||||
const connection = {
|
||||
client: 'pg',
|
||||
connection: 'connection',
|
||||
};
|
||||
expect(validate({ schema, data: connection })).toEqual({ valid: true });
|
||||
});
|
||||
|
||||
test('valid connection schema, object connection', () => {
|
||||
const connection = {
|
||||
client: 'pg',
|
||||
connection: {
|
||||
host: '127.0.0.1',
|
||||
user: 'your_database_user',
|
||||
password: 'your_database_password',
|
||||
database: 'myapp_test',
|
||||
},
|
||||
};
|
||||
expect(validate({ schema, data: connection })).toEqual({ valid: true });
|
||||
});
|
||||
|
||||
test('client missing', () => {
|
||||
const connection = {};
|
||||
expect(() => validate({ schema, data: connection })).toThrow(
|
||||
'Knex connection should have required property "client".'
|
||||
);
|
||||
});
|
||||
|
@ -20,6 +20,9 @@ import schema from './KnexBuilderSchema.json';
|
||||
|
||||
async function knexBuilder({ request, connection }) {
|
||||
let client = knex(connection);
|
||||
if (request.tableName) {
|
||||
client = client(request.tableName);
|
||||
}
|
||||
for (const method of request.query) {
|
||||
if (Object.keys(method).length !== 1) {
|
||||
throw new Error('Invalid query');
|
||||
|
@ -14,10 +14,78 @@
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
import { validate } from '@lowdefy/ajv';
|
||||
import knex from 'knex';
|
||||
import KnexRaw from './KnexRaw';
|
||||
|
||||
const { resolver } = KnexRaw;
|
||||
const mockRaw = jest.fn(() => {
|
||||
return Promise.resolve({ rows: [{ name: 'name' }], _types: 'types' });
|
||||
});
|
||||
|
||||
test('x', () => {
|
||||
expect().toBe();
|
||||
})
|
||||
jest.mock('knex', () =>
|
||||
jest.fn(() => ({
|
||||
raw: mockRaw,
|
||||
}))
|
||||
);
|
||||
|
||||
const { resolver, schema } = KnexRaw;
|
||||
|
||||
const request = {
|
||||
query: 'SELECT * FROM "table" WHERE "name" = :name',
|
||||
parameters: { name: 'name' },
|
||||
};
|
||||
const connection = {
|
||||
client: 'pg',
|
||||
connection: 'connection',
|
||||
};
|
||||
|
||||
test('knexRaw', async () => {
|
||||
const res = await resolver({ request, connection });
|
||||
expect(knex.mock.calls).toEqual([
|
||||
[
|
||||
{
|
||||
client: 'pg',
|
||||
connection: 'connection',
|
||||
},
|
||||
],
|
||||
]);
|
||||
expect(mockRaw.mock.calls).toEqual([
|
||||
[
|
||||
'SELECT * FROM "table" WHERE "name" = :name',
|
||||
{
|
||||
name: 'name',
|
||||
},
|
||||
],
|
||||
]);
|
||||
expect(res).toEqual({
|
||||
rows: [
|
||||
{
|
||||
name: 'name',
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
test('valid request', () => {
|
||||
let request = {
|
||||
query: 'SELECT * FROM "table"',
|
||||
};
|
||||
expect(validate({ schema, data: request })).toEqual({ valid: true });
|
||||
request = {
|
||||
query: 'SELECT * FROM "table" WHERE "name" = :name',
|
||||
parameters: { name: 'name' },
|
||||
};
|
||||
expect(validate({ schema, data: request })).toEqual({ valid: true });
|
||||
request = {
|
||||
query: 'SELECT * FROM "table" WHERE "name" = ?',
|
||||
parameters: ['name'],
|
||||
};
|
||||
expect(validate({ schema, data: request })).toEqual({ valid: true });
|
||||
});
|
||||
|
||||
test('query missing', () => {
|
||||
const request = {};
|
||||
expect(() => validate({ schema, data: request })).toThrow(
|
||||
'KnexRaw request should have required property "query".'
|
||||
);
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user