Skip to content

Commit

Permalink
AddressGenerator @ 100% coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
proletesseract committed Mar 18, 2017
1 parent 0500024 commit 551080f
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/lib/AddressGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ AddressGenerator.getAccountAddressesForGeneration = (options, callback) => {
if (err.code === -12) {
AddressGenerator.runKeypoolRefill(options, callback)
} else {
console.log('ERROR: client.getAddressesByAccount', err)
callback(false)
Logger.writeLog('ADG_004', 'client.getAddressesByAccount', { err })
callback(false, err)
return
}
})
Expand All @@ -84,8 +84,8 @@ AddressGenerator.generateNewAccountAddresses = (options, callback) => {
if (err.code === -12) {
AddressGenerator.runKeypoolRefill(options, callback)
} else {
console.log('ERROR: client.getNewAddress', err)
callback(false)
Logger.writeLog('ADG_005', 'client.getNewAddress', { err })
callback(false, err)
return
}
})
Expand Down
133 changes: 133 additions & 0 deletions test/AddressGenerator.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,5 +150,138 @@ describe('[AddressGenerator]', () => {
maxAddresses: 100,
}, callback)
})
it('fail client.getAddressesByAccount catch all error', (done) => {
const mockClient = {
getAddressesByAccount: () => { return Promise.reject({ code: -17 }) },
}

const callback = (success, err) => {
expect(success).toBe(false)
expect(err.code).toBe(-17)
sinon.assert.calledOnce(mockLogger.writeLog)
done()
}

const mockLogger = {
writeLog: sinon.spy(),
}
AddressGenerator.__set__('Logger', mockLogger)

AddressGenerator.getAccountAddressesForGeneration({
accountName: 'incomingAccount',
client: mockClient,
maxAddresses: 100,
}, callback)
})
it('should return true because the max addresses already generated', (done) => {
const addresses = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]

const mockClient = {
getAddressesByAccount: () => { return Promise.resolve(addresses) },
}

const callback = (success) => {
expect(success).toBe(true)
done()
}

AddressGenerator.getAccountAddressesForGeneration({
accountName: 'incomingAccount',
client: mockClient,
maxAddresses: 10,
}, callback)
})
it('should call the generate function because more addresses are required ', (done) => {
const addresses = [1, 2, 3, 4, 5]

const mockClient = {
getAddressesByAccount: () => { return Promise.resolve(addresses) },
}

const callback = () => {}

AddressGenerator.generateNewAccountAddresses = (options, parsedCallback) => {
expect(parsedCallback).toBe(callback)
expect(options.numToGenerate).toBe(5)
expect(options.client).toBe(mockClient)
expect(options.maxAddresses).toBe(10)
expect(options.accountName).toBe('incomingAccount')
done()
}

AddressGenerator.getAccountAddressesForGeneration({
accountName: 'incomingAccount',
client: mockClient,
maxAddresses: 10,
}, callback)
})
})
describe('(generateNewAccountAddresses)', () => {
before(() => { // reset the rewired functions
AddressGenerator = rewire('../src/lib/AddressGenerator')
})
it('should fail client.getNewAddress with error 12', (done) => {
const mockClient = {
getNewAddress: () => { return Promise.reject({ code: -12 }) },
}

AddressGenerator.runKeypoolRefill = () => {
expect(true).toBe(true)
done()
}

const callback = () => {}

AddressGenerator.generateNewAccountAddresses({
accountName: 'incomingAccount',
client: mockClient,
maxAddresses: 100,
numToGenerate: 100,
}, callback)
})
it('should fail client.getNewAddress catch all error', (done) => {
const mockClient = {
getNewAddress: () => { return Promise.reject({ code: -17 }) },
}

AddressGenerator.runKeypoolRefill = () => {
expect(true).toBe(true)
done()
}

const callback = (success, err) => {
expect(success).toBe(false)
expect(err.code).toBe(-17)
sinon.assert.calledOnce(mockLogger.writeLog)
done()
}
const mockLogger = {
writeLog: sinon.spy(),
}
AddressGenerator.__set__('Logger', mockLogger)
AddressGenerator.generateNewAccountAddresses({
accountName: 'incomingAccount',
client: mockClient,
maxAddresses: 100,
numToGenerate: 100,
}, callback)
})
it('should generate the address and call recurse until numToGenerate is 0', (done) => {
const mockClient = {
getNewAddress: () => { return Promise.resolve('123412341234') },
}

const callback = (success) => {
expect(success).toBe(true)
done()
}

AddressGenerator.generateNewAccountAddresses({
accountName: 'incomingAccount',
client: mockClient,
maxAddresses: 10,
numToGenerate: 10,
}, callback)
})
})
})

0 comments on commit 551080f

Please sign in to comment.