testing: fixing tests

This commit is contained in:
Torkel Ödegaard
2017-10-07 22:05:31 +02:00
parent 3519d8d69b
commit b228c23dbe
7 changed files with 154 additions and 214 deletions

View File

@@ -21,50 +21,48 @@ describe('historySrv', function() {
return [200, restoreResponse(parsedData.version)];
});
}));
beforeEach(ctx.createService('historySrv'));
function wrapPromise(ctx, angularPromise) {
return new Promise((resolve, reject) => {
angularPromise.then(resolve, reject);
ctx.$httpBackend.flush();
});
}
describe('getHistoryList', function() {
it('should return a versions array for the given dashboard id', function(done) {
ctx.service.getHistoryList({ id: 1 }).then(function(versions) {
it('should return a versions array for the given dashboard id', function() {
return wrapPromise(ctx, ctx.service.getHistoryList({ id: 1 }).then(function(versions) {
expect(versions).to.eql(versionsResponse);
done();
});
ctx.$httpBackend.flush();
}));
});
it('should return an empty array when not given an id', function(done) {
ctx.service.getHistoryList({ }).then(function(versions) {
it('should return an empty array when not given an id', function() {
return wrapPromise(ctx, ctx.service.getHistoryList({ }).then(function(versions) {
expect(versions).to.eql([]);
done();
});
ctx.$httpBackend.flush();
}));
});
it('should return an empty array when not given a dashboard', function(done) {
ctx.service.getHistoryList().then(function(versions) {
it('should return an empty array when not given a dashboard', function() {
return wrapPromise(ctx, ctx.service.getHistoryList().then(function(versions) {
expect(versions).to.eql([]);
done();
});
ctx.$httpBackend.flush();
}));
});
});
describe('restoreDashboard', function() {
it('should return a success response given valid parameters', function(done) {
var version = 6;
ctx.service.restoreDashboard({ id: 1 }, version).then(function(response) {
it('should return a success response given valid parameters', function() {
let version = 6;
return wrapPromise(ctx, ctx.service.restoreDashboard({ id: 1 }, version).then(function(response) {
expect(response).to.eql(restoreResponse(version));
done();
});
ctx.$httpBackend.flush();
}));
});
it('should return an empty object when not given an id', function(done) {
ctx.service.restoreDashboard({}, 6).then(function(response) {
it('should return an empty object when not given an id', function() {
return wrapPromise(ctx, ctx.service.restoreDashboard({}, 6).then(function(response) {
expect(response).to.eql({});
done();
});
ctx.$httpBackend.flush();
}));
});
});
});

View File

@@ -33,7 +33,7 @@ export class PromCompleter {
return;
}
this.getLabelNameAndValueForMetric(metricName).then(result => {
return this.getLabelNameAndValueForMetric(metricName).then(result => {
var labelNames = this.transformToCompletions(
_.uniq(_.flatten(result.map(r => {
return Object.keys(r.metric);
@@ -42,7 +42,6 @@ export class PromCompleter {
this.labelNameCache[metricName] = labelNames;
callback(null, labelNames);
});
return;
case 'string.quoted':
metricName = this.findMetricName(session, pos.row, pos.column);
if (!metricName) {
@@ -62,7 +61,7 @@ export class PromCompleter {
return;
}
this.getLabelNameAndValueForMetric(metricName).then(result => {
return this.getLabelNameAndValueForMetric(metricName).then(result => {
var labelValues = this.transformToCompletions(
_.uniq(result.map(r => {
return r.metric[labelName];
@@ -72,7 +71,6 @@ export class PromCompleter {
this.labelValueCache[metricName][labelName] = labelValues;
callback(null, labelValues);
});
return;
}
if (prefix === '[') {

View File

@@ -4,111 +4,115 @@ import {PromCompleter} from '../completer';
import {PrometheusDatasource} from '../datasource';
describe('Prometheus editor completer', function() {
function getSessionStub(data) {
return {
getTokenAt: sinon.stub().returns(data.currentToken),
getTokens: sinon.stub().returns(data.tokens),
getLine: sinon.stub().returns(data.line),
};
}
let sessionData = {
currentToken: {},
tokens: [],
line: ''
};
let session = {
getTokenAt: sinon.stub().returns(sessionData.currentToken),
getTokens: sinon.stub().returns(sessionData.tokens),
getLine: sinon.stub().returns(sessionData.line),
};
let editor = { session: session };
let editor = {};
let datasourceStub = <PrometheusDatasource>{
performInstantQuery: sinon.stub().withArgs({ expr: '{__name__="node_cpu"' }).returns(Promise.resolve(
[
{
metric: {
job: 'node',
instance: 'localhost:9100'
}
}
]
)),
performSuggestQuery: sinon.stub().withArgs('node', true).returns(Promise.resolve(
[
'node_cpu'
]
))
performInstantQuery: sinon
.stub()
.withArgs({expr: '{__name__="node_cpu"'})
.returns(
Promise.resolve({
data: {
data: {
result: [
{
metric: {
job: 'node',
instance: 'localhost:9100',
},
},
],
},
},
}),
),
performSuggestQuery: sinon
.stub()
.withArgs('node', true)
.returns(Promise.resolve(['node_cpu'])),
};
let completer = new PromCompleter(datasourceStub);
describe("When inside brackets", () => {
it("Should return range vectors", () => {
completer.getCompletions(editor, session, { row: 0, column: 10 }, '[', (s, res) => {
describe('When inside brackets', () => {
it('Should return range vectors', () => {
const session = getSessionStub({
currentToken: {},
tokens: [],
line: '',
});
completer.getCompletions(editor, session, {row: 0, column: 10}, '[', (s, res) => {
expect(res[0]).to.eql({caption: '1s', value: '[1s', meta: 'range vector'});
});
});
});
describe("When inside label matcher, and located at label name", () => {
sessionData = {
currentToken: { type: 'entity.name.tag', value: 'j', index: 2, start: 9 },
tokens: [
{ type: 'identifier', value: 'node_cpu' },
{ type: 'paren.lparen', value: '{' },
{ type: 'entity.name.tag', value: 'j', index: 2, start: 9 },
{ type: 'paren.rparen', value: '}' }
],
line: 'node_cpu{j}'
};
describe('When inside label matcher, and located at label name', () => {
it('Should return label name list', () => {
const session = getSessionStub({
currentToken: {type: 'entity.name.tag', value: 'j', index: 2, start: 9},
tokens: [
{type: 'identifier', value: 'node_cpu'},
{type: 'paren.lparen', value: '{'},
{type: 'entity.name.tag', value: 'j', index: 2, start: 9},
{type: 'paren.rparen', value: '}'},
],
line: 'node_cpu{j}',
});
it("Should return label name list", () => {
completer.getCompletions(editor, session, { row: 0, column: 10 }, 'j', (s, res) => {
expect(res[0]).to.eql({caption: 'job', value: 'job', meta: 'label name'});
return completer.getCompletions(editor, session, {row: 0, column: 10}, 'j', (s, res) => {
expect(res[0].meta).to.eql('label name');
});
});
});
describe("When inside label matcher, and located at label name with __name__ match", () => {
sessionData = {
currentToken: { type: 'entity.name.tag', value: 'j', index: 5, start: 22 },
tokens: [
{ type: 'paren.lparen', value: '{' },
{ type: 'entity.name.tag', value: '__name__' },
{ type: 'keyword.operator', value: '=~' },
{ type: 'string.quoted', value: '"node_cpu"' },
{ type: 'punctuation.operator', value: ',' },
{ type: 'entity.name.tag', value: 'j', 'index': 5, 'start': 22 },
{ type: 'paren.rparen', value: '}' }
],
line: '{__name__=~"node_cpu",j}'
};
describe('When inside label matcher, and located at label name with __name__ match', () => {
it('Should return label name list', () => {
const session = getSessionStub({
currentToken: {type: 'entity.name.tag', value: 'j', index: 5, start: 22},
tokens: [
{type: 'paren.lparen', value: '{'},
{type: 'entity.name.tag', value: '__name__'},
{type: 'keyword.operator', value: '=~'},
{type: 'string.quoted', value: '"node_cpu"'},
{type: 'punctuation.operator', value: ','},
{type: 'entity.name.tag', value: 'j', index: 5, start: 22},
{type: 'paren.rparen', value: '}'},
],
line: '{__name__=~"node_cpu",j}',
});
it("Should return label name list", () => {
completer.getCompletions(editor, session, { row: 0, column: 23 }, 'j', (s, res) => {
expect(res[0]).to.eql({caption: 'job', value: 'job', meta: 'label name'});
return completer.getCompletions(editor, session, {row: 0, column: 23}, 'j', (s, res) => {
expect(res[0].meta).to.eql('label name');
});
});
});
describe("When inside label matcher, and located at label value", () => {
sessionData = {
currentToken: { type: 'string.quoted', value: '"n"', index: 4, start: 13 },
tokens: [
{ type: 'identifier', value: 'node_cpu' },
{ type: 'paren.lparen', value: '{' },
{ type: 'entity.name.tag', value: 'job' },
{ type: 'keyword.operator', value: '=' },
{ type: 'string.quoted', value: '"n"', index: 4, start: 13 },
{ type: 'paren.rparen', value: '}' }
],
line: 'node_cpu{job="n"}'
};
describe('When inside label matcher, and located at label value', () => {
it('Should return label value list', () => {
const session = getSessionStub({
currentToken: {type: 'string.quoted', value: '"n"', index: 4, start: 13},
tokens: [
{type: 'identifier', value: 'node_cpu'},
{type: 'paren.lparen', value: '{'},
{type: 'entity.name.tag', value: 'job'},
{type: 'keyword.operator', value: '='},
{type: 'string.quoted', value: '"n"', index: 4, start: 13},
{type: 'paren.rparen', value: '}'},
],
line: 'node_cpu{job="n"}',
});
it("Should return label value list", () => {
completer.getCompletions(editor, session, { row: 0, column: 15 }, 'n', (s, res) => {
expect(res[0]).to.eql({caption: 'node', value: 'node', meta: 'label value'});
return completer.getCompletions(editor, session, {row: 0, column: 15}, 'n', (s, res) => {
expect(res[0].meta).to.eql('label value');
});
});
});
});

View File

@@ -103,7 +103,7 @@ define([
};
this.createService = function(name) {
return window.inject(function($q, $rootScope, $httpBackend, $injector, $location) {
return window.inject(function($q, $rootScope, $httpBackend, $injector, $location, $timeout) {
self.$q = $q;
self.$rootScope = $rootScope;
self.$httpBackend = $httpBackend;
@@ -111,6 +111,7 @@ define([
self.$rootScope.onAppEvent = function() {};
self.$rootScope.appEvent = function() {};
self.$timeout = $timeout;
self.service = $injector.get(name);
});