Statistics
| Branch: | Tag: | Revision:

root / kamaki / clients / utils / test.py @ 4e25b350

History | View | Annotate | Download (5.4 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 tempfile import TemporaryFile
38

    
39
from kamaki.clients import utils
40

    
41

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

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

    
71

    
72
class Utils(TestCase):
73

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

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

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

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

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

    
132
    def test_readall(self):
133
        tstr = '1234567890'
134
        with TemporaryFile() as f:
135
            f.write(tstr)
136
            f.flush()
137
            f.seek(0)
138
            self.assertEqual(utils.readall(f, 5), tstr[:5])
139
            self.assertEqual(utils.readall(f, 10), tstr[5:])
140
            self.assertEqual(utils.readall(f, 1), '')
141
            self.assertRaises(IOError, utils.readall, f, 1, 0)
142

    
143
if __name__ == '__main__':
144
    from sys import argv
145
    from kamaki.clients.test import runTestCase
146
    runTestCase(Utils, 'clients.utils methods', argv[1:])