Statistics
| Branch: | Tag: | Revision:

root / kamaki / clients / utils / test.py @ 7806f19d

History | View | Annotate | Download (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 mock import patch, call
35

    
36
from unittest import TestCase
37
from kamaki.clients import utils
38

    
39

    
40
def _try(assertfoo, foo, *args):
41
    try:
42
        return assertfoo(foo(*args))
43
    except AssertionError:
44
        argstr = '( '
45
        for arg in args:
46
            argstr += '"%s" ' % arg
47
        argstr += ')'
48
        print('::: Method %s failed with args %s' % (foo, argstr))
49
        raise
50

    
51
filter_examples = [
52
    ('', dict(), dict(), dict()),
53
    (
54
        'key',
55
        dict(key1='v1', key2='v2', v=dict(key='v'), val1='k1', val2='k2'),
56
        dict(key1='v1', key2='v2'),
57
        dict(v=dict(key='v'), val1='k1', val2='k2')),
58
    (
59
        'val',
60
        dict(key1='v1', key2='v2', val=dict(key='v'), val1='k1', val2='k2'),
61
        dict(val1='k1', val2='k2', val=dict(key='v')),
62
        dict(key1='v1', key2='v2')),
63
    (
64
        'kv',
65
        dict(kvm='in', mkv='out', kv=''),
66
        dict(kvm='in', kv=''),
67
        dict(mkv='out'))]
68

    
69

    
70
class Utils(TestCase):
71

    
72
    def assert_dicts_are_equal(self, d1, d2):
73
        for k, v in d1.items():
74
            self.assertTrue(k in d2)
75
            if isinstance(v, dict):
76
                self.assert_dicts_are_equal(v, d2[k])
77
            else:
78
                self.assertEqual(unicode(v), unicode(d2[k]))
79

    
80
    def test__matches(self):
81
        for args in (
82
                ('example', 'example'), ('example', 'example', True),
83
                ('example', 'example', False), ('example0', 'example', False),
84
                ('example', '', False), ('', ''),
85
                ('', '', True), ('', '', False)):
86
            _try(self.assertTrue, utils._matches, *args)
87
        for args in (
88
                ('', 'example'), ('example', ''),
89
                ('example', 'example0'), ('example0', 'example'),
90
                ('example', 'example0', True), ('example', 'example0', False),
91
                ('example0', 'example'), ('example0', 'example', True)):
92
            _try(self.assertFalse, utils._matches, *args)
93

    
94
    def test_filter_out(self):
95
        for key, src, exp_in, exp_out in filter_examples:
96
            r = utils.filter_out(src, key)
97
            self.assert_dicts_are_equal(r, exp_out)
98
            for k in exp_in:
99
                self.assertFalse(k in r)
100
            r = utils.filter_out(src, key, True)
101
            if key in src:
102
                expected = dict(src)
103
                expected.pop(key)
104
                self.assert_dicts_are_equal(r, expected)
105
            else:
106
                self.assert_dicts_are_equal(r, src)
107

    
108
    def test_filter_in(self):
109
        for key, src, exp_in, exp_out in filter_examples:
110
            r = utils.filter_in(src, key)
111
            self.assert_dicts_are_equal(r, exp_in)
112
            for k in exp_out:
113
                self.assertFalse(k in r)
114
            r = utils.filter_in(src, key, True)
115
            if key in src:
116
                self.assert_dicts_are_equal(r, {key: src[key]})
117
            else:
118
                self.assert_dicts_are_equal(r, dict())
119

    
120
    def test_path4url(self):
121
        utf = u'\u03a6\u03bf\u03cd\u03c4\u03c3\u03bf\u03c2'.encode('utf-8')
122
        for expected, args in (
123
                ('', ('')),
124
                ('/path1/path2', ('path1', 'path2')),
125
                ('/1/number/0.28', (1, 'number', 0.28)),
126
                ('/1/n/u/m/b/er/X', ('//1//', '//n//u///m////b/er/', 'X//')),
127
                ('/p1/%s/p2' % utf.decode('utf-8'), ('p1', utf, 'p2'))):
128
            self.assertEqual(utils.path4url(*args), expected)
129

    
130

    
131
if __name__ == '__main__':
132
    from sys import argv
133
    from kamaki.clients.test import runTestCase
134
    runTestCase(Utils, 'clients.utils methods', argv[1:])