Skip to content
This repository has been archived by the owner on Dec 16, 2021. It is now read-only.

Commit

Permalink
[DP-1858] change params passing in fetchDapContract and fetchDapObjec…
Browse files Browse the repository at this point in the history
…ts (#159)

* change params passing in fetchDapContract and fetchDapObjects

* version 0.3.0
  • Loading branch information
abvgedeika authored Mar 18, 2019
1 parent b28dfe4 commit c4021db
Show file tree
Hide file tree
Showing 13 changed files with 53 additions and 53 deletions.
2 changes: 1 addition & 1 deletion doc/REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ Returns user's Dap space.

| name | type | description |
|------------|--------|----------------------------------------|
| args.dapId | string | User's dap Id |
| args.contractId | string | User's contract Id |

##### Response

Expand Down
8 changes: 4 additions & 4 deletions lib/api/dashDriveAdapter/AbstractDashDriveAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ class AbstractDashDriveAdapter {

/**
* Fetch DAP Contract from DashDrive State View
* @param {string} dapId
* @param {string} contractId
* @return {Promise<Object>} - Dap contract
*/
fetchDapContract(dapId) {
fetchDapContract(contractId) {
throw new Error('Not implemented');
}

/**
* Fetch DAP Objects from DashDrive State View
* @param {string} dapId
* @param {string} contractId
* @param {string} type - Dap objects type to fetch
* @param options
* @param {Object} options.where - Mongo-like query
Expand All @@ -31,7 +31,7 @@ class AbstractDashDriveAdapter {
* @param {number} options.startAfter - exclusive skip
* @return {Promise<Object[]>}
*/
fetchDapObjects(dapId, type, options) {
fetchDapObjects(contractId, type, options) {
throw new Error('Not implemented');
}
}
Expand Down
12 changes: 6 additions & 6 deletions lib/api/dashDriveAdapter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,16 @@ class DashDriveAdapter extends AbstractDashDriveAdapter {

/**
* Fetch DAP Contract from DashDrive State View
* @param {string} dapId
* @param {string} contractId
* @return {Promise<Object>} - Dap contract
*/
fetchDapContract(dapId) {
return this.request('fetchDPContract', { dapId });
fetchDapContract(contractId) {
return this.request('fetchDPContract', { contractId });
}

/**
* Fetch DAP Objects from DashDrive State View
* @param {string} dapId
* @param {string} contractId
* @param {string} type - Dap objects type to fetch
* @param options
* @param {Object} options.where - Mongo-like query
Expand All @@ -62,8 +62,8 @@ class DashDriveAdapter extends AbstractDashDriveAdapter {
* @param {number} options.startAfter - exclusive skip
* @return {Promise<Object[]>}
*/
fetchDapObjects(dapId, type, options) {
return this.request('fetchDPObjects', { dapId, type, options });
fetchDapObjects(contractId, type, options) {
return this.request('fetchDPObjects', { contractId, type, options });
}
}

Expand Down
10 changes: 5 additions & 5 deletions lib/rpcServer/commands/fetchDapContract.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ const fetchDapContractFactory = (dashDriveAPI) => {
* Returns user dap space
* @typedef fetchDapContract
* @param args - command arguments
* @param {string} args.dapId
* @param {string} args.contractId
* @return {Promise<object>}
*/
async function fetchDapContract(args) {
validator.validate(args);
const { dapId } = args;
return dashDriveAPI.fetchDapContract(dapId);
const { contractId } = args;
return dashDriveAPI.fetchDapContract(contractId);
}

return fetchDapContract;
Expand Down Expand Up @@ -66,9 +66,9 @@ const fetchDapContractFactory = (dashDriveAPI) => {
* title: Parameters
* type: object
* required:
* - dapId
* - contractId
* properties:
* dapId:
* contractId:
* type: string
* default: ''
* description: A user's DAP ID
Expand Down
12 changes: 6 additions & 6 deletions lib/rpcServer/commands/fetchDapObjects.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const fetchDapObjectsFactory = (dashDriveAPI) => {
* Fetches user objects for a given condition
* @typedef fetchDapObjects
* @param args - command arguments
* @param {string} args.dapId
* @param {string} args.contractId
* @param {string} args.type
* @param args.options
* @param {Object} args.options.where - Mongo-like query
Expand All @@ -24,8 +24,8 @@ const fetchDapObjectsFactory = (dashDriveAPI) => {
*/
async function fetchDapObjects(args) {
validator.validate(args);
const { dapId, type, options } = args;
return dashDriveAPI.fetchDapObjects(dapId, type, options);
const { contractId, type, options } = args;
return dashDriveAPI.fetchDapObjects(contractId, type, options);
}

return fetchDapObjects;
Expand Down Expand Up @@ -73,13 +73,13 @@ const fetchDapObjectsFactory = (dashDriveAPI) => {
* title: Parameters
* type: object
* required:
* - dapId
* - contractId
* - type
* properties:
* dapId:
* contractId:
* type: string
* default: ''
* description: A user's DAP ID
* description: A user's contract ID
* type:
* type: string
* default: ''
Expand Down
6 changes: 3 additions & 3 deletions lib/rpcServer/schemas/fetchDapContract.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"type": "object",
"properties": {
"dapId": {
"contractId": {
"type": "string"
}
},
"required": [
"dapId"
"contractId"
]
}
}
6 changes: 3 additions & 3 deletions lib/rpcServer/schemas/fetchDapObjects.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"type": "object",
"properties": {
"dapId": {
"contractId": {
"type": "string"
},
"type": {
Expand All @@ -12,6 +12,6 @@
}
},
"required": [
"dapId", "type", "options"
"contractId", "type", "options"
]
}
}
2 changes: 1 addition & 1 deletion package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@dashevo/dapi",
"version": "0.2.4",
"version": "0.3.0",
"description": "Dash Decentralized API (DAPI)",
"scripts": {
"start": "node index.js",
Expand Down
20 changes: 10 additions & 10 deletions test/api/dashDriveAdapter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,48 +46,48 @@ describe('DashDriveAdapter', () => {
it('Should call \'fetchDPContract\' RPC with the given parameters', async () => {
const dashDrive = new DashDriveAdapter({ host: '127.0.0.1', port: 3000 });

const dapId = 'dapid';
const contractId = 'contractId';
const method = 'fetchDPContract';

const expectedDapContract = { dapId };
const expectedDapContract = { contractId };

sinon.stub(dashDrive.client, 'request')
.withArgs(method, { dapId })
.withArgs(method, { contractId })
.returns(Promise.resolve({ result: expectedDapContract }));

expect(dashDrive.client.request.callCount).to.be.equal(0);

const actualDapContract = await dashDrive.fetchDapContract(dapId);
const actualDapContract = await dashDrive.fetchDapContract(contractId);

expect(dashDrive.client.request.callCount).to.be.equal(1);
expect(actualDapContract).to.be.equal(expectedDapContract);
expect(actualDapContract).not.to.be.equal({ dapId: 'randomid' });
expect(actualDapContract).not.to.be.equal({ contractId: 'randomid' });
});
});

describe('#fetchDPObjects', () => {
it('Should call \'fetchDPObjects\' RPC with the given parameters', async () => {
const dashDrive = new DashDriveAdapter({ host: '127.0.0.1', port: 3000 });

const dapId = 'dapid';
const contractId = 'contractId';
const type = 'contact';
const options = { where: { id: 1 } };
const method = 'fetchDPObjects';

const expectedDapObjects = [{ dapId, id: 1 }];
const expectedDapObjects = [{ contractId, id: 1 }];


sinon.stub(dashDrive.client, 'request')
.withArgs(method, { dapId, type, options })
.withArgs(method, { contractId, type, options })
.returns(Promise.resolve({ result: expectedDapObjects }));

expect(dashDrive.client.request.callCount).to.be.equal(0);

const actualDapObjects = await dashDrive.fetchDapObjects(dapId, type, options);
const actualDapObjects = await dashDrive.fetchDapObjects(contractId, type, options);

expect(dashDrive.client.request.callCount).to.be.equal(1);
expect(actualDapObjects).to.be.equal(expectedDapObjects);
expect(actualDapObjects).not.to.be.equal([{ dapId, id: 2 }]);
expect(actualDapObjects).not.to.be.equal([{ contractId, id: 2 }]);
});
});
});
2 changes: 1 addition & 1 deletion test/fixtures/dashDriveFixture.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const AbstractDashDriveAdapter = require('../../lib/api/dashDriveAdapter/Abstrac
class DashDriveFixture extends AbstractDashDriveAdapter {
addSTPacket(rawStateTransition, rawSTPacket) { return Promise.resolve(); }

fetchDapContract(dapId) { return Promise.resolve({}); }
fetchDapContract(contractId) { return Promise.resolve({}); }
}

module.exports = new DashDriveFixture();
4 changes: 2 additions & 2 deletions test/rpcServer/commands/fetchDapContract.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ describe('fetchDapContract', () => {
it('Should return search results', async () => {
const getDapContract = getDapContractFactory(dashDriveFixture);
expect(spy.callCount).to.be.equal(0);
const contract = await getDapContract({ dapId: '123' });
const contract = await getDapContract({ contractId: '123' });
expect(contract).to.be.an('object');
expect(spy.callCount).to.be.equal(1);
});

it('Should throw an error if arguments are not valid', async () => {
const getDapContract = getDapContractFactory(dashDriveFixture);
expect(spy.callCount).to.be.equal(0);
await expect(getDapContract({ dapId: 123 })).to.be.rejectedWith('should be string');
await expect(getDapContract({ contractId: 123 })).to.be.rejectedWith('should be string');
expect(spy.callCount).to.be.equal(0);
await expect(getDapContract({})).to.be.rejectedWith('should have required property');
expect(spy.callCount).to.be.equal(0);
Expand Down
20 changes: 10 additions & 10 deletions test/rpcServer/commands/fetchDapObjects.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ const dashDriveAdapter = new DashDriveAdapter({ host: 'host', port: 1 });
chai.use(chaiAsPromised);
const { expect } = chai;

const expectedSearchParams = { dapId: '123', type: 'contact', options: { where: { userId: 1 } } };
const expectedResult = [{ dapId: '123', type: 'contact', userId: 1 }];
const expectedSearchParams = { contractId: '123', type: 'contact', options: { where: { userId: 1 } } };
const expectedResult = [{ contractId: '123', type: 'contact', userId: 1 }];

describe('fetchDapContract', () => {
describe('#factory', () => {
Expand All @@ -22,8 +22,8 @@ describe('fetchDapContract', () => {

before(() => {
sinon.stub(dashDriveAdapter, 'fetchDapObjects')
.withArgs(expectedSearchParams.dapId, expectedSearchParams.type, expectedSearchParams.options)
.returns(Promise.resolve(expectedResult));
.withArgs(expectedSearchParams.contractId, expectedSearchParams.type,
expectedSearchParams.options).returns(Promise.resolve(expectedResult));
});

beforeEach(() => {
Expand All @@ -45,17 +45,17 @@ describe('fetchDapContract', () => {
it('Should throw an error if arguments are not valid', async () => {
const fetchDapObjects = fetchDapObjectsFactory(dashDriveAdapter);
expect(dashDriveAdapter.fetchDapObjects.callCount).to.be.equal(0);
await expect(fetchDapObjects({ dapId: 123 })).to.be.rejectedWith('params.dapId should be string');
await expect(fetchDapObjects({ contractId: 123 })).to.be.rejectedWith('params.contractId should be string');
expect(dashDriveAdapter.fetchDapObjects.callCount).to.be.equal(0);
await expect(fetchDapObjects({ dapId: '123' })).to.be.rejectedWith('params should have required property \'type\'');
await expect(fetchDapObjects({ contractId: '123' })).to.be.rejectedWith('params should have required property \'type\'');
expect(dashDriveAdapter.fetchDapObjects.callCount).to.be.equal(0);
await expect(fetchDapObjects({ dapId: '123', type: 1 })).to.be.rejectedWith('params.type should be string');
await expect(fetchDapObjects({ contractId: '123', type: 1 })).to.be.rejectedWith('params.type should be string');
expect(dashDriveAdapter.fetchDapObjects.callCount).to.be.equal(0);
await expect(fetchDapObjects({ dapId: '123', type: 'type' })).to.be.rejectedWith('params should have required property \'options\'');
await expect(fetchDapObjects({ contractId: '123', type: 'type' })).to.be.rejectedWith('params should have required property \'options\'');
expect(dashDriveAdapter.fetchDapObjects.callCount).to.be.equal(0);
await expect(fetchDapObjects({ dapId: '123', type: 'type', options: 1 })).to.be.rejectedWith('params.options should be object');
await expect(fetchDapObjects({ contractId: '123', type: 'type', options: 1 })).to.be.rejectedWith('params.options should be object');
expect(dashDriveAdapter.fetchDapObjects.callCount).to.be.equal(0);
await expect(fetchDapObjects({})).to.be.rejectedWith('params should have required property \'dapId\'');
await expect(fetchDapObjects({})).to.be.rejectedWith('params should have required property \'contractId\'');
expect(dashDriveAdapter.fetchDapObjects.callCount).to.be.equal(0);
await expect(fetchDapObjects()).to.be.rejectedWith('params should be object');
expect(dashDriveAdapter.fetchDapObjects.callCount).to.be.equal(0);
Expand Down

0 comments on commit c4021db

Please sign in to comment.