|
| 1 | +/* tslint:disable:no-relative-imports */ |
| 2 | +import { describe } from 'riteway' |
| 3 | +import { runtimeId, setUpDb } from '../../tests/helpers/utils' |
| 4 | +import { BlockInfoDAO } from './BlockInfoDAO' |
| 5 | + |
| 6 | +const prefix = runtimeId() |
| 7 | + |
| 8 | +describe('BlockInfoDAO.insertBlockInfo', async assert => { |
| 9 | + const db = await setUpDb(prefix) |
| 10 | + const { result, mongoClient } = await db.collection('blockchainInfo') |
| 11 | + const blockchainInfoCollection = result |
| 12 | + const blockInfoDAO = new BlockInfoDAO(blockchainInfoCollection) |
| 13 | + await blockInfoDAO.start() |
| 14 | + const expected = { height: 12345, hash: 'this-is-a-hash', previousHash: 'this-is-a-parent-hash' } |
| 15 | + await blockInfoDAO.insertBlockInfo(expected) |
| 16 | + const actual = await blockchainInfoCollection.find().limit(1).next() |
| 17 | + |
| 18 | + assert({ |
| 19 | + given: 'a lightBlock', |
| 20 | + should: 'store the block in mongodb', |
| 21 | + actual, |
| 22 | + expected, |
| 23 | + }) |
| 24 | + |
| 25 | + await mongoClient.close() |
| 26 | + await db.teardown() |
| 27 | +}) |
| 28 | + |
| 29 | +describe('BlockInfoDAO.getHighestBlock', async assert => { |
| 30 | + const db = await setUpDb(prefix) |
| 31 | + const { result, mongoClient } = await db.collection('blockchainInfo') |
| 32 | + const blockchainInfoCollection = result |
| 33 | + const blockInfoDAO = new BlockInfoDAO(blockchainInfoCollection) |
| 34 | + await blockInfoDAO.start() |
| 35 | + await blockInfoDAO.insertBlockInfo({ height: 1234, hash: 'wrong-hash', previousHash: 'wrong-hash-parent' }) |
| 36 | + const expected = { height: 54321, hash: 'good-hash', previousHash: 'parentHash '} |
| 37 | + await blockInfoDAO.insertBlockInfo(expected) |
| 38 | + const actual = await blockInfoDAO.getHighestBlock() |
| 39 | + |
| 40 | + assert({ |
| 41 | + given: 'multiple blocks in mongo', |
| 42 | + should: 'retrieve the block with the greatest height', |
| 43 | + actual, |
| 44 | + expected, |
| 45 | + }) |
| 46 | + |
| 47 | + await mongoClient.close() |
| 48 | + await db.teardown() |
| 49 | +}) |
0 commit comments