Skip to content

Commit

Permalink
Merge pull request #83 from dgmouris/negative-percent-check-validator
Browse files Browse the repository at this point in the history
Added negative check and is_percentage validator tests
  • Loading branch information
freakboy3742 authored Oct 24, 2020
2 parents 3106551 + f35a19a commit cbf974b
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 3 deletions.
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

0 comments on commit cbf974b

Please sign in to comment.