Statistics
| Branch: | Tag: | Revision:

root / test / ganeti.serializer_unittest.py @ d4fa5c23

History | View | Annotate | Download (1.8 kB)

1 501c95a2 Michael Hanselmann
#!/usr/bin/python
2 501c95a2 Michael Hanselmann
#
3 501c95a2 Michael Hanselmann
4 501c95a2 Michael Hanselmann
# Copyright (C) 2006, 2007, 2008 Google Inc.
5 501c95a2 Michael Hanselmann
#
6 501c95a2 Michael Hanselmann
# This program is free software; you can redistribute it and/or modify
7 501c95a2 Michael Hanselmann
# it under the terms of the GNU General Public License as published by
8 501c95a2 Michael Hanselmann
# the Free Software Foundation; either version 2 of the License, or
9 501c95a2 Michael Hanselmann
# (at your option) any later version.
10 501c95a2 Michael Hanselmann
#
11 501c95a2 Michael Hanselmann
# This program is distributed in the hope that it will be useful, but
12 501c95a2 Michael Hanselmann
# WITHOUT ANY WARRANTY; without even the implied warranty of
13 501c95a2 Michael Hanselmann
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 501c95a2 Michael Hanselmann
# General Public License for more details.
15 501c95a2 Michael Hanselmann
#
16 501c95a2 Michael Hanselmann
# You should have received a copy of the GNU General Public License
17 501c95a2 Michael Hanselmann
# along with this program; if not, write to the Free Software
18 501c95a2 Michael Hanselmann
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 501c95a2 Michael Hanselmann
# 0.0510-1301, USA.
20 501c95a2 Michael Hanselmann
21 501c95a2 Michael Hanselmann
22 501c95a2 Michael Hanselmann
"""Script for unittesting the serializer module"""
23 501c95a2 Michael Hanselmann
24 501c95a2 Michael Hanselmann
25 501c95a2 Michael Hanselmann
import unittest
26 501c95a2 Michael Hanselmann
27 501c95a2 Michael Hanselmann
from ganeti import serializer
28 501c95a2 Michael Hanselmann
29 501c95a2 Michael Hanselmann
30 501c95a2 Michael Hanselmann
class SimplejsonMock(object):
31 501c95a2 Michael Hanselmann
  def dumps(self, data, indent=None):
32 501c95a2 Michael Hanselmann
    return repr(data)
33 501c95a2 Michael Hanselmann
34 501c95a2 Michael Hanselmann
  def loads(self, data):
35 501c95a2 Michael Hanselmann
    return eval(data)
36 501c95a2 Michael Hanselmann
37 501c95a2 Michael Hanselmann
38 501c95a2 Michael Hanselmann
class TestSerializer(unittest.TestCase):
39 501c95a2 Michael Hanselmann
  """Serializer tests"""
40 501c95a2 Michael Hanselmann
41 501c95a2 Michael Hanselmann
  _TESTDATA = [
42 501c95a2 Michael Hanselmann
    "test",
43 501c95a2 Michael Hanselmann
    255,
44 501c95a2 Michael Hanselmann
    [1, 2, 3],
45 501c95a2 Michael Hanselmann
    (1, 2, 3),
46 501c95a2 Michael Hanselmann
    { 1: 2, "foo": "bar", },
47 501c95a2 Michael Hanselmann
    ]
48 501c95a2 Michael Hanselmann
49 501c95a2 Michael Hanselmann
  def setUp(self):
50 501c95a2 Michael Hanselmann
    self._orig_simplejson = serializer.simplejson
51 501c95a2 Michael Hanselmann
    serializer.simplejson = SimplejsonMock()
52 501c95a2 Michael Hanselmann
53 501c95a2 Michael Hanselmann
  def tearDown(self):
54 501c95a2 Michael Hanselmann
    serializer.simplejson = self._orig_simplejson
55 501c95a2 Michael Hanselmann
56 501c95a2 Michael Hanselmann
  def testGeneric(self):
57 501c95a2 Michael Hanselmann
    return self._TestSerializer(serializer.Dump, serializer.Load)
58 501c95a2 Michael Hanselmann
59 501c95a2 Michael Hanselmann
  def testJson(self):
60 501c95a2 Michael Hanselmann
    return self._TestSerializer(serializer.DumpJson, serializer.LoadJson)
61 501c95a2 Michael Hanselmann
62 501c95a2 Michael Hanselmann
  def _TestSerializer(self, dump_fn, load_fn):
63 501c95a2 Michael Hanselmann
    for data in self._TESTDATA:
64 501c95a2 Michael Hanselmann
      self.failUnless(dump_fn(data).endswith("\n"))
65 501c95a2 Michael Hanselmann
      self.failUnlessEqual(load_fn(dump_fn(data)), data)
66 501c95a2 Michael Hanselmann
67 501c95a2 Michael Hanselmann
68 501c95a2 Michael Hanselmann
if __name__ == '__main__':
69 501c95a2 Michael Hanselmann
  unittest.main()