def test_id_shuffling(self):
        manip = ObjectIdShuffler()
        collection = self.db.test

        def incoming_moves_id(son_in):
            son = manip.transform_incoming(son_in, collection)
            if not "_id" in son:
                return True
            for (k, v) in son.items():
                self.assertEqual(k, "_id")
                break
            # Key order matters in SON equality test,
            # matching collections.OrderedDict
            if isinstance(son_in, SON):
                return son_in.to_dict() == son.to_dict()
            return son_in == son

        self.assertTrue(incoming_moves_id({}))
        self.assertTrue(incoming_moves_id({"_id": 12}))
        self.assertTrue(incoming_moves_id({"hello": "world", "_id": 12}))
        self.assertTrue(
            incoming_moves_id(SON([("hello", "world"), ("_id", 12)])))

        def outgoing_is_identity(son):
            return son == manip.transform_outgoing(son, collection)

        qcheck.check_unittest(self, outgoing_is_identity,
                              qcheck.gen_mongo_dict(3))
Ejemplo n.º 2
0
 def test_manipulator_properties(self):
     db = self.client.foo
     self.assertEqual([], db.incoming_manipulators)
     self.assertEqual([], db.incoming_copying_manipulators)
     self.assertEqual([], db.outgoing_manipulators)
     self.assertEqual([], db.outgoing_copying_manipulators)
     db.add_son_manipulator(AutoReference(db))
     db.add_son_manipulator(NamespaceInjector())
     db.add_son_manipulator(ObjectIdShuffler())
     self.assertEqual(1, len(db.incoming_manipulators))
     self.assertEqual(db.incoming_manipulators, ['NamespaceInjector'])
     self.assertEqual(2, len(db.incoming_copying_manipulators))
     for name in db.incoming_copying_manipulators:
         self.assertTrue(name in ('ObjectIdShuffler', 'AutoReference'))
     self.assertEqual([], db.outgoing_manipulators)
     self.assertEqual(['AutoReference'], db.outgoing_copying_manipulators)
Ejemplo n.º 3
0
    def test_id_shuffling(self):
        manip = ObjectIdShuffler()
        collection = self.db.test

        def incoming_moves_id(son_in):
            son = manip.transform_incoming(son_in, collection)
            if not "_id" in son:
                return True
            for (k, v) in son.items():
                self.assertEqual(k, "_id")
                break
            return son_in == son

        self.assert_(incoming_moves_id({}))
        self.assert_(incoming_moves_id({"_id": 12}))
        self.assert_(incoming_moves_id({"hello": "world", "_id": 12}))
        self.assert_(incoming_moves_id(SON([("hello", "world"), ("_id", 12)])))

        def outgoing_is_identity(son):
            return son == manip.transform_outgoing(son, collection)

        qcheck.check_unittest(self, outgoing_is_identity,
                              qcheck.gen_mongo_dict(3))