1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
|
# Copyright 2009-2012 10gen, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Tests for SONManipulators.
"""
import unittest
import sys
sys.path[0:0] = [""]
from bson.objectid import ObjectId
from bson.son import SON
from pymongo.database import Database
from pymongo.son_manipulator import (NamespaceInjector,
ObjectIdInjector,
ObjectIdShuffler,
SONManipulator)
from test.test_connection import get_connection
from test import qcheck
class TestSONManipulator(unittest.TestCase):
def setUp(self):
self.db = Database(get_connection(), "pymongo_test")
def test_basic(self):
manip = SONManipulator()
collection = self.db.test
def incoming_is_identity(son):
return son == manip.transform_incoming(son, collection)
qcheck.check_unittest(self, incoming_is_identity,
qcheck.gen_mongo_dict(3))
def outgoing_is_identity(son):
return son == manip.transform_outgoing(son, collection)
qcheck.check_unittest(self, outgoing_is_identity,
qcheck.gen_mongo_dict(3))
def test_id_injection(self):
manip = ObjectIdInjector()
collection = self.db.test
def incoming_adds_id(son):
son = manip.transform_incoming(son, collection)
assert "_id" in son
return True
qcheck.check_unittest(self, incoming_adds_id,
qcheck.gen_mongo_dict(3))
def outgoing_is_identity(son):
return son == manip.transform_outgoing(son, collection)
qcheck.check_unittest(self, outgoing_is_identity,
qcheck.gen_mongo_dict(3))
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.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))
def test_ns_injection(self):
manip = NamespaceInjector()
collection = self.db.test
def incoming_adds_ns(son):
son = manip.transform_incoming(son, collection)
assert "_ns" in son
return son["_ns"] == collection.name
qcheck.check_unittest(self, incoming_adds_ns,
qcheck.gen_mongo_dict(3))
def outgoing_is_identity(son):
return son == manip.transform_outgoing(son, collection)
qcheck.check_unittest(self, outgoing_is_identity,
qcheck.gen_mongo_dict(3))
if __name__ == "__main__":
unittest.main()
|