Skip to content

Instantly share code, notes, and snippets.

@hserang
Last active March 6, 2018 01:32
Show Gist options
  • Save hserang/9397acabb9fe6c65e629 to your computer and use it in GitHub Desktop.
Save hserang/9397acabb9fe6c65e629 to your computer and use it in GitHub Desktop.
Example fixture factory using rosie and chance.js
'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
}
};
@geertweening
Copy link

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.

@hserang
Copy link
Author

hserang commented Apr 3, 2015

Seeding is added, defaulting to Math.random().
Always returns an array now.
Thanks for the feedback!

@nerdylocks
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment