Skip to content

Commit

Permalink
Merge pull request #13 from multum/develop
Browse files Browse the repository at this point in the history
Simplify tests using snapshots
  • Loading branch information
multum authored Feb 28, 2020
2 parents a7c7201 + c18d1a6 commit 222a4ec
Show file tree
Hide file tree
Showing 7 changed files with 302 additions and 91 deletions.
7 changes: 7 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

const config = {
coverageReporters: ['lcov', 'text-summary'],
};

module.exports = config;
16 changes: 15 additions & 1 deletion lib/dialects/mysql/tables/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,26 @@ exports.columns = columns => {

column = {
name: column_name,
type: { name: data_type, raw: column_type },
type: { name: data_type },
default: column_default,
nullable: is_nullable === 'YES',
identity: auto_increment === 'YES',
};

if (
/^smallint|mediumint|int|bigint/i.test(column_type) &&
column_type.indexOf('zerofill') === -1
) {
/**
* Removing unnecessary parameter for integers in older versions of `mysql`
* @example `bigint(20)` => `bigint`
* @example `bigint(20) zerofill` => `bigint(20) zerofill`
*/
column.type.raw = column_type.replace(/\(.*\)/, '');
} else {
column.type.raw = column_type;
}

if (utils.exists(datetime_precision)) {
column.type.precision = datetime_precision;
} else if (utils.exists(character_maximum_length)) {
Expand Down
6 changes: 0 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,5 @@
"prettier --write",
"git add"
]
},
"jest": {
"coverageReporters": [
"lcov",
"text-summary"
]
}
}
83 changes: 8 additions & 75 deletions test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ const _query = (client, queries) => {
);
};

const _getTableColumn = (table, column) => {
return table && table.columns.find(({ name }) => name === column);
};

exports.setup = ({
schema,
dialect,
Expand All @@ -39,8 +35,6 @@ exports.setup = ({
unique: 'users_u_constraint',
};

const prefix = schema ? `[ ${schema} ] ` : '';

const metalize = new Metalize({ dialect, connectionConfig });

beforeAll(() => {
Expand Down Expand Up @@ -84,78 +78,17 @@ exports.setup = ({
return _query(metalize._client, queries);
});

it(`${prefix}reading tables`, async () => {
const result = await metalize.read({ tables: [table] });
const _table = result.tables.get(table);

expect(_table.primaryKey).toBeDefined();
expect(typeof _table.primaryKey.name).toBe('string');
expect(_table.primaryKey).toMatchObject({
columns: ['id'],
});

expect(_table.columns).toHaveLength(5);

expect(_getTableColumn(_table, 'name')).toMatchObject({
type: {
name: isPostgres ? 'character varying' : 'varchar',
length: 255,
},
default: isPostgres ? "'noname'::character varying" : 'noname',
});

expect(_getTableColumn(_table, 'budget')).toMatchObject({
type: {
name: isPostgres ? 'numeric' : 'decimal',
precision: 16,
scale: 3,
},
});

expect(_table.foreignKeys[0]).toBeDefined();
expect(_table.foreignKeys[0]).toMatchObject({
name: _constraintNames.foreignKey,
columns: ['id', 'child'],
references: {
table: childTable,
columns: ['parent', 'id'],
},
onUpdate: 'RESTRICT',
onDelete: 'CASCADE',
});

const ageColumn = _getTableColumn(_table, 'age');
expect(ageColumn).toBeDefined();
if (isPostgres) {
expect(ageColumn.identity).toMatchObject({
start: '100',
min: '100',
max: '9999',
cycle: false,
});
} else {
expect(ageColumn.identity).toBeTruthy();
}

if (isPostgres) {
expect(_table.checks[0]).toBeDefined();
expect(_table.checks[0].name).toEqual(_constraintNames.check);
expect(typeof _table.checks[0].condition).toEqual('string');
}
it(`[ reading tables ]`, function() {
return expect(
metalize.read({ tables: [table] })
).resolves.toMatchSnapshot();
});

if (isPostgres) {
it(`${prefix}reading sequences`, async () => {
const result = await metalize.read({ sequences: [sequence] });
const _sequence = result.sequences.get(sequence);

expect(_sequence).toMatchObject({
start: '100',
min: '100',
max: '9999',
increment: '1',
cycle: true,
});
it(`[ reading sequences ]`, function() {
return expect(
metalize.read({ sequences: [sequence] })
).resolves.toMatchSnapshot();
});
}

Expand Down
121 changes: 121 additions & 0 deletions test/mysql/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`'mysql' dialect [ reading tables ] 1`] = `
Object {
"sequences": Map {},
"tables": Map {
"metalize_schema.users" => Object {
"columns": Array [
Object {
"default": null,
"identity": false,
"name": "id",
"nullable": false,
"type": Object {
"name": "bigint",
"precision": 19,
"raw": "bigint",
"scale": 0,
},
},
Object {
"default": "noname",
"identity": false,
"name": "name",
"nullable": true,
"type": Object {
"length": 255,
"name": "varchar",
"raw": "varchar(255)",
},
},
Object {
"default": null,
"identity": false,
"name": "budget",
"nullable": true,
"type": Object {
"name": "decimal",
"precision": 16,
"raw": "decimal(16,3)",
"scale": 3,
},
},
Object {
"default": null,
"identity": true,
"name": "age",
"nullable": false,
"type": Object {
"name": "bigint",
"precision": 19,
"raw": "bigint",
"scale": 0,
},
},
Object {
"default": null,
"identity": false,
"name": "child",
"nullable": true,
"type": Object {
"name": "bigint",
"precision": 19,
"raw": "bigint",
"scale": 0,
},
},
],
"foreignKeys": Array [
Object {
"columns": Array [
"id",
"child",
],
"match": "NONE",
"name": "users_f_constraint",
"onDelete": "CASCADE",
"onUpdate": "RESTRICT",
"references": Object {
"columns": Array [
"parent",
"id",
],
"table": "metalize_schema.users_child",
},
},
],
"indexes": Array [
Object {
"columns": Array [
"id",
"child",
],
"name": "index_name",
},
Object {
"columns": Array [
"age",
],
"name": "age_index",
},
],
"primaryKey": Object {
"columns": Array [
"id",
],
"name": "PRIMARY",
},
"unique": Array [
Object {
"columns": Array [
"name",
"age",
],
"name": "users_u_constraint",
},
],
},
},
}
`;
Loading

0 comments on commit 222a4ec

Please sign in to comment.