diff --git a/__tests__/DbRelation.js b/__tests__/DbRelation.js index 5e794a6..eb56df6 100644 --- a/__tests__/DbRelation.js +++ b/__tests__/DbRelation.js @@ -48,9 +48,14 @@ test ('good columns is function', () => { const r = new DbRelation ({ name: 't', - columns: function () { return { - id: {type: 'int'}, - }}, + columns: function () { + + expect (this instanceof DbRelation).toBe(true) + + expect (this.name).toBe('t') + + return { id: {type: 'int'} } + }, pk: 'id', }) diff --git a/__tests__/DbTable.js b/__tests__/DbTable.js index d16661c..2f009fc 100644 --- a/__tests__/DbTable.js +++ b/__tests__/DbTable.js @@ -12,13 +12,23 @@ test ('good', () => { const r = new DbTable ({ name: 't', - columns: function () { return { - id: {type: 'int'}, - }}, + columns: function () { + + expect (this instanceof DbTable).toBe(true) + + expect (this.name).toBe('t') + + return { id: {type: 'int'} } + }, pk: 'id', - data: function () { return [{ - id: 1 - }]} + data: function () { + + expect (this instanceof DbTable).toBe(true) + + expect (this.name).toBe('t') + + return [ {id: 1} ] + } }) expect (r.columns.id.type).toBe ('int') diff --git a/__tests__/DbTrigger.js b/__tests__/DbTrigger.js index b9b3c55..f63381d 100644 --- a/__tests__/DbTrigger.js +++ b/__tests__/DbTrigger.js @@ -1,4 +1,5 @@ const DbTrigger = require ('../lib/model/DbTrigger.js') +const DbTable = require ('../lib/model/DbTable.js') const DbModel = require ('../lib/model/DbModel.js') test ('bad', () => { @@ -24,7 +25,15 @@ test ('not bad', () => { pk: ['id'], triggers: [ {name: 't', phase: 'BEFORE UPDATE', sql: 'NULL;'}, - {name: 't1', phase: 'AFTER UPDATE', sql: function () {return 'NULL;'}}, + {name: 't1', phase: 'AFTER UPDATE', sql: function () { + + expect (this instanceof DbTrigger).toBe(true) + expect (this.table instanceof DbTable).toBe(true) + expect (this.table.name).toBe('users') + expect (this.table.model).toStrictEqual (m) + + return 'NULL;' + }}, ] }) @@ -38,4 +47,8 @@ test ('not bad', () => { expect (t1.sql).toBe ('NULL;') + expect (t1.table.name).toBe ('users') + + expect (t1.table.model).toStrictEqual (m) + }) diff --git a/lib/model/DbTrigger.js b/lib/model/DbTrigger.js index 345e088..acc7538 100644 --- a/lib/model/DbTrigger.js +++ b/lib/model/DbTrigger.js @@ -6,7 +6,7 @@ class DbTrigger extends DbObject { super (o) - if (typeof this.sql === 'function') this.sql = this.sql (this.table) + if (typeof this.sql === 'function') this.sql = this.sql () for (const k of ['phase', 'sql']) if (!this [k] || typeof this [k] !== 'string') throw new Error (`${k} must be a non empty string`)