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

Commit

Permalink
migrate data sets to buckets with test
Browse files Browse the repository at this point in the history
  • Loading branch information
jcbashdown committed Apr 23, 2014
1 parent f8aac8d commit d4bb91f
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 0 deletions.
10 changes: 10 additions & 0 deletions migrations/010_change_buckets_to_data_sets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"""
Change user buckets in users collection to data_sets
We have this as separate module to the migration
due to problems with naming python modules starting with digits
"""
from migrations.lib import change_buckets_to_data_sets


def up(db):
change_buckets_to_data_sets.up(db)
Empty file added migrations/__init__.py
Empty file.
Empty file added migrations/lib/__init__.py
Empty file.
24 changes: 24 additions & 0 deletions migrations/lib/change_buckets_to_data_sets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""
Change user buckets in users collection to data_sets
We have this as separate module to the migration
due to problems with naming python modules starting with digits
"""
import logging

from backdrop.core.records import Record
from backdrop.core.timeutils import utc

log = logging.getLogger(__name__)


def up(db):
mongo_db = db._mongo['backdrop']
users_collection = mongo_db['users']
users = users_collection.find()
for user in users:
if 'buckets' in user:
user['data_sets'] = user['buckets']
del(user['buckets'])

mongo_db['users'].remove(user['email'])
mongo_db['users'].insert(user)
Empty file added tests/migrations/__init__.py
Empty file.
Empty file.
42 changes: 42 additions & 0 deletions tests/migrations/lib/test_change_buckets_to_data_sets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import unittest
from pymongo import MongoClient
from hamcrest import *

from backdrop.core import database, data_set
from backdrop.core.data_set import DataSetConfig
from migrations.lib.change_buckets_to_data_sets import up

HOST = ['localhost']
PORT = 27017
DB_NAME = 'backdrop'
DATA_SET = 'users'


class ChangeBucketsToDataSetTestCase(unittest.TestCase):
def setUp(self):
self.db = database.Database(HOST, PORT, DB_NAME)
self.data_set = data_set.DataSet(
self.db, DataSetConfig(DATA_SET, data_group="group", data_type="type", max_age_expected=1000))
self.mongo_collection = MongoClient(HOST, PORT)[DB_NAME][DATA_SET]
self.mongo_collection.drop()
self.data_sets = [
"carers_allowance_weekly_claims",
"waste_carriers_registration_transactions_by_channel",
"waste_carriers_registration_cost_per_transaction"
]
self.mongo_collection.save({
"_id": "person@digital.cabinet-office.gov.uk",
"buckets": self.data_sets,
"email": "person@digital.cabinet-office.gov.uk"
})

def tearDown(self):
self.mongo_collection.drop()

def test_that_buckets_changes_to_data_sets(self):
up(self.db)
users = self.db.get_repository('users').find({})
assert_that(users.count(), is_(1))
for user in users:
assert_that("buckets" in user, is_(False))
assert_that(user["data_sets"], is_(self.data_sets))

0 comments on commit d4bb91f

Please sign in to comment.