Skip to content

Commit

Permalink
feat(lib): update script content on PATCH
Browse files Browse the repository at this point in the history
Relates to #2
  • Loading branch information
Niklas Kiefer committed Mar 22, 2019
1 parent c2fe219 commit 365e026
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 6 deletions.
22 changes: 22 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Fython {
}

create(params) {

let args = []; // TODO: Store this as array of object like '-c param1'
for (var k in params){
if (params.hasOwnProperty(k)) {
Expand Down Expand Up @@ -57,6 +58,27 @@ class Fython {
});
});
}

patch(id, data, params) {

const {
content
} = data;

if (!content) {
throw new Error('content must be provided!');
}

return new Promise((resolve, reject) => {
fs.writeFile(this._pythonOptions.scriptPath, content, (err) => {
if (err) {
reject(err);
}

resolve(content);
});
});
}
}

module.exports = Fython;
68 changes: 62 additions & 6 deletions test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,58 @@
const expect = require('chai').expect;
const feathers = require('@feathersjs/feathers');
const fs = require('fs');
const logger = require('winston');

const FythonService = require('../lib');

describe('fython service', () => {
const testScript = `
import sys as sys
if len(sys.argv) > 1:
print("Hello " + sys.argv[1] + "!")
else:
print("Hello World!")
`;

const testScriptPath = 'test/helloWorld.py';

describe('fython service', (done) => {
let app, service;

beforeEach(() => {
beforeEach((done) => {
// given
app = feathers();
app.use('/pythonScript', new FythonService({scriptPath: 'test/helloWorld.py'}));
service = app.service('pythonScript');

// assure
expect(service).to.not.be.undefined;
fs.writeFile(testScriptPath, testScript, (err) => {

if(err) {
expect.fail("error appeared in setup: " + err);
}

app.use('/pythonScript', new FythonService({scriptPath: testScriptPath}));
service = app.service('pythonScript');

// assure
expect(service).to.not.be.undefined;

done();
});
});

afterEach((done) => {
// cleanup
fs.unlink(testScriptPath, (err) => {
if (err) {
expect.fail("error appeared in teardown: " + err);
}

done();
});
});

describe('POST', function() {

it('should execute the python script properly', () => {
// when
return service.create({}).then((result) => {
// then
expect(result).to.contain('Hello World!');
Expand All @@ -28,6 +61,7 @@ describe('fython service', () => {


it('should execute the python script with correct first param', () => {
// when
return service.create({
param1: 'Test'
}).then((result) => {
Expand All @@ -40,12 +74,34 @@ describe('fython service', () => {


describe('GET', function() {
// when
it('return script content', () => {
return service.find({}).then((result) => {
// then
expect(result).to.contains('import sys as sys');
});
});
});


describe('PATCH', function() {
// given
const content = '<foo>';

// when
it('update script content', () => {
return service.patch(null, {
content
}).then((result) => {
// then
expect(result).to.eql(content);

return service.find({}).then((result) => {
// then
expect(result).to.eql(content);
});
});
});
});

});

0 comments on commit 365e026

Please sign in to comment.