File: test_timestamp.py

package info (click to toggle)
pymongo 2.2-4%2Bdeb7u1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,664 kB
  • sloc: python: 12,474; ansic: 2,954; makefile: 20
file content (78 lines) | stat: -rw-r--r-- 2,439 bytes parent folder | download
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
# 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 the Timestamp class."""

import datetime
import unittest
import sys
import copy
import pickle
sys.path[0:0] = [""]

from bson.timestamp import Timestamp
from bson.tz_util import utc


class TestTimestamp(unittest.TestCase):

    def setUp(self):
        pass

    def test_timestamp(self):
        t = Timestamp(123, 456)
        self.assertEqual(t.time, 123)
        self.assertEqual(t.inc, 456)
        self.assertTrue(isinstance(t, Timestamp))

    def test_datetime(self):
        d = datetime.datetime(2010, 5, 5, tzinfo=utc)
        t = Timestamp(d, 0)
        self.assertEqual(1273017600, t.time)
        self.assertEqual(d, t.as_datetime())

    def test_datetime_copy_pickle(self):
        d = datetime.datetime(2010, 5, 5, tzinfo=utc)
        t = Timestamp(d, 0)

        dc = copy.deepcopy(d)
        self.assertEqual(dc, t.as_datetime())

        for protocol in [0, 1, 2, -1]:
            pkl = pickle.dumps(d, protocol=protocol)
            dp = pickle.loads(pkl)
            self.assertEqual(dp, t.as_datetime())

    def test_exceptions(self):
        self.assertRaises(TypeError, Timestamp)
        self.assertRaises(TypeError, Timestamp, None, 123)
        self.assertRaises(TypeError, Timestamp, 1.2, 123)
        self.assertRaises(TypeError, Timestamp, 123, None)
        self.assertRaises(TypeError, Timestamp, 123, 1.2)
        self.assertRaises(ValueError, Timestamp, 0, -1)
        self.assertRaises(ValueError, Timestamp, -1, 0)
        self.assertTrue(Timestamp(0, 0))

    def test_equality(self):
        t = Timestamp(1, 1)
        self.assertNotEqual(t, Timestamp(0, 1))
        self.assertNotEqual(t, Timestamp(1, 0))
        self.assertEqual(t, Timestamp(1, 1))

    def test_repr(self):
        t = Timestamp(0, 0)
        self.assertEqual(repr(t), "Timestamp(0, 0)")

if __name__ == "__main__":
    unittest.main()