Skip to content

Commit

Permalink
chore: add some coverage ignore comments
Browse files Browse the repository at this point in the history
Mostly edge cases for driver usage with table names instead of entity names.
  • Loading branch information
B4nan committed Nov 5, 2022
1 parent 8f8f820 commit 3575307
Show file tree
Hide file tree
Showing 14 changed files with 38 additions and 4 deletions.
1 change: 1 addition & 0 deletions packages/core/src/entity/Reference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ export class Reference<T> {
return wrap(this.entity).toJSON!(...args);
}

/* istanbul ignore next */
[inspect.custom](depth: number) {
const object = { ...this };
const hidden = ['meta'];
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/types/ArrayType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export class ArrayType<T extends string | number = string> extends Type<T[] | nu
return platform.marshallArray(value as string[]);
}

/* istanbul ignore next */
if (typeof context === 'boolean' ? context : context?.fromQuery) {
return value;
}
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/types/Type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export abstract class Type<JSType = string, DBType = JSType> {
const ret = inspect(object, { depth });
const name = (this as object).constructor.name;

/* istanbul ignore next */
return ret === '[Object]' ? `[${name}]` : name + ' ' + ret;
}

Expand Down
1 change: 1 addition & 0 deletions packages/core/src/unit-of-work/ChangeSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export class ChangeSet<T> {
const ret = inspect(object, { depth });
const name = `${this.constructor.name}<${this.meta.className}>`;

/* istanbul ignore next */
return ret === '[Object]' ? `[${name}]` : name + ' ' + ret;
}

Expand Down
3 changes: 1 addition & 2 deletions packages/core/src/utils/Configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,7 @@ export class Configuration<D extends IDatabaseDriver = IDatabaseDriver> {
fileName: (className: string) => className,
},
preferReadReplicas: true,
/* istanbul ignore next */
dynamicImportProvider: (id: string) => import(id),
dynamicImportProvider: /* istanbul ignore next */ (id: string) => import(id),
};

