Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for dialects #10

Merged
merged 1 commit into from
Oct 13, 2021
Merged
Show file tree
Hide file tree
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
20 changes: 20 additions & 0 deletions src/MockClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ export class MockClient extends knex.Client {
super(config);

MockClient.tracker = new Tracker(config.mockClient);

if (config.dialect) {
this._attachDialectQueryCompiler(config);
}
}

public acquireConnection() {
Expand Down Expand Up @@ -44,4 +48,20 @@ export class MockClient extends knex.Client {

return MockClient.tracker._handle({ ...rawQuery, method });
}

private _attachDialectQueryCompiler(config: Knex.Config<any> & { mockClient: TrackerConfig }) {
const { resolveClientNameWithAliases } = require('knex/lib/util/helpers');
const { SUPPORTED_CLIENTS } = require('knex/lib/constants');

if (!SUPPORTED_CLIENTS.includes(config.dialect)) {
throw new Error(
`knex-mock-client: Unknown configuration option 'dialect' value ${config.dialect}.\nNote that it is case-sensitive, check documentation for supported values.`
);
}

const resolvedClientName = resolveClientNameWithAliases(config.dialect);
const Dialect = require(`knex/lib/dialects/${resolvedClientName}/index.js`);
const dialect = new Dialect(config);
this.queryCompiler = dialect.queryCompiler.bind(this);
}
}
53 changes: 53 additions & 0 deletions tests/dialects.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import faker from 'faker';
import knex, { Knex } from 'knex';
import { getTracker, MockClient, Tracker } from '../src';

describe('specific dialect', () => {
let db: Knex;
let tracker: Tracker;

describe('postgres', () => {
beforeAll(() => {
db = knex({
client: MockClient,
dialect: 'pg',
});
tracker = getTracker();
});

afterEach(() => {
tracker.reset();
});

it('should allow to mock select query distinctOn', async () => {
const givenData = [{ id: faker.datatype.number() }];
tracker.on.select('table_name').response(givenData);

const data = await db('table_name').distinctOn('age');

expect(data).toEqual(givenData);
expect(tracker.history.select).toHaveLength(1);
});

it('should allow to mock select query noWait', async () => {
const givenData = [{ id: faker.datatype.number() }];
tracker.on.select('table_name').response(givenData);

const data = await db('table_name').select('*').forUpdate().noWait();

expect(data).toEqual(givenData);
expect(tracker.history.select).toHaveLength(1);
});
});

describe('none-exists', () => {
it('should should throw an error when passing none exists dialect', () => {
expect(() =>
knex({
client: MockClient,
dialect: 'none-exists',
})
).toThrowError(/Unknown configuration option 'dialect'/);
});
});
});