Skip to content

Commit

Permalink
add tests for execute
Browse files Browse the repository at this point in the history
  • Loading branch information
hugomontero committed Dec 5, 2023
1 parent b07ef92 commit 1312a53
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/cmd/logic-function.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ module.exports = class LogicFunctionsCommand extends CLICommandBase {
this.ui.stdout.write(` ${index + 1}.- ${JSON.stringify(log)}${os.EOL}`);
});
if (result.err) {
this.stdout.write(this.ui.chalk.red(`Error during Execution:${os.EOL}`));
this.ui.stdout.write(this.ui.chalk.red(`Error during Execution:${os.EOL}`));
this.ui.stdout.write(`${result.err}${os.EOL}`);
} else {
this.ui.stdout.write(this.ui.chalk.cyanBright(`No errors during Execution.${os.EOL}`));
Expand Down
64 changes: 62 additions & 2 deletions src/cmd/logic-function.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ describe('LogicFunctionCommands', () => {
stderr: {
write: sinon.stub()
},
prompt: sinon.stub(),
chalk: {
bold: sinon.stub(),
cyanBright: sinon.stub(),
yellow: sinon.stub(),
grey: sinon.stub(),
red: sinon.stub()
},
};
});
Expand Down Expand Up @@ -177,10 +179,9 @@ describe('LogicFunctionCommands', () => {
logicFunctionCommands.ui.prompt.onCall(1).resolves({ description: 'Logic Function 1' });
let error;
try {
const files = await logicFunctionCommands.create({
await logicFunctionCommands.create({
params: { filepath: PATH_TMP_DIR }
});
console.log(files);
} catch (e) {
error = e;
}
Expand All @@ -189,4 +190,63 @@ describe('LogicFunctionCommands', () => {
});

});

describe('execute', () => {
it('executes a logic function with user provided data', async () => {
nock('https://api.particle.io/v1', )
.intercept('/logic/execute', 'POST')
.reply(200, { result: { status: 'Success', logs: [] } });
await logicFunctionCommands.execute({
params: { filepath: path.join(PATH_FIXTURES_LOGIC_FUNCTIONS, 'lf1_proj') },
data: { foo: 'bar' }
});
expect(logicFunctionCommands.ui.stdout.write.callCount).to.equal(4);
expect(logicFunctionCommands.ui.chalk.bold.callCount).to.equal(1);
expect(logicFunctionCommands.ui.chalk.bold.firstCall.args[0]).to.equal('code.js'); // file name
expect(logicFunctionCommands.ui.chalk.cyanBright.callCount).to.equal(2);
expect(logicFunctionCommands.ui.chalk.cyanBright.firstCall.args[0]).to.equal('Success');
expect(logicFunctionCommands.ui.chalk.cyanBright.secondCall.args[0]).to.equal(`No errors during Execution.${os.EOL}`);
});
it('executes a logic function with user provided data from file', async () => {
nock('https://api.particle.io/v1', )
.intercept('/logic/execute', 'POST')
.reply(200, { result: { status: 'Success', logs: [] } });
await logicFunctionCommands.execute({
params: { filepath: path.join(PATH_FIXTURES_LOGIC_FUNCTIONS, 'lf1_proj') },
dataPath: path.join(PATH_FIXTURES_LOGIC_FUNCTIONS, 'lf1_proj', 'sample', 'data.json')
});
expect(logicFunctionCommands.ui.stdout.write.callCount).to.equal(4);
expect(logicFunctionCommands.ui.chalk.bold.callCount).to.equal(1);
expect(logicFunctionCommands.ui.chalk.bold.firstCall.args[0]).to.equal('code.js'); // file name
expect(logicFunctionCommands.ui.chalk.cyanBright.callCount).to.equal(2);
expect(logicFunctionCommands.ui.chalk.cyanBright.firstCall.args[0]).to.equal('Success');
expect(logicFunctionCommands.ui.chalk.cyanBright.secondCall.args[0]).to.equal(`No errors during Execution.${os.EOL}`);
});
it('executes a logic function with user provided data from file and shows error', async () => {
nock('https://api.particle.io/v1', )
.intercept('/logic/execute', 'POST')
.reply(200, { result: { status: 'Exception', logs: [], err: 'Error message' } });
await logicFunctionCommands.execute({
params: { filepath: path.join(PATH_FIXTURES_LOGIC_FUNCTIONS, 'lf1_proj') },
dataPath: path.join(PATH_FIXTURES_LOGIC_FUNCTIONS, 'lf1_proj', 'sample', 'data.json')
});
expect(logicFunctionCommands.ui.stdout.write.callCount).to.equal(5);
expect(logicFunctionCommands.ui.chalk.bold.firstCall.args[0]).to.equal('code.js'); // file name
expect(logicFunctionCommands.ui.stdout.write.lastCall.args[0]).to.equal(`Error message${os.EOL}`);
});
it('prompts if found multiple files', async () => {
nock('https://api.particle.io/v1', )
.intercept('/logic/execute', 'POST')
.reply(200, { result: { status: 'Success', logs: [] } });
logicFunctionCommands.ui.prompt = sinon.stub();
logicFunctionCommands.ui.prompt.onCall(0).resolves({ file: 'code.js' });
await logicFunctionCommands.execute({
params: { filepath: path.join(PATH_FIXTURES_LOGIC_FUNCTIONS, 'lf2_proj') },
data: { foo: 'bar' }
});
expect(logicFunctionCommands.ui.prompt.callCount).to.equal(1);
expect(logicFunctionCommands.ui.prompt.firstCall.lastArg[0].choices[0].name).to.equal('code.js');
expect(logicFunctionCommands.ui.prompt.firstCall.lastArg[0].choices[1].name).to.equal('code2.js');
});
});
});
7 changes: 7 additions & 0 deletions test/__fixtures__/logic_functions/lf1_proj/code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* @param {import('particle:core').FunctionContext} context
*/
export default function main() {
// Add your code here
console.log('Hello from logic function!');
}
12 changes: 12 additions & 0 deletions test/__fixtures__/logic_functions/lf1_proj/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"$schema": "schemas/logic_function.schema.json",
"logic_function": {
"name": "my logic function",
"description": "my description",
"source": {
"type": "JavaScript"
},
"enabled": true,
"logic_triggers": []
}
}
3 changes: 3 additions & 0 deletions test/__fixtures__/logic_functions/lf1_proj/sample/data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"foo": "bar"
}
7 changes: 7 additions & 0 deletions test/__fixtures__/logic_functions/lf2_proj/code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* @param {import('particle:core').FunctionContext} context
*/
export default function main() {
// Add your code here
console.log('Hello from logic function!');
}
7 changes: 7 additions & 0 deletions test/__fixtures__/logic_functions/lf2_proj/code2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* @param {import('particle:core').FunctionContext} context
*/
export default function main() {
// Add your code here
console.log('Hello from logic function!');
}
12 changes: 12 additions & 0 deletions test/__fixtures__/logic_functions/lf2_proj/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"$schema": "schemas/logic_function.schema.json",
"logic_function": {
"name": "my logic function",
"description": "my description",
"source": {
"type": "JavaScript"
},
"enabled": true,
"logic_triggers": []
}
}

0 comments on commit 1312a53

Please sign in to comment.