Skip to content

Commit

Permalink
Improving code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigocam committed Oct 23, 2018
1 parent 1f19f41 commit df739cb
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 18 deletions.
3 changes: 1 addition & 2 deletions src/ej_configurations/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@
from boogie.fields import EnumField, Enum
from hyperpython import a, div, Text

from ej_conversations.validators import validate_color
from .icons import default_icon_name
from .sanitizer import sanitize_html
from .validators import validate_icon_name
from .validators import validate_icon_name, validate_color


class Format(Enum):
Expand Down
10 changes: 10 additions & 0 deletions src/ej_configurations/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest
from django.db import IntegrityError
from django.core.exceptions import ValidationError

from ej_configurations.models import SocialMediaIcon, Color, Fragment

Expand All @@ -26,6 +27,15 @@ def color(self):
def test_color_representation(self, color):
assert str(color) == 'red: #FF0000'

def test_color_with_invalid_value(self, db):
color = Color(name='invalid', hex_value='*')
with pytest.raises(ValidationError):
color.full_clean()

def test_color_with_valid_value(self, db):
color = Color(name='invalid', hex_value='#FF0000')
color.full_clean()


class TestFragment:
@pytest.fixture
Expand Down
16 changes: 16 additions & 0 deletions src/ej_configurations/validators.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
import re

from django.core.exceptions import ValidationError
from django.utils.translation import ugettext as _

from hyperpython.components.fa_icons import COLLECTIONS

try:
from colortools import COLOR_NAMES

COLOR_NAMES = dict(COLOR_NAMES)
except ImportError:
COLOR_NAMES = {}

COLOR_RE = re.compile(r'^#[0-9A-Fa-f]{3,4}(?:[0-9A-Fa-f]{3,4})?$')


def validate_color(color):
if not COLOR_RE.fullmatch(color) and color not in COLOR_NAMES:
raise ValidationError(_(f"'{color}' is a bad color value"))


def validate_icon_name(icon_name):
if icon_name not in COLLECTIONS:
Expand Down
16 changes: 0 additions & 16 deletions src/ej_conversations/validators.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,6 @@
import re

from django.core.exceptions import ValidationError
from django.utils.translation import ugettext_lazy as _

try:
from colortools import COLOR_NAMES

COLOR_NAMES = dict(COLOR_NAMES)
except ImportError:
COLOR_NAMES = {}

COLOR_RE = re.compile(r'^#[0-9A-Fa-f]{3,4}(?:[0-9A-Fa-f]{3,4})?$')


def validate_color(color):
if not COLOR_RE.fullmatch(color) and color not in COLOR_NAMES:
raise ValidationError(_("'{color}' is a bad color value", color=color))


def is_not_empty(text):
if not text.strip():
Expand Down
Empty file.
29 changes: 29 additions & 0 deletions src/ej_notifications/tests/test_routes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import pytest

from ej_notifications import routes
from ej_users.models import User


@pytest.fixture
def user(db):
return User.objects.create_user('email@server.com', 'password')


class TestRoute:
def test_index(self, rf, user):
request = rf.get('', {})
request.user = user
response = routes.index(request)
assert response == {
'content_title': 'List of notifications',
'user': user,
'notifications': ['hello', 'world']
}

def test_cluster(self, rf, user):
request = rf.get('', {})
request.user = user
response = routes.clusters(request)
assert response == {
'user': user,
}

0 comments on commit df739cb

Please sign in to comment.