utils.py 1.73 KB
Newer Older
Stavros Sachtouris's avatar
Stavros Sachtouris committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# Copyright (C) 2016 GRNET S.A.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License

from soi import utils
from mock import patch
17
from soi.tests import fakes
Stavros Sachtouris's avatar
Stavros Sachtouris committed
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41


def test_patch_class_methods():
    """Test utils.patch_class_methods"""
    class DummyClass:
        """use it for testing"""
        def a_method(self, an_arg):
            """a method"""

        def an_other_method(self, an_arg, antoher_arg):
            """a method"""

    function_map = {
        'a_method': lambda self, x: 'replace {0}'.format(x),
        'an_other_method': lambda self, x, y: 'replace {0} {1}'.format(x, y),
    }

    utils.patch_class_methods(DummyClass, function_map)
    client = DummyClass()

    assert client.a_method('arg1') == 'replace arg1'
    assert client.an_other_method('arg1', 'arg2') == 'replace arg1 arg2'


42 43 44
@patch('soi.tests.fakes.DummyClass.get_from_response', return_value='g f r')
@patch('soi.tests.fakes.FakeReq.get_response', return_value='my response')
def test_empty_list_200(gr, gfr):
Stavros Sachtouris's avatar
Stavros Sachtouris committed
45
    """Test the empty list method"""
46 47 48 49 50
    cls, req = fakes.DummyClass(), fakes.FakeReq()
    r = utils.empty_list_200(cls, req)
    assert r == 'g f r'
    gfr.assert_called_once_with('my response', 'empty list', [])
    gr.assert_called_once_with(cls.app)