我正在尝试实施策略模式以在PostgreSQL和MongoDB上插入值。
在我的测试类中,连接测试正在运行,但创建不成功:
const { equal, deepEqual, ok } = require('assert');
const Context = require('./../db/strategies/base/contextStrategy');
const Postgres = require('./../db/strategies/postgres');
const context = new Context(new Postgres());
const MOCK_HEROI_CADASTRAR = {
NAME:'New Name Create',
POWER: 'New Power Create'
};
describe('Postgress CRUD tests', function () {
this.timeout(Infinity);
it('Connection', async function (){
const result = await context.isConnected();
equal(result, true);
});
it('Cadastrar', async function (){
const result = await context.create(MOCK_HEROI_CADASTRAR);
console.log('result', result)
deepEqual(result, MOCK_HEROI_CADASTRAR);
});
});
以下是我的PostgreSQL策略,该策略定义为使用isConnected
,create
,delete
,read
和update
实现接口的类:
const ICrud = require('./interfaces/interfaceCrud');
const Sequelize = require('sequelize');
class Postgres extends ICrud{
constructor(){
super();
this._driver = null;
this._herois = null;
this._connect();
}
_connect(){
this._driver = new Sequelize(
'heroes', //database
'gablemos', //user
'root', //password
{
host:'192.168.99.100', //ip host
dialect: 'postgres', //tipo de dialeto da base de dados
quoteIdentifiers: false, //desabilita case sensitive
operatorsAliases: false //desabilita deprecated
}
);
this.defineModel();
}
async defineModel(){
this._herois = this._driver.define('heroes',{
//Colunas e definições das colunas
ID:{
type: Sequelize.INTEGER,
required: true,
primaryKey: true,
autoIncrement: true
},
NAME:{
type: Sequelize.STRING,
required: true,
},
POWER:{
type: Sequelize.STRING,
required: true,
},
}, {
tableName: 'TB_HEROES', //nome da tablea
freezeTableName: false,
timestamps: false
});
}
async isConnected(){
try {
await this._driver.authenticate();
return true;
}catch (error) {
console.error('Impossible to connect'. error);
return false;
}
}
async create(item){
const {dataValues} = await this._herois.create(item);
return dataValues;
}
}
module.exports = Postgres;
我不知道我在哪里做错了什么,但是如果我在策略类上创建MOCK对象并使用它,它就可以正常工作,但是当我将该对象作为参数传递时,就会出现问题,如屏幕截图。
答案 0 :(得分:0)
我的上下文正在接收参数,但是没有使用,比调用create时要使用。