------------------------------------------------------------
revno: 6691
committer: Barry Warsaw <[email protected]>
branch nick: nopickles
timestamp: Tue 2009-02-17 21:40:55 -0500
message:
Add a "mock and monkey" layer to set up testing infrastructure, e.g. for
predictable dates and times.
Fill out the autorespond.txt test to show that when the date flips over, the
response counts reset.
Add a date/time factory which we should use rather than the built-in datetime
for now() and today() so that they can be appropriately tested.
added:
src/mailman/utilities/datetime.py
modified:
src/mailman/database/autorespond.py
src/mailman/docs/autorespond.txt
src/mailman/testing/layers.py
=== modified file 'src/mailman/database/autorespond.py'
--- src/mailman/database/autorespond.py 2009-02-17 04:57:30 +0000
+++ src/mailman/database/autorespond.py 2009-02-18 02:40:55 +0000
@@ -27,7 +27,6 @@
]
-from datetime import date
from storm.locals import And, Date, Int, Reference
from zope.interface import implements
@@ -37,6 +36,7 @@
from mailman.interfaces.autorespond import (
IAutoResponseRecord, IAutoResponseSet, Response)
from mailman.interfaces.mailinglist import IMailingList
+from mailman.utilities.datetime import today
@@ -58,7 +58,7 @@
self.mailing_list = mailing_list
self.address = address
self.response_type = response_type
- self.date_sent = date.today()
+ self.date_sent = today()
@@ -75,7 +75,7 @@
And(AutoResponseRecord.address == address,
AutoResponseRecord.mailing_list == self._mailing_list,
AutoResponseRecord.response_type == response_type,
- AutoResponseRecord.date_sent == date.today())).count()
+ AutoResponseRecord.date_sent == today())).count()
def response_sent(self, address, response_type):
"""See `IAutoResponseSet`."""
=== modified file 'src/mailman/docs/autorespond.txt'
--- src/mailman/docs/autorespond.txt 2009-02-17 04:57:30 +0000
+++ src/mailman/docs/autorespond.txt 2009-02-18 02:40:55 +0000
@@ -60,3 +60,13 @@
1
>>> response_set.todays_count(address, Response.command)
2
+
+Now the day flips over and all the counts reset.
+
+ >>> from mailman.utilities.datetime import factory
+ >>> factory.fast_forward()
+
+ >>> response_set.todays_count(address, Response.hold)
+ 0
+ >>> response_set.todays_count(address, Response.command)
+ 0
=== modified file 'src/mailman/testing/layers.py'
--- src/mailman/testing/layers.py 2009-01-17 02:04:21 +0000
+++ src/mailman/testing/layers.py 2009-02-18 02:40:55 +0000
@@ -22,6 +22,7 @@
__metaclass__ = type
__all__ = [
'ConfigLayer',
+ 'MockAndMonkeyLayer',
'SMTPLayer',
]
@@ -40,6 +41,7 @@
from mailman.core.logging import get_handler
from mailman.i18n import _
from mailman.testing.helpers import SMTPServer
+from mailman.utilities.datetime import factory
from mailman.utilities.string import expand
@@ -47,7 +49,24 @@
-class ConfigLayer:
+class MockAndMonkeyLayer:
+ """Layer for mocking and monkey patching for testing."""
+
+ @classmethod
+ def setUp(cls):
+ factory.testing_mode = True
+
+ @classmethod
+ def tearDown(cls):
+ factory.testing_mode = False
+
+ @classmethod
+ def testTearDown(cls):
+ factory.reset()
+
+
+
+class ConfigLayer(MockAndMonkeyLayer):
"""Layer for pushing and popping test configurations."""
var_dir = None
=== added file 'src/mailman/utilities/datetime.py'
--- src/mailman/utilities/datetime.py 1970-01-01 00:00:00 +0000
+++ src/mailman/utilities/datetime.py 2009-02-18 02:40:55 +0000
@@ -0,0 +1,69 @@
+# Copyright (C) 2009 by the Free Software Foundation, Inc.
+#
+# This file is part of GNU Mailman.
+#
+# GNU Mailman is free software: you can redistribute it and/or modify it under
+# the terms of the GNU General Public License as published by the Free
+# Software Foundation, either version 3 of the License, or (at your option)
+# any later version.
+#
+# GNU Mailman is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+# more details.
+#
+# You should have received a copy of the GNU General Public License along with
+# GNU Mailman. If not, see <http://www.gnu.org/licenses/>.
+
+"""Datetime utilities.
+
+Use these functions to produce variable times rather than the built-in
+datetime.datetime.now() and datetime.date.today(). These are better
+instrumented for testing purposes.
+"""
+
+
+from __future__ import absolute_import, unicode_literals
+
+__metaclass__ = type
+__all__ = [
+ ]
+
+
+import datetime
+
+
+
+class DateFactory:
+ """A factory for today() and now() that works with testing."""
+
+ # Set to True to produce predictable dates and times.
+ testing_mode = False
+ # The predictable time.
+ predictable_now = None
+ predictable_today = None
+
+ def now(self, tz=None):
+ return (self.predictable_now
+ if self.testing_mode
+ else datetime.datetime.now(tz))
+
+ def today(self):
+ return (self.predictable_today
+ if self.testing_mode
+ else datetime.date.today())
+
+ @classmethod
+ def reset(cls):
+ cls.predictable_now = datetime.datetime(2005, 8, 1, 7, 49, 23)
+ cls.predictable_today = cls.predictable_now.today()
+
+ @classmethod
+ def fast_forward(cls, days=1):
+ cls.predictable_today += datetime.timedelta(days=days)
+
+
+factory = DateFactory()
+factory.reset()
+today = factory.today
+now = factory.now
--
Primary development focus
https://code.launchpad.net/~mailman-coders/mailman/3.0
Your team Mailman Checkins is subscribed to branch lp:mailman.
To unsubscribe from this branch go to
https://code.launchpad.net/~mailman-coders/mailman/3.0/+edit-subscription.
_______________________________________________
Mailman-checkins mailing list
[email protected]
Unsubscribe:
http://mail.python.org/mailman/options/mailman-checkins/archive%40jab.org