00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 """Tests for the son module."""
00016
00017 import unittest
00018 import sys
00019 sys.path[0:0] = [""]
00020
00021 from bson.son import SON
00022
00023
00024 class TestSON(unittest.TestCase):
00025
00026 def setUp(self):
00027 pass
00028
00029 def test_ordered_dict(self):
00030 a = SON()
00031 a["hello"] = "world"
00032 a["mike"] = "awesome"
00033 a["hello_"] = "mike"
00034 self.assertEqual(a.items(), [("hello", "world"),
00035 ("mike", "awesome"),
00036 ("hello_", "mike")])
00037
00038 b = SON({"hello": "world"})
00039 self.assertEqual(b["hello"], "world")
00040 self.assertRaises(KeyError, lambda: b["goodbye"])
00041
00042 def test_to_dict(self):
00043 a = SON()
00044 b = SON([("blah", SON())])
00045 c = SON([("blah", [SON()])])
00046 d = SON([("blah", {"foo": SON()})])
00047 self.assertEqual({}, a.to_dict())
00048 self.assertEqual({"blah": {}}, b.to_dict())
00049 self.assertEqual({"blah": [{}]}, c.to_dict())
00050 self.assertEqual({"blah": {"foo": {}}}, d.to_dict())
00051 self.assertEqual(dict, a.to_dict().__class__)
00052 self.assertEqual(dict, b.to_dict()["blah"].__class__)
00053 self.assertEqual(dict, c.to_dict()["blah"][0].__class__)
00054 self.assertEqual(dict, d.to_dict()["blah"]["foo"].__class__)
00055
00056
00057 if __name__ == "__main__":
00058 unittest.main()