Skip to content

Commit

Permalink
Added unit test for inasafe#2132
Browse files Browse the repository at this point in the history
  • Loading branch information
timlinux committed Jul 20, 2015
1 parent fec8607 commit a8f1c28
Showing 1 changed file with 61 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@
# noinspection PyUnresolvedReferences
import qgis # pylint: disable=unused-import
from PyQt4.QtCore import QSettings
import json

from safe.gui.tools.minimum_needs.needs_profile import NeedsProfile
from safe.common.minimum_needs import MinimumNeeds

from safe_extras.parameters.parameter_exceptions import (
InvalidMaximumError,
ValueOutOfBounds)

class TestNeedsProfile(NeedsProfile):
"""Since we don't want to change the actual minimum needs settings in
Expand Down Expand Up @@ -128,3 +131,60 @@ def test_03_root_directory(self):
minimum_needs2.root_directory,
minimum_needs2._root_directory
)

def test_issue_2132(self):
"""Test that floats are cast to strings for precision handler."""
profile = TestNeedsProfile()
json_string = (
'{"resources": [{'
'"Resource name": "Rice", '
'"Frequency": "weekly", '
'"Default": 2.8, '
'"Maximum allowed": 5.0, '
'"Minimum allowed": 1.0, '
'"Unit": "kg",'
'"Units": "kgs",'
'"Unit abbreviation": "kg",'
'"Resource description": "Rice as a staple food",'
'"Readable sentence": "Test sentance"'
'}]}')
profile.minimum_needs = json.loads(json_string)
profile.get_needs_parameters()

def test_maximum_greater_than_minimum(self):
"""Test that that if maximum is less than minimum causes error."""
profile = TestNeedsProfile()
json_string = (
'{"resources": [{'
'"Resource name": "Rice", '
'"Frequency": "weekly", '
'"Default": 2.8, '
'"Maximum allowed": 6.0, '
'"Minimum allowed": 7.0, '
'"Unit": "kg",'
'"Units": "kgs",'
'"Unit abbreviation": "kg",'
'"Resource description": "Rice as a staple food",'
'"Readable sentence": "Test sentance"'
'}]}')
profile.minimum_needs = json.loads(json_string)
self.assertRaises(InvalidMaximumError, profile.get_needs_parameters)

def test_default_is_valid(self):
"""Test that that if maximum is less than minimum causes error."""
profile = TestNeedsProfile()
json_string = (
'{"resources": [{'
'"Resource name": "Rice", '
'"Frequency": "weekly", '
'"Default": 10, '
'"Maximum allowed": 9.0, '
'"Minimum allowed": 2.0, '
'"Unit": "kg",'
'"Units": "kgs",'
'"Unit abbreviation": "kg",'
'"Resource description": "Rice as a staple food",'
'"Readable sentence": "Test sentance"'
'}]}')
profile.minimum_needs = json.loads(json_string)
self.assertRaises(ValueOutOfBounds, profile.get_needs_parameters)

0 comments on commit a8f1c28

Please sign in to comment.