From ee1d881dfa293ee5679795739931d93055793e08 Mon Sep 17 00:00:00 2001 From: Nicolas Delaby <nicolas@nexedi.com> Date: Thu, 7 Feb 2008 18:08:34 +0000 Subject: [PATCH] Add test for addToDate git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@19145 20353a03-c40f-0410-a6d1-a30d3c3de9de --- product/ERP5Type/tests/testDateUtils.py | 127 ++++++++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 product/ERP5Type/tests/testDateUtils.py diff --git a/product/ERP5Type/tests/testDateUtils.py b/product/ERP5Type/tests/testDateUtils.py new file mode 100644 index 0000000000..dbdaf99d48 --- /dev/null +++ b/product/ERP5Type/tests/testDateUtils.py @@ -0,0 +1,127 @@ +############################################################################## +# +# Copyright (c) 2007 Nexedi SARL and Contributors. All Rights Reserved. +# Jerome Perrin <jerome@nexedi.com> +# +# WARNING: This program as such is intended to be used by professional +# programmers who take the whole responsability of assessing all potential +# consequences resulting from its eventual inadequacies and bugs +# End users who are looking for a ready-to-use solution with commercial +# garantees and support are strongly adviced to contract a Free Software +# Service Company +# +# This program 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 2 +# of the License, or (at your option) any later version. +# +# This program 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 this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +# +############################################################################## + +import unittest + +# Make it possible to use Globals.get_request +class DummyRequest(dict): + __allow_access_to_unprotected_subobjects__ = 1 + def set(self, k, v): + self[k] = v + +global request +request = DummyRequest() + +def get_request(): + global request + return request + +# apply patch (before it's imported by other modules) +import Globals +Globals.get_request = get_request + + +# Initialize ERP5Form Product to load monkey patches +from Testing import ZopeTestCase +ZopeTestCase.installProduct('ERP5Type') + + +from DateTime import DateTime +from Products.ERP5Type.DateUtils import addToDate + +class TestDateUtils(unittest.TestCase): + """Tests field value caching system + """ + + def getTitle(self): + return "Date Utils" + + def test_integer_add_to_date(self): + date = DateTime('2000/01/01 UTC') + self.assertEqual(DateTime('2000/01/01 00:01:30 UTC').ISO(), + addToDate(date, second=90).ISO()) + self.assertEqual(DateTime('2000/01/01 01:30:00 UTC').ISO(), + addToDate(date, minute=90).ISO()) + self.assertEqual(DateTime('2000/01/04 18:00:00 UTC').ISO(), + addToDate(date, hour=90).ISO()) + self.assertEqual(DateTime('2000/03/31 UTC').ISO(), + addToDate(date, day=90).ISO()) + self.assertEqual(DateTime('2007/07/01 UTC').ISO(), + addToDate(date, month=90).ISO()) + self.assertEqual(DateTime('2090/01/01 UTC').ISO(), + addToDate(date, year=90).ISO()) + + def test_negative_add_to_date(self): + date = DateTime('2000/01/01 UTC') + self.assertEqual(DateTime('1999/12/31 23:59:59 UTC').ISO(), + addToDate(date, second=-1).ISO()) + self.assertEqual(DateTime('1999/12/31 23:59:00 UTC').ISO(), + addToDate(date, minute=-1).ISO()) + self.assertEqual(DateTime('1999/12/31 23:00:00 UTC').ISO(), + addToDate(date, hour=-1).ISO()) + self.assertEqual(DateTime('1999/12/31 00:00:00 UTC').ISO(), + addToDate(date, day=-1).ISO()) + self.assertEqual(DateTime('1999/12/01 00:00:00 UTC').ISO(), + addToDate(date, month=-1).ISO()) + self.assertEqual(DateTime('1999/01/01').ISO(), + addToDate(date, year=-1).ISO()) + + def test_float_add_to_date(self): + date = DateTime('2000/01/01 UTC') + self.assertEqual(DateTime('2000/01/01').ISO(), + addToDate(date, second=0.5).ISO()) + self.assertEqual(DateTime('2000/01/01 00:00:30 UTC').ISO(), + addToDate(date, minute=0.5).ISO()) + self.assertEqual(DateTime('2000/01/01 00:30:00 UTC').ISO(), + addToDate(date, hour=0.5).ISO()) + self.assertEqual(DateTime('2000/01/01 12:00:00 UTC').ISO(), + addToDate(date, day=0.5).ISO()) + self.assertEqual(DateTime('2000/01/16 12:00:00 UTC').ISO(), + addToDate(date, month=0.5).ISO()) + self.assertEqual(DateTime('2000/07/01').ISO(), + addToDate(date, year=0.5).ISO()) + + def test_complex_float_add_to_date(self): + complex_date = DateTime('2004/03/16 01:23:54 UTC') + self.assertEqual(DateTime('2004/03/16 01:23:54 UTC').ISO(), + addToDate(complex_date, second=0.5).ISO()) + self.assertEqual(DateTime('2004/03/16 01:24:24 UTC').ISO(), + addToDate(complex_date, minute=0.5).ISO()) + self.assertEqual(DateTime('2004/03/16 01:53:54 UTC').ISO(), + addToDate(complex_date, hour=0.5).ISO()) + self.assertEqual(DateTime('2004/03/16 13:23:54 UTC').ISO(), + addToDate(complex_date, day=0.5).ISO()) + self.assertEqual(DateTime('2004/03/31 13:23:54 UTC').ISO(), + addToDate(complex_date, month=0.5).ISO()) + self.assertEqual(DateTime('2004/09/16 01:23:54 UTC').ISO(), + addToDate(complex_date, year=0.5).ISO()) + +def test_suite(): + suite = unittest.TestSuite() + suite.addTest(unittest.makeSuite(TestDateUtils)) + return suite -- 2.30.9