Ejemplo n.º 1
0
def run():
    parser = build_parser()
    args = parser.parse_args()
    s = IndexApplier(args.source, args.dest)
    s.run()
Ejemplo n.º 2
0
class TestIndexOperations(unittest.TestCase):
    def create_connection(self):
        conn = MagicMock(MongoClient)
        db = MagicMock(Database)
        coll = MagicMock(Collection)
        conn.__getitem__ = MagicMock(return_value=db)
        db.__getitem__ = MagicMock(return_value=coll)
        return conn

    def setUp(self):
        self.src = self.create_connection()
        self.dest = self.create_connection()
        self.applier = IndexApplier('localhost', 'localhost')
        self.applier._src = self.src
        self.applier._dest = self.dest

    def test_get_databases(self):
        dbs = ['wifastportal', 'workflows', 'local']
        self.src.database_names = MagicMock(return_value=dbs)
        res = self.applier.get_public_databases(self.src)
        assert 'wifastportal' in res
        assert 'workflows' in res
        assert 'local' not in res

    def test_get_collection_names(self):
        colls = ['mypornocollection']
        self.src['db'].collection_names = MagicMock(return_value=colls)
        res = self.applier.get_collection_names(self.src, 'db')
        assert 'mypornocollection' in res

    def test_get_index_information(self):
        self.src['db']['coll'].index_information = MagicMock(return_value=2)
        assert self.applier._get_index_information(self.src, 'db', 'coll') == 2

    def test_create_collection(self):
        self.src['db'].collection_names = MagicMock(return_value=['test'])
        self.applier.create_collection(self.src, 'db', 'test1')
        self.applier.create_collection(self.src, 'db', 'test')
        self.src['db'].create_collection.assert_called_with('test1')

    def test_get_indexes(self):
        self.src.database_names = MagicMock(return_value=['test'])
        self.src['db'].collection_names = MagicMock(return_value=['test'])
        index_info = {'_id_': {'key': [('_id', 1)]}}
        self.src['db']['coll'].index_information = MagicMock(return_value=index_info)
        res = self.applier._get_all_indexes_on_server(self.src)
        assert res == {'test': {'test': [{'key': [('_id', 1)]}]}}

    def test_transform_key(self):
        old_key = [('business_id', 1.0)]
        new_key = self.applier._transform_key(old_key)
        assert new_key == [('business_id', 1)]

    def test_create_index(self):
        self.src['db']['coll'].create_index = MagicMock()
        index = {'key': [('business_id', 1), ('client_mac', 1)], 'unique': True}
        self.applier.create_index(self.src, 'db', 'coll', index)
        self.src['db']['coll'].create_index.assert_called_with(index['key'], unique=True)

    def test_run(self):
        some_index = {'key': [('_id', 1)]}
        all_indexes = {'some_db': {'some_coll': [some_index]}}
        self.applier._get_all_indexes_on_server = MagicMock(return_value=all_indexes)
        self.applier.create_index = MagicMock()
        self.applier.run()
        self.applier.create_index.assert_called_with(self.dest, 'some_db', 'some_coll',
                                                     some_index)