Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Micah - made mongo store date longs so query can get transactions ord…

…ered by date.
  • Loading branch information...
commit 732f9da021179e8cef184fb97c5c3852805f0cac 1 parent 5d1afb4
Micah Silverman authored
View
2  features/history.feature
@@ -32,4 +32,4 @@ Feature: History
Then I should get back the following transactions
| date | amount | description | category |
| 2/2/2012 | 13.45 | another cool purchase | another cool categroy |
- | 2/4/2012 | 15.45 | a fourth cool purchase | a fourth cool categroy |
+ | 2/4/2012 | 15.45 | a fourth cool purchase | a fourth cool categroy |
View
17 features/step_definitions/history_steps.js
@@ -9,7 +9,8 @@ Given(/^the transaction data has been submitted$/, function(step) {
mongo.open(function(err, db) {
global.mongodb = db;
- _.each(self.transactions, function(transaction, index) {
+ // reverse proves that we should get ascending order by date later
+ _.each(self.transactions.reverse(), function(transaction, index) {
Transaction.insert(transaction, function(err, transaction) {
if (err) throw err;
if (index === self.transactions.length-1) {
@@ -39,10 +40,10 @@ When(/^I get the history$/, function(step) {
Then(/^I should get back the history with ids$/, function(step) {
var self = this;
_.each(this.resBody.result, function(transaction, index) {
- assert.equal(transaction.date, self.transactions[index].date);
- assert.equal(transaction.amount, self.transactions[index].amount);
- assert.equal(transaction.description, self.transactions[index].description);
- assert.equal(transaction.category, self.transactions[index].category);
+ assert.equal(transaction.date, self.expected[index].date);
+ assert.equal(transaction.amount, self.expected[index].amount);
+ assert.equal(transaction.description, self.expected[index].description);
+ assert.equal(transaction.category, self.expected[index].category);
assert.notEqual(transaction._id, null);
})
step.done();
@@ -52,7 +53,7 @@ When(/^I process transactions with the following dates$/, function(step, table)
var self = this;
var ids = [];
-
+
_.each(table.hashes(), function(row) {
var transaction = _.find(self.resBody.result, function(transaction) {
return (transaction.date === row.date)
@@ -60,7 +61,7 @@ When(/^I process transactions with the following dates$/, function(step, table)
ids.push(transaction._id);
})
-
+
var endpoint = "http://localhost:3000/api/v1/process";
request({
@@ -85,4 +86,4 @@ var pullIds = function(transactions) {
Then(/^I should get back the following transactions$/, function(step, table) {
assert.deepEqual(table.hashes(), pullIds(this.resBody.result));
step.done();
-});
+});
View
3  features/step_definitions/transaction_steps.js
@@ -4,6 +4,7 @@ var helpers = require('../support/helpers');
Given(/^I have the following transaction data$/, function(step, table) {
this.transactions = table.hashes();
this.transaction = this.transactions[0];
+ this.expected = table.hashes();
step.done();
});
@@ -36,4 +37,4 @@ Then(/^I should get a successful response$/, function(step) {
})
})
})
-});
+});
View
19 lib/models/transaction_model.js
@@ -8,6 +8,8 @@ var Transaction = exports;
var collectionName = 'transactions';
Transaction.insert = function(transaction, callback) {
+ // convert date string to long date
+ transaction.date = new Date(transaction.date).getTime();
mongodb.collection(collectionName, function(err, collection) {
collection.insert(transaction, function(err, transaction) {
if (err) {
@@ -35,5 +37,18 @@ Transaction.remove = function(transactions, callback) {
}
Transaction.find = function(selector, fieldList, callback) {
- MongoHelper.find(selector, fieldList, collectionName, callback);
-}
+ MongoHelper.find(selector, fieldList, collectionName, function(err, results) {
+ if (err) {
+ return callback(err);
+ } else {
+ _.each(results, function(result) {
+ if (result.date) {
+ var date = new Date(result.date);
+ result.date = "" + (date.getMonth()+1) + "/" +
+ date.getDate() + "/" + date.getFullYear();
+ }
+ });
+ return callback(null, results);
+ }
+ });
+}
View
4 lib/services/history_service.js
@@ -2,7 +2,7 @@ var Transaction = require('../models/transaction_model.js')
module.exports = {
getAllHistory: function(callback) {
- Transaction.find({}, {}, callback);
+ Transaction.find({$query: {}, $orderby: {date:1}}, {}, callback);
},
process: function(transactions, callback) {
var self = this;
@@ -10,4 +10,4 @@ module.exports = {
self.getAllHistory(callback);
})
}
-}
+}
View
37 test/unit/models/transaction_model.test.js
@@ -0,0 +1,37 @@
+/**
+ * Module dependencies.
+ */
+var testHelper = require('test_helper');
+var describe = testHelper.describe(exports);
+var app = testHelper.createTestServer();
+var controller = require('controllers/site_controller')(app);
+var service = require('services/transaction_service');
+var _ = require('underscore')._
+
+/**
+ * Required modules for stubbing
+ */
+var Transaction = require('models/transaction_model');
+
+describe('when inserting a transaction', function(it) {
+ var expectedDate = '2/2/2012';
+ var testTransaction = { date: expectedDate, amount: '12.50',
+ desc: 'some description', category: 'some category' };
+
+ beforeEach(function(test) {
+ test.stub(mongodb, 'collection').yields(null, _collection);
+ test.stub(_collection, 'insert', function(transaction, callback) {
+ callback(null, transaction);
+ });
+ });
+
+ it('should convert a the passed in date string to a Date object', function(test) {
+ Transaction.insert(testTransaction, function(err, transaction) {
+ var gotDateObj = new Date(transaction.date);
+ var gotDate = "" + (gotDateObj.getMonth()+1) + "/" +
+ gotDateObj.getDate() + "/" + gotDateObj.getFullYear();
+ assert.equal(expectedDate, gotDate);
+ test.finish();
+ });
+ });
+});
Please sign in to comment.
Something went wrong with that request. Please try again.