00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 """Tests for the Timestamp class."""
00016
00017 import datetime
00018 import unittest
00019 import sys
00020 sys.path[0:0] = [""]
00021
00022 from bson.timestamp import Timestamp
00023 from bson.tz_util import utc
00024
00025
00026 class TestTimestamp(unittest.TestCase):
00027
00028 def setUp(self):
00029 pass
00030
00031 def test_timestamp(self):
00032 t = Timestamp(123, 456)
00033 self.assertEqual(t.time, 123)
00034 self.assertEqual(t.inc, 456)
00035 self.assert_(isinstance(t, Timestamp))
00036
00037 def test_datetime(self):
00038 d = datetime.datetime(2010, 5, 5, tzinfo=utc)
00039 t = Timestamp(d, 0)
00040 self.assertEqual(1273017600, t.time)
00041 self.assertEqual(d, t.as_datetime())
00042
00043 def test_exceptions(self):
00044 self.assertRaises(TypeError, Timestamp)
00045 self.assertRaises(TypeError, Timestamp, None, 123)
00046 self.assertRaises(TypeError, Timestamp, 1.2, 123)
00047 self.assertRaises(TypeError, Timestamp, 123, None)
00048 self.assertRaises(TypeError, Timestamp, 123, 1.2)
00049 self.assertRaises(ValueError, Timestamp, 0, -1)
00050 self.assertRaises(ValueError, Timestamp, -1, 0)
00051 self.assert_(Timestamp(0, 0))
00052
00053 def test_equality(self):
00054 t = Timestamp(1,1)
00055 self.assertNotEqual(t, Timestamp(0,1))
00056 self.assertNotEqual(t, Timestamp(1,0))
00057 self.assertEqual(t, Timestamp(1,1))
00058
00059 def test_repr(self):
00060 t = Timestamp(0, 0)
00061 self.assertEqual(repr(t), "Timestamp(0, 0)")
00062
00063 if __name__ == "__main__":
00064 unittest.main()