Testing properties that are date-related

T

Tim Chase

Are there best practices for testing dates that are properties
which take the current date into consideration? I have something
like

class Foo:
def _get_year_range(self):
return (self._min_year, self._max_year)
def _set_year_range(self, year):
if isinstance(year, tuple):
_min, _max = map(window_date, year)
if _min > _max: _min, _max = _max, _min
else: # a raw int
_min = _max = window_date(year)
self._min_year, self._max_year = _min, _max
year_range = property(
fget=_get_year_range,
fget=_get_year_range,
)

The problem is that the behavior of the window_date function
depends on the current date (the function makes a guess about
adding the century if the year was <100). It *does* take an
"around" parameter that defaults to the current date. So for
pure testing of the window_date() function, I can hard-code some
date where I know what the expected values should be.

However if I want to write a test-harness for my property, I have
no way (AFAIK) to pass in this fixed date to _set_year_range() so
that the success/failure of my tests doesn't depend on the day I
run them:

class TestFoo:
def test_year_range(self):
around = date(2011,1,1)
f = Foo()
f.year_range = (97, 84)
self.assertEqual(f.year_range, (1984, 1997))

Any suggestions/tips/hints? Thanks,

-tkc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top