From 30cb05b669ff27a3b43950cecf561caeeac5ca16 Mon Sep 17 00:00:00 2001 From: xufengli Date: Fri, 28 Jul 2017 21:56:34 +0100 Subject: [PATCH 1/5] add unit test with different datatypes for PouchDB --- .../test/data/bond-network/queries.qry | 20 +++++ packages/composer-rest-server/test/queries.js | 88 ++++++++++++++++--- 2 files changed, 95 insertions(+), 13 deletions(-) diff --git a/packages/composer-rest-server/test/data/bond-network/queries.qry b/packages/composer-rest-server/test/data/bond-network/queries.qry index 50c197dfd9..aa9287936f 100644 --- a/packages/composer-rest-server/test/data/bond-network/queries.qry +++ b/packages/composer-rest-server/test/data/bond-network/queries.qry @@ -2,3 +2,23 @@ query findBondByFaceAmount { description: "Find all bonds with a face amount greater than _$faceAmount" statement: SELECT org.acme.bond.BondAsset WHERE (bond.faceAmount > _$faceAmount) } +query findBondByPaymentFrequencyPeriod { + description: "Find all bonds with a payment frequecy period _$period" + statement: SELECT org.acme.bond.BondAsset WHERE (bond.paymentFrequency.period == _$period) +} +query findBondByPaymentFrequencyPeriodMultiplier { + description: "Find all bonds with a payment frequecy period multiplier _$multiplier" + statement: SELECT org.acme.bond.BondAsset WHERE (bond.paymentFrequency.periodMultiplier == _$multiplier) +} +query findBondByExchangeId { + description: "Find all bonds with a set of exchange ids _$exchangeId" + statement: SELECT org.acme.bond.BondAsset WHERE (bond.exchangeId == _$exchangeId) +} +query findBondByIssuer { + description: "Find all bonds with an issuer _$issuer" + statement: SELECT org.acme.bond.BondAsset WHERE (bond.issuer == _$issuer) +} +query findBondByMaturity { + description: "Find all bonds with a maturity not greater than _$maturity" + statement: SELECT org.acme.bond.BondAsset WHERE (bond.maturity <= _$maturity) +} \ No newline at end of file diff --git a/packages/composer-rest-server/test/queries.js b/packages/composer-rest-server/test/queries.js index e79a822d20..5155a62102 100644 --- a/packages/composer-rest-server/test/queries.js +++ b/packages/composer-rest-server/test/queries.js @@ -38,6 +38,7 @@ const bfs_fs = BrowserFS.BFSRequire('fs'); $class: 'org.acme.bond.Bond', dayCountFraction: 'EOM', exchangeId: [ + 'LDN', 'NYSE' ], faceAmount: 1000, @@ -45,7 +46,7 @@ const bfs_fs = BrowserFS.BFSRequire('fs'); 'AliceCorp' ], issuer: 'resource:org.acme.bond.Issuer#1', - maturity: '2018-02-27T21:03:52.000Z', + maturity: '2017-02-27T21:03:52.000Z', parValue: 1000, paymentFrequency: { $class: 'org.acme.bond.PaymentFrequency', @@ -60,14 +61,14 @@ const bfs_fs = BrowserFS.BFSRequire('fs'); $class: 'org.acme.bond.Bond', dayCountFraction: 'EOM', exchangeId: [ - 'NYSE' + 'LDN' ], faceAmount: 1000, instrumentId: [ 'BobCorp' ], - issuer: 'resource:org.acme.bond.Issuer#1', - maturity: '2018-02-27T21:03:52.000Z', + issuer: 'resource:org.acme.bond.Issuer#2', + maturity: '2017-02-27T21:03:52.000Z', parValue: 1000, paymentFrequency: { $class: 'org.acme.bond.PaymentFrequency', @@ -88,13 +89,13 @@ const bfs_fs = BrowserFS.BFSRequire('fs'); instrumentId: [ 'CharlieCorp' ], - issuer: 'resource:org.acme.bond.Issuer#1', + issuer: 'resource:org.acme.bond.Issuer#3', maturity: '2018-02-27T21:03:52.000Z', parValue: 1000, paymentFrequency: { $class: 'org.acme.bond.PaymentFrequency', - period: 'MONTH', - periodMultiplier: 6 + period: 'YEAR', + periodMultiplier: 1 } } }, { @@ -110,13 +111,13 @@ const bfs_fs = BrowserFS.BFSRequire('fs'); instrumentId: [ 'DogeCorp' ], - issuer: 'resource:org.acme.bond.Issuer#1', + issuer: 'resource:org.acme.bond.Issuer#4', maturity: '2018-02-27T21:03:52.000Z', parValue: 1000, paymentFrequency: { $class: 'org.acme.bond.PaymentFrequency', - period: 'MONTH', - periodMultiplier: 6 + period: 'YEAR', + periodMultiplier: 1 } } }]; @@ -164,14 +165,16 @@ const bfs_fs = BrowserFS.BFSRequire('fs'); assetRegistry = assetRegistry_; return assetRegistry.addAll([ serializer.fromJSON(assetData[0]), - serializer.fromJSON(assetData[1]) + serializer.fromJSON(assetData[1]), + serializer.fromJSON(assetData[2]), + serializer.fromJSON(assetData[3]) ]); }); }); describe(`GET / namespaces[${namespaces}]`, () => { - it('should return all of the assets', () => { + it('should return all of the assets with a double type variable', () => { return chai.request(app) .get('/api/queries/findBondByFaceAmount?faceAmount=500') .then((res) => { @@ -182,7 +185,66 @@ const bfs_fs = BrowserFS.BFSRequire('fs'); ]); }); }); - + it('should return all of the assets with an enum type variable', () => { + return chai.request(app) + .get('/api/queries/findBondByPaymentFrequencyPeriod?period=MONTH') + .then((res) => { + res.should.be.json; + res.body.should.deep.equal([ + assetData[0], + assetData[1], + ]); + }); + }); + it('should return all of the assets with an integer type variable', () => { + return chai.request(app) + .get('/api/queries/findBondByPaymentFrequencyPeriodMultiplier?multiplier=6') + .then((res) => { + res.should.be.json; + res.body.should.deep.equal([ + assetData[0], + assetData[1], + ]); + }); + }); + it('should return all of the assets with an array of exchangeId variable', () => { + return chai.request(app) + .get('/api/queries/findBondByExchangeId?exchangeId[]=LDN&exchangeId[]=NYSE') + .then((res) => { + res.should.be.json; + res.body.should.deep.equal([ + assetData[0] + ]); + }); + }); + it('should return all of the assets with a relationship variable', () => { + return chai.request(app) + .get('/api/queries/findBondByIssuer?issuer=resource:org.acme.bond.Issuer#1') + .then((res) => { + res.should.be.json; + res.body.should.deep.equal([ + assetData[0] + ]); + }); + }); + it('should return all of the assets with a datetime variable', () => { + return chai.request(app) + .get('/api/queries/findBondByMaturity?maturity=2017-02-27T21:03:52.000Z') + .then((res) => { + res.should.be.json; + res.body.should.deep.equal([ + assetData[0], + assetData[1] + ]); + }); + }); + it('should return a 404 if the specified asset does not exist', () => { + return chai.request(app) + .get('/api/queries/findBondByMaturity?maturity=2019-02-27T21:03:52.000Z') + .catch((err) => { + err.response.should.have.status(404); + }); + }); }); }); }); From 7bf48b2fb4c0cc79ecb225caaa3e72d4154d78a1 Mon Sep 17 00:00:00 2001 From: xufengli Date: Mon, 31 Jul 2017 12:18:05 +0100 Subject: [PATCH 2/5] add unit test for PouchDB --- .../test/data/bond-network/queries.qry | 12 +----- packages/composer-rest-server/test/queries.js | 43 ++++--------------- 2 files changed, 11 insertions(+), 44 deletions(-) diff --git a/packages/composer-rest-server/test/data/bond-network/queries.qry b/packages/composer-rest-server/test/data/bond-network/queries.qry index aa9287936f..2c164d96c3 100644 --- a/packages/composer-rest-server/test/data/bond-network/queries.qry +++ b/packages/composer-rest-server/test/data/bond-network/queries.qry @@ -10,15 +10,7 @@ query findBondByPaymentFrequencyPeriodMultiplier { description: "Find all bonds with a payment frequecy period multiplier _$multiplier" statement: SELECT org.acme.bond.BondAsset WHERE (bond.paymentFrequency.periodMultiplier == _$multiplier) } -query findBondByExchangeId { - description: "Find all bonds with a set of exchange ids _$exchangeId" +query findBondByExchangeIdUnsupported { + description: "Find all bonds with a set of exchange id _$exchangeId is unsupported" statement: SELECT org.acme.bond.BondAsset WHERE (bond.exchangeId == _$exchangeId) -} -query findBondByIssuer { - description: "Find all bonds with an issuer _$issuer" - statement: SELECT org.acme.bond.BondAsset WHERE (bond.issuer == _$issuer) -} -query findBondByMaturity { - description: "Find all bonds with a maturity not greater than _$maturity" - statement: SELECT org.acme.bond.BondAsset WHERE (bond.maturity <= _$maturity) } \ No newline at end of file diff --git a/packages/composer-rest-server/test/queries.js b/packages/composer-rest-server/test/queries.js index 5155a62102..62fd1f2b0a 100644 --- a/packages/composer-rest-server/test/queries.js +++ b/packages/composer-rest-server/test/queries.js @@ -38,8 +38,7 @@ const bfs_fs = BrowserFS.BFSRequire('fs'); $class: 'org.acme.bond.Bond', dayCountFraction: 'EOM', exchangeId: [ - 'LDN', - 'NYSE' + 'LDN' ], faceAmount: 1000, instrumentId: [ @@ -61,7 +60,7 @@ const bfs_fs = BrowserFS.BFSRequire('fs'); $class: 'org.acme.bond.Bond', dayCountFraction: 'EOM', exchangeId: [ - 'LDN' + 'NYSE' ], faceAmount: 1000, instrumentId: [ @@ -85,7 +84,7 @@ const bfs_fs = BrowserFS.BFSRequire('fs'); exchangeId: [ 'NYSE' ], - faceAmount: 1000, + faceAmount: 500, instrumentId: [ 'CharlieCorp' ], @@ -99,7 +98,7 @@ const bfs_fs = BrowserFS.BFSRequire('fs'); } } }, { - // $class: 'org.acme.bond.BondAsset', + $class: 'org.acme.bond.BondAsset', ISINCode: 'ISIN_4', bond: { $class: 'org.acme.bond.Bond', @@ -107,7 +106,7 @@ const bfs_fs = BrowserFS.BFSRequire('fs'); exchangeId: [ 'NYSE' ], - faceAmount: 1000, + faceAmount: 500, instrumentId: [ 'DogeCorp' ], @@ -207,35 +206,11 @@ const bfs_fs = BrowserFS.BFSRequire('fs'); ]); }); }); - it('should return all of the assets with an array of exchangeId variable', () => { - return chai.request(app) - .get('/api/queries/findBondByExchangeId?exchangeId[]=LDN&exchangeId[]=NYSE') - .then((res) => { - res.should.be.json; - res.body.should.deep.equal([ - assetData[0] - ]); - }); - }); - it('should return all of the assets with a relationship variable', () => { + it('should return a 404 if the query specified variable is an unsupported array type', () => { return chai.request(app) - .get('/api/queries/findBondByIssuer?issuer=resource:org.acme.bond.Issuer#1') - .then((res) => { - res.should.be.json; - res.body.should.deep.equal([ - assetData[0] - ]); - }); - }); - it('should return all of the assets with a datetime variable', () => { - return chai.request(app) - .get('/api/queries/findBondByMaturity?maturity=2017-02-27T21:03:52.000Z') - .then((res) => { - res.should.be.json; - res.body.should.deep.equal([ - assetData[0], - assetData[1] - ]); + .get('/api/queries/findBondByExchangeIdUnsupported?exchangeId[]=LDN') + .catch((err) => { + err.response.should.have.status(404); }); }); it('should return a 404 if the specified asset does not exist', () => { From fa33c4a0031ec4fa955609f9707d8ccbbf190cee Mon Sep 17 00:00:00 2001 From: xufengli Date: Mon, 31 Jul 2017 13:48:18 +0100 Subject: [PATCH 3/5] update the test data --- packages/composer-rest-server/test/queries.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/composer-rest-server/test/queries.js b/packages/composer-rest-server/test/queries.js index 62fd1f2b0a..4527307fc9 100644 --- a/packages/composer-rest-server/test/queries.js +++ b/packages/composer-rest-server/test/queries.js @@ -50,7 +50,7 @@ const bfs_fs = BrowserFS.BFSRequire('fs'); paymentFrequency: { $class: 'org.acme.bond.PaymentFrequency', period: 'MONTH', - periodMultiplier: 6 + periodMultiplier: 7 } } }, { @@ -72,7 +72,7 @@ const bfs_fs = BrowserFS.BFSRequire('fs'); paymentFrequency: { $class: 'org.acme.bond.PaymentFrequency', period: 'MONTH', - periodMultiplier: 6 + periodMultiplier: 7 } } }, { @@ -197,7 +197,7 @@ const bfs_fs = BrowserFS.BFSRequire('fs'); }); it('should return all of the assets with an integer type variable', () => { return chai.request(app) - .get('/api/queries/findBondByPaymentFrequencyPeriodMultiplier?multiplier=6') + .get('/api/queries/findBondByPaymentFrequencyPeriodMultiplier?multiplier=7') .then((res) => { res.should.be.json; res.body.should.deep.equal([ From 1ab68823bd61f7192d3ff2627593f95396ba94cf Mon Sep 17 00:00:00 2001 From: xufengli Date: Mon, 31 Jul 2017 16:07:37 +0100 Subject: [PATCH 4/5] add more test cases for non-existing values --- packages/composer-rest-server/test/queries.js | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/packages/composer-rest-server/test/queries.js b/packages/composer-rest-server/test/queries.js index 4527307fc9..7376cf691c 100644 --- a/packages/composer-rest-server/test/queries.js +++ b/packages/composer-rest-server/test/queries.js @@ -184,6 +184,13 @@ const bfs_fs = BrowserFS.BFSRequire('fs'); ]); }); }); + it('should return a 404 if query a double type vairable with a non-existing value', () => { + return chai.request(app) + .get('/api/queries/findBondByFaceAmount?faceAmount=10000') + .catch((err) => { + err.response.should.have.status(404); + }); + }); it('should return all of the assets with an enum type variable', () => { return chai.request(app) .get('/api/queries/findBondByPaymentFrequencyPeriod?period=MONTH') @@ -195,6 +202,13 @@ const bfs_fs = BrowserFS.BFSRequire('fs'); ]); }); }); + it('should return a 404 if query an enum type vairable with a non-existing value', () => { + return chai.request(app) + .get('/api/queries/findBondByPaymentFrequencyPeriod?period=QUARTER') + .catch((err) => { + err.response.should.have.status(404); + }); + }); it('should return all of the assets with an integer type variable', () => { return chai.request(app) .get('/api/queries/findBondByPaymentFrequencyPeriodMultiplier?multiplier=7') @@ -206,6 +220,13 @@ const bfs_fs = BrowserFS.BFSRequire('fs'); ]); }); }); + it('should return a 404 if query an integer type vairable with a non-existing value', () => { + return chai.request(app) + .get('/api/queries/findBondByPaymentFrequencyPeriodMultiplier?multiplier=6') + .catch((err) => { + err.response.should.have.status(404); + }); + }); it('should return a 404 if the query specified variable is an unsupported array type', () => { return chai.request(app) .get('/api/queries/findBondByExchangeIdUnsupported?exchangeId[]=LDN') From 0c192645d7994ca7b0fc269520c4a8fec0f5dcc2 Mon Sep 17 00:00:00 2001 From: xufengli Date: Mon, 31 Jul 2017 16:34:58 +0100 Subject: [PATCH 5/5] add a test case for non-existing query --- packages/composer-rest-server/test/queries.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/packages/composer-rest-server/test/queries.js b/packages/composer-rest-server/test/queries.js index 7376cf691c..efbc84f5f6 100644 --- a/packages/composer-rest-server/test/queries.js +++ b/packages/composer-rest-server/test/queries.js @@ -241,6 +241,13 @@ const bfs_fs = BrowserFS.BFSRequire('fs'); err.response.should.have.status(404); }); }); + it('should return a 404 if the specified query does not exist', () => { + return chai.request(app) + .get('/api/queries/nonDefinedQuery?wombat=cuddly') + .catch((err) => { + err.response.should.have.status(404); + }); + }); }); }); });