Skip to content

Commit

Permalink
Add more tests for mysql driver + fix MySqlDriver.isConnected() method
Browse files Browse the repository at this point in the history
  • Loading branch information
B4nan committed Jan 13, 2019
1 parent 2d195b0 commit a0a5f77
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 32 deletions.
6 changes: 5 additions & 1 deletion lib/MikroORM.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export class MikroORM {
return this.driver;
}

isConnected(): boolean {
async isConnected(): Promise<boolean> {
return this.driver.isConnected();
}

Expand All @@ -88,5 +88,9 @@ export interface Options {
debug?: boolean;
baseDir?: string;
clientUrl?: string;
host?: string;
port?: number;
user?: string;
password?: string;
multipleStatements?: boolean;
}
4 changes: 2 additions & 2 deletions lib/drivers/DatabaseDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ export abstract class DatabaseDriver implements IDatabaseDriver {
/**
* Are we connected to the database
*/
abstract isConnected(): boolean;
abstract async isConnected(): Promise<boolean>;

/**
* Closes the database connection (aka disconnect)
*/
abstract close(force: boolean);
abstract async close(force: boolean): Promise<void>;

/**
* Finds selection of entities
Expand Down
4 changes: 2 additions & 2 deletions lib/drivers/IDatabaseDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ export interface IDatabaseDriver {
/**
* Are we connected to the database
*/
isConnected(): boolean;
isConnected(): Promise<boolean>;

/**
* Closes the database connection (aka disconnect)
*/
close(force: boolean);
close(force: boolean): Promise<void>;

/**
* Finds selection of entities
Expand Down
4 changes: 2 additions & 2 deletions lib/drivers/MongoDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ export class MongoDriver extends DatabaseDriver {
this.db = this.client.db(this.options.dbName);
}

close(force: boolean) {
async close(force: boolean): Promise<void> {
return this.client.close(force);
}

isConnected(): boolean {
async isConnected(): Promise<boolean> {
return this.client.isConnected();
}

Expand Down
37 changes: 19 additions & 18 deletions lib/drivers/MySqlDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,24 @@ export class MySqlDriver extends DatabaseDriver {
await this.connection.end({ force });
}

isConnected(): boolean {
// FIXME we need to test this properly
return (this.connection as any).state === 'connected';
async isConnected(): Promise<boolean> {
try {
await this.connection.query('SELECT 1');
return true;
} catch {
return false;
}
}

async begin(savepoint: string = null): Promise<void> {
async begin(): Promise<void> {
await this.connection.beginTransaction();
}

async commit(savepoint: string = null): Promise<void> {
async commit(): Promise<void> {
await this.connection.commit();
}

async rollback(savepoint: string = null): Promise<void> {
async rollback(): Promise<void> {
await this.connection.rollback();
}

Expand All @@ -48,15 +52,15 @@ export class MySqlDriver extends DatabaseDriver {
return res[0][0].count;
}

async find<T extends BaseEntity>(entityName: string, where: FilterQuery<T>, populate: string[], orderBy: { [p: string]: 1 | -1 }, limit: number, offset: number): Promise<T[]> {
async find<T extends BaseEntity>(entityName: string, where: FilterQuery<T>, populate: string[] = [], orderBy: { [p: string]: 1 | -1 } = {}, limit?: number, offset?: number): Promise<T[]> {
const qb = new QueryBuilder(entityName, this.metadata);
qb.select('*').populate(populate).where(where).orderBy(orderBy).limit(limit, offset);
const res = await this.execute(qb);

return res[0];
}

async findOne<T extends BaseEntity>(entityName: string, where: FilterQuery<T> | string, populate: string[]): Promise<T> {
async findOne<T extends BaseEntity>(entityName: string, where: FilterQuery<T> | string, populate: string[] = []): Promise<T> {
if (Utils.isPrimaryKey(where)) {
where = { id: where };
}
Expand Down Expand Up @@ -163,16 +167,13 @@ export class MySqlDriver extends DatabaseDriver {
}

private getConnectionOptions(): ConnectionOptions {
const ret = {} as any;

if (this.options.clientUrl) {
const url = new URL(this.options.clientUrl);
ret.host = url.hostname;
ret.port = url.port;
ret.user = url.username;
ret.password = url.password;
ret.database = url.pathname.replace(/^\//, '');
}
const ret = {} as ConnectionOptions;
const url = new URL(this.options.clientUrl);
ret.host = this.options.host || url.hostname;
ret.port = this.options.port || +url.port;
ret.user = this.options.user || url.username;
ret.password = this.options.password || url.password;
ret.database = this.options.dbName || url.pathname.replace(/^\//, '');

if (this.options.multipleStatements) {
ret.multipleStatements = this.options.multipleStatements;
Expand Down
27 changes: 26 additions & 1 deletion tests/EntityManager.mysql.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Collection, EntityManager, MikroORM } from '../lib';
import { Collection, EntityManager, MikroORM, MySqlDriver } from '../lib';
import { Author2, Publisher2, PublisherType, Book2, BookTag2, Test2 } from './entities-mysql';
import { initORMMySql, wipeDatabaseMySql } from './bootstrap';

Expand All @@ -12,6 +12,31 @@ describe('EntityManagerMySql', () => {
beforeAll(async () => orm = await initORMMySql());
beforeEach(async () => wipeDatabaseMySql(orm.em));

test('isConnected()', async () => {
expect(await orm.isConnected()).toBe(true);
await orm.close(true);
expect(await orm.isConnected()).toBe(false);
await orm.connect();
expect(await orm.isConnected()).toBe(true);
});

test('transactions', async () => {
const god = new Author2('God', 'hello@heaven.god');
await orm.em.getDriver<MySqlDriver>().begin();
await orm.em.persist(god);
await orm.em.getDriver<MySqlDriver>().rollback();
const res1 = await orm.em.findOne(Author2.name, { name: 'God' });
expect(res1).toBeNull();
orm.em.unsetIdentity(god);

await orm.em.getDriver<MySqlDriver>().begin();
const god2 = new Author2('God', 'hello@heaven.god');
await orm.em.persist(god2);
await orm.em.getDriver<MySqlDriver>().commit();
const res2 = await orm.em.findOne(Author2.name, { name: 'God' });
expect(res2).not.toBeNull();
});

test('should load entities', async () => {
expect(orm).toBeInstanceOf(MikroORM);
expect(orm.em).toBeInstanceOf(EntityManager);
Expand Down
4 changes: 2 additions & 2 deletions tests/MikroORM.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ describe('MikroORM', () => {

expect(orm).toBeInstanceOf(MikroORM);
expect(orm.em).toBeInstanceOf(EntityManager);
expect(orm.isConnected()).toBe(true);
expect(await orm.isConnected()).toBe(true);

await orm.close();
expect(orm.isConnected()).toBe(false);
expect(await orm.isConnected()).toBe(false);
});

});
7 changes: 3 additions & 4 deletions tests/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,16 @@ export async function initORM() {
}

export async function initORMMySql() {
let port = '3307';
let port = 3307;

if (process.env.ORM_PORT) {
port = process.env.ORM_PORT;
port = +process.env.ORM_PORT;
}

const orm = await MikroORM.init({
entitiesDirs: ['entities-mysql'],
entitiesDirsTs: ['entities-mysql'], // just to raise coverage :]
dbName: `mikro_orm_test`,
clientUrl: `mysql://root@127.0.0.1:${port}/mikro_orm_test`,
port,
baseDir: __dirname,
driver: MySqlDriver,
debug: true,
Expand Down

0 comments on commit a0a5f77

Please sign in to comment.