Skip to content
This repository has been archived by the owner on May 30, 2020. It is now read-only.

Commit

Permalink
Added allow_none argument to min_value/max_value decorator
Browse files Browse the repository at this point in the history
Added tests for low_temperature and high_temperature property in configuration
  • Loading branch information
ercpe committed Dec 24, 2015
1 parent b5f9c0c commit 31e1a36
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 2 deletions.
41 changes: 40 additions & 1 deletion src/maxd/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,18 @@ def _wrapper(*args):

return _wrapper

def max_value(max):
def max_value(max, allow_none=True):
def _inner(func):
def _wrapper(*args):
value = func(*args)

if value is None:
if allow_none:
return value
else:
logger.info("Limiting value of %s to max %s" % (value, max))
return max

if value > max:
logger.info("Limiting value of %s to max %s" % (value, max))
return max
Expand All @@ -44,6 +51,26 @@ def _wrapper(*args):
return _wrapper
return _inner

def min_value(min, allow_none=True):
def _inner(func):
def _wrapper(*args):
value = func(*args)

if value is None:
if allow_none:
return value
else:
logger.info("Limiting value of %s to min %s" % (value, min))
return min

if value < min:
logger.info("Limiting value of %s to min %s" % (value, min))
return min

return value
return _wrapper
return _inner


class CalendarConfig(collections.namedtuple('CalendarConfig', ('name', 'url', 'username', 'password'))):

Expand Down Expand Up @@ -100,3 +127,15 @@ def calendars(self):
@timediff
def warmup_duration(self):
return self.get_int('GENERAL', 'warmup', 30)

@property
@max_value(30)
@min_value(5)
def high_temperature(self):
return self.get_int('GENERAL', 'high_temperature', None)

@property
@max_value(30)
@min_value(5)
def low_temperature(self):
return self.get_int('GENERAL', 'low_temperature', None)
33 changes: 32 additions & 1 deletion tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import datetime
import pytest

from maxd.config import Configuration, timediff, max_value
from maxd.config import Configuration, timediff, max_value, min_value


class TestConfig(object):
Expand Down Expand Up @@ -95,3 +95,34 @@ def test_max_value_decorator_limit_to_max(self):
def test():
return 20
assert test() == 10

def test_min_value_decorator(self):
@min_value(5)
def test():
return 10
assert test() == 10

def test_min_value_decorator_limit_to_min(self):
@min_value(10)
def test():
return 5
assert test() == 10

def test_min_value_decorator_none(self):
@min_value(5)
def test():
return None
assert test() is None

@min_value(5, allow_none=False)
def test2():
return None
assert test2() == 5

def test_get_high_temperature_not_set(self):
cfg = Configuration('/dev/null')
assert cfg.high_temperature is None

def test_get_low_temperature_not_set(self):
cfg = Configuration('/dev/null')
assert cfg.low_temperature is None

0 comments on commit 31e1a36

Please sign in to comment.