use direct db connection datasource name for provisioning, #614

This commit is contained in:
Alexander Zobnin
2018-08-28 22:34:41 +03:00
parent 2e84893279
commit 8096c0a65e
12 changed files with 158 additions and 21 deletions

View File

@@ -0,0 +1,62 @@
'use strict';
System.register(['../zabbix/connectors/dbConnector'], function (_export, _context) {
"use strict";
var DBConnector;
return {
setters: [function (_zabbixConnectorsDbConnector) {
DBConnector = _zabbixConnectorsDbConnector.default;
}],
execute: function () {
describe('DBConnector', function () {
var ctx = {};
var backendSrvMock = {};
var datasourceSrvMock = {
loadDatasource: jest.fn().mockResolvedValue({ id: 42, name: 'foo', meta: {} }),
getAll: jest.fn().mockReturnValue([{ id: 42, name: 'foo' }])
};
describe('When init DB connector', function () {
beforeEach(function () {
ctx.options = {
datasourceId: 42,
datasourceName: undefined
};
});
it('should load datasource by name by default', function () {
ctx.options = {
datasourceName: 'bar'
};
var dbConnector = new DBConnector(ctx.options, backendSrvMock, datasourceSrvMock);
dbConnector.loadDBDataSource();
expect(datasourceSrvMock.getAll).not.toHaveBeenCalled();
expect(datasourceSrvMock.loadDatasource).toHaveBeenCalledWith('bar');
});
it('should load datasource by id if name not present', function () {
var dbConnector = new DBConnector(ctx.options, backendSrvMock, datasourceSrvMock);
dbConnector.loadDBDataSource();
expect(datasourceSrvMock.getAll).toHaveBeenCalled();
expect(datasourceSrvMock.loadDatasource).toHaveBeenCalledWith('foo');
});
it('should throw error if no name and id specified', function () {
ctx.options = {};
var dbConnector = new DBConnector(ctx.options, backendSrvMock, datasourceSrvMock);
return expect(dbConnector.loadDBDataSource()).rejects.toBe('SQL Data Source name should be specified');
});
it('should throw error if datasource with given id is not found', function () {
ctx.options.datasourceId = 45;
var dbConnector = new DBConnector(ctx.options, backendSrvMock, datasourceSrvMock);
return expect(dbConnector.loadDBDataSource()).rejects.toBe('SQL Data Source with ID 45 not found');
});
});
});
}
};
});
//# sourceMappingURL=dbConnector.test.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../../src/datasource-zabbix/specs/dbConnector.test.js"],"names":["DBConnector","describe","ctx","backendSrvMock","datasourceSrvMock","loadDatasource","jest","fn","mockResolvedValue","id","name","meta","getAll","mockReturnValue","beforeEach","options","datasourceId","datasourceName","undefined","it","dbConnector","loadDBDataSource","expect","not","toHaveBeenCalled","toHaveBeenCalledWith","rejects","toBe"],"mappings":";;;;;;;;AAAOA,iB;;;;AAEPC,eAAS,aAAT,EAAwB,YAAM;AAC5B,YAAIC,MAAM,EAAV;AACA,YAAMC,iBAAiB,EAAvB;AACA,YAAMC,oBAAoB;AACxBC,0BAAgBC,KAAKC,EAAL,GAAUC,iBAAV,CACd,EAAEC,IAAI,EAAN,EAAUC,MAAM,KAAhB,EAAuBC,MAAM,EAA7B,EADc,CADQ;AAIxBC,kBAAQN,KAAKC,EAAL,GAAUM,eAAV,CAA0B,CAChC,EAAEJ,IAAI,EAAN,EAAUC,MAAM,KAAhB,EADgC,CAA1B;AAJgB,SAA1B;;AASAT,iBAAS,wBAAT,EAAmC,YAAM;AACvCa,qBAAW,YAAM;AACfZ,gBAAIa,OAAJ,GAAc;AACZC,4BAAc,EADF;AAEZC,8BAAgBC;AAFJ,aAAd;AAID,WALD;;AAOAC,aAAG,2CAAH,EAAgD,YAAM;AACpDjB,gBAAIa,OAAJ,GAAc;AACZE,8BAAgB;AADJ,aAAd;AAGA,gBAAMG,cAAc,IAAIpB,WAAJ,CAAgBE,IAAIa,OAApB,EAA6BZ,cAA7B,EAA6CC,iBAA7C,CAApB;AACAgB,wBAAYC,gBAAZ;AACAC,mBAAOlB,kBAAkBQ,MAAzB,EAAiCW,GAAjC,CAAqCC,gBAArC;AACAF,mBAAOlB,kBAAkBC,cAAzB,EAAyCoB,oBAAzC,CAA8D,KAA9D;AACD,WARD;;AAUAN,aAAG,kDAAH,EAAuD,YAAM;AAC3D,gBAAMC,cAAc,IAAIpB,WAAJ,CAAgBE,IAAIa,OAApB,EAA6BZ,cAA7B,EAA6CC,iBAA7C,CAApB;AACAgB,wBAAYC,gBAAZ;AACAC,mBAAOlB,kBAAkBQ,MAAzB,EAAiCY,gBAAjC;AACAF,mBAAOlB,kBAAkBC,cAAzB,EAAyCoB,oBAAzC,CAA8D,KAA9D;AACD,WALD;;AAOAN,aAAG,gDAAH,EAAqD,YAAM;AACzDjB,gBAAIa,OAAJ,GAAc,EAAd;AACA,gBAAMK,cAAc,IAAIpB,WAAJ,CAAgBE,IAAIa,OAApB,EAA6BZ,cAA7B,EAA6CC,iBAA7C,CAApB;AACA,mBAAOkB,OAAOF,YAAYC,gBAAZ,EAAP,EAAuCK,OAAvC,CAA+CC,IAA/C,CAAoD,0CAApD,CAAP;AACD,WAJD;;AAMAR,aAAG,6DAAH,EAAkE,YAAM;AACtEjB,gBAAIa,OAAJ,CAAYC,YAAZ,GAA2B,EAA3B;AACA,gBAAMI,cAAc,IAAIpB,WAAJ,CAAgBE,IAAIa,OAApB,EAA6BZ,cAA7B,EAA6CC,iBAA7C,CAApB;AACA,mBAAOkB,OAAOF,YAAYC,gBAAZ,EAAP,EAAuCK,OAAvC,CAA+CC,IAA/C,CAAoD,sCAApD,CAAP;AACD,WAJD;AAKD,SApCD;AAqCD,OAjDD","file":"dbConnector.test.js","sourcesContent":["import DBConnector from '../zabbix/connectors/dbConnector';\n\ndescribe('DBConnector', () => {\n let ctx = {};\n const backendSrvMock = {};\n const datasourceSrvMock = {\n loadDatasource: jest.fn().mockResolvedValue(\n { id: 42, name: 'foo', meta: {} }\n ),\n getAll: jest.fn().mockReturnValue([\n { id: 42, name: 'foo' }\n ])\n };\n\n describe('When init DB connector', () => {\n beforeEach(() => {\n ctx.options = {\n datasourceId: 42,\n datasourceName: undefined\n };\n });\n\n it('should load datasource by name by default', () => {\n ctx.options = {\n datasourceName: 'bar'\n };\n const dbConnector = new DBConnector(ctx.options, backendSrvMock, datasourceSrvMock);\n dbConnector.loadDBDataSource();\n expect(datasourceSrvMock.getAll).not.toHaveBeenCalled();\n expect(datasourceSrvMock.loadDatasource).toHaveBeenCalledWith('bar');\n });\n\n it('should load datasource by id if name not present', () => {\n const dbConnector = new DBConnector(ctx.options, backendSrvMock, datasourceSrvMock);\n dbConnector.loadDBDataSource();\n expect(datasourceSrvMock.getAll).toHaveBeenCalled();\n expect(datasourceSrvMock.loadDatasource).toHaveBeenCalledWith('foo');\n });\n\n it('should throw error if no name and id specified', () => {\n ctx.options = {};\n const dbConnector = new DBConnector(ctx.options, backendSrvMock, datasourceSrvMock);\n return expect(dbConnector.loadDBDataSource()).rejects.toBe('SQL Data Source name should be specified');\n });\n\n it('should throw error if datasource with given id is not found', () => {\n ctx.options.datasourceId = 45;\n const dbConnector = new DBConnector(ctx.options, backendSrvMock, datasourceSrvMock);\n return expect(dbConnector.loadDBDataSource()).rejects.toBe('SQL Data Source with ID 45 not found');\n });\n });\n});\n"]}