'Test mapping of namespaces
Cases:
upsert/delete/update of documents:
1. in namespace set, mapping provided
2. outside of namespace set, mapping provided'

def test_namespace_mapping(self):

    included_names = ['includedb1.includecol1', 'includedb1.includecol2', 'includedb2.includecol1']
    excluded_names = ['includedb2.excludecol2', 'excludedb3.excludecol1']
    dest_mapping = {'includedb1.*': 'newdb1.*', 'includedb2.includecol1': 'newdb2.newcol1'}
    self.reset_opman(dest_mapping=dest_mapping)
    docman = self.opman.doc_managers[0]
    namespace_config = self.opman.namespace_config
    self.opman.start()
    base_doc = {'_id': 1, 'name': 'superman'}
    for ns in included_names:
        (db, coll) = ns.split('.', 1)
        self.primary_conn[db][coll].insert_one(base_doc)
        assert_soon((lambda : (len(docman._search()) == 1)))
        self.assertEqual(docman._search()[0]['ns'], namespace_config.map_namespace(ns))
        bad = [d for d in docman._search() if (d['ns'] == ns)]
        self.assertEqual(len(bad), 0)
        self.primary_conn[db][coll].update_one({'_id': 1}, {'$set': {'weakness': 'kryptonite'}})
        def update_complete():
            docs = docman._search()
            for d in docs:
                if (d.get('weakness') == 'kryptonite'):
                    return True
                return False
        assert_soon(update_complete)
        self.assertEqual(docman._search()[0]['ns'], namespace_config.map_namespace(ns))
        bad = [d for d in docman._search() if (d['ns'] == ns)]
        self.assertEqual(len(bad), 0)
        self.primary_conn[db][coll].delete_one({'_id': 1})
        assert_soon((lambda : (len(docman._search()) == 0)))
        bad = [d for d in docman._search() if (d['ns'] == namespace_config.map_namespace(ns))]
        self.assertEqual(len(bad), 0)
        self.primary_conn[db][coll].drop()
        self.opman.doc_managers[0]._delete()
    for ns in excluded_names:
        (db, coll) = ns.split('.', 1)
        self.primary_conn[db][coll].insert_one(base_doc)
        time.sleep(1)
        self.assertEqual(len(docman._search()), 0)
        self.primary_conn[db][coll].update_one({'_id': 1}, {'$set': {'weakness': 'kryptonite'}})
        time.sleep(1)
        self.assertEqual(len(docman._search()), 0)
