Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added negative check and is_percentage validator tests #83

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/75.misc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added a negative percentage check in the validator, and added less than dunder to Percentage class.
3 changes: 3 additions & 0 deletions src/colosseum/units.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ def __eq__(self, other):
return self.val == other.val and self.suffix == other.suffix
return False

def __lt__(self, other):
return self.val < other.val


px = PixelUnit()

Expand Down
4 changes: 4 additions & 0 deletions src/colosseum/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ def is_percentage(value):
error_msg = 'Value {value} is not a Percent unit'.format(value=value)
raise ValidationError(error_msg)

if value < units.Percent(0):
error_msg = 'Value {value} can not negative'.format(value=value)
raise ValidationError(error_msg)

return value


Expand Down
20 changes: 19 additions & 1 deletion tests/test_validators.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,38 @@
from unittest import TestCase

from colosseum.shapes import Rect
from colosseum.units import px
from colosseum.units import px, percent
from colosseum.validators import (
ValidationError,
is_border_spacing,
is_cursor,
is_integer,
is_number,
is_percentage,
is_quote,
is_rect,
is_uri,
)
from colosseum.wrappers import Quotes


class PercentTests(TestCase):
def test_percentage(self):
percent_value = is_percentage("100%")

self.assertEqual(percent_value, 100*percent)
self.assertEqual(type(percent_value), type(percent))

with self.assertRaises(ValidationError):
is_percentage("-100%")

with self.assertRaises(ValidationError):
is_percentage("100")

with self.assertRaises(ValidationError):
is_percentage('spam')


class NumericTests(TestCase):

def test_integer(self):
Expand Down
2 changes: 0 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,12 @@ deps =
commands = flake8 {posargs}

[testenv:towncrier-check]
skip_install = True
deps =
{[testenv:towncrier]deps}
commands =
python -m towncrier.check

[testenv:towncrier]
skip_install = True
deps =
towncrier >= 18.5.0
commands =
Expand Down