00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 """Tests for the dbref module."""
00016
00017 import unittest
00018 import sys
00019 sys.path[0:0] = [""]
00020
00021 from bson.objectid import ObjectId
00022 from bson.dbref import DBRef
00023
00024 from copy import deepcopy
00025
00026
00027 class TestDBRef(unittest.TestCase):
00028
00029 def setUp(self):
00030 pass
00031
00032 def test_creation(self):
00033 a = ObjectId()
00034 self.assertRaises(TypeError, DBRef)
00035 self.assertRaises(TypeError, DBRef, "coll")
00036 self.assertRaises(TypeError, DBRef, 4, a)
00037 self.assertRaises(TypeError, DBRef, 1.5, a)
00038 self.assertRaises(TypeError, DBRef, a, a)
00039 self.assertRaises(TypeError, DBRef, None, a)
00040 self.assertRaises(TypeError, DBRef, "coll", a, 5)
00041 self.assert_(DBRef("coll", a))
00042 self.assert_(DBRef(u"coll", a))
00043 self.assert_(DBRef(u"coll", 5))
00044 self.assert_(DBRef(u"coll", 5, "database"))
00045
00046 def test_read_only(self):
00047 a = DBRef("coll", ObjectId())
00048
00049 def foo():
00050 a.collection = "blah"
00051
00052 def bar():
00053 a.id = "aoeu"
00054
00055 self.assertEqual("coll", a.collection)
00056 a.id
00057 self.assertEqual(None, a.database)
00058 self.assertRaises(AttributeError, foo)
00059 self.assertRaises(AttributeError, bar)
00060
00061 def test_repr(self):
00062 self.assertEqual(repr(DBRef("coll", ObjectId("1234567890abcdef12345678"))),
00063 "DBRef('coll', ObjectId('1234567890abcdef12345678'))")
00064 self.assertEqual(repr(DBRef(u"coll", ObjectId("1234567890abcdef12345678"))),
00065 "DBRef(u'coll', ObjectId('1234567890abcdef12345678'))")
00066 self.assertEqual(repr(DBRef("coll", 5, foo="bar")),
00067 "DBRef('coll', 5, foo='bar')")
00068 self.assertEqual(repr(DBRef("coll", ObjectId("1234567890abcdef12345678"), "foo")),
00069 "DBRef('coll', ObjectId('1234567890abcdef12345678'), 'foo')")
00070 self.assertEqual(repr(DBRef("coll", 5, "baz", foo="bar", baz=4)),
00071 "DBRef('coll', 5, 'baz', foo='bar', baz=4)")
00072
00073 def test_cmp(self):
00074 self.assertEqual(DBRef("coll", ObjectId("1234567890abcdef12345678")),
00075 DBRef(u"coll", ObjectId("1234567890abcdef12345678")))
00076 self.assertNotEqual(DBRef("coll", ObjectId("1234567890abcdef12345678")),
00077 DBRef(u"coll", ObjectId("1234567890abcdef12345678"), "foo"))
00078 self.assertNotEqual(DBRef("coll", ObjectId("1234567890abcdef12345678")),
00079 DBRef("col", ObjectId("1234567890abcdef12345678")))
00080 self.assertNotEqual(DBRef("coll", ObjectId("1234567890abcdef12345678")),
00081 DBRef("coll", ObjectId("123456789011")))
00082 self.assertNotEqual(DBRef("coll", ObjectId("1234567890abcdef12345678")), 4)
00083 self.assertEqual(DBRef("coll", ObjectId("1234567890abcdef12345678"), "foo"),
00084 DBRef(u"coll", ObjectId("1234567890abcdef12345678"), "foo"))
00085 self.assertNotEqual(DBRef("coll", ObjectId("1234567890abcdef12345678"), "foo"),
00086 DBRef(u"coll", ObjectId("1234567890abcdef12345678"), "bar"))
00087
00088 def test_kwargs(self):
00089 self.assertEqual(DBRef("coll", 5, foo="bar"), DBRef("coll", 5, foo="bar"))
00090 self.assertNotEqual(DBRef("coll", 5, foo="bar"), DBRef("coll", 5))
00091 self.assertNotEqual(DBRef("coll", 5, foo="bar"), DBRef("coll", 5, foo="baz"))
00092 self.assertEqual("bar", DBRef("coll", 5, foo="bar").foo)
00093 self.assertRaises(KeyError, getattr, DBRef("coll", 5, foo="bar"), "bar")
00094
00095 def test_deepcopy(self):
00096 a = DBRef('coll', 'asdf', 'db', x=[1])
00097 b = deepcopy(a)
00098
00099 self.assertEqual(a, b)
00100 self.assertNotEqual(id(a), id(b.x))
00101 self.assertEqual(a.x, b.x)
00102 self.assertNotEqual(id(a.x), id(b.x))
00103
00104 b.x[0] = 2
00105 self.assertEqual(a.x, [1])
00106 self.assertEqual(b.x, [2])
00107
00108
00109
00110
00111 if __name__ == "__main__":
00112 unittest.main()