UNPKG

5.99 kBJavaScriptView Raw
1/*
2 * Licensed under the Apache License, Version 2.0 (the "License");
3 * you may not use this file except in compliance with the License.
4 * You may obtain a copy of the License at
5 *
6 * http://www.apache.org/licenses/LICENSE-2.0
7 *
8 * Unless required by applicable law or agreed to in writing, software
9 * distributed under the License is distributed on an "AS IS" BASIS,
10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 * See the License for the specific language governing permissions and
12 * limitations under the License.
13 */
14
15'use strict';
16
17const chai = require('chai');
18const path = require('path');
19const tmp = require('tmp-promise');
20const fs = require('fs');
21
22chai.should();
23chai.use(require('chai-things'));
24chai.use(require('chai-as-promised'));
25
26const Commands = require('../lib/commands');
27
28describe('cicero-cli', () => {
29 const models = [path.resolve(__dirname, 'models/dom.cto'),path.resolve(__dirname, 'models/money.cto')];
30 const hlModel = path.resolve(__dirname, 'models/org.hyperledger.composer.system.cto');
31 const sample1 = path.resolve(__dirname, 'data/sample1.json');
32 const sample2 = path.resolve(__dirname, 'data/sample2.json');
33 const sampleText1 = fs.readFileSync(sample1, 'utf8');
34 const sampleText2 = fs.readFileSync(sample2, 'utf8');
35
36 describe('#validate', () => {
37 it('should validate against a model', async () => {
38 const result = await Commands.validate(sample1, null, models);
39 JSON.parse(result).should.deep.equal(JSON.parse(sampleText1));
40 });
41
42 it('should fail to validate against a model', async () => {
43 try {
44 const result = await Commands.validate(sample2, null, models);
45 JSON.parse(result).should.deep.equal(JSON.parse(sampleText2));
46 } catch (err) {
47 err.message.should.equal('Instance undefined invalid enum value true for field CurrencyCode');
48 }
49 });
50 });
51
52 describe('#compile', () => {
53
54 it('should compile to a Go model', async () => {
55 const dir = await tmp.dir({ unsafeCleanup: true});
56 await Commands.compile('Go', null, models, dir.path);
57 fs.readdirSync(dir.path).length.should.be.above(0);
58 dir.cleanup();
59 });
60 it('should compile to a PlantUML model', async () => {
61 const dir = await tmp.dir({ unsafeCleanup: true});
62 await Commands.compile('PlantUML', null, models, dir.path);
63 fs.readdirSync(dir.path).length.should.be.above(0);
64 dir.cleanup();
65 });
66 it('should compile to a Typescript model', async () => {
67 const dir = await tmp.dir({ unsafeCleanup: true});
68 await Commands.compile('Typescript', null, models, dir.path);
69 fs.readdirSync(dir.path).length.should.be.above(0);
70 dir.cleanup();
71 });
72 it('should compile to a Java model', async () => {
73 const dir = await tmp.dir({ unsafeCleanup: true});
74 await Commands.compile('Java', null, models, dir.path);
75 fs.readdirSync(dir.path).length.should.be.above(0);
76 dir.cleanup();
77 });
78 it('should compile to a JSONSchema model', async () => {
79 const dir = await tmp.dir({ unsafeCleanup: true});
80 await Commands.compile('JSONSchema', null, models, dir.path);
81 fs.readdirSync(dir.path).length.should.be.above(0);
82 dir.cleanup();
83 });
84 it('should compile to a XMLSchema model', async () => {
85 const dir = await tmp.dir({ unsafeCleanup: true});
86 await Commands.compile('XMLSchema', null, models, dir.path);
87 fs.readdirSync(dir.path).length.should.be.above(0);
88 dir.cleanup();
89 });
90 it('should not compile to an unknown model', async () => {
91 const dir = await tmp.dir({ unsafeCleanup: true});
92 await Commands.compile('BLAH', null, models, dir.path);
93 fs.readdirSync(dir.path).length.should.be.equal(0);
94 dir.cleanup();
95 });
96 });
97
98 describe('#get', () => {
99 it('should save external dependencies', async () => {
100 const dir = await tmp.dir({ unsafeCleanup: true});
101 await Commands.get(null, models, dir.path);
102 fs.readdirSync(dir.path).should.eql([
103 '@models.accordproject.org.cicero.contract.cto',
104 'dom.cto',
105 'money.cto'
106 ]);
107 dir.cleanup();
108 });
109
110 it('should save external dependencies for an external model', async () => {
111 const dir = await tmp.dir({ unsafeCleanup: true});
112 await Commands.get(null,['https://models.accordproject.org/patents/patent.cto'], dir.path);
113 fs.readdirSync(dir.path).should.eql([
114 '@models.accordproject.org.address.cto',
115 '@models.accordproject.org.geo.cto',
116 '@models.accordproject.org.money.cto',
117 '@models.accordproject.org.organization.cto',
118 '@models.accordproject.org.patents.patent.cto',
119 '@models.accordproject.org.person.cto',
120 '@models.accordproject.org.product.cto',
121 '@models.accordproject.org.usa.residency.cto',
122 '@models.accordproject.org.value.cto'
123 ]);
124 dir.cleanup();
125 });
126
127 it('should fail saving external dependencies for an external model but with the wrong system model', async () => {
128 const dir = await tmp.dir({ unsafeCleanup: true});
129 try {
130 await Commands.get(hlModel,['https://models.accordproject.org/patents/patent.cto'], dir.path);
131 } catch (err) {
132 err.message.should.contain('Relationship transactionInvoked must be to an asset or participant, but is to org.hyperledger.composer.system.Transaction');
133 }
134 });
135 });
136});
\No newline at end of file