00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 """Tests for the Binary wrapper."""
00016
00017 import unittest
00018 import sys
00019 sys.path[0:0] = [""]
00020
00021 from bson.binary import Binary
00022
00023
00024 class TestBinary(unittest.TestCase):
00025
00026 def setUp(self):
00027 pass
00028
00029 def test_binary(self):
00030 a_string = "hello world"
00031 a_binary = Binary("hello world")
00032 self.assert_(a_binary.startswith("hello"))
00033 self.assert_(a_binary.endswith("world"))
00034 self.assert_(isinstance(a_binary, Binary))
00035 self.assertFalse(isinstance(a_string, Binary))
00036
00037 def test_exceptions(self):
00038 self.assertRaises(TypeError, Binary, None)
00039 self.assertRaises(TypeError, Binary, u"hello")
00040 self.assertRaises(TypeError, Binary, 5)
00041 self.assertRaises(TypeError, Binary, 10.2)
00042 self.assertRaises(TypeError, Binary, "hello", None)
00043 self.assertRaises(TypeError, Binary, "hello", "100")
00044 self.assertRaises(ValueError, Binary, "hello", -1)
00045 self.assertRaises(ValueError, Binary, "hello", 256)
00046 self.assert_(Binary("hello", 0))
00047 self.assert_(Binary("hello", 255))
00048
00049 def test_subtype(self):
00050 b = Binary("hello")
00051 self.assertEqual(b.subtype, 2)
00052 c = Binary("hello", 100)
00053 self.assertEqual(c.subtype, 100)
00054
00055 def test_equality(self):
00056 b = Binary("hello")
00057 c = Binary("hello", 100)
00058 self.assertNotEqual(b, c)
00059 self.assertEqual(c, Binary("hello", 100))
00060 self.assertEqual(b, Binary("hello"))
00061 self.assertNotEqual(b, Binary("hello "))
00062 self.assertNotEqual("hello", Binary("hello"))
00063
00064 def test_repr(self):
00065 b = Binary("hello world")
00066 self.assertEqual(repr(b), "Binary('hello world', 2)")
00067 c = Binary("\x08\xFF")
00068 self.assertEqual(repr(c), "Binary('\\x08\\xff', 2)")
00069 d = Binary("test", 100)
00070 self.assertEqual(repr(d), "Binary('test', 100)")
00071
00072 if __name__ == "__main__":
00073 unittest.main()