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

Commit

Permalink
Switch migrations to use Repository as appropriate
Browse files Browse the repository at this point in the history
In order for the _updated_at field to be correctly applied the
migrations that operate on buckets need to be using the Repository class
to modify the data in mongodb.

The Repository find() method had to be modified to be able to use both
classes that provide a to_mongo_query() method (the Query class) or a
Dictionary as would be usual with a mongodb find.

I have added some tests to make sure that Repository will work correctly
when accepting both types of find() argument.
  • Loading branch information
tombooth committed Dec 16, 2013
1 parent 8f60ebe commit 98cd538
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 5 deletions.
5 changes: 4 additions & 1 deletion backdrop/core/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,10 @@ def find(self, query, sort=None, limit=None):

self._validate_sort(sort)

return self._mongo_driver.find(query.to_mongo_query(), sort, limit)
is_class = hasattr(query, 'to_mongo_query')
mongo_query = query.to_mongo_query() if is_class else query

return self._mongo_driver.find(mongo_query, sort, limit)

def group(self, group_by, query, sort=None, limit=None, collect=None):
if sort:
Expand Down
2 changes: 1 addition & 1 deletion migrations/001_add_week_start.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
def up(db):
for name in db.collection_names():
log.info("Migrating collection: {0}".format(name))
collection = db.get_collection(name)
collection = db.get_repository(name)
query = {
"_timestamp": {"$exists": True},
"_week_start_at": {"$exists": False}
Expand Down
2 changes: 1 addition & 1 deletion migrations/002_add_month_start.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
def up(db):
for name in db.collection_names():
log.info("Migrating collection: {0}".format(name))
collection = db.get_collection(name)
collection = db.get_repository(name)
query = {
"_timestamp": {"$exists": True},
"_month_start_at": {"$exists": False}
Expand Down
2 changes: 1 addition & 1 deletion migrations/005_add_hour_and_day_start.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
def up(db):
for name in db.collection_names():
log.info("Migrating collection: {0}".format(name))
collection = db.get_collection(name)
collection = db.get_repository(name)
query = {
"_timestamp": {"$exists": True},
"_day_start_at": {"$exists": False}
Expand Down
2 changes: 1 addition & 1 deletion migrations/007_add_quarter_start.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
def up(db):
for name in db.collection_names():
log.info("Migrating collection: {0}".format(name))
collection = db.get_collection(name)
collection = db.get_repository(name)
query = {
"_timestamp": {"$exists": True},
"_quarter_start_at": {"$exists": False}
Expand Down
27 changes: 27 additions & 0 deletions tests/core/integration/test_database_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,33 @@ def test_period_query_for_data_with_no__week_start_at(self):
assert_that(result, has_item(has_entry("_count", 1)))


class TestRepositoryIntegration_Finding(RepositoryIntegrationTest):
def test_query_class(self):
self.mongo_collection.save({"foo": 1})
self.mongo_collection.save({"foo": 2})
self.mongo_collection.save({"foo": 3})

result = self.repo.find(Query.create())

assert_that(list(result), contains(
has_entry("foo", 1),
has_entry("foo", 2),
has_entry("foo", 3),
))

def test_query_map(self):
self.mongo_collection.save({"foo": 1})
self.mongo_collection.save({"foo": 2})
self.mongo_collection.save({"foo": 3})

result = self.repo.find({"foo": 1})

assert_that(result.count(), equal_to(1))
assert_that(list(result), contains(
has_entry("foo", 1),
))


class TestDatabase(unittest.TestCase):
def setUp(self):
self.db = Database('localhost', 27017, 'backdrop_test')
Expand Down

0 comments on commit 98cd538

Please sign in to comment.