Skip to content

Commit

Permalink
Merge pull request #26 from druids/code-coverage-up
Browse files Browse the repository at this point in the history
Improved code coverage with tests
  • Loading branch information
matllubos authored Jan 6, 2017
2 parents f3f518b + 2468e52 commit 7b5ae0b
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 10 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ Status](https://travis-ci.org/druids/django-chamber.svg?branch=master)](https://
[![Coverage
Status](https://coveralls.io/repos/github/druids/django-chamber/badge.svg?branch=master)](https://coveralls.io/github/druids/django-chamber?branch=master)

Utilities library for [django-is-core](https://github.com/matllubos/django-is-core/tree/v1.3).
This library contains useful functions and classes mainly for a web development with Django (form and model fields,
shortcuts, advanced datastructure, decoraters etc.). For more details see examples below.

## Reference

Expand Down Expand Up @@ -51,6 +52,16 @@ Maximum upload size can be specified in project settings under `MAX_FILE_UPLOAD_

### Utils

#### `chamber.utils.remove_accent`

```python
remove_accent('ěščřžýáíé') # 'escrzyaie'
```

#### `chamber.utils.get_class_method`

It returns a method of a given class or instance.

#### `chamber.utils.datastructures.AbstractEnum`

Base enumeration class with controlled `__getattr__`.
Expand Down
16 changes: 10 additions & 6 deletions chamber/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
import unicodedata

from django.utils.functional import cached_property
from django.utils.safestring import mark_safe, SafeData
from django.utils.text import normalize_newlines
from django.utils.html import escape
from django.template.defaultfilters import stringfilter
from django.utils.safestring import SafeData, mark_safe
from django.utils.text import normalize_newlines


def remove_accent(string_with_diacritics):
"""
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
48 changes: 46 additions & 2 deletions example/dj/apps/test_chamber/tests/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,46 @@
from .datastructures import *
from .decorators import *
# -*- coding: UTF-8 -*-
from __future__ import unicode_literals

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, 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))
2 changes: 1 addition & 1 deletion example/dj/apps/test_chamber/tests/utils/datastructures.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from django.test import TestCase

from chamber.utils.datastructures import (ChoicesEnum, ChoicesNumEnum, Enum, NumEnum, OrderedSet)
from chamber.utils.datastructures import ChoicesEnum, ChoicesNumEnum, Enum, NumEnum, OrderedSet

from germanium.tools import assert_equal, assert_raises

Expand Down

0 comments on commit 7b5ae0b

Please sign in to comment.