Last active
March 6, 2018 01:32
-
-
Save hserang/9397acabb9fe6c65e629 to your computer and use it in GitHub Desktop.
Example fixture factory using rosie and chance.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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 | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
chance.bool();
here? https://gist.github.com/hserang/9397acabb9fe6c65e629#file-example_factory-L36chance.pick(['invoice', 'queued', 'completed']);
here? https://gist.github.com/hserang/9397acabb9fe6c65e629#file-example_factory-L36