Add and update unit tests for new create cloud variable functionality.

This commit is contained in:
Karishma Chadha 2018-11-14 15:46:51 -05:00
parent 3fc1b89b30
commit fad4d381bb
2 changed files with 119 additions and 6 deletions
test/unit

View file

@ -2,14 +2,18 @@ const test = require('tap').test;
const Cloud = require('../../src/io/cloud');
const Target = require('../../src/engine/target');
const Variable = require('../../src/engine/variable');
const Runtime = require('../../src/engine/runtime');
test('spec', t => {
const cloud = new Cloud();
const runtime = new Runtime();
const cloud = new Cloud(runtime);
t.type(cloud, 'object');
t.type(cloud.postData, 'function');
t.type(cloud.requestUpdateVariable, 'function');
t.type(cloud.updateCloudVariable, 'function');
t.type(cloud.requestCreateCloudVariable, 'function');
t.type(cloud.createCloudVariable, 'function');
t.type(cloud.setProvider, 'function');
t.type(cloud.setStage, 'function');
t.type(cloud.clear, 'function');
@ -17,7 +21,8 @@ test('spec', t => {
});
test('stage and provider are null initially', t => {
const cloud = new Cloud();
const runtime = new Runtime();
const cloud = new Cloud(runtime);
t.strictEquals(cloud.provider, null);
t.strictEquals(cloud.stage, null);
@ -25,7 +30,8 @@ test('stage and provider are null initially', t => {
});
test('setProvider sets the provider', t => {
const cloud = new Cloud();
const runtime = new Runtime();
const cloud = new Cloud(runtime);
const provider = {
foo: 'a fake provider'
@ -37,7 +43,7 @@ test('setProvider sets the provider', t => {
t.end();
});
test('postData updates the variable', t => {
test('postData update message updates the variable', t => {
const stage = new Target();
const fooVar = new Variable(
'a fake var id',
@ -49,7 +55,8 @@ test('postData updates the variable', t => {
t.strictEquals(fooVar.value, 0);
const cloud = new Cloud();
const runtime = new Runtime();
const cloud = new Cloud(runtime);
cloud.setStage(stage);
cloud.postData({varUpdate: {
name: 'foo',
@ -59,6 +66,30 @@ test('postData updates the variable', t => {
t.end();
});
test('postData create message sets isCloud flag on the variable and updates runtime cloud limit', t => {
const stage = new Target();
const fooVar = new Variable(
'a fake var id',
'foo',
Variable.SCALAR_TYPE,
false /* isCloud */
);
stage.variables[fooVar.id] = fooVar;
t.strictEquals(fooVar.value, 0);
t.strictEquals(fooVar.isCloud, false);
const runtime = new Runtime();
const cloud = new Cloud(runtime);
cloud.setStage(stage);
cloud.postData({varCreate: {
name: 'foo'
}});
t.strictEquals(fooVar.isCloud, true);
t.strictEquals(runtime.hasCloudData(), true);
t.end();
});
test('requestUpdateVariable calls provider\'s updateVariable function', t => {
let updateVariableCalled = false;
let mockVarName = '';
@ -74,7 +105,8 @@ test('requestUpdateVariable calls provider\'s updateVariable function', t => {
updateVariable: mockUpdateVariable
};
const cloud = new Cloud();
const runtime = new Runtime();
const cloud = new Cloud(runtime);
cloud.setProvider(provider);
cloud.requestUpdateVariable('foo', 3);
t.equals(updateVariableCalled, true);
@ -82,3 +114,31 @@ test('requestUpdateVariable calls provider\'s updateVariable function', t => {
t.strictEquals(mockVarValue, 3);
t.end();
});
test('requestCreateCloudVariable calls provider\'s createVariable function', t => {
let createVariableCalled = false;
const mockVariable = new Variable('a var id', 'my var', Variable.SCALAR_TYPE, false);
let mockVarName;
let mockVarValue;
const mockCreateVariable = (name, value) => {
createVariableCalled = true;
mockVarName = name;
mockVarValue = value;
return;
};
const provider = {
createVariable: mockCreateVariable
};
const runtime = new Runtime();
const cloud = new Cloud(runtime);
cloud.setProvider(provider);
cloud.requestCreateCloudVariable(mockVariable);
t.equals(createVariableCalled, true);
t.strictEquals(mockVarName, 'my var');
t.strictEquals(mockVarValue, 0);
// Calling requestCreateCloudVariable does not set isCloud flag on variable
t.strictEquals(mockVariable.isCloud, false);
t.end();
});