This repository has been archived by the owner on Apr 15, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #570 from LiskHQ/563_migrate_block
Migrate block command - Closes #563
- Loading branch information
Showing
3 changed files
with
210 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* LiskHQ/lisk-commander | ||
* Copyright © 2017–2018 Lisk Foundation | ||
* | ||
* See the LICENSE file at the top-level directory of this distribution | ||
* for licensing information. | ||
* | ||
* Unless otherwise agreed in a custom licensing agreement with the Lisk Foundation, | ||
* no part of this software, including this file, may be copied, modified, | ||
* propagated, or distributed except according to the terms contained in the | ||
* LICENSE file. | ||
* | ||
* Removal or modification of this copyright notice is prohibited. | ||
* | ||
*/ | ||
import BaseCommand from '../../base'; | ||
import getAPIClient from '../../utils/api'; | ||
import query from '../../utils/query'; | ||
|
||
export default class GetCommand extends BaseCommand { | ||
async run() { | ||
const { args: { blockIds } } = this.parse(GetCommand); | ||
const req = blockIds.map(blockId => ({ | ||
query: { | ||
limit: 1, | ||
blockId, | ||
}, | ||
placeholder: { | ||
id: blockId, | ||
message: 'Block not found.', | ||
}, | ||
})); | ||
const client = getAPIClient(this.userConfig.api); | ||
const results = await query(client, 'blocks', req); | ||
this.print(results); | ||
} | ||
} | ||
|
||
GetCommand.args = [ | ||
{ | ||
name: 'blockIds', | ||
required: true, | ||
description: 'Comma-separated block ID(s) to get information about.', | ||
parse: input => input.split(',').filter(Boolean), | ||
}, | ||
]; | ||
|
||
GetCommand.flags = { | ||
...BaseCommand.flags, | ||
}; | ||
|
||
GetCommand.description = ` | ||
Gets block information from the blockchain. | ||
`; | ||
|
||
GetCommand.examples = [ | ||
'block:get 17108498772892203620', | ||
'block:get 17108498772892203620,8541428004955961162', | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
/* | ||
* LiskHQ/lisk-commander | ||
* Copyright © 2017–2018 Lisk Foundation | ||
* | ||
* See the LICENSE file at the top-level directory of this distribution | ||
* for licensing information. | ||
* | ||
* Unless otherwise agreed in a custom licensing agreement with the Lisk Foundation, | ||
* no part of this software, including this file, may be copied, modified, | ||
* propagated, or distributed except according to the terms contained in the | ||
* LICENSE file. | ||
* | ||
* Removal or modification of this copyright notice is prohibited. | ||
* | ||
*/ | ||
import { test } from '@oclif/test'; | ||
import * as config from '../../../src/utils/config'; | ||
import * as print from '../../../src/utils/print'; | ||
import * as api from '../../../src/utils/api'; | ||
import * as query from '../../../src/utils/query'; | ||
|
||
describe('block:get', () => { | ||
const endpoint = 'blocks'; | ||
const apiConfig = { | ||
nodes: ['http://local.host'], | ||
network: 'main', | ||
}; | ||
const printMethodStub = sandbox.stub(); | ||
const apiClientStub = sandbox.stub(); | ||
const setupTest = () => | ||
test | ||
.stub(print, 'default', sandbox.stub().returns(printMethodStub)) | ||
.stub(config, 'getConfig', sandbox.stub().returns({ api: apiConfig })) | ||
.stub(api, 'default', sandbox.stub().returns(apiClientStub)); | ||
|
||
describe('block:get', () => { | ||
setupTest() | ||
.stdout() | ||
.command(['block:get']) | ||
.catch(error => | ||
expect(error.message).to.contain('Missing 1 required arg'), | ||
) | ||
.it('should throw an error when arg is not provided'); | ||
}); | ||
|
||
describe('block:get blockId', () => { | ||
const blockId = '9249767683252385637'; | ||
const queryResult = { | ||
id: blockId, | ||
height: 1, | ||
}; | ||
|
||
setupTest() | ||
.stdout() | ||
.stub(query, 'default', sandbox.stub().resolves(queryResult)) | ||
.command(['block:get', blockId]) | ||
.it('should get block info and display as an object', () => { | ||
expect(api.default).to.be.calledWithExactly(apiConfig); | ||
expect(query.default).to.be.calledWithExactly(apiClientStub, endpoint, [ | ||
{ | ||
query: { | ||
limit: 1, | ||
blockId, | ||
}, | ||
placeholder: { | ||
id: blockId, | ||
message: 'Block not found.', | ||
}, | ||
}, | ||
]); | ||
return expect(printMethodStub).to.be.calledWithExactly(queryResult); | ||
}); | ||
}); | ||
|
||
describe('block:get blockIds', () => { | ||
const blockIds = ['9249767683252385637', '12690684816503689985']; | ||
const blockIdsWithEmpty = ['9249767683252385637', '']; | ||
const queryResult = [ | ||
{ | ||
id: blockIds[0], | ||
height: 3, | ||
}, | ||
{ | ||
id: blockIds[1], | ||
height: 4, | ||
}, | ||
]; | ||
|
||
setupTest() | ||
.stdout() | ||
.stub(query, 'default', sandbox.stub().resolves(queryResult)) | ||
.command(['block:get', blockIds.join(',')]) | ||
.it('should get blocks info and display as an array', () => { | ||
expect(api.default).to.be.calledWithExactly(apiConfig); | ||
expect(query.default).to.be.calledWithExactly(apiClientStub, endpoint, [ | ||
{ | ||
query: { | ||
limit: 1, | ||
blockId: blockIds[0], | ||
}, | ||
placeholder: { | ||
id: blockIds[0], | ||
message: 'Block not found.', | ||
}, | ||
}, | ||
{ | ||
query: { | ||
limit: 1, | ||
blockId: blockIds[1], | ||
}, | ||
placeholder: { | ||
id: blockIds[1], | ||
message: 'Block not found.', | ||
}, | ||
}, | ||
]); | ||
return expect(printMethodStub).to.be.calledWithExactly(queryResult); | ||
}); | ||
|
||
setupTest() | ||
.stdout() | ||
.stub(query, 'default', sandbox.stub().resolves(queryResult)) | ||
.command(['block:get', blockIdsWithEmpty.join(',')]) | ||
.it( | ||
'should get blocks info only using non-empty args and display as an array', | ||
() => { | ||
expect(api.default).to.be.calledWithExactly(apiConfig); | ||
expect(query.default).to.be.calledWithExactly( | ||
apiClientStub, | ||
endpoint, | ||
[ | ||
{ | ||
query: { | ||
limit: 1, | ||
blockId: blockIdsWithEmpty[0], | ||
}, | ||
placeholder: { | ||
id: blockIdsWithEmpty[0], | ||
message: 'Block not found.', | ||
}, | ||
}, | ||
], | ||
); | ||
return expect(printMethodStub).to.be.calledWithExactly(queryResult); | ||
}, | ||
); | ||
}); | ||
}); |