On 10/07/11 07:38, Peter Otten wrote:
Are there best practices for testing dates that are properties
which take the current date into consideration
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
Temporarily replace the window_date() function with something that you can
control completely. Assuming Foo and window_date are defined in foo.py:
# untested
import foo
class TestFoo:
def setUp(self):
foo.window_date = functools.partial(foo.window_date,
around=date(2011, 1, 1))
def tearDown(self):
foo.window_date = foo.window_date.func
def test_year_range(self):
f = Foo()
f.year_range = (97, 84)
self.assertEqual(f.year_range, (1984, 1997))
I had to twiddle my class code a bit to make sure it referenced
module.window_date() everywhere instead of just a raw
window_date() (originally pulled in via "from module import
window_date") so that it picked up the new function, but
otherwise it worked like a charm.
(there was also the matter of some function properties that were
used for an undetailed parameter to allow for suggesting that the
window_date bias backwards, forwards or around the current date,
but a little hackery took care of that too, just copying the
germane properties from the saved function object to the result
of the partial() call)
Thanks!
-tkc
--
http://mail.python.org/mailman/listinfo/python-list