Skip to content

Commit

Permalink
Added tests for get_class_method
Browse files Browse the repository at this point in the history
  • Loading branch information
LukasRychtecky committed Jan 5, 2017
1 parent ad7e31d commit 2468e52
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
12 changes: 8 additions & 4 deletions chamber/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@


def remove_accent(string_with_diacritics):
"Removes a diacritics from a given string"
"""
Removes a diacritics from a given string"
"""
return unicodedata.normalize('NFKD', string_with_diacritics).encode('ASCII', 'ignore').decode('ASCII')


def get_class_method(cls_or_inst, method_name):
cls = cls_or_inst
if not isinstance(cls, type):
cls = cls_or_inst.__class__
"""
Returns a method from a given class or instance. When the method doest not exist, it returns `None`. Also works with
properties and cached properties.
"""
cls = cls_or_inst if isinstance(cls_or_inst, type) else cls_or_inst.__class__
meth = getattr(cls, method_name, None)
if isinstance(meth, property):
meth = meth.fget
Expand Down
31 changes: 30 additions & 1 deletion example/dj/apps/test_chamber/tests/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,43 @@
from .datastructures import * # NOQA
from .decorators import * # NOQA

from django.utils.functional import cached_property
from django.test import TestCase

from chamber.utils import remove_accent
from chamber.utils import remove_accent, get_class_method

from germanium.anotations import data_provider
from germanium.tools import assert_equal


class TestClass(object):

def method(self):
pass

@property
def property_method(self):
pass

@cached_property
def cached_property_method(self):
pass


class UtilsTestCase(TestCase):

def test_should_remove_accent_from_string(self):
assert_equal('escrzyaie', remove_accent('ěščřžýáíé'))

classes_and_method_names = [
[TestClass.method, TestClass, 'method'],
[TestClass.method, TestClass(), 'method'],
[TestClass.property_method.fget, TestClass, 'property_method'],
[TestClass.property_method.fget, TestClass(), 'property_method'],
[TestClass.cached_property_method.func, TestClass, 'cached_property_method'],
[TestClass.cached_property_method.func, TestClass(), 'cached_property_method'],
]

@data_provider(classes_and_method_names)
def test_should_return_class_method(self, expected_method, cls_or_inst, method_name):
assert_equal(expected_method, get_class_method(cls_or_inst, method_name))

0 comments on commit 2468e52

Please sign in to comment.