Statistics
| Branch: | Tag: | Revision:

root / kamaki / clients / utils / test.py @ 480585cd

History | View | Annotate | Download (4.6 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 setUp(self):
81
        pass
82

    
83
    def tearDown(self):
84
        pass
85

    
86
    def test__matches(self):
87
        for args in (
88
                ('example', 'example'), ('example', 'example', True),
89
                ('example', 'example', False), ('example0', 'example', False),
90
                ('example', '', False), ('', ''),
91
                ('', '', True), ('', '', False)):
92
            _try(self.assertTrue, utils._matches, *args)
93
        for args in (
94
                ('', 'example'), ('example', ''),
95
                ('example', 'example0'), ('example0', 'example'),
96
                ('example', 'example0', True), ('example', 'example0', False),
97
                ('example0', 'example'), ('example0', 'example', True)):
98
            _try(self.assertFalse, utils._matches, *args)
99

    
100
    def test_filter_out(self):
101
        for key, src, exp_in, exp_out in filter_examples:
102
            r = utils.filter_out(src, key)
103
            self.assert_dicts_are_equal(r, exp_out)
104
            for k in exp_in:
105
                self.assertFalse(k in r)
106
            r = utils.filter_out(src, key, True)
107
            if key in src:
108
                expected = dict(src)
109
                expected.pop(key)
110
                self.assert_dicts_are_equal(r, expected)
111
            else:
112
                self.assert_dicts_are_equal(r, src)
113

    
114
    def test_filter_in(self):
115
        for key, src, exp_in, exp_out in filter_examples:
116
            r = utils.filter_in(src, key)
117
            self.assert_dicts_are_equal(r, exp_in)
118
            for k in exp_out:
119
                self.assertFalse(k in r)
120
            r = utils.filter_in(src, key, True)
121
            if key in src:
122
                self.assert_dicts_are_equal(r, {key: src[key]})
123
            else:
124
                self.assert_dicts_are_equal(r, dict())
125

    
126

    
127
if __name__ == '__main__':
128
    from sys import argv
129
    from kamaki.clients.test import runTestCase
130
    runTestCase(Utils, 'clients.utils methods', argv[1:])