Skip to content

Commit

Permalink
Merge branch 'master' of github.com:bitpay/bitcore
Browse files Browse the repository at this point in the history
  • Loading branch information
nitsujlangston committed Feb 27, 2019
2 parents 44b49b9 + 659494f commit 56bd7a7
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 39 deletions.
19 changes: 11 additions & 8 deletions packages/bitcore-node/src/models/coin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,17 @@ class CoinModel extends BaseModel<ICoin> {

async getBalanceAtTime(params: { query: any; time: string; chain: string; network: string }) {
let { query, time, chain, network } = params;
const block = await BlockStorage.collection.findOne({
$query: {
chain,
network,
timeNormalized: { $lte: new Date(time) }
},
$orderBy: { timeNormalized: -1 }
});
const [block] = await BlockStorage.collection
.find({
$query: {
chain,
network,
timeNormalized: { $lte: new Date(time) }
}
})
.limit(1)
.sort({ timeNormalized: -1 })
.toArray();
const blockHeight = block!.height;
const combinedQuery = Object.assign(
{},
Expand Down
9 changes: 8 additions & 1 deletion packages/bitcore-node/test/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { StateStorage } from '../../src/models/state';
import * as sinon from 'sinon';
import sinon from 'sinon';
import { BlockStorage } from '../../src/models/block';
import { TransactionStorage } from '../../src/models/transaction';
import { CoinStorage } from '../../src/models/coin';
Expand Down Expand Up @@ -58,3 +58,10 @@ export function mockStorage(toReturn, collectionMethods = {}) {
} as any;
return Storage;
}

export function mockModel(collectionName: string, toReturn: any, collectionMethods = {}) {
const isStubbed: sinon.SinonStub = Storage.db!.collection as sinon.SinonStub;
if (isStubbed.withArgs) {
isStubbed.withArgs(collectionName).returns(mockCollection(toReturn, collectionMethods));
}
}
60 changes: 30 additions & 30 deletions packages/bitcore-node/test/unit/models/coin.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { SpentHeightIndicators } from '../../../src/types/Coin';
import { ObjectId } from 'mongodb';
import sinon from 'sinon';
import { BlockStorage } from '../../../src/models/block';
import { mockStorage } from '../../helpers/index.js';
import { mockStorage, mockModel } from '../../helpers/index.js';

describe('Coin Model', function () {
describe('Coin Model', function() {
describe('_apiTransform', () => {
it('should return the transform object with coin info', () => {
let id = new ObjectId();
Expand Down Expand Up @@ -91,41 +91,41 @@ describe('Coin Model', function () {
sandbox.restore();
});

it('should return an object with confirmed, unconfirmed, and balance when additional time parameter is passed in', async () => {
let id = new ObjectId("5c364e342ab5602e97a56f0e");
it('should return an object with confirmed, unconfirmed, and balance when additional time parameter is passed in', async () => {
let id = new ObjectId('5c364e342ab5602e97a56f0e');
let chain = 'BTC';
let network = 'regtest';
let time = new Date().toISOString();
let query = { wallets: id, 'wallets.0': { $exists: true } };
let matchObject = {
"$or": [
let matchObject = {
$or: [
{
"spentHeight": {
"$gt": 123
spentHeight: {
$gt: 123
}
},
{
"spentHeight": {
"$lt": 0
spentHeight: {
$lt: 0
}
}
],
"mintHeight": {
"$lte": 123
mintHeight: {
$lte: 123
},
"wallets": new ObjectId('5c364e342ab5602e97a56f0e'),
"wallets.0": { '$exists': true },
}
wallets: new ObjectId('5c364e342ab5602e97a56f0e'),
'wallets.0': { $exists: true }
};

let blockModelHeight = { height: 123 };
mockStorage([{_id: 'confirmed', balance: 123123}, {_id: 'unconfirmed', balance: 1}]);
Object.assign(BlockStorage.collection.findOne, sandbox.stub().resolves(blockModelHeight));
mockModel('coins', [{ _id: 'confirmed', balance: 123123 }, { _id: 'unconfirmed', balance: 1 }]);
mockModel('blocks', blockModelHeight);
let coinModelAggregateSpy = CoinStorage.collection.aggregate as sinon.SinonSpy;
let blockModelFindOneSpy = BlockStorage.collection.findOne as sinon.SinonSpy;
let blockModelFindSpy = BlockStorage.collection.find as sinon.SinonSpy;

const result = await CoinStorage.getBalanceAtTime({query, time, chain, network});
expect(coinModelAggregateSpy.called).to.deep.equal(true, "CoinStorage.aggregation should have been called");
expect(blockModelFindOneSpy.called).to.deep.equal(true, 'BlockModel.findOne should have been called');
const result = await CoinStorage.getBalanceAtTime({ query, time, chain, network });
expect(coinModelAggregateSpy.called).to.deep.equal(true, 'CoinStorage.aggregation should have been called');
expect(blockModelFindSpy.called).to.deep.equal(true, 'BlockModel.find should have been called');
expect(coinModelAggregateSpy.getCall(0).args[0][0].$match).to.deep.equal(matchObject);
expect(result).to.has.property('confirmed');
expect(result).to.has.property('unconfirmed');
Expand All @@ -143,20 +143,20 @@ describe('Coin Model', function () {
sandbox.restore();
});

it('should return an object with confirmed, unconfirmed, and balance', async () => {
let id = new ObjectId("5c364e342ab5602e97a56f0e");
it('should return an object with confirmed, unconfirmed, and balance', async () => {
let id = new ObjectId('5c364e342ab5602e97a56f0e');
let query = {
"wallets": id,
'wallets.0': { "$exists": true },
"spentHeight": { "$lt": 0 },
"mintHeight": { "$gt": -3 }
wallets: id,
'wallets.0': { $exists: true },
spentHeight: { $lt: 0 },
mintHeight: { $gt: -3 }
};

mockStorage([{_id: 'confirmed', balance: 123123}, {_id: 'unconfirmed', balance: 1}]);
mockStorage([{ _id: 'confirmed', balance: 123123 }, { _id: 'unconfirmed', balance: 1 }]);
let coinModelAggregateSpy = CoinStorage.collection.aggregate as sinon.SinonSpy;

const result = await CoinStorage.getBalance({query});
expect(coinModelAggregateSpy.called).to.deep.equal(true, "CoinStorage.aggregation should have been called");
const result = await CoinStorage.getBalance({ query });
expect(coinModelAggregateSpy.called).to.deep.equal(true, 'CoinStorage.aggregation should have been called');
expect(coinModelAggregateSpy.getCall(0).args[0][0].$match).to.deep.equal(query);
expect(result).to.has.property('confirmed');
expect(result).to.has.property('unconfirmed');
Expand Down

0 comments on commit 56bd7a7

Please sign in to comment.