Skip to content
This repository has been archived by the owner on Mar 24, 2021. It is now read-only.

Commit

Permalink
Set '_updated_at' when a record is saved
Browse files Browse the repository at this point in the history
The '_updated_at' should when the record was last updated. This was
missed in the earlier save work.
  • Loading branch information
robyoung authored and jcbashdown committed Jun 2, 2014
1 parent e4d3ff4 commit c4f6b60
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
2 changes: 1 addition & 1 deletion backdrop/core/storage/mongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from bson import Code

from .. import timeutils
from ... import statsd


logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -83,4 +82,5 @@ def empty(self, data_set_id):
self._coll(data_set_id).remove({})

def save(self, data_set_id, record):
record['_updated_at'] = timeutils.now()
self._coll(data_set_id).save(record)
17 changes: 13 additions & 4 deletions tests/core/storage/test_mongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
amount of setup and mocking. Small unit tests are in doctest format in the
module itself.
"""
from hamcrest import assert_that, is_, has_item, has_entries, is_not
import datetime

from hamcrest import assert_that, is_, has_item, has_entries, is_not, \
has_entry, instance_of
from nose.tools import assert_raises
from mock import Mock

Expand All @@ -26,6 +29,9 @@ def setup(self):
def teardown(self):
self.mongo.drop_database('backdrop_test')

def _save(self, dataset_id, **kwargs):
self.db[dataset_id].save(kwargs)

# ALIVE
def test_alive_returns_true_if_mongo_is_up(self):
assert_that(self.engine.alive(), is_(True))
Expand Down Expand Up @@ -69,9 +75,6 @@ def test_create_fails_if_collection_already_exists(self):
assert_raises(CollectionInvalid,
self.engine.create_dataset, 'foo_bar', 0)

def _save(self, dataset_id, **kwargs):
self.db[dataset_id].save(kwargs)

# GET LAST UPDATED
def test_get_last_udpated(self):
self._save('foo_bar', _id='first', _updated_at=d_tz(2013, 3, 1))
Expand Down Expand Up @@ -101,6 +104,12 @@ def test_save_a_record(self):

assert_that(self.db['foo_bar'].count(), is_(1))

def test_save_a_record_adds_an_updated_at(self):
self.engine.save('foo_bar', {'_id': 'first'})

assert_that(self.db['foo_bar'].find_one(),
has_entry('_updated_at', instance_of(datetime.datetime)))


class TestReconnectingSave(object):
def test_reconnecting_save_retries(self):
Expand Down

0 comments on commit c4f6b60

Please sign in to comment.