~ubuntu-branches/ubuntu/wily/python-oslo.serialization/wily

« back to all changes in this revision

Viewing changes to tests/test_msgpackutils.py

  • Committer: Package Import Robot
  • Author(s): Thomas Goirand, James Page, Thomas Goirand
  • Date: 2015-09-04 11:07:51 UTC
  • mfrom: (1.1.7) (2.3.2 experimental)
  • Revision ID: package-import@ubuntu.com-20150904110751-fi44m99ihewx1p33
Tags: 1.8.0-1
[ James Page ]
* Fixup typo in transitional package description (LP: #1471561).

[ Thomas Goirand ]
* New upstream release.
* Fixed build-depends for this release.
* Refreshed patch.
* Removed final dot in short desc and fixed Priority: extra for transition
  packages.
* Watch file now using github tag and not broken PyPi.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#    Copyright (C) 2015 Yahoo! Inc. All Rights Reserved.
2
 
#
3
 
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
4
 
#    not use this file except in compliance with the License. You may obtain
5
 
#    a copy of the License at
6
 
#
7
 
#         http://www.apache.org/licenses/LICENSE-2.0
8
 
#
9
 
#    Unless required by applicable law or agreed to in writing, software
10
 
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11
 
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12
 
#    License for the specific language governing permissions and limitations
13
 
#    under the License.
14
 
 
15
 
import datetime
16
 
import itertools
17
 
import sys
18
 
import uuid
19
 
 
20
 
import netaddr
21
 
from oslotest import base as test_base
22
 
from pytz import timezone
23
 
import six
24
 
import six.moves.xmlrpc_client as xmlrpclib
25
 
import testtools
26
 
 
27
 
from oslo_serialization import msgpackutils
28
 
 
29
 
# NOTE(harlowja): itertools.count only started to take a step value
30
 
# in python 2.7+ so we can't use it in 2.6...
31
 
if sys.version_info[0:2] == (2, 6):
32
 
    _PY26 = True
33
 
else:
34
 
    _PY26 = False
35
 
 
36
 
 
37
 
_TZ_FMT = '%Y-%m-%d %H:%M:%S %Z%z'
38
 
 
39
 
 
40
 
def _dumps_loads(obj):
41
 
    obj = msgpackutils.dumps(obj)
42
 
    return msgpackutils.loads(obj)
43
 
 
44
 
 
45
 
class MsgPackUtilsTestMixin(test_base.BaseTestCase):
46
 
    def test_list(self):
47
 
        self.assertEqual(_dumps_loads([1, 2, 3]), [1, 2, 3])
48
 
 
49
 
    def test_empty_list(self):
50
 
        self.assertEqual(_dumps_loads([]), [])
51
 
 
52
 
    def test_tuple(self):
53
 
        # Seems like we do lose whether it was a tuple or not...
54
 
        #
55
 
        # Maybe fixed someday:
56
 
        #
57
 
        # https://github.com/msgpack/msgpack-python/issues/98
58
 
        self.assertEqual(_dumps_loads((1, 2, 3)), [1, 2, 3])
59
 
 
60
 
    def test_dict(self):
61
 
        self.assertEqual(_dumps_loads(dict(a=1, b=2, c=3)),
62
 
                         dict(a=1, b=2, c=3))
63
 
 
64
 
    def test_empty_dict(self):
65
 
        self.assertEqual(_dumps_loads({}), {})
66
 
 
67
 
    def test_complex_dict(self):
68
 
        src = {
69
 
            'now': datetime.datetime(1920, 2, 3, 4, 5, 6, 7),
70
 
            'later': datetime.datetime(1921, 2, 3, 4, 5, 6, 9),
71
 
            'a': 1,
72
 
            'b': 2.0,
73
 
            'c': [],
74
 
            'd': set([1, 2, 3]),
75
 
            'zzz': uuid.uuid4(),
76
 
            'yyy': 'yyy',
77
 
            'ddd': b'bbb',
78
 
            'today': datetime.date.today(),
79
 
        }
80
 
        self.assertEqual(_dumps_loads(src), src)
81
 
 
82
 
    def test_itercount(self):
83
 
        it = itertools.count(1)
84
 
        six.next(it)
85
 
        six.next(it)
86
 
        it2 = _dumps_loads(it)
87
 
        self.assertEqual(six.next(it), six.next(it2))
88
 
 
89
 
        it = itertools.count(0)
90
 
        it2 = _dumps_loads(it)
91
 
        self.assertEqual(six.next(it), six.next(it2))
92
 
 
93
 
    @testtools.skipIf(_PY26, 'itertools.count step not supported')
94
 
    def test_itercount_step(self):
95
 
        it = itertools.count(1, 3)
96
 
        it2 = _dumps_loads(it)
97
 
        self.assertEqual(six.next(it), six.next(it2))
98
 
 
99
 
    def test_set(self):
100
 
        self.assertEqual(_dumps_loads(set([1, 2])), set([1, 2]))
101
 
 
102
 
    def test_empty_set(self):
103
 
        self.assertEqual(_dumps_loads(set([])), set([]))
104
 
 
105
 
    def test_frozenset(self):
106
 
        self.assertEqual(_dumps_loads(frozenset([1, 2])), frozenset([1, 2]))
107
 
 
108
 
    def test_empty_frozenset(self):
109
 
        self.assertEqual(_dumps_loads(frozenset([])), frozenset([]))
110
 
 
111
 
    def test_datetime_preserve(self):
112
 
        x = datetime.datetime(1920, 2, 3, 4, 5, 6, 7)
113
 
        self.assertEqual(_dumps_loads(x), x)
114
 
 
115
 
    def test_datetime(self):
116
 
        x = xmlrpclib.DateTime()
117
 
        x.decode("19710203T04:05:06")
118
 
        self.assertEqual(_dumps_loads(x), x)
119
 
 
120
 
    def test_ipaddr(self):
121
 
        thing = {'ip_addr': netaddr.IPAddress('1.2.3.4')}
122
 
        self.assertEqual(_dumps_loads(thing), thing)
123
 
 
124
 
    def test_today(self):
125
 
        today = datetime.date.today()
126
 
        self.assertEqual(today, _dumps_loads(today))
127
 
 
128
 
    def test_datetime_tz_clone(self):
129
 
        eastern = timezone('US/Eastern')
130
 
        now = datetime.datetime.now()
131
 
        e_dt = eastern.localize(now)
132
 
        e_dt2 = _dumps_loads(e_dt)
133
 
        self.assertEqual(e_dt, e_dt2)
134
 
        self.assertEqual(e_dt.strftime(_TZ_FMT), e_dt2.strftime(_TZ_FMT))
135
 
 
136
 
    def test_datetime_tz_different(self):
137
 
        eastern = timezone('US/Eastern')
138
 
        pacific = timezone('US/Pacific')
139
 
        now = datetime.datetime.now()
140
 
 
141
 
        e_dt = eastern.localize(now)
142
 
        p_dt = pacific.localize(now)
143
 
 
144
 
        self.assertNotEqual(e_dt, p_dt)
145
 
        self.assertNotEqual(e_dt.strftime(_TZ_FMT), p_dt.strftime(_TZ_FMT))
146
 
 
147
 
        e_dt2 = _dumps_loads(e_dt)
148
 
        p_dt2 = _dumps_loads(p_dt)
149
 
 
150
 
        self.assertNotEqual(e_dt2, p_dt2)
151
 
        self.assertNotEqual(e_dt2.strftime(_TZ_FMT), p_dt2.strftime(_TZ_FMT))
152
 
 
153
 
        self.assertEqual(e_dt, e_dt2)
154
 
        self.assertEqual(p_dt, p_dt2)