'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):

    source_ns = ['test.test1', 'test.test2']
    phony_ns = ['test.phony1', 'test.phony2']
    dest_mapping = {'test.test1': 'test.test1_dest', 'test.test2': 'test.test2_dest'}
    self.opman.namespace_config = NamespaceConfig(namespace_set=source_ns, namespace_options=dest_mapping)
    docman = self.opman.doc_managers[0]
    self.opman.start()
    base_doc = {'_id': 1, 'name': 'superman'}
    for ns in source_ns:
        (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'], dest_mapping[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'], dest_mapping[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'] == dest_mapping[ns])]
        self.assertEqual(len(bad), 0)
        self.primary_conn[db][coll].delete_many({})
        self.opman.doc_managers[0]._delete()
    for ns in phony_ns:
        (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)
