Statistics
| Branch: | Tag: | Revision:

root / kamaki / cli / utils / test.py @ b3cb58c2

History | View | Annotate | Download (4.5 kB)

1
# Copyright 2013 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or
4
# without modification, are permitted provided that the following
5
# conditions are met:
6
#
7
#   1. Redistributions of source code must retain the above
8
#      copyright notice, this list of conditions and the following
9
#      disclaimer.
10
#
11
#   2. Redistributions in binary form must reproduce the above
12
#      copyright notice, this list of conditions and the following
13
#      disclaimer in the documentation and/or other materials
14
#      provided with the distribution.
15
#
16
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
# POSSIBILITY OF SUCH DAMAGE.
28
#
29
# The views and conclusions contained in the software and
30
# documentation are those of the authors and should not be
31
# interpreted as representing official policies, either expressed
32
# or implied, of GRNET S.A.
33

    
34
from unittest import TestCase
35
#from tempfile import NamedTemporaryFile
36
#from mock import patch, call
37
from itertools import product
38

    
39

    
40
class UtilsMethods(TestCase):
41

    
42
    def assert_dicts_are_equal(self, d1, d2):
43
        for k, v in d1.items():
44
            self.assertTrue(k in d2)
45
            if isinstance(v, dict):
46
                self.assert_dicts_are_equal(v, d2[k])
47
            else:
48
                self.assertEqual(unicode(v), unicode(d2[k]))
49

    
50
    def test_guess_mime_type(self):
51
        from kamaki.cli.utils import guess_mime_type
52
        from mimetypes import guess_type
53
        for args in product(
54
                ('file.txt', 'file.png', 'file.zip', 'file.gz', None, 'X'),
55
                ('a type', None),
56
                ('an/encoding', None)):
57
            filename, ctype, cencoding = args
58
            if filename:
59
                exp_type, exp_enc = guess_type(filename)
60
                self.assertEqual(
61
                    guess_mime_type(*args),
62
                    (exp_type or ctype, exp_enc or cencoding))
63
            else:
64
                self.assertRaises(AssertionError, guess_mime_type, *args)
65

    
66
    def test_pretty_keys(self):
67
        from kamaki.cli.utils import pretty_keys
68
        for args, exp in (
69
                (
70
                    ({'k1': 'v1', 'k1_k2': 'v2'}, ),
71
                    {'k1': 'v1', 'k1 k2': 'v2'}),
72
                (
73
                    ({'k1': 'v1', 'k1_k2': 'v2'}, '1'),
74
                    {'k': 'v1', 'k _k2': 'v2'}),
75
                (
76
                    ({'k1_k2': 'v1', 'k1': {'k2': 'v2', 'k2_k3': 'v3'}}, ),
77
                    {'k1 k2': 'v1', 'k1': {'k2': 'v2', 'k2_k3': 'v3'}}),
78
                (
79
                    (
80
                        {'k1_k2': 'v1', 'k1': {'k2': 'v2', 'k2_k3': 'v3'}},
81
                        '_',
82
                        True),
83
                    {'k1 k2': 'v1', 'k1': {'k2': 'v2', 'k2 k3': 'v3'}}),
84
                (
85
                    (
86
                        {
87
                            'k1_k2': {'k_1': 'v_1', 'k_2': {'k_3': 'v_3'}},
88
                            'k1': {'k2': 'v2', 'k2_k3': 'v3'}},
89
                        '_',
90
                        True),
91
                    {
92
                        'k1 k2': {'k 1': 'v_1', 'k 2': {'k 3': 'v_3'}},
93
                        'k1': {'k2': 'v2', 'k2 k3': 'v3'}}),
94
                (
95
                    (
96
                        {
97
                            'k1_k2': {'k_1': 'v_1', 'k_2': {'k_3': 'v_3'}},
98
                            'k1': {'k2': 'v2', 'k2_k3': 'v3'}},
99
                        '1',
100
                        True),
101
                    {
102
                        'k _k2': {'k_': 'v_1', 'k_2': {'k_3': 'v_3'}},
103
                        'k': {'k2': 'v2', 'k2_k3': 'v3'}})
104
            ):
105
            initial = dict(args[0])
106
            self.assert_dicts_are_equal(pretty_keys(*args), exp)
107
            self.assert_dicts_are_equal(initial, args[0])
108

    
109

    
110
if __name__ == '__main__':
111
    from sys import argv
112
    from kamaki.cli.test import runTestCase
113
    runTestCase(UtilsMethods, 'UtilsMethods', argv[1:])