static readonly PLATFORMS = {
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/utils/ConfigurationLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ export class ConfigurationLoader {

const distDir = pathExistsSync(process.cwd() + '/dist');
const buildDir = pathExistsSync(process.cwd() + '/build');
/* istanbul ignore next */
const path = distDir ? 'dist' : (buildDir ? 'build' : 'src');
paths.push(`./${path}/mikro-orm.config.js`);
paths.push('./mikro-orm.config.js');
Expand Down Expand Up @@ -245,7 +246,7 @@ export class ConfigurationLoader {
}

/** @internal */
static async commonJSCompat(options: Partial<Options> = {}): Promise<void> {
static async commonJSCompat(options: Partial<Options>): Promise<void> {
if (await this.isESM()) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/utils/TransactionContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class TransactionContext {
/**
* Returns current EntityManager (if available).
*/
static getEntityManager(name = 'default'): EntityManager | undefined {
static getEntityManager(name = /* istanbul ignore next */ 'default'): EntityManager | undefined {
const context = TransactionContext.currentTransactionContext();
return context?.em.name === name ? context.em : undefined;
}
Expand Down
5 changes: 5 additions & 0 deletions packages/knex/src/AbstractSqlDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ export abstract class AbstractSqlDriver<C extends AbstractSqlConnection = Abstra
if (pks.length > 1) { // owner has composite pk
pk = Utils.getPrimaryKeyCond(data as T, pks);
} else {
/* istanbul ignore next */
res.insertId = data[pks[0]] ?? res.insertId ?? res.row[pks[0]];
pk = [res.insertId];
}
Expand All @@ -322,6 +323,7 @@ export abstract class AbstractSqlDriver<C extends AbstractSqlConnection = Abstra
const fields = Utils.flatten(props.map(prop => prop.fieldNames));
const params: unknown[] = [];

/* istanbul ignore next */
const tableName = meta ? this.getTableName(meta, options) : this.platform.quoteIdentifier(entityName);
let sql = `insert into ${tableName} `;
sql += fields.length > 0 ? '(' + fields.map(k => this.platform.quoteIdentifier(k)).join(', ') + ')' : `(${this.platform.quoteIdentifier(pks[0])})`;
Expand Down Expand Up @@ -388,6 +390,7 @@ export abstract class AbstractSqlDriver<C extends AbstractSqlConnection = Abstra
let res = { affectedRows: 0, insertId: 0, row: {} } as QueryResult<T>;

if (Utils.isPrimaryKey(where) && pks.length === 1) {
/* istanbul ignore next */
where = { [meta?.primaryKeys[0] ?? pks[0]]: where } as FilterQuery<T>;
}

Expand All @@ -396,7 +399,9 @@ export abstract class AbstractSqlDriver<C extends AbstractSqlConnection = Abstra
.withSchema(this.getSchemaName(meta, options));

if (options.upsert) {
/* istanbul ignore next */
const uniqueFields = Utils.isPlainObject(where) ? Object.keys(where) : meta!.primaryKeys;
/* istanbul ignore next */
qb.insert(data as T)
.onConflict(uniqueFields.map(p => meta?.properties[p]?.fieldNames[0] ?? p))
.merge(Object.keys(data).filter(f => !uniqueFields.includes(f)));
Expand Down
1 change: 1 addition & 0 deletions packages/knex/src/query/QueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,7 @@ export class QueryBuilder<T extends object = AnyEntity> {
const [a, f] = this.helper.splitField(field);
const prop = this.helper.getProperty(f, a);

/* istanbul ignore next */
if (prop && [ReferenceType.ONE_TO_MANY, ReferenceType.MANY_TO_MANY].includes(prop.reference)) {
return;
}
Expand Down
1 change: 1 addition & 0 deletions packages/mariadb/src/MariaDbPlatform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export class MariaDbPlatform extends AbstractSqlPlatform {
}

getFullTextIndexExpression(indexName: string, schemaName: string | undefined, tableName: string, columns: SimpleColumnMeta[]): string {
/* istanbul ignore next */
const quotedTableName = this.quoteIdentifier(schemaName ? `${schemaName}.${tableName}` : tableName);
const quotedColumnNames = columns.map(c => this.quoteIdentifier(c.name));
const quotedIndexName = this.quoteIdentifier(indexName);
Expand Down
1 change: 1 addition & 0 deletions packages/mongodb/src/MongoDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export class MongoDriver extends DatabaseDriver<MongoConnection> {
async findOne<T extends object, P extends string = never>(entityName: string, where: FilterQuery<T>, options: FindOneOptions<T, P> = { populate: [], orderBy: {} }): Promise<EntityData<T> | null> {
if (this.metadata.find(entityName)?.virtual) {
const [item] = await this.findVirtual(entityName, where, options as FindOptions<T, any>);
/* istanbul ignore next */
return item ?? null;
}

Expand Down
1 change: 1 addition & 0 deletions packages/mysql/src/MySqlPlatform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export class MySqlPlatform extends AbstractSqlPlatform {
}

getFullTextIndexExpression(indexName: string, schemaName: string | undefined, tableName: string, columns: SimpleColumnMeta[]): string {
/* istanbul ignore next */
const quotedTableName = this.quoteIdentifier(schemaName ? `${schemaName}.${tableName}` : tableName);
const quotedColumnNames = columns.map(c => this.quoteIdentifier(c.name));
const quotedIndexName = this.quoteIdentifier(indexName);
Expand Down
1 change: 1 addition & 0 deletions packages/mysql/src/MySqlSchemaHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ export class MySqlSchemaHelper extends SchemaHelper {
return ret;
}

/* istanbul ignore next kept for BC */
getForeignKeysSQL(tableName: string, schemaName?: string): string {
return `select distinct k.constraint_name as constraint_name, k.column_name as column_name, k.referenced_table_name as referenced_table_name, k.referenced_column_name as referenced_column_name, c.update_rule as update_rule, c.delete_rule as delete_rule `
+ `from information_schema.key_column_usage k `
Expand Down
20 changes: 20 additions & 0 deletions tests/QueryBuilder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2419,6 +2419,26 @@ describe('QueryBuilder', () => {
expect(qb.getParams()).toEqual(['baz', 1]);
}

{
const qb = pg.em.createQueryBuilder(FooBar2, 'fb1');
qb.select('*')
.distinct()
.joinAndSelect('fb1.baz', 'fz')
.leftJoinAndSelect('fz.bar', 'fb2')
.where({ 'fz.name': 'baz' })
.limit(1);
const sql = 'select distinct "fb1".*, ' +
'"fz"."id" as "fz__id", "fz"."name" as "fz__name", "fz"."version" as "fz__version", ' +
'"fb2"."id" as "fb2__id", "fb2"."name" as "fb2__name", "fb2"."name with space" as "fb2__name with space", "fb2"."baz_id" as "fb2__baz_id", "fb2"."foo_bar_id" as "fb2__foo_bar_id", "fb2"."version" as "fb2__version", "fb2"."blob" as "fb2__blob", "fb2"."array" as "fb2__array", "fb2"."object_property" as "fb2__object_property", (select 123) as "fb2__random", ' +
'(select 123) as "random" from "foo_bar2" as "fb1" ' +
'inner join "foo_baz2" as "fz" on "fb1"."baz_id" = "fz"."id" ' +
'left join "foo_bar2" as "fb2" on "fz"."id" = "fb2"."baz_id" ' +
'where "fz"."name" = $1 ' +
'limit $2';
expect(qb.getQuery()).toEqual(sql);
expect(qb.getParams()).toEqual(['baz', 1]);
}

const qb01 = pg.em.createQueryBuilder(FooBar2);
qb01.insert({ array: [] } as any);
expect(qb01.getFormattedQuery()).toEqual(`insert into "foo_bar2" ("array") values ('{}') returning "id", "version"`);
Expand Down

0 comments on commit 3575307

Please sign in to comment.