Skip to content

Commit

Permalink
(feature) Add generateText from data to command line and testing (#349)
Browse files Browse the repository at this point in the history
* (feature) Add generateText from data to command line and testing

Signed-off-by: Jerome Simeon <[email protected]>

* (fix) Add testing for CLI's generateText

Signed-off-by: Jerome Simeon <[email protected]>

* (chore) Bump to Ergo 0.8.1

Signed-off-by: Jerome Simeon <[email protected]>
  • Loading branch information
jeromesimeon authored and dselman committed Apr 30, 2019
1 parent 1338a78 commit 622238e
Show file tree
Hide file tree
Showing 13 changed files with 390 additions and 36 deletions.
41 changes: 13 additions & 28 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions packages/cicero-cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,38 @@ require('yargs')
return;
}
})
.command('generateText', 'generate sample text from template data', (yargs) => {
yargs.option('template', {
describe: 'path to the directory with the template',
type: 'string'
});
yargs.option('data', {
describe: 'path to the template data text',
type: 'string'
});
yargs.option('out', {
describe: 'path to the output file',
type: 'string'
});
}, (argv) => {
if (argv.verbose) {
Logger.info(`generate text from data ${argv.data} using a template ${argv.template}`);
}

try {
argv = Commands.validateGenerateTextArgs(argv);
return Commands.generateText(argv.template, argv.data, argv.out)
.then((result) => {
Logger.info(result);
})
.catch((err) => {
Logger.error(err.message);
});
} catch (err){
Logger.error(err.message);
return;
}
})
.command('archive', 'archive a template directory', (yargs) => {
yargs.option('target', {
describe: 'the target language of the archive',
Expand Down
46 changes: 45 additions & 1 deletion packages/cicero-cli/lib/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ class Commands {
clause.parse(sampleText, currentTime);
if (outPath) {
Logger.info('Creating file: ' + outPath);
fs.writeFileSync(outPath, JSON.stringify(clause.getData()));
fs.writeFileSync(outPath, JSON.stringify(clause.getData(),null,2));
}
return clause.getData();
})
Expand All @@ -159,6 +159,33 @@ class Commands {
});
}

/**
* Generate a sample text from JSON data using a template
*
* @param {string} templatePath to the template path directory or file
* @param {string} dataPath to the data file
* @param {string} outPath to the contract file
* @returns {object} Promise to the result of parsing
*/
static generateText(templatePath, dataPath, outPath) {
let clause;
const dataJson = JSON.parse(fs.readFileSync(dataPath, 'utf8'));

return Commands.loadTemplate(templatePath)
.then((template) => {
clause = new Clause(template);
clause.setData(dataJson);
if (outPath) {
Logger.info('Creating file: ' + outPath);
fs.writeFileSync(outPath, clause.generateText());
}
return clause.generateText();
})
.catch((err) => {
Logger.error(err.message);
});
}

/**
* Set default params before we parse a sample text using a template
*
Expand All @@ -176,6 +203,23 @@ class Commands {
return argv;
}

/**
* Set default params before we generatet a sample text using a template
*
* @param {object} argv the inbound argument values object
* @returns {object} a modfied argument object
*/
static validateGenerateTextArgs(argv) {
argv = Commands.validateCommonArgs(argv);
argv = Commands.setDefaultFileArg(argv, 'data', 'data.json', ((argv, argDefaultName) => { return path.resolve(argv.template,argDefaultName); }));

if(argv.verbose) {
Logger.info(`generate text from data ${argv.data} using a template ${argv.template}`);
}

return argv;
}

/**
* Initializes a sample text using a template
*
Expand Down
121 changes: 119 additions & 2 deletions packages/cicero-cli/test/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@ describe('cicero-cli', () => {
const templateJs = path.resolve(__dirname, 'data/latedeliveryandpenalty_js/');
const templateArchive = path.resolve(__dirname, 'data/latedeliveryandpenalty.cta');
const sample = path.resolve(__dirname, 'data/latedeliveryandpenalty/', 'sample.txt');
const data = path.resolve(__dirname, 'data/latedeliveryandpenalty/', 'data.json');
const request = path.resolve(__dirname, 'data/latedeliveryandpenalty/', 'request.json');
const state = path.resolve(__dirname, 'data/latedeliveryandpenalty/', 'state.json');
const contract = path.resolve(__dirname, 'data/latedeliveryandpenalty/', 'contract.json');
const dataOut = path.resolve(__dirname, 'data/latedeliveryandpenalty/', 'data_out.json');
const sampleOut = path.resolve(__dirname, 'data/latedeliveryandpenalty/', 'sample_out.txt');
const parseReponse = {
'$class':'org.accordproject.latedeliveryandpenalty.TemplateModel',
'forceMajeure':true,
Expand All @@ -53,8 +55,10 @@ describe('cicero-cli', () => {
},
'fractionalPart':'days'
};
const generateTextResponse = 'Late Delivery and Penalty. In case of delayed delivery except for Force Majeure cases, the Seller shall pay to the Buyer for every 9 days of delay penalty amounting to 7% of the total value of the Equipment whose delivery has been delayed. Any fractional part of a days is to be considered a full days. The total amount of penalty shall not however, exceed 2% of the total value of the Equipment involved in late delivery. If the delay is more than 2 weeks, the Buyer is entitled to terminate this Contract.';

const sampleErr = path.resolve(__dirname, 'data/latedeliveryandpenalty/', 'sample_err.txt');
const dataErr = path.resolve(__dirname, 'data/latedeliveryandpenalty/', 'data_err.json');
const stateErr = path.resolve(__dirname, 'data/latedeliveryandpenalty/', 'state_err.json');
const requestErr = path.resolve(__dirname, 'data/latedeliveryandpenalty/', 'request_err.json');

Expand All @@ -79,12 +83,39 @@ describe('cicero-cli', () => {

describe('#parsesave', async () => {
it('should parse a clause using a template and save to a JSON file', async () => {
const result = await Commands.parse(template, sample, contract);
const result = await Commands.parse(template, sample, dataOut);
delete result.clauseId;
result.should.eql(parseReponse);
});
});

describe('#generateText', () => {
it('should generate the text for a clause using a template', async () => {
const result = await Commands.generateText(template, data, null);
delete result.clauseId;
result.should.eql(generateTextResponse);
});

it('should generate the text for a clause using a template archive', async () => {
const result = await Commands.generateText(templateArchive, data, null);
delete result.clauseId;
result.should.eql(generateTextResponse);
});

it('should fail generating the text for a clause using a template', async () => {
const result = await Commands.generateText(template, dataErr, null);
should.equal(result,undefined);
});
});

describe('#generateTextsave', async () => {
it('should generate the text for a clause using a template and save to a JSON file', async () => {
const result = await Commands.generateText(template, data, sampleOut);
delete result.clauseId;
result.should.eql(generateTextResponse);
});
});

describe('#archive', async () => {
it('should create a valid ergo archive', async () => {
const archiveName = 'test.cta';
Expand Down Expand Up @@ -184,6 +215,92 @@ describe('cicero-cli', () => {
});
});

describe('#validateGenerateTextArgs', () => {
it('no args specified', () => {
process.chdir(path.resolve(__dirname, 'data/latedeliveryandpenalty/'));
const args = Commands.validateGenerateTextArgs({
_: ['generateText'],
});
args.template.should.match(/cicero-cli\/test\/data\/latedeliveryandpenalty$/);
args.data.should.match(/data.json$/);
});
it('all args specified', () => {
process.chdir(path.resolve(__dirname, 'data/latedeliveryandpenalty/'));
const args = Commands.validateGenerateTextArgs({
_: ['generateText'],
template: './',
data: 'data.json'
});
args.template.should.match(/cicero-cli\/test\/data\/latedeliveryandpenalty$/);
args.data.should.match(/data.json$/);
});
it('all args specified, parent folder', () => {
process.chdir(path.resolve(__dirname, 'data/'));
const args = Commands.validateGenerateTextArgs({
_: ['generateText'],
template: 'latedeliveryandpenalty',
data: 'latedeliveryandpenalty/data.json'
});
args.template.should.match(/cicero-cli\/test\/data\/latedeliveryandpenalty$/);
args.data.should.match(/data.json$/);
});
it('all args specified, archive', () => {
process.chdir(path.resolve(__dirname, 'data/'));
const args = Commands.validateGenerateTextArgs({
_: ['generateText'],
template: 'latedeliveryandpenalty.cta',
data: 'latedeliveryandpenalty/data.json'
});
args.template.should.match(/cicero-cli\/test\/data\/latedeliveryandpenalty.cta$/);
args.data.should.match(/data.json$/);
});
it('all args specified, parent folder, no sample', () => {
process.chdir(path.resolve(__dirname, 'data/'));
const args = Commands.validateGenerateTextArgs({
_: ['generateText'],
template: 'latedeliveryandpenalty',
});
args.template.should.match(/cicero-cli\/test\/data\/latedeliveryandpenalty$/);
args.data.should.match(/data.json$/);
});
it('all args specified, child folder, no sample', () => {
process.chdir(path.resolve(__dirname, 'data/latedeliveryandpenalty/grammar'));
const args = Commands.validateGenerateTextArgs({
_: ['generateText'],
template: '../',
});
args.template.should.match(/cicero-cli\/test\/data\/latedeliveryandpenalty$/);
args.data.should.match(/data.json$/);
});
it('no flags specified', () => {
const args = Commands.validateGenerateTextArgs({
_: ['generateText', path.resolve(__dirname, 'data/latedeliveryandpenalty/')],
});
args.template.should.match(/cicero-cli\/test\/data\/latedeliveryandpenalty$/);
args.data.should.match(/data.json$/);
});
it('verbose flag specified', () => {
process.chdir(path.resolve(__dirname, 'data/latedeliveryandpenalty/'));
Commands.validateGenerateTextArgs({
_: ['generateText'],
verbose: true
});
});
it('bad package.json', () => {
process.chdir(path.resolve(__dirname, 'data/'));
(() => Commands.validateGenerateTextArgs({
_: ['generateText'],
})).should.throw(' not a valid cicero template. Make sure that package.json exists and that it has a cicero entry.');
});
it('bad data.json', () => {
process.chdir(path.resolve(__dirname, 'data/latedeliveryandpenalty/'));
(() => Commands.validateGenerateTextArgs({
_: ['generateText'],
data: 'data_en.json'
})).should.throw('A data.json file is required. Try the --data flag or create a data.json in the root folder of your template.');
});
});

describe('#init', () => {
it('should initialize a clause using a template', async () => {
const response = await Commands.init(template, sample);
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
contract.json
data_out.json
sample_out.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"$class":"org.accordproject.latedeliveryandpenalty.TemplateModel","clauseId":"426d00d8-4293-4244-bd25-e66aa21eb0a4","forceMajeure":true,"penaltyDuration":{"$class":"org.accordproject.time.Duration","amount":9,"unit":"days"},"penaltyPercentage":7,"capPercentage":2,"termination":{"$class":"org.accordproject.time.Duration","amount":2,"unit":"weeks"},"fractionalPart":"days"}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"$class":"org.accordproject.foo.TemplateModel","clauseId":"426d00d8-4293-4244-bd25-e66aa21eb0a4","forceMajeure":true,"penaltyDuration":{"$class":"org.accordproject.time.Duration","amount":9,"unit":"days"},"penaltyPercentage":7,"capPercentage":2,"termination":{"$class":"org.accordproject.time.Duration","amount":2,"unit":"weeks"},"fractionalPart":"days"}
2 changes: 1 addition & 1 deletion packages/cicero-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"webpack-cli": "^3.1.0"
},
"dependencies": {
"@accordproject/ergo-compiler": "0.8.0",
"@accordproject/ergo-compiler": "0.8.1",
"axios": "0.18.0",
"composer-concerto": "0.70.2",
"debug": "4.1.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/cicero-engine/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@
},
"dependencies": {
"@accordproject/cicero-core": "0.12.0",
"@accordproject/ergo-compiler": "0.8.0",
"@accordproject/ergo-engine": "0.8.0",
"@accordproject/ergo-compiler": "0.8.1",
"@accordproject/ergo-engine": "0.8.1",
"composer-concerto": "0.70.2",
"moment-mini": "2.22.1"
},
Expand Down
11 changes: 11 additions & 0 deletions packages/cicero-test/lib/steps.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,17 @@ Given('that the contract says', async function (contractText) {
this.clause.parse(contractText);
});

Given('that the contract data is', async function (contractData) {
const dataJson = JSON.parse(contractData);
if (!this.clause) {
const templateDir = Path.resolve(Util.resolveRootDir(this.parameters),'.');
const clause = await loadClause(templateDir);
this.request = clause.getTemplate().getMetadata().getRequest();
this.clause = clause;
}
this.clause.setData(dataJson);
});

Given('the default( sample) contract', async function () {
if (!this.clause) {
const templateDir = Path.resolve(Util.resolveRootDir(this.parameters),'.');
Expand Down
2 changes: 1 addition & 1 deletion packages/cicero-test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"dependencies": {
"@accordproject/cicero-core": "0.12.0",
"@accordproject/cicero-engine": "0.12.0",
"@accordproject/ergo-test": "0.8.0",
"@accordproject/ergo-test": "0.8.1",
"chai": "4.2.0",
"chai-things": "0.2.0",
"cucumber": "^5.1.0"
Expand Down
Loading

0 comments on commit 622238e

Please sign in to comment.