-
-
Save hserang/9397acabb9fe6c65e629 to your computer and use it in GitHub Desktop.
'use strict'; | |
var _ = require('lodash'); | |
var Chance = require('chance'); | |
var Factory = require('rosie').Factory; | |
function getExternalTransaction(chance) { | |
function getAmount() { | |
return chance.floating({ | |
fixed: 2, | |
min: 1, | |
max: 500 | |
}); | |
} | |
function getId() { | |
return chance.natural({ | |
min: 1, | |
max: 99999 | |
}); | |
} | |
Factory.define('externalTransaction') | |
.attr('external_account_id', chance.hash({length: 64})) | |
.attr('source_amount', getAmount()) | |
.attr('source_currency', chance.currency().code) | |
.attr('source_account_id', getId()) | |
.attr('destination_amount', getAmount()) | |
.attr('destination_currency', chance.currency().code) | |
.attr('destination_account_id', getId()) | |
.attr('amount', getAmount()) | |
.attr('currency', chance.currency().code) | |
.attr('invoice_id', 'bc7e8a24e2911a5827c9b33d618531ef094937f2b3803a591c625d0ede1fffc6') | |
.attr('memos', chance.sentence({words: 5})) | |
.attr('deposit', true) | |
.attr('data', {}) | |
.attr('status', 'invoice') | |
.attr('ripple_transaction_id', null) | |
.attr('uid', getId().toString()) | |
.attr('to_account_id', getId()); | |
return Factory.build('externalTransaction'); | |
} | |
function makeValidTransactions(howMany, seed) { | |
howMany = howMany || 1; | |
seed = seed || Math.random(); | |
var transactionsData = ( | |
_.times(howMany, function() { | |
var chance = new Chance(seed); | |
return getExternalTransaction(chance); | |
})); | |
return transactionsData; | |
} | |
/*eslint-disable max-len*/ | |
var apiResponse = { | |
success: function(originalResponse, updates) { | |
updates = updates || {}; | |
// construct working response from original data | |
// and specific data added by sequelize | |
var response = { | |
external_transactions: [_.extend({}, { | |
id: updates.id, | |
createdAt: updates.createdAt, | |
updatedAt: updates.updatedAt}, originalResponse) | |
], | |
success: true | |
}; | |
return response; | |
}, | |
error: { | |
} | |
}; | |
/*eslint-enable*/ | |
module.exports = { | |
transactions: { | |
valid: makeValidTransactions, | |
invalid: {} | |
}, | |
apiResponse: { | |
success: apiResponse.success, | |
error: apiResponse.error | |
} | |
}; |
Line 56: agree, one transaction can be be an array with one element
Line 93: I would prefer the current structure to see what is exported more easily without the need to look up in the class and get the additional information you would need. In this case that doesn't really add up since you would still have to look at the methods to determine what properties to pass in. Still I think I prefer the current structure unless it becomes a lot bigger.
Seeding is added, defaulting to Math.random().
Always returns an array now.
Thanks for the feedback!
chance.bool();
here? https://gist.github.com/hserang/9397acabb9fe6c65e629#file-example_factory-L36
chance.pick(['invoice', 'queued', 'completed']);
here? https://gist.github.com/hserang/9397acabb9fe6c65e629#file-example_factory-L36
Line 51: If you decide to use this for unit tests, you might want to provide a seed so that the tests are deterministic.
Line 56: It's confusing to have a different return type for one case
Line 63/86: Looks like you don't need this disable statement anymore
Line 93: You can just do
apiResponse: apiResponse