]> code.communitydata.science - rises_declines_wikia_code.git/blob - mediawiki_dump_tools/Mediawiki-Utilities/mw/types/tests/test_timestamp.py
Initial commit
[rises_declines_wikia_code.git] / mediawiki_dump_tools / Mediawiki-Utilities / mw / types / tests / test_timestamp.py
1 import pickle
2
3 from nose.tools import eq_
4
5 from ..timestamp import LONG_MW_TIME_STRING, Timestamp
6
7
8 def test_self():
9     t1 = Timestamp(1234567890)
10
11     # Unix timestamp
12     eq_(t1, Timestamp(int(t1)))
13
14     # Short format
15     eq_(t1, Timestamp(t1.short_format()))
16
17     # Long format
18     eq_(t1, Timestamp(t1.long_format()))
19
20
21 def test_comparison():
22     t1 = Timestamp(1234567890)
23     t2 = Timestamp(1234567891)
24
25     assert t1 < t2, "Less than comparison failed"
26     assert t2 > t1, "Greater than comparison failed"
27     assert not t2 < t1, "Not less than comparison failed"
28     assert not t1 > t2, "Not greater than comparison failed"
29
30     assert t1 <= t2, "Less than or equal to comparison failed"
31     assert t1 <= t1, "Less than or equal to comparison failed"
32     assert t2 >= t1, "Greater than or equal to comparison failed"
33     assert t2 >= t2, "Greater than or equal to comparison failed"
34     assert not t2 <= t1, "Not less than or equal to comparison failed"
35     assert not t1 >= t2, "Not greater than or equal to comparison failed"
36     
37
38
39 def test_subtraction():
40     t1 = Timestamp(1234567890)
41     t2 = Timestamp(1234567891)
42
43     eq_(t2 - t1, 1)
44     eq_(t1 - t2, -1)
45     eq_(t2 - 1, t1)
46
47
48 def test_strptime():
49     eq_(
50         Timestamp("2009-02-13T23:31:30Z"),
51         Timestamp.strptime("2009-02-13T23:31:30Z", LONG_MW_TIME_STRING)
52     )
53
54     eq_(
55         Timestamp.strptime(
56             "expires 03:20, 21 November 2013 (UTC)",
57             "expires %H:%M, %d %B %Y (UTC)"
58         ),
59         Timestamp("2013-11-21T03:20:00Z")
60     )
61
62
63 def test_strftime():
64     eq_(
65         Timestamp("2009-02-13T23:31:30Z").strftime(LONG_MW_TIME_STRING),
66         "2009-02-13T23:31:30Z"
67     )
68
69     eq_(
70         Timestamp("2009-02-13T23:31:30Z").strftime("expires %H:%M, %d %B %Y (UTC)"),
71         "expires 23:31, 13 February 2009 (UTC)"
72     )
73
74
75 def test_serialization():
76     timestamp = Timestamp(1234567890)
77     eq_(
78         timestamp,
79         Timestamp.deserialize(timestamp.serialize())
80     )
81
82 def test_pickling():
83     timestamp = Timestamp(1234567890)
84     eq_(
85         timestamp,
86         pickle.loads(pickle.dumps(timestamp))
87     )

Community Data Science Collective || Want to submit a patch?