diff --git a/.coveragerc b/.coveragerc new file mode 100644 index 0000000..03ff52f --- /dev/null +++ b/.coveragerc @@ -0,0 +1,3 @@ +[run] +branch = True +source = parsedatetime diff --git a/.gitignore b/.gitignore index 3348489..1db5058 100644 --- a/.gitignore +++ b/.gitignore @@ -23,6 +23,7 @@ pip-log.txt # Unit test / coverage reports .coverage .tox +tests/htmlcov/ #Translations *.mo diff --git a/.travis.yml b/.travis.yml index 858b928..6fb6251 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,8 +6,14 @@ python: - "3.4" - "pypy" +before_install: + - pip install codecov + install: - travis_retry pip install . script: - - python run_tests.py parsedatetime + - nosetests + +after_success: + - codecov diff --git a/Makefile b/Makefile index e4420a4..9738c57 100644 --- a/Makefile +++ b/Makefile @@ -26,7 +26,7 @@ lint: flake8 parsedatetime > violations.flake8.txt test: - python run_tests.py + nosetests build: clean python setup.py sdist upload diff --git a/README.rst b/README.rst index 7bb0843..25ce68c 100644 --- a/README.rst +++ b/README.rst @@ -12,6 +12,10 @@ Python 2.7 or greater is required for parsedatetime version 1.0 or greater. :target: https://travis-ci.org/bear/parsedatetime :alt: Travis CI +.. image:: http://codecov.io/github/bear/parsedatetime/coverage.svg?branch=master + :target: http://codecov.io/github/bear/parsedatetime + :alt: Codecov + ========== Installing ========== @@ -26,7 +30,7 @@ Running Tests From the source directory:: - python run_tests.py parsedatetime + nosetests To run tests on several python versions, type ``tox``:: diff --git a/parsedatetime/tests/TestAlternativeAbbreviations.py b/parsedatetime/tests/TestAlternativeAbbreviations.py deleted file mode 100644 index 6657f97..0000000 --- a/parsedatetime/tests/TestAlternativeAbbreviations.py +++ /dev/null @@ -1,96 +0,0 @@ -import unittest, time, datetime -import parsedatetime as pdt - -class pdtLocale_en(pdt.pdt_locales.pdtLocale_icu): - """Update en locale to include a bunch of different abbreviations""" - def __init__(self): - super(pdtLocale_en, self).__init__(localeID='en_us') - self.Weekdays = [ 'monday', 'tuesday', 'wednesday', - 'thursday', 'friday', 'saturday', 'sunday', - ] - self.shortWeekdays = [ 'mon|mond', 'tue|tues', 'wed|wedn', - 'thu|thur|thurs', 'fri|frid', 'sat|sa', 'sun|su', - ] - self.Months = [ 'january', 'february', 'march', - 'april', 'may', 'june', - 'july', 'august', 'september', - 'october', 'november', 'december', - ] - self.shortMonths = [ 'jan|janu', 'feb|febr', 'mar|marc', - 'apr|apri', 'may', 'jun|june', - 'jul', 'aug|augu', 'sep|sept', - 'oct|octo', 'nov|novem', 'dec|decem', - ] - -class test(unittest.TestCase): - - - @pdt.tests.assertEqualWithComparator - def assertExpectedResult(self, result, check, **kwargs): - return pdt.tests.compareResultByTimeTuplesAndFlags(result, check, **kwargs) - - def setUp(self): - pdt.pdtLocales['en_us'] = pdtLocale_en # override for the test - self.ptc = pdt.Constants('en_us', usePyICU=False) - self.cal = pdt.Calendar(self.ptc) - self.yr, self.mth, self.dy, self.hr, self.mn, self.sec, self.wd, self.yd, self.isdst = time.localtime() - - - def testDaysOfWeek(self): - start = datetime.datetime(2014, 10, 25, self.hr, self.mn, self.sec).timetuple() - - target = datetime.datetime(2014, 10, 26, self.hr, self.mn, self.sec).timetuple() - self.assertExpectedResult(self.cal.parse('sunday', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('sun', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('su', start), (target, 1)) - - target = datetime.datetime(2014, 10, 27, self.hr, self.mn, self.sec).timetuple() - self.assertExpectedResult(self.cal.parse('Monday', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('mon', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('mond', start), (target, 1)) - - target = datetime.datetime(2014, 10, 28, self.hr, self.mn, self.sec).timetuple() - self.assertExpectedResult(self.cal.parse('tuesday', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('tues', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('tue', start), (target, 1)) - - target = datetime.datetime(2014, 10, 29, self.hr, self.mn, self.sec).timetuple() - self.assertExpectedResult(self.cal.parse('wednesday', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('wedn', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('wed', start), (target, 1)) - - target = datetime.datetime(2014, 10, 30, self.hr, self.mn, self.sec).timetuple() - self.assertExpectedResult(self.cal.parse('thursday', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('thu', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('thur', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('thurs', start), (target, 1)) - - target = datetime.datetime(2014, 10, 31, self.hr, self.mn, self.sec).timetuple() - self.assertExpectedResult(self.cal.parse('friday', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('fri', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('frid', start), (target, 1)) - - target = datetime.datetime(2014, 11, 1, self.hr, self.mn, self.sec).timetuple() - self.assertExpectedResult(self.cal.parse('saturday', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('sat', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('sa', start), (target, 1)) - - def testMonths(self): - start = datetime.datetime(2014,1, 1, self.hr, self.mn, self.sec).timetuple() - for dates, expected_date in [ - ('jan|janu|january', datetime.datetime(2014, 1, 1, self.hr, self.mn, self.sec).timetuple() ), - ('feb|febr|february', datetime.datetime(2014, 2, 1, self.hr, self.mn, self.sec).timetuple() ), - ('mar|marc|march', datetime.datetime(2014, 3, 1, self.hr, self.mn, self.sec).timetuple() ), - ('apr|apri|april', datetime.datetime(2014, 4, 1, self.hr, self.mn, self.sec).timetuple() ), - ('may|may', datetime.datetime(2014, 5, 1, self.hr, self.mn, self.sec).timetuple() ), - ('jun|june', datetime.datetime(2014, 6, 1, self.hr, self.mn, self.sec).timetuple() ), - ('jul|july', datetime.datetime(2014, 7, 1, self.hr, self.mn, self.sec).timetuple() ), - ('aug|augu|august', datetime.datetime(2014, 8, 1, self.hr, self.mn, self.sec).timetuple() ), - ('sep|sept|september', datetime.datetime(2014, 9, 1, self.hr, self.mn, self.sec).timetuple() ), - ('oct|octo|october', datetime.datetime(2014, 10, 1, self.hr, self.mn, self.sec).timetuple() ), - ('nov|novem|november', datetime.datetime(2014, 11, 1, self.hr, self.mn, self.sec).timetuple() ), - ('dec|decem|december', datetime.datetime(2014, 12, 1, self.hr, self.mn, self.sec).timetuple() ) - ]: - for dateText in dates.split("|"): - # print dateText - self.assertExpectedResult(self.cal.parse(dateText, start), (expected_date, 1)) diff --git a/parsedatetime/tests/TestAustralianLocale.py b/parsedatetime/tests/TestAustralianLocale.py deleted file mode 100644 index 7584073..0000000 --- a/parsedatetime/tests/TestAustralianLocale.py +++ /dev/null @@ -1,81 +0,0 @@ - -""" -Test parsing of simple date and times using the Australian locale -""" - -import unittest, time, datetime -import parsedatetime as pdt - -class test(unittest.TestCase): - - @pdt.tests.assertEqualWithComparator - def assertExpectedResult(self, result, check, **kwargs): - return pdt.tests.compareResultByTimeTuplesAndFlags(result, check, **kwargs) - - def setUp(self): - self.ptc = pdt.Constants('en_AU', usePyICU=False) - self.cal = pdt.Calendar(self.ptc) - - self.yr, self.mth, self.dy, self.hr, self.mn, self.sec, self.wd, self.yd, self.isdst = time.localtime() - - if self.ptc.localeID != 'en_AU': - raise unittest.SkipTest('Locale not set to en_AU - check if PyICU is installed') - - def testTimes(self): - start = datetime.datetime(self.yr, self.mth, self.dy, self.hr, self.mn, self.sec).timetuple() - target = datetime.datetime(self.yr, self.mth, self.dy, 23, 0, 0).timetuple() - - self.assertExpectedResult(self.cal.parse('11:00:00 PM', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('11:00 PM', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('11 PM', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('11PM', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('2300', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('23:00', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('11p', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('11pm', start), (target, 2)) - - target = datetime.datetime(self.yr, self.mth, self.dy, 11, 0, 0).timetuple() - - self.assertExpectedResult(self.cal.parse('11:00:00 AM', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('11:00 AM', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('11 AM', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('11AM', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('1100', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('11:00', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('11a', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('11am', start), (target, 2)) - - target = datetime.datetime(self.yr, self.mth, self.dy, 7, 30, 0).timetuple() - - self.assertExpectedResult(self.cal.parse('730', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('0730', start), (target, 2)) - - target = datetime.datetime(self.yr, self.mth, self.dy, 17, 30, 0).timetuple() - - self.assertExpectedResult(self.cal.parse('1730', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('173000', start), (target, 2)) - - def testDates(self): - start = datetime.datetime(self.yr, self.mth, self.dy, self.hr, self.mn, self.sec).timetuple() - target = datetime.datetime(2006, 8, 25, self.hr, self.mn, self.sec).timetuple() - - self.assertExpectedResult(self.cal.parse('25-08-2006', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('25/08/2006', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('25.08.2006', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('25-8-06', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('25/8/06', start), (target, 1)) - - if self.mth > 8 or (self.mth == 8 and self.dy > 25): - target = datetime.datetime(self.yr+1, 8, 25, self.hr, self.mn, self.sec).timetuple() - else: - target = datetime.datetime(self.yr, 8, 25, self.hr, self.mn, self.sec).timetuple() - - self.assertExpectedResult(self.cal.parse('25-8', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('25/8', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('25.8', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('25-08', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('25/08', start), (target, 1)) - - -if __name__ == "__main__": - unittest.main() diff --git a/parsedatetime/tests/TestComplexDateTimes.py b/parsedatetime/tests/TestComplexDateTimes.py deleted file mode 100644 index 07bd095..0000000 --- a/parsedatetime/tests/TestComplexDateTimes.py +++ /dev/null @@ -1,77 +0,0 @@ - -""" -Test parsing of complex date and times -""" - -import unittest, time, datetime -import parsedatetime as pdt - -class test(unittest.TestCase): - - @pdt.tests.assertEqualWithComparator - def assertExpectedResult(self, result, check, **kwargs): - return pdt.tests.compareResultByTimeTuplesAndFlags(result, check, **kwargs) - - - def setUp(self): - self.cal = pdt.Calendar() - self.yr, self.mth, self.dy, self.hr, self.mn, self.sec, self.wd, self.yd, self.isdst = time.localtime() - - def testDates(self): - start = datetime.datetime(self.yr, self.mth, self.dy, self.hr, self.mn, self.sec).timetuple() - target = datetime.datetime(2006, 8, 25, 17, 0, 0).timetuple() - - self.assertExpectedResult(self.cal.parse('08/25/2006 5pm', start), (target, 3)) - self.assertExpectedResult(self.cal.parse('5pm on 08.25.2006', start), (target, 3)) - self.assertExpectedResult(self.cal.parse('5pm August 25, 2006', start), (target, 3)) - self.assertExpectedResult(self.cal.parse('5pm August 25th, 2006', start), (target, 3)) - self.assertExpectedResult(self.cal.parse('5pm 25 August, 2006', start), (target, 3)) - self.assertExpectedResult(self.cal.parse('5pm 25th August, 2006', start), (target, 3)) - self.assertExpectedResult(self.cal.parse('Aug 25, 2006 5pm', start), (target, 3)) - self.assertExpectedResult(self.cal.parse('Aug 25th, 2006 5pm', start), (target, 3)) - self.assertExpectedResult(self.cal.parse('25 Aug, 2006 5pm', start), (target, 3)) - self.assertExpectedResult(self.cal.parse('25th Aug 2006, 5pm', start), (target, 3)) - - if self.mth > 8 or (self.mth == 8 and self.dy > 5): - target = datetime.datetime(self.yr + 1, 8, 5, 17, 0, 0).timetuple() - else: - target = datetime.datetime(self.yr, 8, 5, 17, 0, 0).timetuple() - - self.assertExpectedResult(self.cal.parse('8/5 at 5pm', start), (target, 3)) - self.assertExpectedResult(self.cal.parse('5pm 8.5', start), (target, 3)) - self.assertExpectedResult(self.cal.parse('08/05 5pm', start), (target, 3)) - self.assertExpectedResult(self.cal.parse('August 5 5pm', start), (target, 3)) - self.assertExpectedResult(self.cal.parse('5pm Aug 05', start), (target, 3)) - self.assertExpectedResult(self.cal.parse('Aug 05 5pm', start), (target, 3)) - self.assertExpectedResult(self.cal.parse('Aug 05th 5pm', start), (target, 3)) - self.assertExpectedResult(self.cal.parse('5 August 5pm', start), (target, 3)) - self.assertExpectedResult(self.cal.parse('5th August 5pm', start), (target, 3)) - self.assertExpectedResult(self.cal.parse('5pm 05 Aug', start), (target, 3)) - self.assertExpectedResult(self.cal.parse('05 Aug 5pm', start), (target, 3)) - self.assertExpectedResult(self.cal.parse('05th Aug 5pm', start), (target, 3)) - - self.assertExpectedResult(self.cal.parse('August 5th 5pm', start), (target, 3)) - - if self.mth > 8 or (self.mth == 8 and self.dy > 5): - target = datetime.datetime(self.yr + 1, 8, 5, 12, 0, 0).timetuple() - else: - target = datetime.datetime(self.yr, 8, 5, 12, 0, 0).timetuple() - - self.assertExpectedResult(self.cal.parse('August 5th 12:00', start), (target, 3)) - self.assertExpectedResult(self.cal.parse('August 5th 12pm', start), (target, 3)) - self.assertExpectedResult(self.cal.parse('August 5th 12:00pm', start), (target, 3)) - self.assertExpectedResult(self.cal.parse('August 5th 12 pm', start), (target, 3)) - self.assertExpectedResult(self.cal.parse('August 5th 12:00 pm', start), (target, 3)) - - if self.mth > 8 or (self.mth == 8 and self.dy > 5): - target = datetime.datetime(self.yr + 1, 8, 22, 3, 26, 0).timetuple() - else: - target = datetime.datetime(self.yr, 8, 22, 3, 26, 0).timetuple() - - self.assertExpectedResult(self.cal.parse('August 22nd 3:26', start), (target, 3)) - self.assertExpectedResult(self.cal.parse('August 22nd 3:26am', start), (target, 3)) - self.assertExpectedResult(self.cal.parse('August 22nd 3:26 am', start), (target, 3)) - - -if __name__ == "__main__": - unittest.main() diff --git a/parsedatetime/tests/TestErrors.py b/parsedatetime/tests/TestErrors.py deleted file mode 100644 index 629a388..0000000 --- a/parsedatetime/tests/TestErrors.py +++ /dev/null @@ -1,59 +0,0 @@ - -""" -Test parsing of units -""" - -import unittest, time, datetime -import parsedatetime as pdt - -class test(unittest.TestCase): - - @pdt.tests.assertEqualWithComparator - def assertExpectedResult(self, result, check, **kwargs): - return pdt.tests.compareResultByTimeTuplesAndFlags(result, check, **kwargs) - - @pdt.tests.assertEqualWithComparator - def assertExpectedErrorFlag(self, result, check, **kwargs): - return pdt.tests.compareResultByFlags(result, check, **kwargs) - - def setUp(self): - self.cal = pdt.Calendar() - self.yr, self.mth, self.dy, self.hr, self.mn, self.sec, self.wd, self.yd, self.isdst = time.localtime() - - def testErrors(self): - s = datetime.datetime.now() - start = s.timetuple() - - # These tests all return current date/time as they are out of range - self.assertExpectedResult(self.cal.parse('01/0', start), (start, 0)) - self.assertExpectedResult(self.cal.parse('08/35', start), (start, 0)) - self.assertExpectedResult(self.cal.parse('18/35', start), (start, 0)) - self.assertExpectedResult(self.cal.parse('1799', start), (start, 0)) - self.assertExpectedResult(self.cal.parse('781', start), (start, 0)) - self.assertExpectedResult(self.cal.parse('2702', start), (start, 0)) - self.assertExpectedResult(self.cal.parse('78', start), (start, 0)) - self.assertExpectedResult(self.cal.parse('11', start), (start, 0)) - self.assertExpectedResult(self.cal.parse('1', start), (start, 0)) - self.assertExpectedResult(self.cal.parse('174565', start), (start, 0)) - self.assertExpectedResult(self.cal.parse('177505', start), (start, 0)) - # ensure short month names do not cause false positives within a word - jun (june) - self.assertExpectedResult(self.cal.parse('injunction', start), (start, 0)) - # ensure short month names do not cause false positives at the start of a word - jul (juuly) - self.assertExpectedResult(self.cal.parse('julius', start), (start, 0)) - # ensure short month names do not cause false positives at the end of a word - mar (march) - self.assertExpectedResult(self.cal.parse('lamar', start), (start, 0)) - # ensure short weekday names do not cause false positives within a word - mon (monday) - self.assertExpectedResult(self.cal.parse('demonize', start), (start, 0)) - # ensure short weekday names do not cause false positives at the start of a word - mon (monday) - self.assertExpectedResult(self.cal.parse('money', start), (start, 0)) - # ensure short weekday names do not cause false positives at the end of a word - th (thursday) - self.assertExpectedResult(self.cal.parse('month', start), (start, 0)) - self.assertExpectedErrorFlag(self.cal.parse('30/030/01/071/07', start), (start, 0)) - # overflow due to Python's datetime - self.assertExpectedResult(self.cal.parse('12345 y', start), (start, 0)) - self.assertExpectedResult(self.cal.parse('654321 w', start), (start, 0)) - self.assertExpectedResult(self.cal.parse('3700000 d', start), (start, 0)) - - -if __name__ == "__main__": - unittest.main() diff --git a/parsedatetime/tests/TestFrenchLocale.py b/parsedatetime/tests/TestFrenchLocale.py deleted file mode 100644 index 56eb6a9..0000000 --- a/parsedatetime/tests/TestFrenchLocale.py +++ /dev/null @@ -1,133 +0,0 @@ - -""" -Test parsing of simple date and times using the French locale - -Note: requires PyICU -""" -from __future__ import unicode_literals -import unittest, time, datetime -import parsedatetime as pdt - -class test(unittest.TestCase): - - @pdt.tests.assertEqualWithComparator - def assertExpectedResult(self, result, check, **kwargs): - return pdt.tests.compareResultByTimeTuplesAndFlags(result, check, **kwargs) - - def setUp(self): - self.ptc = pdt.Constants('fr_FR', usePyICU=True) - self.cal = pdt.Calendar(self.ptc) - - self.yr, self.mth, self.dy, self.hr, self.mn, self.sec, self.wd, self.yd, self.isdst = time.localtime() - - if self.ptc.localeID != 'fr_FR': - raise unittest.SkipTest('Locale not set to fr_FR - check if PyICU is installed') - - def testTimes(self): - if self.ptc.localeID == 'fr_FR': - start = datetime.datetime(self.yr, self.mth, self.dy, self.hr, self.mn, self.sec).timetuple() - target = datetime.datetime(self.yr, self.mth, self.dy, 23, 0, 0).timetuple() - - self.assertExpectedResult(self.cal.parse('2300', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('23:00', start), (target, 2)) - - target = datetime.datetime(self.yr, self.mth, self.dy, 11, 0, 0).timetuple() - - self.assertExpectedResult(self.cal.parse('1100', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('11:00', start), (target, 2)) - - target = datetime.datetime(self.yr, self.mth, self.dy, 7, 30, 0).timetuple() - - self.assertExpectedResult(self.cal.parse('730', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('0730', start), (target, 2)) - - target = datetime.datetime(self.yr, self.mth, self.dy, 17, 30, 0).timetuple() - - self.assertExpectedResult(self.cal.parse('1730', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('173000', start), (target, 2)) - - def testDates(self): - if self.ptc.localeID == 'fr_FR': - start = datetime.datetime(self.yr, self.mth, self.dy, self.hr, self.mn, self.sec).timetuple() - target = datetime.datetime(2006, 8, 25, self.hr, self.mn, self.sec).timetuple() - - self.assertExpectedResult(self.cal.parse('25/08/2006', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('25/8/06', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('ao\xfbt 25, 2006', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('ao\xfbt 25 2006', start), (target, 1)) - - if self.mth > 8 or (self.mth == 8 and self.dy > 25): - target = datetime.datetime(self.yr+1, 8, 25, self.hr, self.mn, self.sec).timetuple() - else: - target = datetime.datetime(self.yr, 8, 25, self.hr, self.mn, self.sec).timetuple() - - self.assertExpectedResult(self.cal.parse('25/8', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('25/08', start), (target, 1)) - - def testWeekDays(self): - if self.ptc.localeID == 'fr_FR': - start = datetime.datetime(self.yr, self.mth, self.dy, self.hr, self.mn, self.sec).timetuple() - - o1 = self.ptc.CurrentDOWParseStyle - o2 = self.ptc.DOWParseStyle - - # set it up so the current dow returns current day - self.ptc.CurrentDOWParseStyle = True - self.ptc.DOWParseStyle = 1 - - for i in range(0,7): - dow = self.ptc.shortWeekdays[i] - - result = self.cal.parse(dow, start) - - yr, mth, dy, hr, mn, sec, wd, yd, isdst = result[0] - - self.assertEqual(wd, i) - - self.ptc.CurrentDOWParseStyle = o1 - self.ptc.DOWParseStyle = o2 - -class pdtLocale_fr(pdt.pdt_locales.pdtLocale_icu): - """French locale with french today/tomorrow/yesterday""" - def __init__(self): - super(pdtLocale_fr, self).__init__(localeID='fr_FR') - self.dayOffsets.update({"aujourd'hui": 0, 'demain': 1, 'hier': -1}) - -class TestDayOffsets(test): - # test how Aujourd'hui/Demain/Hier are parsed - def setUp(self): - super(TestDayOffsets, self).setUp() - self.__old_pdtlocale_fr = pdt.pdtLocales.get('fr_FR') # save for later - pdt.pdtLocales['fr_FR'] = pdtLocale_fr # override for the test - self.ptc = pdt.Constants('fr_FR', usePyICU=False) - self.cal = pdt.Calendar(self.ptc) - - def test_dayoffsets(self): - start = datetime.datetime(self.yr, self.mth, self.dy, 9) - for date_string, expected_day_offset in [ - ("Aujourd'hui", 0), - ("aujourd'hui", 0), - ("Demain", 1), - ("demain", 1), - ("Hier", -1), - ("hier", -1), - ("today", 0), # assume default names exist - ("tomorrow", 1), - ("yesterday", -1), - ("au jour de hui", None)]: - got_dt, rc = self.cal.parseDT(date_string, start) - if expected_day_offset is not None: - self.assertEqual(rc, 1) - target = (start + datetime.timedelta(days=expected_day_offset)) - self.assertEqual(got_dt, target) - else: - self.assertEqual(rc, 0) - - def tearDown(self): - if self.__old_pdtlocale_fr is not None: # restore the locale - pdt.pdtLocales['fr_FR'] = self.__old_pdtlocale_fr - super(TestDayOffsets, self).tearDown() - - -if __name__ == "__main__": - unittest.main() diff --git a/parsedatetime/tests/TestGermanLocale.py b/parsedatetime/tests/TestGermanLocale.py deleted file mode 100644 index a6bac3b..0000000 --- a/parsedatetime/tests/TestGermanLocale.py +++ /dev/null @@ -1,66 +0,0 @@ - -""" -Test parsing of simple date and times using the German locale -""" - -import unittest, time, datetime -import parsedatetime as pdt - - -class test(unittest.TestCase): - - @pdt.tests.assertEqualWithComparator - def assertExpectedResult(self, result, check, **kwargs): - return pdt.tests.compareResultByTimeTuplesAndFlags(result, check, **kwargs) - - def setUp(self): - self.ptc = pdt.Constants('de_DE', usePyICU=False) - self.cal = pdt.Calendar(self.ptc) - - self.yr, self.mth, self.dy, self.hr, self.mn, self.sec, self.wd, self.yd, self.isdst = time.localtime() - - if self.ptc.localeID != 'de_DE': - raise unittest.SkipTest('Locale not set to de_DE - check if PyICU is installed') - - def testTimes(self): - start = datetime.datetime(self.yr, self.mth, self.dy, self.hr, self.mn, self.sec).timetuple() - target = datetime.datetime(self.yr, self.mth, self.dy, 23, 0, 0).timetuple() - - self.assertExpectedResult(self.cal.parse('23:00:00', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('23:00', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('2300', start), (target, 2)) - - target = datetime.datetime(self.yr, self.mth, self.dy, 11, 0, 0).timetuple() - - self.assertExpectedResult(self.cal.parse('11:00:00', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('11:00', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('1100', start), (target, 2)) - - target = datetime.datetime(self.yr, self.mth, self.dy, 7, 30, 0).timetuple() - - self.assertExpectedResult(self.cal.parse('730', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('0730', start), (target, 2)) - - target = datetime.datetime(self.yr, self.mth, self.dy, 17, 30, 0).timetuple() - - self.assertExpectedResult(self.cal.parse('1730', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('173000', start), (target, 2)) - - def testDates(self): - start = datetime.datetime(self.yr, self.mth, self.dy, self.hr, self.mn, self.sec).timetuple() - target = datetime.datetime(2006, 8, 25, self.hr, self.mn, self.sec).timetuple() - - self.assertExpectedResult(self.cal.parse('25.08.2006', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('25.8.06', start), (target, 1)) - - if self.mth > 8 or (self.mth == 8 and self.dy > 25): - target = datetime.datetime(self.yr+1, 8, 25, self.hr, self.mn, self.sec).timetuple() - else: - target = datetime.datetime(self.yr, 8, 25, self.hr, self.mn, self.sec).timetuple() - - self.assertExpectedResult(self.cal.parse('25.8', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('25.08', start), (target, 1)) - - -if __name__ == "__main__": - unittest.main() diff --git a/parsedatetime/tests/TestMultiple.py b/parsedatetime/tests/TestMultiple.py deleted file mode 100644 index 8c7ff98..0000000 --- a/parsedatetime/tests/TestMultiple.py +++ /dev/null @@ -1,81 +0,0 @@ - -""" -Test parsing of strings with multiple chunks -""" - -import unittest, time, datetime -import parsedatetime as pdt - -class test(unittest.TestCase): - - @pdt.tests.assertEqualWithComparator - def assertExpectedResult(self, result, check, **kwargs): - return pdt.tests.compareResultByTimeTuplesAndFlags(result, check, **kwargs) - - def setUp(self): - self.cal = pdt.Calendar() - self.yr, self.mth, self.dy, self.hr, self.mn, self.sec, self.wd, self.yd, self.isdst = time.localtime() - - def testSimpleMultipleItems(self): - s = datetime.datetime.now() - t = self.cal.inc(s, year=3) + datetime.timedelta(days=5, weeks=2) - - start = s.timetuple() - target = t.timetuple() - - self.assertExpectedResult(self.cal.parse('3 years 2 weeks 5 days', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('3years 2weeks 5days', start), (target, 1)) - - def testMultipleItemsSingleCharUnits(self): - s = datetime.datetime.now() - t = self.cal.inc(s, year=3) + datetime.timedelta(days=5, weeks=2) - - start = s.timetuple() - target = t.timetuple() - - self.assertExpectedResult(self.cal.parse('3 y 2 w 5 d', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('3y 2w 5d', start), (target, 1)) - - t = self.cal.inc(s, year=3) + datetime.timedelta(hours=5, minutes=50) - target = t.timetuple() - - self.assertExpectedResult(self.cal.parse('3y 5h 50m', start), (target, 3)) - - def testMultipleItemsWithPunctuation(self): - s = datetime.datetime.now() - t = self.cal.inc(s, year=3) + datetime.timedelta(days=5, weeks=2) - - start = s.timetuple() - target = t.timetuple() - - self.assertExpectedResult(self.cal.parse('3 years, 2 weeks, 5 days', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('3 years, 2 weeks and 5 days', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('3y, 2w, 5d ', start), (target, 1)) - - def testUnixATStyle(self): - s = datetime.datetime.now() - t = s + datetime.timedelta(days=3) - - t = t.replace(hour=16, minute=0, second=0) - - start = s.timetuple() - target = t.timetuple() - - self.assertExpectedResult(self.cal.parse('4pm + 3 days', start), (target, 3)) - self.assertExpectedResult(self.cal.parse('4pm +3 days', start), (target, 3)) - - def testUnixATStyleNegative(self): - s = datetime.datetime.now() - t = s + datetime.timedelta(days=-3) - - t = t.replace(hour=16, minute=0, second=0) - - start = s.timetuple() - target = t.timetuple() - - self.assertExpectedResult(self.cal.parse('4pm - 3 days', start), (target, 3)) - self.assertExpectedResult(self.cal.parse('4pm -3 days', start), (target, 3)) - - -if __name__ == "__main__": - unittest.main() diff --git a/parsedatetime/tests/TestNlp.py b/parsedatetime/tests/TestNlp.py deleted file mode 100644 index 8d50a96..0000000 --- a/parsedatetime/tests/TestNlp.py +++ /dev/null @@ -1,99 +0,0 @@ - -""" -Test parsing of strings that are phrases -""" - -import unittest, time, datetime, sys -import parsedatetime as pdt - -class test(unittest.TestCase): - - # a special compare function for nlp returned data - @pdt.tests.assertEqualWithComparator - def assertExpectedResult(self, result, check, dateOnly=False): - target = result - value = check - - if target is None and value is None: - return True - - if (target is None and value is not None) or (target is not None and value is None): - return False - - if len(target) != len(value): - return False - - for i in range(0, len(target)): - target_date = target[i][0] - value_date = value[i][0] - - if target_date.year != value_date.year or target_date.month != value_date.month or target_date.day != value_date.day or target_date.hour != value_date.hour or target_date.minute != value_date.minute: - return False - if target[i][1] != value[i][1]: - return False - if target[i][2] != value[i][2]: - return False - if target[i][3] != value[i][3]: - return False - if target[i][4] != value[i][4]: - return False - - return True - - def setUp(self): - self.cal = pdt.Calendar() - self.yr, self.mth, self.dy, self.hr, self.mn, self.sec, self.wd, self.yd, self.isdst = time.localtime() - - def testLongPhrase(self): - # note: these tests do not need to be as dynamic as the others because this is still based - # on the parse() function, so all tests of the actual processing of the datetime - # value returned are applicable to this. Here we are concerned with ensuring the - # correct portions of text and their positions are extracted and processed. - start = datetime.datetime(2013, 8, 1, 21, 25, 0).timetuple() - target = ((datetime.datetime(2013, 8, 5, 20, 0), 3, 17, 37, 'At 8PM on August 5th'), - (datetime.datetime(2013, 8, 9, 21, 0), 3, 72, 90, 'next Friday at 9PM'), - (datetime.datetime(2013, 8, 1, 21, 30, 0), 2, 120, 132, 'in 5 minutes'), - (datetime.datetime(2013, 8, 8, 9, 0), 1, 173, 182, 'next week')) - - # positive testing - self.assertExpectedResult(self.cal.nlp("I'm so excited!! At 8PM on August 5th i'm going to fly to Florida" - ". Then next Friday at 9PM i'm going to Dog n Bone! And in 5 " - "minutes I'm going to eat some food! Talk to you next week.", start), target) - - target = datetime.datetime(self.yr, self.mth, self.dy, 17, 0, 0).timetuple() - - def testQuotes(self): - # quotes should not interfere with datetime language recognition - start = datetime.datetime(2013, 8, 1, 21, 25, 0).timetuple() - target = self.cal.nlp("I'm so excited!! At '8PM on August 5th' i'm going to fly to Florida" - ". Then 'next Friday at 9PM' i'm going to Dog n Bone! And in '5 " - "minutes' I'm going to eat some food! Talk to you \"next week\"", start) - - self.assertEqual(target[0][4], "At '8PM on August 5th") - self.assertEqual(target[1][4], "next Friday at 9PM") - self.assertEqual(target[2][4], "in '5 minutes") - self.assertEqual(target[3][4], "next week") - - def testPrefixes(self): - # nlp has special handling for on/in/at prefixes - start = datetime.datetime(2013, 8, 1, 21, 25, 0).timetuple() - - target = self.cal.nlp("Buy a balloon on Monday", start) - self.assertEqual(target[0][4], "on Monday") - - target = self.cal.nlp("Buy a balloon at noon", start) - self.assertEqual(target[0][4], "at noon") - - target = self.cal.nlp("Buy a balloon in a month", start) - self.assertEqual(target[0][4], "in a month") - - # Should no longer pull "on" off the end of balloon - target = self.cal.nlp("Buy a balloon Monday", start) - self.assertEqual(target[0][4], "Monday") - - def testFalsePositives(self): - # negative testing - no matches should return None - start = datetime.datetime(2013, 8, 1, 21, 25, 0).timetuple() - self.assertExpectedResult(self.cal.nlp("Next, I'm so excited!! So many things that are going to happen every week!!", start), None) - self.assertExpectedResult(self.cal.nlp("$300", start), None) - self.assertExpectedResult(self.cal.nlp("300ml", start), None) diff --git a/parsedatetime/tests/TestPhrases.py b/parsedatetime/tests/TestPhrases.py deleted file mode 100644 index f72ef58..0000000 --- a/parsedatetime/tests/TestPhrases.py +++ /dev/null @@ -1,130 +0,0 @@ - -""" -Test parsing of strings that are phrases -""" - -import time -import datetime -import unittest -import parsedatetime as pdt - -class test(unittest.TestCase): - - @pdt.tests.assertEqualWithComparator - def assertExpectedResult(self, result, check, **kwargs): - return pdt.tests.compareResultByTimeTuplesAndFlags(result, check, **kwargs) - - def setUp(self): - self.cal = pdt.Calendar() - self.yr, self.mth, self.dy, self.hr, self.mn, self.sec, self.wd, self.yd, self.isdst = time.localtime() - - def testPhrases(self): - # - # NOTE - this test will fail under certain conditions - # It is building an absolute date for comparison and then testing - # the parsing of relative phrases and as such will fail if run - # near the midnight transition. - # Thanks to Chris Petrilli for asking about it and prompting me - # to create this note! - # - start = datetime.datetime(self.yr, self.mth, self.dy, self.hr, self.mn, self.sec).timetuple() - target = datetime.datetime(self.yr, self.mth, self.dy, 16, 0, 0).timetuple() - - self.assertExpectedResult(self.cal.parse('flight from SFO at 4pm', start), (target, 2)) - - target = datetime.datetime(self.yr, self.mth, self.dy, 17, 0, 0).timetuple() - - self.assertExpectedResult(self.cal.parse('eod', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('meeting eod', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('eod meeting', start), (target, 2)) - - target = datetime.datetime(self.yr, self.mth, self.dy, 17, 0, 0) + datetime.timedelta(days=1) - target = target.timetuple() - - self.assertExpectedResult(self.cal.parse('tomorrow eod', start), (target, 3)) - self.assertExpectedResult(self.cal.parse('eod tomorrow', start), (target, 3)) - - def testPhraseWithDays_DOWStyle_1_False(self): - s = datetime.datetime.now() - - # find out what day we are currently on - # and determine what the next day of week is - t = s + datetime.timedelta(days=1) - start = s.timetuple() - - (yr, mth, dy, _, _, _, wd, yd, isdst) = t.timetuple() - - target = (yr, mth, dy, 17, 0, 0, wd, yd, isdst) - - d = self.wd + 1 - if d > 6: - d = 0 - - day = self.cal.ptc.Weekdays[d] - - self.assertExpectedResult(self.cal.parse('eod %s' % day, start), (target, 3)) - - # find out what day we are currently on - # and determine what the previous day of week is - t = s + datetime.timedelta(days=6) - - (yr, mth, dy, hr, mn, sec, wd, yd, isdst) = t.timetuple() - - target = (yr, mth, dy, 17, 0, 0, wd, yd, isdst) - - d = self.wd - 1 - if d < 0: - d = 6 - - day = self.cal.ptc.Weekdays[d] - - self.assertExpectedResult(self.cal.parse('eod %s' % day, start), (target, 3)) - - def testEndOfPhrases(self): - s = datetime.datetime.now() - - # find out what month we are currently on - # set the day to 1 and then go back a day - # to get the end of the current month - (yr, mth, _, hr, mn, sec, _, _, _) = s.timetuple() - - mth += 1 - if mth > 12: - mth = 1 - yr += 1 - - t = datetime.datetime(yr, mth, 1, 9, 0, 0) + datetime.timedelta(days=-1) - - start = s.timetuple() - target = t.timetuple() - - self.assertExpectedResult(self.cal.parse('eom', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('meeting eom', start), (target, 1)) - - s = datetime.datetime.now() - - (yr, mth, dy, hr, mn, sec, wd, yd, isdst) = s.timetuple() - - t = datetime.datetime(yr, 12, 31, 9, 0, 0) - - start = s.timetuple() - target = t.timetuple() - - self.assertExpectedResult(self.cal.parse('eoy', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('meeting eoy', start), (target, 1)) - - def testLastPhrases(self): - for day in (11, 12, 13, 14, 15, 16, 17): - start = datetime.datetime(2012, 11, day, 9, 0, 0) - - (yr, mth, dy, _, _, _, wd, yd, isdst) = start.timetuple() - - n = 4 - wd - if n >= 0: - n -= 7 - - target = start + datetime.timedelta(days=n) - - #print '*********', start, target, n, self.cal.parse('last friday', start.timetuple()) - - self.assertExpectedResult(self.cal.parse('last friday', start.timetuple()), (target.timetuple(), 1), dateOnly=True) diff --git a/parsedatetime/tests/TestRanges.py b/parsedatetime/tests/TestRanges.py deleted file mode 100644 index 753500b..0000000 --- a/parsedatetime/tests/TestRanges.py +++ /dev/null @@ -1,69 +0,0 @@ - -""" -Test parsing of simple date and times -""" - -import unittest, time, datetime -import parsedatetime as pdt - -class test(unittest.TestCase): - - @pdt.tests.assertEqualWithComparator - def assertExpectedResult(self, result, check, **kwargs): - return pdt.tests.compareResultByTimeTupleRangesAndFlags(result, check, **kwargs) - - def setUp(self): - self.cal = pdt.Calendar() - self.yr, self.mth, self.dy, self.hr, self.mn, self.sec, self.wd, self.yd, self.isdst = time.localtime() - - def testTimes(self): - start = datetime.datetime(self.yr, self.mth, self.dy, self.hr, self.mn, self.sec).timetuple() - - targetStart = datetime.datetime(self.yr, self.mth, self.dy, 14, 0, 0).timetuple() - targetEnd = datetime.datetime(self.yr, self.mth, self.dy, 17, 30, 0).timetuple() - - self.assertExpectedResult(self.cal.evalRanges("2 pm - 5:30 pm", start), (targetStart, targetEnd, 2)) - self.assertExpectedResult(self.cal.evalRanges("2pm - 5:30pm", start), (targetStart, targetEnd, 2)) - self.assertExpectedResult(self.cal.evalRanges("2:00:00 pm - 5:30:00 pm", start), (targetStart, targetEnd, 2)) - self.assertExpectedResult(self.cal.evalRanges("2 - 5:30pm", start), (targetStart, targetEnd, 2)) - self.assertExpectedResult(self.cal.evalRanges("14:00 - 17:30", start), (targetStart, targetEnd, 2)) - - targetStart = datetime.datetime(self.yr, self.mth, self.dy, 10, 0, 0).timetuple() - targetEnd = datetime.datetime(self.yr, self.mth, self.dy, 13, 30, 0).timetuple() - - self.assertExpectedResult(self.cal.evalRanges("10AM - 1:30PM", start), (targetStart, targetEnd, 2)) - self.assertExpectedResult(self.cal.evalRanges("10:00:00 am - 1:30:00 pm", start), (targetStart, targetEnd, 2)) - self.assertExpectedResult(self.cal.evalRanges("10:00 - 13:30", start), (targetStart, targetEnd, 2)) - - targetStart = datetime.datetime(self.yr, self.mth, self.dy, 15, 30, 0).timetuple() - targetEnd = datetime.datetime(self.yr, self.mth, self.dy, 17, 0, 0).timetuple() - - self.assertExpectedResult(self.cal.evalRanges("today 3:30-5PM", start), (targetStart, targetEnd, 2)) - - def testDates(self): - start = datetime.datetime(self.yr, self.mth, self.dy, self.hr, self.mn, self.sec).timetuple() - - targetStart = datetime.datetime(2006, 8, 29, self.hr, self.mn, self.sec).timetuple() - targetEnd = datetime.datetime(2006, 9, 2,self.hr, self.mn, self.sec).timetuple() - - self.assertExpectedResult(self.cal.evalRanges("August 29, 2006 - September 2, 2006", start), (targetStart, targetEnd, 1)) - self.assertExpectedResult(self.cal.evalRanges("August 29 - September 2, 2006", start), (targetStart, targetEnd, 1)) - - targetStart = datetime.datetime(2006, 8, 29, self.hr, self.mn, self.sec).timetuple() - targetEnd = datetime.datetime(2006, 9, 2, self.hr, self.mn, self.sec).timetuple() - - self.assertExpectedResult(self.cal.evalRanges("08/29/06 - 09/02/06", start), (targetStart, targetEnd, 1)) - - - #def testSubRanges(self): - # start = datetime.datetime(self.yr, self.mth, self.dy, self.hr, self.mn, self.sec).timetuple() - - # targetStart = datetime.datetime(2006, 8, 1, 9, 0, 0).timetuple() - # targetEnd = datetime.datetime(2006, 8, 15, 9, 0, 0).timetuple() - - # self.assertExpectedResult(self.cal.evalRanges("August 1-15, 2006", start), (targetStart, targetEnd, 1)) - - -if __name__ == "__main__": - unittest.main() - diff --git a/parsedatetime/tests/TestSimpleDateTimes.py b/parsedatetime/tests/TestSimpleDateTimes.py deleted file mode 100644 index f34f50c..0000000 --- a/parsedatetime/tests/TestSimpleDateTimes.py +++ /dev/null @@ -1,374 +0,0 @@ - -""" -Test parsing of simple date and times -""" - -import unittest, time, datetime, string -import parsedatetime as pdt - -class test(unittest.TestCase): - - @pdt.tests.assertEqualWithComparator - def assertExpectedResult(self, result, check, **kwargs): - return pdt.tests.compareResultByTimeTuplesAndFlags(result, check, **kwargs) - - def setUp(self): - self.cal = pdt.Calendar() - self.yr, self.mth, self.dy, self.hr, self.mn, self.sec, self.wd, self.yd, self.isdst = time.localtime() - - def testDays(self): - s = datetime.datetime.now() - t = s + datetime.timedelta(days=1) - - start = s.timetuple() - target = t.timetuple() - - d = self.wd + 1 - - if d > 6: - d = 0 - - day = self.cal.ptc.Weekdays[d] - - self.assertExpectedResult(self.cal.parse(day, start), (target, 1)) - - t = s + datetime.timedelta(days=6) - - target = t.timetuple() - - d = self.wd - 1 - - if d < 0: - d = 6 - - day = self.cal.ptc.Weekdays[d] - - self.assertExpectedResult(self.cal.parse(day, start), (target, 1)) - - def testTimes(self): - start = datetime.datetime(self.yr, self.mth, self.dy, self.hr, self.mn, self.sec).timetuple() - target = datetime.datetime(self.yr, self.mth, self.dy, 23, 0, 0).timetuple() - - self.assertExpectedResult(self.cal.parse('11:00:00 PM', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('11:00 PM', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('11 PM', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('11PM', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('2300', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('23:00', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('11p', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('11pm', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('11:00:00 P.M.', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('11:00 P.M.', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('11 P.M.', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('11P.M.', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('11p.m.', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('11 p.m.', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('"11 p.m."', start), (target, 2)) - - target = datetime.datetime(self.yr, self.mth, self.dy, 11, 0, 0).timetuple() - - self.assertExpectedResult(self.cal.parse('11:00:00 AM', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('11:00 AM', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('11 AM', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('11AM', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('1100', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('11:00', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('11a', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('11am', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('11:00:00 A.M.', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('11:00 A.M.', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('11 A.M.', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('11A.M.', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('11a.m.', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('11 a.m.', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('(11 a.m.)', start), (target, 2)) - - target = datetime.datetime(self.yr, self.mth, self.dy, 7, 30, 0).timetuple() - - self.assertExpectedResult(self.cal.parse('730', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('0730', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('0730am', start), (target, 2)) - - target = datetime.datetime(self.yr, self.mth, self.dy, 17, 30, 0).timetuple() - - self.assertExpectedResult(self.cal.parse('1730', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('173000', start), (target, 2)) - - # Should not parse as a time due to prefix - self.assertExpectedResult(self.cal.parse('$300', start), (start, 0)) - self.assertExpectedResult(self.cal.parse('300ml', start), (start, 0)) - - def testDates(self): - start = datetime.datetime(self.yr, self.mth, self.dy, self.hr, self.mn, self.sec).timetuple() - target = datetime.datetime(2006, 8, 25, self.hr, self.mn, self.sec).timetuple() - - self.assertExpectedResult(self.cal.parse('08/25/2006', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('08.25.2006', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('2006/08/25', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('2006/8/25', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('2006-08-25', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('8/25/06', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('August 25, 2006', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('Aug 25, 2006', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('Aug. 25, 2006', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('August 25 2006', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('Aug 25 2006', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('Aug. 25 2006', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('25 August 2006', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('25 Aug 2006', start), (target, 1)) - - if self.mth > 8 or (self.mth == 8 and self.dy > 25): - target = datetime.datetime(self.yr + 1, 8, 25, self.hr, self.mn, self.sec).timetuple() - else: - target = datetime.datetime(self.yr, 8, 25, self.hr, self.mn, self.sec).timetuple() - - self.assertExpectedResult(self.cal.parse('8/25', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('8.25', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('08/25', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('August 25', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('Aug 25', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('Aug. 25', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('"8.25"', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('(8.25)', start), (target, 1)) - - # Should not parse as dates - self.assertExpectedResult(self.cal.parse('$1.23', start), (start, 0)) - self.assertExpectedResult(self.cal.parse('$12.34', start), (start, 0)) - - # added test to ensure 4-digit year is recognized in the absence of day - target = datetime.datetime(2013, 8, 1, self.hr, self.mn, self.sec).timetuple() - self.assertExpectedResult(self.cal.parse('Aug. 2013', start), (target, 1)) - - def testLeapDays(self): - start = datetime.datetime(self.yr, self.mth, self.dy, self.hr, self.mn, self.sec).timetuple() - target = datetime.datetime(2000, 2, 29, self.hr, self.mn, self.sec).timetuple() - - self.assertExpectedResult(self.cal.parse('02/29/2000', start), (target, 1)) - - target = datetime.datetime(2004, 2, 29, self.hr, self.mn, self.sec).timetuple() - - self.assertExpectedResult(self.cal.parse('02/29/2004', start), (target, 1)) - - target = datetime.datetime(2008, 2, 29, self.hr, self.mn, self.sec).timetuple() - - self.assertExpectedResult(self.cal.parse('02/29/2008', start), (target, 1)) - - target = datetime.datetime(2012, 2, 29, self.hr, self.mn, self.sec).timetuple() - - self.assertExpectedResult(self.cal.parse('02/29/2012', start), (target, 1)) - - dNormal = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31) - dLeap = (31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31) - - for i in range(1,12): - self.assertTrue(self.cal.ptc.daysInMonth(i, 1999), dNormal[i - 1]) - self.assertTrue(self.cal.ptc.daysInMonth(i, 2000), dLeap[i - 1]) - self.assertTrue(self.cal.ptc.daysInMonth(i, 2001), dNormal[i - 1]) - self.assertTrue(self.cal.ptc.daysInMonth(i, 2002), dNormal[i - 1]) - self.assertTrue(self.cal.ptc.daysInMonth(i, 2003), dNormal[i - 1]) - self.assertTrue(self.cal.ptc.daysInMonth(i, 2004), dLeap[i - 1]) - self.assertTrue(self.cal.ptc.daysInMonth(i, 2005), dNormal[i - 1]) - - def testDaySuffixes(self): - start = datetime.datetime(self.yr, self.mth, self.dy, self.hr, self.mn, self.sec).timetuple() - target = datetime.datetime(2008, 8, 22, self.hr, self.mn, self.sec).timetuple() - - self.assertExpectedResult(self.cal.parse('August 22nd, 2008', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('Aug 22nd, 2008', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('Aug. 22nd, 2008', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('August 22nd 2008', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('Aug 22nd 2008', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('Aug. 22nd 2008', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('22nd August 2008', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('22nd Aug 2008', start), (target, 1)) - - target = datetime.datetime(1949, 12, 31, self.hr, self.mn, self.sec).timetuple() - - self.assertExpectedResult(self.cal.parse('December 31st, 1949', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('Dec 31st, 1949', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('December 31st 1949', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('Dec 31st 1949', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('31st December 1949', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('31st Dec 1949', start), (target, 1)) - - target = datetime.datetime(2008, 8, 23, self.hr, self.mn, self.sec).timetuple() - - self.assertExpectedResult(self.cal.parse('August 23rd, 2008', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('Aug 23rd, 2008', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('Aug. 23rd, 2008', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('August 23rd 2008', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('Aug 23rd 2008', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('Aug. 23rd 2008', start), (target, 1)) - - target = datetime.datetime(2008, 8, 25, self.hr, self.mn, self.sec).timetuple() - - self.assertExpectedResult(self.cal.parse('August 25th, 2008', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('Aug 25th, 2008', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('Aug. 25th, 2008', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('August 25th 2008', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('Aug 25th 2008', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('Aug. 25th 2008', start), (target, 1)) - - def testSpecialTimes(self): - start = datetime.datetime(self.yr, self.mth, self.dy, self.hr, self.mn, self.sec).timetuple() - target = datetime.datetime(self.yr, self.mth, self.dy, 6, 0, 0).timetuple() - - self.assertExpectedResult(self.cal.parse('morning', start), (target, 2)) - - target = datetime.datetime(self.yr, self.mth, self.dy, 8, 0, 0).timetuple() - - self.assertExpectedResult(self.cal.parse('breakfast', start), (target, 2)) - - target = datetime.datetime(self.yr, self.mth, self.dy, 12, 0, 0).timetuple() - - self.assertExpectedResult(self.cal.parse('lunch', start), (target, 2)) - - target = datetime.datetime(self.yr, self.mth, self.dy, 13, 0, 0).timetuple() - - self.assertExpectedResult(self.cal.parse('afternoon', start), (target, 2)) - - target = datetime.datetime(self.yr, self.mth, self.dy, 18, 0, 0).timetuple() - - self.assertExpectedResult(self.cal.parse('evening', start), (target, 2)) - - target = datetime.datetime(self.yr, self.mth, self.dy, 19,0, 0).timetuple() - - self.assertExpectedResult(self.cal.parse('dinner', start), (target, 2)) - - target = datetime.datetime(self.yr, self.mth, self.dy, 21, 0, 0).timetuple() - - self.assertExpectedResult(self.cal.parse('night', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('tonight', start), (target, 2)) - - def testMidnight(self): - start = datetime.datetime(self.yr, self.mth, self.dy, self.hr, self.mn, self.sec).timetuple() - target = datetime.datetime(self.yr, self.mth, self.dy, 0, 0, 0).timetuple() - - self.assertExpectedResult(self.cal.parse('midnight', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('12:00:00 AM', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('12:00 AM', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('12 AM', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('12AM', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('12am', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('12a', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('0000', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('00:00', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('12:00:00 A.M.', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('12:00 A.M.', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('12 A.M.', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('12A.M.', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('12a.m.', start), (target, 2)) - - def testNoon(self): - start = datetime.datetime(self.yr, self.mth, self.dy, self.hr, self.mn, self.sec).timetuple() - target = datetime.datetime(self.yr, self.mth, self.dy, 12, 0, 0).timetuple() - - self.assertExpectedResult(self.cal.parse('noon', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('12:00:00 PM', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('12:00 PM', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('12 PM', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('12PM', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('12pm', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('12p', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('1200', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('12:00', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('12:00:00 P.M.', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('12:00 P.M.', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('12 P.M.', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('12P.M.', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('12p.m.', start), (target, 2)) - - - def testDaysOfWeek(self): - start = datetime.datetime(2014, 10, 25, self.hr, self.mn, self.sec).timetuple() - - target = datetime.datetime(2014, 10, 26, self.hr, self.mn, self.sec).timetuple() - self.assertExpectedResult(self.cal.parse('sunday', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('sun', start), (target, 1)) - - target = datetime.datetime(2014, 10, 27, self.hr, self.mn, self.sec).timetuple() - self.assertExpectedResult(self.cal.parse('Monday', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('mon', start), (target, 1)) - - target = datetime.datetime(2014, 10, 28, self.hr, self.mn, self.sec).timetuple() - self.assertExpectedResult(self.cal.parse('tuesday', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('tues', start), (target, 1)) - - target = datetime.datetime(2014, 10, 29, self.hr, self.mn, self.sec).timetuple() - self.assertExpectedResult(self.cal.parse('wednesday', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('wed', start), (target, 1)) - - target = datetime.datetime(2014, 10, 30, self.hr, self.mn, self.sec).timetuple() - self.assertExpectedResult(self.cal.parse('thursday', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('thu', start), (target, 1)) - - target = datetime.datetime(2014, 10, 31, self.hr, self.mn, self.sec).timetuple() - self.assertExpectedResult(self.cal.parse('friday', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('fri', start), (target, 1)) - - target = datetime.datetime(2014, 11, 1, self.hr, self.mn, self.sec).timetuple() - self.assertExpectedResult(self.cal.parse('saturday', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('sat', start), (target, 1)) - - def testWordBoundaries(self): - # Ensure that keywords appearing at the start of a word are not parsed - # as if they were standalone keywords. For example, "10 dogs" should - # not be interpreted the same as "10 d" - start = datetime.datetime(self.yr, self.mth, self.dy, self.hr, self.mn, self.sec).timetuple() - target = datetime.datetime.now().timetuple() - - keywords = [] - loc = self.cal.ptc.locale - - # Test all known keywords for the locale - keywords.extend(loc.meridian) - keywords.extend(loc.Weekdays) - keywords.extend(loc.shortWeekdays) - keywords.extend(loc.Months) - keywords.extend(loc.shortMonths) - keywords.extend(loc.numbers.keys()) - keywords.extend(loc.Modifiers.keys()) - keywords.extend(loc.dayOffsets.keys()) - keywords.extend(loc.re_sources.keys()) - keywords.extend(loc.small.keys()) - keywords.extend(loc.magnitude.keys()) - - for units in loc.units.values(): - keywords.extend(units) - - # Finally, test all lowercase letters to be particularly thorough - it - # would be very difficult to track down bugs due to single letters. - keywords.extend(list(string.ascii_lowercase)) - - for keyword in keywords: - phrase = '1 %sfoo' % keyword - self.assertExpectedResult(self.cal.parse(phrase, start), (target, 0)) - - def testYearParseStyle(self): - config = pdt.Constants() - config.YearParseStyle = 0 - calendar = pdt.Calendar(config) - start = datetime.datetime(self.yr, self.mth, self.dy, - self.hr, self.mn, self.sec).timetuple() - target = datetime.datetime(self.yr, 7, 28, - self.hr, self.mn, self.sec).timetuple() - self.assertExpectedResult(calendar.parse('7/28', start), (target, 1)) - - # def testMonths(self): - # - # start = datetime.datetime(self.yr, self.mth, self.dy, self.hr, self.mn, self.sec).timetuple() - # - # target = datetime.datetime(self.yr, self.mth, self.dy, 12, 0, 0).timetuple() - # - # self.assertExpectedResult(self.cal.parse('jun', start), (target, 2)) - # self.assertExpectedResult(self.cal.parse('12:00:00 PM', start), (target, 2)) - # self.assertExpectedResult(self.cal.parse('12:00 PM', start), (target, 2)) - # self.assertExpectedResult(self.cal.parse('12 PM', start), (target, 2)) - # self.assertExpectedResult(self.cal.parse('12PM', start), (target, 2)) - # self.assertExpectedResult(self.cal.parse('12pm', start), (target, 2)) - # self.assertExpectedResult(self.cal.parse('12p', start), (target, 2)) - # self.assertExpectedResult(self.cal.parse('1200', start), (target, 2)) - # self.assertExpectedResult(self.cal.parse('12:00', start), (target, 2)) - -if __name__ == "__main__": - unittest.main() diff --git a/parsedatetime/tests/TestSimpleOffsets.py b/parsedatetime/tests/TestSimpleOffsets.py deleted file mode 100644 index b6e8b43..0000000 --- a/parsedatetime/tests/TestSimpleOffsets.py +++ /dev/null @@ -1,175 +0,0 @@ - -""" -Test parsing of 'simple' offsets -""" - -import time -import datetime -import calendar -import unittest -import parsedatetime as pdt - -def _truncateResult(result, trunc_seconds=True, trunc_hours=False): - try: - dt, flag = result - except ValueError: - # wtf?! - return result - if trunc_seconds: - dt = dt[:5] + (0,) * 4 - if trunc_hours: - dt = dt[:3] + (0,) * 6 - return dt, flag - -_tr = _truncateResult - -class test(unittest.TestCase): - - @pdt.tests.assertEqualWithComparator - def assertExpectedResult(self, result, check, **kwargs): - return pdt.tests.compareResultByTimeTuplesAndFlags(result, check, **kwargs) - - def setUp(self): - self.cal = pdt.Calendar() - self.yr, self.mth, self.dy, self.hr, self.mn, self.sec, self.wd, self.yd, self.isdst = time.localtime() - - def testNow(self): - s = datetime.datetime.now() - - start = s.timetuple() - target = s.timetuple() - - self.assertExpectedResult(self.cal.parse('now', start), (target, 2)) - - def testMinutesFromNow(self): - s = datetime.datetime.now() - t = s + datetime.timedelta(minutes=5) - - start = s.timetuple() - target = t.timetuple() - - self.assertExpectedResult(self.cal.parse('5 minutes from now', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('5 min from now', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('5m from now', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('in 5 minutes', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('in 5 min', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('5 minutes', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('5 min', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('5m', start), (target, 2)) - - self.assertExpectedResult(self.cal.parse('five minutes from now', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('five min from now', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('in five minutes', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('in five min', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('five minutes', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('five min', start), (target, 2)) - - def testMinutesBeforeNow(self): - s = datetime.datetime.now() - t = s + datetime.timedelta(minutes=-5) - - start = s.timetuple() - target = t.timetuple() - - self.assertExpectedResult(self.cal.parse('5 minutes before now', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('5 min before now', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('5m before now', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('5 minutes ago', start), (target, 2)) - - self.assertExpectedResult(self.cal.parse('five minutes before now', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('five min before now', start), (target, 2)) - - def testWeekFromNow(self): - s = datetime.datetime.now() - t = s + datetime.timedelta(weeks=1) - - start = s.timetuple() - target = t.timetuple() - - self.assertExpectedResult(self.cal.parse('in 1 week', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('1 week from now', start), (target, 3)) - self.assertExpectedResult(self.cal.parse('in one week', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('one week from now', start), (target, 3)) - self.assertExpectedResult(self.cal.parse('in a week', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('a week from now', start), (target, 3)) - self.assertExpectedResult(self.cal.parse('in 7 days', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('7 days from now', start), (target, 3)) - self.assertExpectedResult(self.cal.parse('in seven days', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('seven days from now', start), (target, 3)) - self.assertEqual(_tr(self.cal.parse('next week', start), - trunc_hours=True), - _tr((target, 1), trunc_hours=True)) - - def testNextWeekDay(self): - start = datetime.datetime.now() - target = start + datetime.timedelta(days=4 + 7 - start.weekday()) - start = start.timetuple() - target = target.timetuple() - - self.assertExpectedResult(self.cal.parse('next friday', start), - (target, 1), dateOnly=True) - self.assertExpectedResult(self.cal.parse('next friday?', start), - (target, 1), dateOnly=True) - self.cal.ptc.StartTimeFromSourceTime = True - self.assertExpectedResult(self.cal.parse('next friday', start), - (target, 1)) - - def testWeekBeforeNow(self): - s = datetime.datetime.now() - t = s + datetime.timedelta(weeks=-1) - - start = s.timetuple() - target = t.timetuple() - - self.assertEqual(_tr(self.cal.parse('1 week before now', start)), - _tr((target, 3))) - self.assertEqual(_tr(self.cal.parse('one week before now', start)), - _tr((target, 3))) - self.assertEqual(_tr(self.cal.parse('a week before now', start)), - _tr((target, 3))) - self.assertEqual(_tr(self.cal.parse('7 days before now', start)), - _tr((target, 3))) - self.assertEqual(_tr(self.cal.parse('seven days before now', start)), - _tr((target, 3))) - self.assertEqual(_tr(self.cal.parse('1 week ago', start)), - _tr((target, 1))) - self.assertEqual(_tr(self.cal.parse('a week ago', start)), - _tr((target, 1))) - self.assertEqual(_tr(self.cal.parse('last week', start), trunc_hours=True), - _tr((target, 1), trunc_hours=True)) - - def testNextMonth(self): - s = datetime.datetime(self.yr, self.mth, self.dy, self.hr, self.mn, self.sec) + datetime.timedelta(days=1) - t = self.cal.inc(s, year=1) - - start = s.timetuple() - target = t.timetuple() - - phrase = 'next %s %s' % (calendar.month_name[t.month], t.day) - - self.assertEqual(_tr(self.cal.parse(phrase, start)), - _tr((target, 1))) - - def testSpecials(self): - s = datetime.datetime.now() - t = datetime.datetime(self.yr, self.mth, self.dy, 9, 0, 0) + datetime.timedelta(days=1) - - start = s.timetuple() - target = t.timetuple() - - self.assertExpectedResult(self.cal.parse('tomorrow', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('next day', start), (target, 1)) - - t = datetime.datetime(self.yr, self.mth, self.dy, 9, 0, 0) + datetime.timedelta(days=-1) - target = t.timetuple() - - self.assertExpectedResult(self.cal.parse('yesterday', start), (target, 1)) - - t = datetime.datetime(self.yr, self.mth, self.dy, 9, 0, 0) - target = t.timetuple() - - self.assertExpectedResult(self.cal.parse('today', start), (target, 1)) - - -if __name__ == "__main__": - unittest.main() diff --git a/parsedatetime/tests/TestSimpleOffsetsHours.py b/parsedatetime/tests/TestSimpleOffsetsHours.py deleted file mode 100644 index 45f8c28..0000000 --- a/parsedatetime/tests/TestSimpleOffsetsHours.py +++ /dev/null @@ -1,80 +0,0 @@ -""" -Test parsing of 'simple' offsets -""" - -import unittest, time, datetime -import parsedatetime as pdt - -class test(unittest.TestCase): - - @pdt.tests.assertEqualWithComparator - def assertExpectedResult(self, result, check, **kwargs): - return pdt.tests.compareResultByTimeTuplesAndFlags(result, check, **kwargs) - - def setUp(self): - self.cal = pdt.Calendar() - self.yr, self.mth, self.dy, self.hr, self.mn, self.sec, self.wd, self.yd, self.isdst = time.localtime() - - def testHoursFromNow(self): - s = datetime.datetime.now() - t = s + datetime.timedelta(hours=5) - - start = s.timetuple() - target = t.timetuple() - - self.assertExpectedResult(self.cal.parse('5 hours from now', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('5 hour from now', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('5 hr from now', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('in 5 hours', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('in 5 hour', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('5 hours', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('5 hr', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('5h', start), (target, 2)) - - self.assertExpectedResult(self.cal.parse('five hours from now', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('five hour from now', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('five hr from now', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('in five hours', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('in five hour', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('five hours', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('five hr', start), (target, 2)) - - # Test "an" - t = s + datetime.timedelta(hours=1) - target = t.timetuple() - - self.assertExpectedResult(self.cal.parse('an hour from now', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('in an hour', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('an hour', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('an hr', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('an h', start), (target, 2)) - - # No match, should require a word boundary - self.assertExpectedResult(self.cal.parse('anhour', start), (start, 0)) - self.assertExpectedResult(self.cal.parse('an hamburger', start), (start, 0)) - - def testHoursBeforeNow(self): - s = datetime.datetime.now() - t = s + datetime.timedelta(hours=-5) - - start = s.timetuple() - target = t.timetuple() - - self.assertExpectedResult(self.cal.parse('5 hours before now', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('5 hr before now', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('5h before now', start), (target, 2)) - - self.assertExpectedResult(self.cal.parse('five hours before now', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('five hr before now', start), (target, 2)) - - # Test "an" - t = s + datetime.timedelta(hours=-1) - target = t.timetuple() - - self.assertExpectedResult(self.cal.parse('an hour before now', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('an hr before now', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('an h before now', start), (target, 2)) - - -if __name__ == "__main__": - unittest.main() diff --git a/parsedatetime/tests/TestSimpleOffsetsNoon.py b/parsedatetime/tests/TestSimpleOffsetsNoon.py deleted file mode 100644 index 2f9a702..0000000 --- a/parsedatetime/tests/TestSimpleOffsetsNoon.py +++ /dev/null @@ -1,50 +0,0 @@ - -""" -Test parsing of 'simple' offsets -""" - -import unittest, time, datetime -import parsedatetime as pdt - -class test(unittest.TestCase): - - @pdt.tests.assertEqualWithComparator - def assertExpectedResult(self, result, check, **kwargs): - return pdt.tests.compareResultByTimeTuplesAndFlags(result, check, **kwargs) - - def setUp(self): - self.cal = pdt.Calendar() - self.yr, self.mth, self.dy, self.hr, self.mn, self.sec, self.wd, self.yd, self.isdst = time.localtime() - - def testOffsetAfterNoon(self): - s = datetime.datetime(self.yr, self.mth, self.dy, 10, 0, 0) - t = datetime.datetime(self.yr, self.mth, self.dy, 12, 0, 0) + datetime.timedelta(hours=5) - - start = s.timetuple() - target = t.timetuple() - - self.assertExpectedResult(self.cal.parse('5 hours after 12pm', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('five hours after 12pm', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('5 hours after 12 pm', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('5 hours after 12:00pm', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('5 hours after 12:00 pm', start), (target, 2)) - #self.assertExpectedResult(self.cal.parse('5 hours after noon', start), (target, 2)) - #self.assertExpectedResult(self.cal.parse('5 hours from noon', start), (target, 2)) - - def testOffsetBeforeNoon(self): - s = datetime.datetime.now() - t = datetime.datetime(self.yr, self.mth, self.dy, 12, 0, 0) + datetime.timedelta(hours=-5) - - start = s.timetuple() - target = t.timetuple() - - #self.assertExpectedResult(self.cal.parse('5 hours before noon', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('5 hours before 12pm', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('five hours before 12pm', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('5 hours before 12 pm', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('5 hours before 12:00pm', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('5 hours before 12:00 pm', start), (target, 2)) - - -if __name__ == "__main__": - unittest.main() diff --git a/parsedatetime/tests/TestStartTimeFromSourceTime.py b/parsedatetime/tests/TestStartTimeFromSourceTime.py deleted file mode 100644 index 135ab21..0000000 --- a/parsedatetime/tests/TestStartTimeFromSourceTime.py +++ /dev/null @@ -1,58 +0,0 @@ - -""" -Test parsing of strings that are phrases with the -ptc.StartTimeFromSourceTime flag set to True -""" - -import time -import datetime -import unittest -import parsedatetime as pdt - - -class test(unittest.TestCase): - - @pdt.tests.assertEqualWithComparator - def assertExpectedResult(self, result, check, **kwargs): - return pdt.tests.compareResultByTimeTuplesAndFlags(result, check, **kwargs) - - def setUp(self): - self.cal = pdt.Calendar() - self.cal.ptc.StartTimeFromSourceTime = True - self.yr, self.mth, self.dy, self.hr, self.mn, self.sec, self.wd, self.yd, self.isdst = time.localtime() - - def testEndOfPhrases(self): - s = datetime.datetime.now() - - # find out what month we are currently on - # set the day to 1 and then go back a day - # to get the end of the current month - (yr, mth, dy, hr, mn, sec, _, _, _) = s.timetuple() - - m = mth - mth += 1 - if mth > 12: - mth = 1 - yr += 1 - - s = datetime.datetime(yr, m, dy, 13, 14, 15) - t = datetime.datetime(yr, mth, 1, 13, 14, 15) + datetime.timedelta(days=-1) - - start = s.timetuple() - target = t.timetuple() - - self.assertExpectedResult(self.cal.parse('eom', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('meeting eom', start), (target, 1)) - - s = datetime.datetime.now() - - (yr, mth, dy, hr, mn, sec, wd, yd, isdst) = s.timetuple() - - s = datetime.datetime(yr, mth, 1, 13, 14, 15) - t = datetime.datetime(yr, 12, 31, 13, 14, 15) - - start = s.timetuple() - target = t.timetuple() - - self.assertExpectedResult(self.cal.parse('eoy', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('meeting eoy', start), (target, 1)) diff --git a/parsedatetime/tests/TestUnits.py b/parsedatetime/tests/TestUnits.py deleted file mode 100644 index aaf9c5c..0000000 --- a/parsedatetime/tests/TestUnits.py +++ /dev/null @@ -1,139 +0,0 @@ - -""" -Test parsing of units -""" - -import unittest, time, datetime -import parsedatetime as pdt - -class test(unittest.TestCase): - - @pdt.tests.assertEqualWithComparator - def assertExpectedResult(self, result, check, **kwargs): - return pdt.tests.compareResultByTimeTuplesAndFlags(result, check, **kwargs) - - def setUp(self): - self.cal = pdt.Calendar() - self.yr, self.mth, self.dy, self.hr, self.mn, self.sec, self.wd, self.yd, self.isdst = time.localtime() - - def testMinutes(self): - s = datetime.datetime.now() - t = s + datetime.timedelta(minutes=1) - h = s - datetime.timedelta(minutes=1) - - start = s.timetuple() - target = t.timetuple() - history = h.timetuple() - - self.assertExpectedResult(self.cal.parse('1 minutes', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('1 minute', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('1 min', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('1min', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('1 m', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('1m', start), (target, 2)) - - self.assertExpectedResult(self.cal.parse('1 minutes ago', start), (history, 2)) - self.assertExpectedResult(self.cal.parse('1 minute ago', start), (history, 2)) - - def testHours(self): - s = datetime.datetime.now() - t = s + datetime.timedelta(hours=1) - h = s - datetime.timedelta(hours=1) - - start = s.timetuple() - target = t.timetuple() - history = h.timetuple() - - self.assertExpectedResult(self.cal.parse('1 hour', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('1 hours', start), (target, 2)) - self.assertExpectedResult(self.cal.parse('1 hr', start), (target, 2)) - - self.assertExpectedResult(self.cal.parse('1 hour ago', start), (history, 2)) - self.assertExpectedResult(self.cal.parse('1 hours ago', start), (history, 2)) - - def testDays(self): - s = datetime.datetime.now() - t = s + datetime.timedelta(days=1) - - start = s.timetuple() - target = t.timetuple() - - self.assertExpectedResult(self.cal.parse('1 day', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('1 days', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('1days', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('1 dy', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('1 d', start), (target, 1)) - - def testNegativeDays(self): - s = datetime.datetime.now() - t = s + datetime.timedelta(days=-1) - - start = s.timetuple() - target = t.timetuple() - - self.assertExpectedResult(self.cal.parse('-1 day', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('-1 days', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('-1days', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('-1 dy', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('-1 d', start), (target, 1)) - - self.assertExpectedResult(self.cal.parse('- 1 day', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('- 1 days', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('- 1days', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('- 1 dy', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('- 1 d', start), (target, 1)) - - self.assertExpectedResult(self.cal.parse('1 day ago', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('1 days ago', start), (target, 1)) - - - def testWeeks(self): - s = datetime.datetime.now() - t = s + datetime.timedelta(weeks=1) - h = s - datetime.timedelta(weeks=1) - - start = s.timetuple() - target = t.timetuple() - history = h.timetuple() - - self.assertExpectedResult(self.cal.parse('1 week', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('1week', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('1 weeks', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('1 wk', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('1 w', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('1w', start), (target, 1)) - - self.assertExpectedResult(self.cal.parse('1 week ago', start), (history, 1)) - self.assertExpectedResult(self.cal.parse('1 weeks ago', start), (history, 1)) - - def testMonths(self): - s = datetime.datetime.now() - t = self.cal.inc(s, month=1) - h = self.cal.inc(s, month=-1) - - start = s.timetuple() - target = t.timetuple() - history = h.timetuple() - - self.assertExpectedResult(self.cal.parse('1 month', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('1 months', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('1month', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('1 month ago', start), (history, 1)) - self.assertExpectedResult(self.cal.parse('1 months ago', start), (history, 1)) - - def testYears(self): - s = datetime.datetime.now() - t = self.cal.inc(s, year=1) - - start = s.timetuple() - target = t.timetuple() - - self.assertExpectedResult(self.cal.parse('1 year', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('1 years', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('1 yr', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('1 y', start), (target, 1)) - self.assertExpectedResult(self.cal.parse('1y', start), (target, 1)) - - -if __name__ == "__main__": - unittest.main() diff --git a/run_tests.py b/run_tests.py deleted file mode 100644 index 2ac6e43..0000000 --- a/run_tests.py +++ /dev/null @@ -1,47 +0,0 @@ -import sys, os - -from unittest import TestLoader, main - -class ScanningLoader(TestLoader): - - def loadTestsFromModule(self, module): - """ - Return a suite of all tests cases contained in the given module - """ - tests = [TestLoader.loadTestsFromModule(self,module)] - - if hasattr(module, "additional_tests"): - tests.append(module.additional_tests()) - - if hasattr(module, '__path__'): - for directory in module.__path__: - for file in os.listdir(directory): - if file.endswith('.py') and file != '__init__.py': - if file.lower().startswith('test'): - submodule = module.__name__+'.'+file[:-3] - else: - continue - else: - subpkg = os.path.join(directory, file, '__init__.py') - - if os.path.exists(subpkg): - submodule = module.__name__ + '.' + file - else: - continue - - tests.append(self.loadTestsFromName(submodule)) - - if len(tests) > 1: - return self.suiteClass(tests) - else: - return tests[0] # don't create a nested suite for only one return - - -if __name__ == '__main__': - if len(sys.argv) == 1: - testname = 'parsedatetime' - else: - testname = None - - main(module=testname, testLoader=ScanningLoader()) - diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 0000000..b5921b5 --- /dev/null +++ b/setup.cfg @@ -0,0 +1,12 @@ +[nosetests] +where = tests +verbosity = 2 + +with-coverage = true +cover-min-percentage = 72 +cover-package = parsedatetime +cover-html = true +cover-html-dir = htmlcov +cover-erase = true +cover-inclusive = true +cover-branches = true diff --git a/setup.py b/setup.py index 64974a7..977c2b7 100644 --- a/setup.py +++ b/setup.py @@ -3,31 +3,35 @@ from setuptools import setup, find_packages from parsedatetime import __version__ + def read(*paths): """Build a file path from *paths* and return the contents.""" with open(os.path.join(*paths), 'r') as f: return f.read() -setup(name='parsedatetime', - version=__version__, - author='Mike Taylor', - author_email='bear@bear.im', - url='http://github.com/bear/parsedatetime/', - download_url='https://pypi.python.org/pypi/parsedatetime/', - description='Parse human-readable date/time text.', - license='Apache License 2.0', - packages=find_packages(exclude=['tests*']), - platforms=['Any'], - long_description=(read('README.rst')), - classifiers=['Development Status :: 5 - Production/Stable', - 'Intended Audience :: Developers', - 'License :: OSI Approved :: Apache Software License', - 'Operating System :: OS Independent', - 'Topic :: Text Processing', - 'Topic :: Software Development :: Libraries :: Python Modules', - 'Programming Language :: Python :: 2.7', - 'Programming Language :: Python :: 3', - 'Programming Language :: Python :: 3.3', - 'Programming Language :: Python :: 3.4', - ] - ) +setup( + name='parsedatetime', + version=__version__, + author='Mike Taylor', + author_email='bear@bear.im', + url='http://github.com/bear/parsedatetime/', + download_url='https://pypi.python.org/pypi/parsedatetime/', + description='Parse human-readable date/time text.', + license='Apache License 2.0', + packages=find_packages(exclude=['tests*']), + platforms=['Any'], + long_description=(read('README.rst')), + test_suite='nose.collector', + classifiers=[ + 'Development Status :: 5 - Production/Stable', + 'Intended Audience :: Developers', + 'License :: OSI Approved :: Apache Software License', + 'Operating System :: OS Independent', + 'Topic :: Text Processing', + 'Topic :: Software Development :: Libraries :: Python Modules', + 'Programming Language :: Python :: 2.7', + 'Programming Language :: Python :: 3', + 'Programming Language :: Python :: 3.3', + 'Programming Language :: Python :: 3.4', + ] +) diff --git a/tests/TestAlternativeAbbreviations.py b/tests/TestAlternativeAbbreviations.py new file mode 100644 index 0000000..e720a38 --- /dev/null +++ b/tests/TestAlternativeAbbreviations.py @@ -0,0 +1,135 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals +import unittest +import time +import datetime +import parsedatetime as pdt + +from . import utils + + +class pdtLocale_en(pdt.pdt_locales.pdtLocale_icu): + + """Update en locale to include a bunch of different abbreviations""" + + def __init__(self): + super(pdtLocale_en, self).__init__(localeID='en_us') + + self.Weekdays = [ + 'monday', 'tuesday', 'wednesday', + 'thursday', 'friday', 'saturday', 'sunday'] + + self.shortWeekdays = [ + 'mon|mond', 'tue|tues', 'wed|wedn', + 'thu|thur|thurs', 'fri|frid', 'sat|sa', 'sun|su'] + + self.Months = [ + 'january', 'february', 'march', + 'april', 'may', 'june', + 'july', 'august', 'september', + 'october', 'november', 'december'] + + self.shortMonths = [ + 'jan|janu', 'feb|febr', 'mar|marc', + 'apr|apri', 'may', 'jun|june', + 'jul', 'aug|augu', 'sep|sept', + 'oct|octo', 'nov|novem', 'dec|decem'] + + +class test(unittest.TestCase): + + @utils.assertEqualWithComparator + def assertExpectedResult(self, result, check, **kwargs): + return utils.compareResultByTimeTuplesAndFlags(result, check, **kwargs) + + def setUp(self): + pdt.pdtLocales['en_us'] = pdtLocale_en # override for the test + self.ptc = pdt.Constants('en_us', usePyICU=False) + self.cal = pdt.Calendar(self.ptc) + (self.yr, self.mth, self.dy, self.hr, + self.mn, self.sec, self.wd, self.yd, self.isdst) = time.localtime() + + def testDaysOfWeek(self): + start = datetime.datetime( + 2014, 10, 25, self.hr, self.mn, self.sec).timetuple() + + target = datetime.datetime( + 2014, 10, 26, self.hr, self.mn, self.sec).timetuple() + self.assertExpectedResult(self.cal.parse('sunday', start), (target, 1)) + self.assertExpectedResult(self.cal.parse('sun', start), (target, 1)) + self.assertExpectedResult(self.cal.parse('su', start), (target, 1)) + + target = datetime.datetime( + 2014, 10, 27, self.hr, self.mn, self.sec).timetuple() + self.assertExpectedResult(self.cal.parse('Monday', start), (target, 1)) + self.assertExpectedResult(self.cal.parse('mon', start), (target, 1)) + self.assertExpectedResult(self.cal.parse('mond', start), (target, 1)) + + target = datetime.datetime( + 2014, 10, 28, self.hr, self.mn, self.sec).timetuple() + self.assertExpectedResult( + self.cal.parse('tuesday', start), (target, 1)) + self.assertExpectedResult(self.cal.parse('tues', start), (target, 1)) + self.assertExpectedResult(self.cal.parse('tue', start), (target, 1)) + + target = datetime.datetime( + 2014, 10, 29, self.hr, self.mn, self.sec).timetuple() + self.assertExpectedResult( + self.cal.parse('wednesday', start), (target, 1)) + self.assertExpectedResult(self.cal.parse('wedn', start), (target, 1)) + self.assertExpectedResult(self.cal.parse('wed', start), (target, 1)) + + target = datetime.datetime( + 2014, 10, 30, self.hr, self.mn, self.sec).timetuple() + self.assertExpectedResult( + self.cal.parse('thursday', start), (target, 1)) + self.assertExpectedResult(self.cal.parse('thu', start), (target, 1)) + self.assertExpectedResult(self.cal.parse('thur', start), (target, 1)) + self.assertExpectedResult(self.cal.parse('thurs', start), (target, 1)) + + target = datetime.datetime( + 2014, 10, 31, self.hr, self.mn, self.sec).timetuple() + self.assertExpectedResult(self.cal.parse('friday', start), (target, 1)) + self.assertExpectedResult(self.cal.parse('fri', start), (target, 1)) + self.assertExpectedResult(self.cal.parse('frid', start), (target, 1)) + + target = datetime.datetime( + 2014, 11, 1, self.hr, self.mn, self.sec).timetuple() + self.assertExpectedResult( + self.cal.parse('saturday', start), (target, 1)) + self.assertExpectedResult(self.cal.parse('sat', start), (target, 1)) + self.assertExpectedResult(self.cal.parse('sa', start), (target, 1)) + + def testMonths(self): + start = datetime.datetime( + 2014, 1, 1, self.hr, self.mn, self.sec).timetuple() + for dates, expected_date in [ + ('jan|janu|january', datetime.datetime( + 2014, 1, 1, self.hr, self.mn, self.sec).timetuple()), + ('feb|febr|february', datetime.datetime( + 2014, 2, 1, self.hr, self.mn, self.sec).timetuple()), + ('mar|marc|march', datetime.datetime( + 2014, 3, 1, self.hr, self.mn, self.sec).timetuple()), + ('apr|apri|april', datetime.datetime( + 2014, 4, 1, self.hr, self.mn, self.sec).timetuple()), + ('may|may', datetime.datetime( + 2014, 5, 1, self.hr, self.mn, self.sec).timetuple()), + ('jun|june', datetime.datetime( + 2014, 6, 1, self.hr, self.mn, self.sec).timetuple()), + ('jul|july', datetime.datetime( + 2014, 7, 1, self.hr, self.mn, self.sec).timetuple()), + ('aug|augu|august', datetime.datetime( + 2014, 8, 1, self.hr, self.mn, self.sec).timetuple()), + ('sep|sept|september', datetime.datetime( + 2014, 9, 1, self.hr, self.mn, self.sec).timetuple()), + ('oct|octo|october', datetime.datetime( + 2014, 10, 1, self.hr, self.mn, self.sec).timetuple()), + ('nov|novem|november', datetime.datetime( + 2014, 11, 1, self.hr, self.mn, self.sec).timetuple()), + ('dec|decem|december', datetime.datetime( + 2014, 12, 1, self.hr, self.mn, self.sec).timetuple()) + ]: + for dateText in dates.split("|"): + # print dateText + self.assertExpectedResult( + self.cal.parse(dateText, start), (expected_date, 1)) diff --git a/tests/TestAustralianLocale.py b/tests/TestAustralianLocale.py new file mode 100644 index 0000000..f3fd34b --- /dev/null +++ b/tests/TestAustralianLocale.py @@ -0,0 +1,107 @@ +# -*- coding: utf-8 -*- +""" +Test parsing of simple date and times using the Australian locale +""" +from __future__ import unicode_literals + +import unittest +import time +import datetime +import parsedatetime as pdt + +from . import utils + + +class test(unittest.TestCase): + + @utils.assertEqualWithComparator + def assertExpectedResult(self, result, check, **kwargs): + return utils.compareResultByTimeTuplesAndFlags(result, check, **kwargs) + + def setUp(self): + self.ptc = pdt.Constants('en_AU', usePyICU=False) + self.cal = pdt.Calendar(self.ptc) + + (self.yr, self.mth, self.dy, self.hr, + self.mn, self.sec, self.wd, self.yd, self.isdst) = time.localtime() + + if self.ptc.localeID != 'en_AU': + raise unittest.SkipTest( + 'Locale not set to en_AU - check if PyICU is installed') + + def testTimes(self): + start = datetime.datetime( + self.yr, self.mth, self.dy, self.hr, self.mn, self.sec).timetuple() + target = datetime.datetime( + self.yr, self.mth, self.dy, 23, 0, 0).timetuple() + + self.assertExpectedResult( + self.cal.parse('11:00:00 PM', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('11:00 PM', start), (target, 2)) + self.assertExpectedResult(self.cal.parse('11 PM', start), (target, 2)) + self.assertExpectedResult(self.cal.parse('11PM', start), (target, 2)) + self.assertExpectedResult(self.cal.parse('2300', start), (target, 2)) + self.assertExpectedResult(self.cal.parse('23:00', start), (target, 2)) + self.assertExpectedResult(self.cal.parse('11p', start), (target, 2)) + self.assertExpectedResult(self.cal.parse('11pm', start), (target, 2)) + + target = datetime.datetime( + self.yr, self.mth, self.dy, 11, 0, 0).timetuple() + + self.assertExpectedResult( + self.cal.parse('11:00:00 AM', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('11:00 AM', start), (target, 2)) + self.assertExpectedResult(self.cal.parse('11 AM', start), (target, 2)) + self.assertExpectedResult(self.cal.parse('11AM', start), (target, 2)) + self.assertExpectedResult(self.cal.parse('1100', start), (target, 2)) + self.assertExpectedResult(self.cal.parse('11:00', start), (target, 2)) + self.assertExpectedResult(self.cal.parse('11a', start), (target, 2)) + self.assertExpectedResult(self.cal.parse('11am', start), (target, 2)) + + target = datetime.datetime( + self.yr, self.mth, self.dy, 7, 30, 0).timetuple() + + self.assertExpectedResult(self.cal.parse('730', start), (target, 2)) + self.assertExpectedResult(self.cal.parse('0730', start), (target, 2)) + + target = datetime.datetime( + self.yr, self.mth, self.dy, 17, 30, 0).timetuple() + + self.assertExpectedResult(self.cal.parse('1730', start), (target, 2)) + self.assertExpectedResult(self.cal.parse('173000', start), (target, 2)) + + def testDates(self): + start = datetime.datetime( + self.yr, self.mth, self.dy, self.hr, self.mn, self.sec).timetuple() + target = datetime.datetime( + 2006, 8, 25, self.hr, self.mn, self.sec).timetuple() + + self.assertExpectedResult( + self.cal.parse('25-08-2006', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('25/08/2006', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('25.08.2006', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('25-8-06', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('25/8/06', start), (target, 1)) + + if self.mth > 8 or (self.mth == 8 and self.dy > 25): + target = datetime.datetime( + self.yr + 1, 8, 25, self.hr, self.mn, self.sec).timetuple() + else: + target = datetime.datetime( + self.yr, 8, 25, self.hr, self.mn, self.sec).timetuple() + + self.assertExpectedResult(self.cal.parse('25-8', start), (target, 1)) + self.assertExpectedResult(self.cal.parse('25/8', start), (target, 1)) + self.assertExpectedResult(self.cal.parse('25.8', start), (target, 1)) + self.assertExpectedResult(self.cal.parse('25-08', start), (target, 1)) + self.assertExpectedResult(self.cal.parse('25/08', start), (target, 1)) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/TestComplexDateTimes.py b/tests/TestComplexDateTimes.py new file mode 100644 index 0000000..b43f48c --- /dev/null +++ b/tests/TestComplexDateTimes.py @@ -0,0 +1,116 @@ +# -*- coding: utf-8 -*- +""" +Test parsing of complex date and times +""" +from __future__ import unicode_literals + +import time +import datetime +import unittest +import parsedatetime as pdt + +from . import utils + + +class test(unittest.TestCase): + + @utils.assertEqualWithComparator + def assertExpectedResult(self, result, check, **kwargs): + return utils.compareResultByTimeTuplesAndFlags(result, check, **kwargs) + + def setUp(self): + self.cal = pdt.Calendar() + (self.yr, self.mth, self.dy, self.hr, + self.mn, self.sec, self.wd, self.yd, self.isdst) = time.localtime() + + def testDates(self): + start = datetime.datetime( + self.yr, self.mth, self.dy, self.hr, self.mn, self.sec).timetuple() + target = datetime.datetime(2006, 8, 25, 17, 0, 0).timetuple() + + self.assertExpectedResult( + self.cal.parse('08/25/2006 5pm', start), (target, 3)) + self.assertExpectedResult( + self.cal.parse('5pm on 08.25.2006', start), (target, 3)) + self.assertExpectedResult( + self.cal.parse('5pm August 25, 2006', start), (target, 3)) + self.assertExpectedResult( + self.cal.parse('5pm August 25th, 2006', start), (target, 3)) + self.assertExpectedResult( + self.cal.parse('5pm 25 August, 2006', start), (target, 3)) + self.assertExpectedResult( + self.cal.parse('5pm 25th August, 2006', start), (target, 3)) + self.assertExpectedResult( + self.cal.parse('Aug 25, 2006 5pm', start), (target, 3)) + self.assertExpectedResult( + self.cal.parse('Aug 25th, 2006 5pm', start), (target, 3)) + self.assertExpectedResult( + self.cal.parse('25 Aug, 2006 5pm', start), (target, 3)) + self.assertExpectedResult( + self.cal.parse('25th Aug 2006, 5pm', start), (target, 3)) + + if self.mth > 8 or (self.mth == 8 and self.dy > 5): + target = datetime.datetime(self.yr + 1, 8, 5, 17, 0, 0).timetuple() + else: + target = datetime.datetime(self.yr, 8, 5, 17, 0, 0).timetuple() + + self.assertExpectedResult( + self.cal.parse('8/5 at 5pm', start), (target, 3)) + self.assertExpectedResult( + self.cal.parse('5pm 8.5', start), (target, 3)) + self.assertExpectedResult( + self.cal.parse('08/05 5pm', start), (target, 3)) + self.assertExpectedResult( + self.cal.parse('August 5 5pm', start), (target, 3)) + self.assertExpectedResult( + self.cal.parse('5pm Aug 05', start), (target, 3)) + self.assertExpectedResult( + self.cal.parse('Aug 05 5pm', start), (target, 3)) + self.assertExpectedResult( + self.cal.parse('Aug 05th 5pm', start), (target, 3)) + self.assertExpectedResult( + self.cal.parse('5 August 5pm', start), (target, 3)) + self.assertExpectedResult( + self.cal.parse('5th August 5pm', start), (target, 3)) + self.assertExpectedResult( + self.cal.parse('5pm 05 Aug', start), (target, 3)) + self.assertExpectedResult( + self.cal.parse('05 Aug 5pm', start), (target, 3)) + self.assertExpectedResult( + self.cal.parse('05th Aug 5pm', start), (target, 3)) + + self.assertExpectedResult( + self.cal.parse('August 5th 5pm', start), (target, 3)) + + if self.mth > 8 or (self.mth == 8 and self.dy > 5): + target = datetime.datetime(self.yr + 1, 8, 5, 12, 0, 0).timetuple() + else: + target = datetime.datetime(self.yr, 8, 5, 12, 0, 0).timetuple() + + self.assertExpectedResult( + self.cal.parse('August 5th 12:00', start), (target, 3)) + self.assertExpectedResult( + self.cal.parse('August 5th 12pm', start), (target, 3)) + self.assertExpectedResult( + self.cal.parse('August 5th 12:00pm', start), (target, 3)) + self.assertExpectedResult( + self.cal.parse('August 5th 12 pm', start), (target, 3)) + self.assertExpectedResult( + self.cal.parse('August 5th 12:00 pm', start), (target, 3)) + + if self.mth > 8 or (self.mth == 8 and self.dy > 5): + target = datetime.datetime( + self.yr + 1, 8, 22, 3, 26, 0).timetuple() + else: + target = datetime.datetime(self.yr, 8, 22, 3, 26, 0).timetuple() + + self.assertExpectedResult( + self.cal.parse('August 22nd 3:26', start), (target, 3)) + self.assertExpectedResult( + self.cal.parse('August 22nd 3:26am', start), (target, 3)) + self.assertExpectedResult( + self.cal.parse('August 22nd 3:26 am', start), (target, 3)) + + +if __name__ == "__main__": + unittest.main() diff --git a/parsedatetime/tests/TestContext.py b/tests/TestContext.py similarity index 100% rename from parsedatetime/tests/TestContext.py rename to tests/TestContext.py diff --git a/parsedatetime/tests/TestConvertUnitAsWords.py b/tests/TestConvertUnitAsWords.py similarity index 100% rename from parsedatetime/tests/TestConvertUnitAsWords.py rename to tests/TestConvertUnitAsWords.py diff --git a/parsedatetime/tests/TestDelta.py b/tests/TestDelta.py similarity index 100% rename from parsedatetime/tests/TestDelta.py rename to tests/TestDelta.py diff --git a/tests/TestErrors.py b/tests/TestErrors.py new file mode 100644 index 0000000..ffa911e --- /dev/null +++ b/tests/TestErrors.py @@ -0,0 +1,77 @@ +# -*- coding: utf-8 -*- +""" +Test parsing of units +""" +from __future__ import unicode_literals + +import time +import datetime +import unittest +import parsedatetime as pdt + +from . import utils + + +class test(unittest.TestCase): + + @utils.assertEqualWithComparator + def assertExpectedResult(self, result, check, **kwargs): + return utils.compareResultByTimeTuplesAndFlags(result, check, **kwargs) + + @utils.assertEqualWithComparator + def assertExpectedErrorFlag(self, result, check, **kwargs): + return utils.compareResultByFlags(result, check, **kwargs) + + def setUp(self): + self.cal = pdt.Calendar() + (self.yr, self.mth, self.dy, self.hr, + self.mn, self.sec, self.wd, self.yd, self.isdst) = time.localtime() + + def testErrors(self): + s = datetime.datetime.now() + start = s.timetuple() + + # These tests all return current date/time as they are out of range + self.assertExpectedResult(self.cal.parse('01/0', start), (start, 0)) + self.assertExpectedResult(self.cal.parse('08/35', start), (start, 0)) + self.assertExpectedResult(self.cal.parse('18/35', start), (start, 0)) + self.assertExpectedResult(self.cal.parse('1799', start), (start, 0)) + self.assertExpectedResult(self.cal.parse('781', start), (start, 0)) + self.assertExpectedResult(self.cal.parse('2702', start), (start, 0)) + self.assertExpectedResult(self.cal.parse('78', start), (start, 0)) + self.assertExpectedResult(self.cal.parse('11', start), (start, 0)) + self.assertExpectedResult(self.cal.parse('1', start), (start, 0)) + self.assertExpectedResult(self.cal.parse('174565', start), (start, 0)) + self.assertExpectedResult(self.cal.parse('177505', start), (start, 0)) + # ensure short month names do not cause false positives within a word - + # jun (june) + self.assertExpectedResult( + self.cal.parse('injunction', start), (start, 0)) + # ensure short month names do not cause false positives at the start of + # a word - jul (juuly) + self.assertExpectedResult(self.cal.parse('julius', start), (start, 0)) + # ensure short month names do not cause false positives at the end of a + # word - mar (march) + self.assertExpectedResult(self.cal.parse('lamar', start), (start, 0)) + # ensure short weekday names do not cause false positives within a word + # - mon (monday) + self.assertExpectedResult( + self.cal.parse('demonize', start), (start, 0)) + # ensure short weekday names do not cause false positives at the start + # of a word - mon (monday) + self.assertExpectedResult(self.cal.parse('money', start), (start, 0)) + # ensure short weekday names do not cause false positives at the end of + # a word - th (thursday) + self.assertExpectedResult(self.cal.parse('month', start), (start, 0)) + self.assertExpectedErrorFlag( + self.cal.parse('30/030/01/071/07', start), (start, 0)) + # overflow due to Python's datetime + self.assertExpectedResult(self.cal.parse('12345 y', start), (start, 0)) + self.assertExpectedResult( + self.cal.parse('654321 w', start), (start, 0)) + self.assertExpectedResult( + self.cal.parse('3700000 d', start), (start, 0)) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/TestFrenchLocale.py b/tests/TestFrenchLocale.py new file mode 100644 index 0000000..2a0f8f2 --- /dev/null +++ b/tests/TestFrenchLocale.py @@ -0,0 +1,172 @@ +# -*- coding: utf-8 -*- +""" +Test parsing of simple date and times using the French locale + +Note: requires PyICU +""" +from __future__ import unicode_literals +import time +import datetime +import unittest +import parsedatetime as pdt + +from . import utils + + +class test(unittest.TestCase): + + @utils.assertEqualWithComparator + def assertExpectedResult(self, result, check, **kwargs): + return utils.compareResultByTimeTuplesAndFlags(result, check, **kwargs) + + def setUp(self): + self.ptc = pdt.Constants('fr_FR', usePyICU=True) + self.cal = pdt.Calendar(self.ptc) + + (self.yr, self.mth, self.dy, self.hr, + self.mn, self.sec, self.wd, self.yd, self.isdst) = time.localtime() + + if self.ptc.localeID != 'fr_FR': + raise unittest.SkipTest( + 'Locale not set to fr_FR - check if PyICU is installed') + + def testTimes(self): + if self.ptc.localeID == 'fr_FR': + start = datetime.datetime( + self.yr, self.mth, self.dy, + self.hr, self.mn, self.sec).timetuple() + target = datetime.datetime( + self.yr, self.mth, self.dy, 23, 0, 0).timetuple() + + self.assertExpectedResult( + self.cal.parse('2300', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('23:00', start), (target, 2)) + + target = datetime.datetime( + self.yr, self.mth, self.dy, 11, 0, 0).timetuple() + + self.assertExpectedResult( + self.cal.parse('1100', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('11:00', start), (target, 2)) + + target = datetime.datetime( + self.yr, self.mth, self.dy, 7, 30, 0).timetuple() + + self.assertExpectedResult( + self.cal.parse('730', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('0730', start), (target, 2)) + + target = datetime.datetime( + self.yr, self.mth, self.dy, 17, 30, 0).timetuple() + + self.assertExpectedResult( + self.cal.parse('1730', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('173000', start), (target, 2)) + + def testDates(self): + if self.ptc.localeID == 'fr_FR': + start = datetime.datetime( + self.yr, self.mth, self.dy, + self.hr, self.mn, self.sec).timetuple() + target = datetime.datetime( + 2006, 8, 25, self.hr, self.mn, self.sec).timetuple() + + self.assertExpectedResult( + self.cal.parse('25/08/2006', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('25/8/06', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('ao\xfbt 25, 2006', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('ao\xfbt 25 2006', start), (target, 1)) + + if self.mth > 8 or (self.mth == 8 and self.dy > 25): + target = datetime.datetime( + self.yr + 1, 8, 25, self.hr, self.mn, self.sec).timetuple() + else: + target = datetime.datetime( + self.yr, 8, 25, self.hr, self.mn, self.sec).timetuple() + + self.assertExpectedResult( + self.cal.parse('25/8', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('25/08', start), (target, 1)) + + def testWeekDays(self): + if self.ptc.localeID == 'fr_FR': + start = datetime.datetime( + self.yr, self.mth, self.dy, + self.hr, self.mn, self.sec).timetuple() + + o1 = self.ptc.CurrentDOWParseStyle + o2 = self.ptc.DOWParseStyle + + # set it up so the current dow returns current day + self.ptc.CurrentDOWParseStyle = True + self.ptc.DOWParseStyle = 1 + + for i in range(0, 7): + dow = self.ptc.shortWeekdays[i] + + result = self.cal.parse(dow, start) + + yr, mth, dy, hr, mn, sec, wd, yd, isdst = result[0] + + self.assertEqual(wd, i) + + self.ptc.CurrentDOWParseStyle = o1 + self.ptc.DOWParseStyle = o2 + + +class pdtLocale_fr(pdt.pdt_locales.pdtLocale_icu): + + """French locale with french today/tomorrow/yesterday""" + + def __init__(self): + super(pdtLocale_fr, self).__init__(localeID='fr_FR') + self.dayOffsets.update({"aujourd'hui": 0, 'demain': 1, 'hier': -1}) + + +class TestDayOffsets(test): + # test how Aujourd'hui/Demain/Hier are parsed + + def setUp(self): + super(TestDayOffsets, self).setUp() + self.__old_pdtlocale_fr = pdt.pdtLocales.get('fr_FR') # save for later + pdt.pdtLocales['fr_FR'] = pdtLocale_fr # override for the test + self.ptc = pdt.Constants('fr_FR', usePyICU=False) + self.cal = pdt.Calendar(self.ptc) + + def test_dayoffsets(self): + start = datetime.datetime(self.yr, self.mth, self.dy, 9) + for date_string, expected_day_offset in [ + ("Aujourd'hui", 0), + ("aujourd'hui", 0), + ("Demain", 1), + ("demain", 1), + ("Hier", -1), + ("hier", -1), + ("today", 0), # assume default names exist + ("tomorrow", 1), + ("yesterday", -1), + ("au jour de hui", None)]: + got_dt, rc = self.cal.parseDT(date_string, start) + if expected_day_offset is not None: + self.assertEqual(rc, 1) + target = (start + datetime.timedelta(days=expected_day_offset)) + self.assertEqual(got_dt, target) + else: + self.assertEqual(rc, 0) + + def tearDown(self): + if self.__old_pdtlocale_fr is not None: # restore the locale + pdt.pdtLocales['fr_FR'] = self.__old_pdtlocale_fr + super(TestDayOffsets, self).tearDown() + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/TestGermanLocale.py b/tests/TestGermanLocale.py new file mode 100644 index 0000000..ae3a607 --- /dev/null +++ b/tests/TestGermanLocale.py @@ -0,0 +1,92 @@ +# -*- coding: utf-8 -*- +""" +Test parsing of simple date and times using the German locale +""" +from __future__ import unicode_literals + +import unittest +import time +import datetime +import parsedatetime as pdt + +from . import utils + + +class test(unittest.TestCase): + + @utils.assertEqualWithComparator + def assertExpectedResult(self, result, check, **kwargs): + return utils.compareResultByTimeTuplesAndFlags(result, check, **kwargs) + + def setUp(self): + self.ptc = pdt.Constants('de_DE', usePyICU=False) + self.cal = pdt.Calendar(self.ptc) + + (self.yr, self.mth, self.dy, self.hr, + self.mn, self.sec, self.wd, self.yd, self.isdst) = time.localtime() + + if self.ptc.localeID != 'de_DE': + raise unittest.SkipTest( + 'Locale not set to de_DE - check if PyICU is installed') + + def testTimes(self): + start = datetime.datetime( + self.yr, self.mth, self.dy, self.hr, self.mn, self.sec).timetuple() + target = datetime.datetime( + self.yr, self.mth, self.dy, 23, 0, 0).timetuple() + + self.assertExpectedResult( + self.cal.parse('23:00:00', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('23:00', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('2300', start), (target, 2)) + + target = datetime.datetime( + self.yr, self.mth, self.dy, 11, 0, 0).timetuple() + + self.assertExpectedResult( + self.cal.parse('11:00:00', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('11:00', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('1100', start), (target, 2)) + + target = datetime.datetime( + self.yr, self.mth, self.dy, 7, 30, 0).timetuple() + + self.assertExpectedResult(self.cal.parse('730', start), (target, 2)) + self.assertExpectedResult(self.cal.parse('0730', start), (target, 2)) + + target = datetime.datetime( + self.yr, self.mth, self.dy, 17, 30, 0).timetuple() + + self.assertExpectedResult(self.cal.parse('1730', start), (target, 2)) + self.assertExpectedResult(self.cal.parse('173000', start), (target, 2)) + + def testDates(self): + start = datetime.datetime( + self.yr, self.mth, self.dy, self.hr, self.mn, self.sec).timetuple() + target = datetime.datetime( + 2006, 8, 25, self.hr, self.mn, self.sec).timetuple() + + self.assertExpectedResult( + self.cal.parse('25.08.2006', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('25.8.06', start), (target, 1)) + + if self.mth > 8 or (self.mth == 8 and self.dy > 25): + target = datetime.datetime( + self.yr + 1, 8, 25, self.hr, self.mn, self.sec).timetuple() + else: + target = datetime.datetime( + self.yr, 8, 25, self.hr, self.mn, self.sec).timetuple() + + self.assertExpectedResult( + self.cal.parse('25.8', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('25.08', start), (target, 1)) + + +if __name__ == "__main__": + unittest.main() diff --git a/parsedatetime/tests/TestInc.py b/tests/TestInc.py similarity index 52% rename from parsedatetime/tests/TestInc.py rename to tests/TestInc.py index 9a890c7..c515d1f 100644 --- a/parsedatetime/tests/TestInc.py +++ b/tests/TestInc.py @@ -1,48 +1,61 @@ - +# -*- coding: utf-8 -*- """ Test Calendar.Inc() routine """ +from __future__ import unicode_literals -import unittest, time, datetime +import time +import datetime +import unittest import parsedatetime as pdt +from . import utils + class test(unittest.TestCase): - @pdt.tests.assertEqualWithComparator + @utils.assertEqualWithComparator def assertExpectedResult(self, result, check, **kwargs): - return pdt.tests.compareResultByTimeTuplesAndFlags((result, 1), (check, 1), **kwargs) + return utils.compareResultByTimeTuplesAndFlags((result, 1), + (check, 1), **kwargs) def setUp(self): self.cal = pdt.Calendar() - self.yr, self.mth, self.dy, self.hr, self.mn, self.sec, self.wd, self.yd, self.isdst = time.localtime() + (self.yr, self.mth, self.dy, self.hr, + self.mn, self.sec, self.wd, self.yd, self.isdst) = time.localtime() def testIncMonths(self): s = datetime.datetime(2006, 1, 1, 12, 0, 0) t = datetime.datetime(2006, 2, 1, 12, 0, 0) - self.assertExpectedResult(self.cal.inc(s, month=1).timetuple(), t.timetuple()) + self.assertExpectedResult( + self.cal.inc(s, month=1).timetuple(), t.timetuple()) s = datetime.datetime(2006, 12, 1, 12, 0, 0) - t = datetime.datetime(2007, 1, 1, 12, 0, 0) - self.assertExpectedResult(self.cal.inc(s, month=1).timetuple(), t.timetuple()) + t = datetime.datetime(2007, 1, 1, 12, 0, 0) + self.assertExpectedResult( + self.cal.inc(s, month=1).timetuple(), t.timetuple()) # leap year, Feb 1 s = datetime.datetime(2008, 2, 1, 12, 0, 0) t = datetime.datetime(2008, 3, 1, 12, 0, 0) - self.assertExpectedResult(self.cal.inc(s, month=1).timetuple(), t.timetuple()) + self.assertExpectedResult( + self.cal.inc(s, month=1).timetuple(), t.timetuple()) # leap year, Feb 29 s = datetime.datetime(2008, 2, 29, 12, 0, 0) t = datetime.datetime(2008, 3, 29, 12, 0, 0) - self.assertExpectedResult(self.cal.inc(s, month=1).timetuple(), t.timetuple()) + self.assertExpectedResult( + self.cal.inc(s, month=1).timetuple(), t.timetuple()) - s = datetime.datetime(2006, 1, 1, 12, 0, 0) + s = datetime.datetime(2006, 1, 1, 12, 0, 0) t = datetime.datetime(2005, 12, 1, 12, 0, 0) - self.assertExpectedResult(self.cal.inc(s, month=-1).timetuple(), t.timetuple()) + self.assertExpectedResult( + self.cal.inc(s, month=-1).timetuple(), t.timetuple()) # End of month Jan 31 to Feb - Febuary only has 28 days s = datetime.datetime(2006, 1, 31, 12, 0, 0) t = datetime.datetime(2006, 2, 28, 12, 0, 0) - self.assertExpectedResult(self.cal.inc(s, month=1).timetuple(), t.timetuple()) + self.assertExpectedResult( + self.cal.inc(s, month=1).timetuple(), t.timetuple()) # walk thru months and make sure month increment doesn't set the day # to be past the last day of the new month @@ -56,32 +69,39 @@ def testIncMonths(self): t = datetime.datetime(2006, m + 1, d, 12, 0, 0) - self.assertExpectedResult(self.cal.inc(s, month=1).timetuple(), t.timetuple()) + self.assertExpectedResult( + self.cal.inc(s, month=1).timetuple(), t.timetuple()) def testIncYears(self): s = datetime.datetime(2006, 1, 1, 12, 0, 0) t = datetime.datetime(2007, 1, 1, 12, 0, 0) - self.assertExpectedResult(self.cal.inc(s, year=1).timetuple(), t.timetuple()) + self.assertExpectedResult( + self.cal.inc(s, year=1).timetuple(), t.timetuple()) s = datetime.datetime(2006, 1, 1, 12, 0, 0) t = datetime.datetime(2008, 1, 1, 12, 0, 0) - self.assertExpectedResult(self.cal.inc(s, year=2).timetuple(), t.timetuple()) + self.assertExpectedResult( + self.cal.inc(s, year=2).timetuple(), t.timetuple()) s = datetime.datetime(2006, 12, 31, 12, 0, 0) t = datetime.datetime(2007, 12, 31, 12, 0, 0) - self.assertExpectedResult(self.cal.inc(s, year=1).timetuple(), t.timetuple()) + self.assertExpectedResult( + self.cal.inc(s, year=1).timetuple(), t.timetuple()) s = datetime.datetime(2006, 12, 31, 12, 0, 0) t = datetime.datetime(2005, 12, 31, 12, 0, 0) - self.assertExpectedResult(self.cal.inc(s, year=-1).timetuple(), t.timetuple()) + self.assertExpectedResult( + self.cal.inc(s, year=-1).timetuple(), t.timetuple()) s = datetime.datetime(2008, 3, 1, 12, 0, 0) t = datetime.datetime(2009, 3, 1, 12, 0, 0) - self.assertExpectedResult(self.cal.inc(s, year=1).timetuple(), t.timetuple()) + self.assertExpectedResult( + self.cal.inc(s, year=1).timetuple(), t.timetuple()) s = datetime.datetime(2008, 3, 1, 12, 0, 0) t = datetime.datetime(2007, 3, 1, 12, 0, 0) - self.assertExpectedResult(self.cal.inc(s, year=-1).timetuple(), t.timetuple()) + self.assertExpectedResult( + self.cal.inc(s, year=-1).timetuple(), t.timetuple()) if __name__ == "__main__": diff --git a/tests/TestMultiple.py b/tests/TestMultiple.py new file mode 100644 index 0000000..ca42f48 --- /dev/null +++ b/tests/TestMultiple.py @@ -0,0 +1,100 @@ +# -*- coding: utf-8 -*- +""" +Test parsing of strings with multiple chunks +""" +from __future__ import unicode_literals + +import time +import datetime +import unittest +import parsedatetime as pdt + +from . import utils + + +class test(unittest.TestCase): + + @utils.assertEqualWithComparator + def assertExpectedResult(self, result, check, **kwargs): + return utils.compareResultByTimeTuplesAndFlags(result, check, **kwargs) + + def setUp(self): + self.cal = pdt.Calendar() + (self.yr, self.mth, self.dy, self.hr, + self.mn, self.sec, self.wd, self.yd, self.isdst) = time.localtime() + + def testSimpleMultipleItems(self): + s = datetime.datetime.now() + t = self.cal.inc(s, year=3) + datetime.timedelta(days=5, weeks=2) + + start = s.timetuple() + target = t.timetuple() + + self.assertExpectedResult( + self.cal.parse('3 years 2 weeks 5 days', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('3years 2weeks 5days', start), (target, 1)) + + def testMultipleItemsSingleCharUnits(self): + s = datetime.datetime.now() + t = self.cal.inc(s, year=3) + datetime.timedelta(days=5, weeks=2) + + start = s.timetuple() + target = t.timetuple() + + self.assertExpectedResult( + self.cal.parse('3 y 2 w 5 d', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('3y 2w 5d', start), (target, 1)) + + t = self.cal.inc(s, year=3) + datetime.timedelta(hours=5, minutes=50) + target = t.timetuple() + + self.assertExpectedResult( + self.cal.parse('3y 5h 50m', start), (target, 3)) + + def testMultipleItemsWithPunctuation(self): + s = datetime.datetime.now() + t = self.cal.inc(s, year=3) + datetime.timedelta(days=5, weeks=2) + + start = s.timetuple() + target = t.timetuple() + + self.assertExpectedResult( + self.cal.parse('3 years, 2 weeks, 5 days', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('3 years, 2 weeks and 5 days', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('3y, 2w, 5d ', start), (target, 1)) + + def testUnixATStyle(self): + s = datetime.datetime.now() + t = s + datetime.timedelta(days=3) + + t = t.replace(hour=16, minute=0, second=0) + + start = s.timetuple() + target = t.timetuple() + + self.assertExpectedResult( + self.cal.parse('4pm + 3 days', start), (target, 3)) + self.assertExpectedResult( + self.cal.parse('4pm +3 days', start), (target, 3)) + + def testUnixATStyleNegative(self): + s = datetime.datetime.now() + t = s + datetime.timedelta(days=-3) + + t = t.replace(hour=16, minute=0, second=0) + + start = s.timetuple() + target = t.timetuple() + + self.assertExpectedResult( + self.cal.parse('4pm - 3 days', start), (target, 3)) + self.assertExpectedResult( + self.cal.parse('4pm -3 days', start), (target, 3)) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/TestNlp.py b/tests/TestNlp.py new file mode 100644 index 0000000..a177b7f --- /dev/null +++ b/tests/TestNlp.py @@ -0,0 +1,125 @@ +# -*- coding: utf-8 -*- +""" +Test parsing of strings that are phrases +""" +from __future__ import unicode_literals + +import time +import datetime +import unittest +import parsedatetime as pdt + +from . import utils + + +class test(unittest.TestCase): + + # a special compare function for nlp returned data + + @utils.assertEqualWithComparator + def assertExpectedResult(self, result, check, dateOnly=False): + target = result + value = check + + if target is None and value is None: + return True + + if (target is None and value is not None) or \ + (target is not None and value is None): + return False + + if len(target) != len(value): + return False + + for i in range(0, len(target)): + target_date = target[i][0] + value_date = value[i][0] + + if target_date.year != value_date.year or \ + target_date.month != value_date.month or \ + target_date.day != value_date.day or \ + target_date.hour != value_date.hour or \ + target_date.minute != value_date.minute: + return False + if target[i][1] != value[i][1]: + return False + if target[i][2] != value[i][2]: + return False + if target[i][3] != value[i][3]: + return False + if target[i][4] != value[i][4]: + return False + + return True + + def setUp(self): + self.cal = pdt.Calendar() + (self.yr, self.mth, self.dy, self.hr, + self.mn, self.sec, self.wd, self.yd, self.isdst) = time.localtime() + + def testLongPhrase(self): + # note: these tests do not need to be as dynamic as the others because + # this is still based on the parse() function, so all tests of + # the actual processing of the datetime value returned are + # applicable to this. Here we are concerned with ensuring the + # correct portions of text and their positions are extracted and + # processed. + start = datetime.datetime(2013, 8, 1, 21, 25, 0).timetuple() + target = ((datetime.datetime(2013, 8, 5, 20, 0), + 3, 17, 37, 'At 8PM on August 5th'), + (datetime.datetime(2013, 8, 9, 21, 0), + 3, 72, 90, 'next Friday at 9PM'), + (datetime.datetime(2013, 8, 1, 21, 30, 0), + 2, 120, 132, 'in 5 minutes'), + (datetime.datetime(2013, 8, 8, 9, 0), + 1, 173, 182, 'next week')) + + # positive testing + self.assertExpectedResult(self.cal.nlp( + "I'm so excited!! At 8PM on August 5th i'm going to fly to " + "Florida. Then next Friday at 9PM i'm going to Dog n Bone! " + "And in 5 minutes I'm going to eat some food! Talk to you " + "next week.", start), target) + + target = datetime.datetime( + self.yr, self.mth, self.dy, 17, 0, 0).timetuple() + + def testQuotes(self): + # quotes should not interfere with datetime language recognition + start = datetime.datetime(2013, 8, 1, 21, 25, 0).timetuple() + target = self.cal.nlp( + "I'm so excited!! At '8PM on August 5th' i'm going to fly to " + "Florida. Then 'next Friday at 9PM' i'm going to Dog n Bone! " + "And in '5 minutes' I'm going to eat some food! Talk to you " + '"next week"', start) + + self.assertEqual(target[0][4], "At '8PM on August 5th") + self.assertEqual(target[1][4], "next Friday at 9PM") + self.assertEqual(target[2][4], "in '5 minutes") + self.assertEqual(target[3][4], "next week") + + def testPrefixes(self): + # nlp has special handling for on/in/at prefixes + start = datetime.datetime(2013, 8, 1, 21, 25, 0).timetuple() + + target = self.cal.nlp("Buy a balloon on Monday", start) + self.assertEqual(target[0][4], "on Monday") + + target = self.cal.nlp("Buy a balloon at noon", start) + self.assertEqual(target[0][4], "at noon") + + target = self.cal.nlp("Buy a balloon in a month", start) + self.assertEqual(target[0][4], "in a month") + + # Should no longer pull "on" off the end of balloon + target = self.cal.nlp("Buy a balloon Monday", start) + self.assertEqual(target[0][4], "Monday") + + def testFalsePositives(self): + # negative testing - no matches should return None + start = datetime.datetime(2013, 8, 1, 21, 25, 0).timetuple() + self.assertExpectedResult(self.cal.nlp( + "Next, I'm so excited!! So many things that are going to " + "happen every week!!", start), None) + self.assertExpectedResult(self.cal.nlp("$300", start), None) + self.assertExpectedResult(self.cal.nlp("300ml", start), None) diff --git a/tests/TestPhrases.py b/tests/TestPhrases.py new file mode 100644 index 0000000..9c0e60d --- /dev/null +++ b/tests/TestPhrases.py @@ -0,0 +1,152 @@ +# -*- coding: utf-8 -*- +""" +Test parsing of strings that are phrases +""" +from __future__ import unicode_literals + +import time +import datetime +import unittest +import parsedatetime as pdt + +from . import utils + + +class test(unittest.TestCase): + + @utils.assertEqualWithComparator + def assertExpectedResult(self, result, check, **kwargs): + return utils.compareResultByTimeTuplesAndFlags(result, check, **kwargs) + + def setUp(self): + self.cal = pdt.Calendar() + (self.yr, self.mth, self.dy, self.hr, + self.mn, self.sec, self.wd, self.yd, self.isdst) = time.localtime() + + def testPhrases(self): + # + # NOTE - this test will fail under certain conditions + # It is building an absolute date for comparison and then + # testing the parsing of relative phrases and as such will fail + # if run near the midnight transition. + # Thanks to Chris Petrilli for asking about it and prompting me + # to create this note! + # + start = datetime.datetime( + self.yr, self.mth, self.dy, self.hr, self.mn, self.sec).timetuple() + target = datetime.datetime( + self.yr, self.mth, self.dy, 16, 0, 0).timetuple() + + self.assertExpectedResult( + self.cal.parse('flight from SFO at 4pm', start), (target, 2)) + + target = datetime.datetime( + self.yr, self.mth, self.dy, 17, 0, 0).timetuple() + + self.assertExpectedResult( + self.cal.parse('eod', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('meeting eod', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('eod meeting', start), (target, 2)) + + target = datetime.datetime( + self.yr, self.mth, self.dy, 17, 0, 0) + datetime.timedelta(days=1) + target = target.timetuple() + + self.assertExpectedResult( + self.cal.parse('tomorrow eod', start), (target, 3)) + self.assertExpectedResult( + self.cal.parse('eod tomorrow', start), (target, 3)) + + def testPhraseWithDays_DOWStyle_1_False(self): + s = datetime.datetime.now() + + # find out what day we are currently on + # and determine what the next day of week is + t = s + datetime.timedelta(days=1) + start = s.timetuple() + + (yr, mth, dy, _, _, _, wd, yd, isdst) = t.timetuple() + + target = (yr, mth, dy, 17, 0, 0, wd, yd, isdst) + + d = self.wd + 1 + if d > 6: + d = 0 + + day = self.cal.ptc.Weekdays[d] + + self.assertExpectedResult( + self.cal.parse('eod %s' % day, start), (target, 3)) + + # find out what day we are currently on + # and determine what the previous day of week is + t = s + datetime.timedelta(days=6) + + (yr, mth, dy, hr, mn, sec, wd, yd, isdst) = t.timetuple() + + target = (yr, mth, dy, 17, 0, 0, wd, yd, isdst) + + d = self.wd - 1 + if d < 0: + d = 6 + + day = self.cal.ptc.Weekdays[d] + + self.assertExpectedResult( + self.cal.parse('eod %s' % day, start), (target, 3)) + + def testEndOfPhrases(self): + s = datetime.datetime.now() + + # find out what month we are currently on + # set the day to 1 and then go back a day + # to get the end of the current month + (yr, mth, _, hr, mn, sec, _, _, _) = s.timetuple() + + mth += 1 + if mth > 12: + mth = 1 + yr += 1 + + t = datetime.datetime( + yr, mth, 1, 9, 0, 0) + datetime.timedelta(days=-1) + + start = s.timetuple() + target = t.timetuple() + + self.assertExpectedResult( + self.cal.parse('eom', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('meeting eom', start), (target, 1)) + + s = datetime.datetime.now() + + (yr, mth, dy, hr, mn, sec, wd, yd, isdst) = s.timetuple() + + t = datetime.datetime(yr, 12, 31, 9, 0, 0) + + start = s.timetuple() + target = t.timetuple() + + self.assertExpectedResult( + self.cal.parse('eoy', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('meeting eoy', start), (target, 1)) + + def testLastPhrases(self): + for day in (11, 12, 13, 14, 15, 16, 17): + start = datetime.datetime(2012, 11, day, 9, 0, 0) + + (yr, mth, dy, _, _, _, wd, yd, isdst) = start.timetuple() + + n = 4 - wd + if n >= 0: + n -= 7 + + target = start + datetime.timedelta(days=n) + + self.assertExpectedResult( + self.cal.parse('last friday', start.timetuple()), + (target.timetuple(), 1), dateOnly=True) diff --git a/tests/TestRanges.py b/tests/TestRanges.py new file mode 100644 index 0000000..075e07e --- /dev/null +++ b/tests/TestRanges.py @@ -0,0 +1,106 @@ +# -*- coding: utf-8 -*- +""" +Test parsing of simple date and times +""" +from __future__ import unicode_literals + +import time +import datetime +import unittest +import parsedatetime as pdt + +from . import utils + + +class test(unittest.TestCase): + + @utils.assertEqualWithComparator + def assertExpectedResult(self, result, check, **kwargs): + return utils.compareResultByTimeTupleRangesAndFlags( + result, check, **kwargs) + + def setUp(self): + self.cal = pdt.Calendar() + (self.yr, self.mth, self.dy, self.hr, + self.mn, self.sec, self.wd, self.yd, self.isdst) = time.localtime() + + def testTimes(self): + start = datetime.datetime( + self.yr, self.mth, self.dy, self.hr, self.mn, self.sec).timetuple() + + targetStart = datetime.datetime( + self.yr, self.mth, self.dy, 14, 0, 0).timetuple() + targetEnd = datetime.datetime( + self.yr, self.mth, self.dy, 17, 30, 0).timetuple() + + self.assertExpectedResult(self.cal.evalRanges( + "2 pm - 5:30 pm", start), (targetStart, targetEnd, 2)) + self.assertExpectedResult(self.cal.evalRanges( + "2pm - 5:30pm", start), (targetStart, targetEnd, 2)) + self.assertExpectedResult(self.cal.evalRanges( + "2:00:00 pm - 5:30:00 pm", start), (targetStart, targetEnd, 2)) + self.assertExpectedResult(self.cal.evalRanges( + "2 - 5:30pm", start), (targetStart, targetEnd, 2)) + self.assertExpectedResult(self.cal.evalRanges( + "14:00 - 17:30", start), (targetStart, targetEnd, 2)) + + targetStart = datetime.datetime( + self.yr, self.mth, self.dy, 10, 0, 0).timetuple() + targetEnd = datetime.datetime( + self.yr, self.mth, self.dy, 13, 30, 0).timetuple() + + self.assertExpectedResult(self.cal.evalRanges( + "10AM - 1:30PM", start), (targetStart, targetEnd, 2)) + self.assertExpectedResult(self.cal.evalRanges( + "10:00:00 am - 1:30:00 pm", start), (targetStart, targetEnd, 2)) + self.assertExpectedResult(self.cal.evalRanges( + "10:00 - 13:30", start), (targetStart, targetEnd, 2)) + + targetStart = datetime.datetime( + self.yr, self.mth, self.dy, 15, 30, 0).timetuple() + targetEnd = datetime.datetime( + self.yr, self.mth, self.dy, 17, 0, 0).timetuple() + + self.assertExpectedResult( + self.cal.evalRanges("today 3:30-5PM", start), + (targetStart, targetEnd, 2)) + + def testDates(self): + start = datetime.datetime( + self.yr, self.mth, self.dy, self.hr, self.mn, self.sec).timetuple() + + targetStart = datetime.datetime( + 2006, 8, 29, self.hr, self.mn, self.sec).timetuple() + targetEnd = datetime.datetime( + 2006, 9, 2, self.hr, self.mn, self.sec).timetuple() + + self.assertExpectedResult( + self.cal.evalRanges("August 29, 2006 - September 2, 2006", start), + (targetStart, targetEnd, 1)) + self.assertExpectedResult( + self.cal.evalRanges("August 29 - September 2, 2006", start), + (targetStart, targetEnd, 1)) + + targetStart = datetime.datetime( + 2006, 8, 29, self.hr, self.mn, self.sec).timetuple() + targetEnd = datetime.datetime( + 2006, 9, 2, self.hr, self.mn, self.sec).timetuple() + + self.assertExpectedResult( + self.cal.evalRanges("08/29/06 - 09/02/06", start), + (targetStart, targetEnd, 1)) + + def _testSubRanges(self): + start = datetime.datetime( + self.yr, self.mth, self.dy, self.hr, self.mn, self.sec).timetuple() + + targetStart = datetime.datetime(2006, 8, 1, 9, 0, 0).timetuple() + targetEnd = datetime.datetime(2006, 8, 15, 9, 0, 0).timetuple() + + self.assertExpectedResult( + self.cal.evalRanges("August 1-15, 2006", start), + (targetStart, targetEnd, 1)) + + +if __name__ == "__main__": + unittest.main() diff --git a/parsedatetime/tests/TestRussianLocale.py b/tests/TestRussianLocale.py similarity index 54% rename from parsedatetime/tests/TestRussianLocale.py rename to tests/TestRussianLocale.py index 7fb7123..ed8c883 100644 --- a/parsedatetime/tests/TestRussianLocale.py +++ b/tests/TestRussianLocale.py @@ -3,77 +3,81 @@ Test parsing of simple date and times using the Russian locale """ from __future__ import unicode_literals -import unittest +import sys import time import datetime -import sys +import unittest import parsedatetime as pdt +from . import utils + class test(unittest.TestCase): - @pdt.tests.assertEqualWithComparator + + @utils.assertEqualWithComparator def assertExpectedResult(self, result, check, **kwargs): - return pdt.tests.compareResultByTimeTuplesAndFlags(result, check, - **kwargs) + return utils.compareResultByTimeTuplesAndFlags(result, check, **kwargs) def setUp(self): locale = 'ru_RU' self.ptc = pdt.Constants(locale, usePyICU=False) self.cal = pdt.Calendar(self.ptc) - self.yr, self.mth, self.dy, self.hr, self.mn, self.sec, self.wd, self.yd, self.isdst = time.localtime() + (self.yr, self.mth, self.dy, self.hr, + self.mn, self.sec, self.wd, self.yd, self.isdst) = time.localtime() if self.ptc.localeID != locale: raise unittest.SkipTest( 'Locale not set to %s - check if PyICU is installed' % locale) def testTimes(self): - start = datetime.datetime(self.yr, self.mth, self.dy, self.hr, self.mn, - self.sec).timetuple() - target = datetime.datetime(self.yr, self.mth, self.dy, 23, 0, - 0).timetuple() + start = datetime.datetime( + self.yr, self.mth, self.dy, self.hr, self.mn, self.sec).timetuple() + target = datetime.datetime( + self.yr, self.mth, self.dy, 23, 0, 0).timetuple() - self.assertExpectedResult(self.cal.parse('23:00:00', start), - (target, 2)) + self.assertExpectedResult( + self.cal.parse('23:00:00', start), (target, 2)) self.assertExpectedResult(self.cal.parse('23:00', start), (target, 2)) self.assertExpectedResult(self.cal.parse('2300', start), (target, 2)) - target = datetime.datetime(self.yr, self.mth, self.dy, 11, 0, - 0).timetuple() + target = datetime.datetime( + self.yr, self.mth, self.dy, 11, 0, 0).timetuple() - self.assertExpectedResult(self.cal.parse('11:00:00', start), - (target, 2)) + self.assertExpectedResult( + self.cal.parse('11:00:00', start), (target, 2)) self.assertExpectedResult(self.cal.parse('11:00', start), (target, 2)) self.assertExpectedResult(self.cal.parse('1100', start), (target, 2)) - target = datetime.datetime(self.yr, self.mth, self.dy, 7, 30, - 0).timetuple() + target = datetime.datetime( + self.yr, self.mth, self.dy, 7, 30, 0).timetuple() self.assertExpectedResult(self.cal.parse('730', start), (target, 2)) self.assertExpectedResult(self.cal.parse('0730', start), (target, 2)) - target = datetime.datetime(self.yr, self.mth, self.dy, 17, 30, - 0).timetuple() + target = datetime.datetime( + self.yr, self.mth, self.dy, 17, 30, 0).timetuple() self.assertExpectedResult(self.cal.parse('1730', start), (target, 2)) self.assertExpectedResult(self.cal.parse('173000', start), (target, 2)) def testDates(self): - start = datetime.datetime(self.yr, self.mth, self.dy, self.hr, self.mn, - self.sec).timetuple() - target = datetime.datetime(2006, 8, 25, self.hr, self.mn, - self.sec).timetuple() + start = datetime.datetime( + self.yr, self.mth, self.dy, self.hr, self.mn, self.sec).timetuple() + target = datetime.datetime( + 2006, 8, 25, self.hr, self.mn, self.sec).timetuple() - self.assertExpectedResult(self.cal.parse('25.08.2006', start), - (target, 1)) - self.assertExpectedResult(self.cal.parse('25.8.06', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('25.08.2006', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('25.8.06', start), (target, 1)) if self.mth > 8 or (self.mth == 8 and self.dy > 25): - target = datetime.datetime(self.yr + 1, 8, 25, self.hr, self.mn, - self.sec).timetuple() + target = datetime.datetime( + self.yr + 1, 8, 25, self.hr, self.mn, self.sec).timetuple() else: - target = datetime.datetime(self.yr, 8, 25, self.hr, self.mn, - self.sec).timetuple() + target = datetime.datetime( + self.yr, 8, 25, self.hr, self.mn, self.sec).timetuple() self.assertExpectedResult(self.cal.parse('25.8', start), (target, 1)) self.assertExpectedResult(self.cal.parse('25.08', start), (target, 1)) @@ -81,20 +85,22 @@ def testDates(self): def testDatesLang(self): if sys.version_info >= (3, 0): target = datetime.datetime(2006, 8, 25, 23, 5).timetuple() - self.assertExpectedResult(self.cal.parse('25 августа 2006 23:05'), (target, 3)) - target = datetime.datetime(2006, 8, 25, self.hr, self.mn, self.sec).timetuple() - self.assertExpectedResult(self.cal.parse('25 августа 2006'), (target, 1)) + self.assertExpectedResult( + self.cal.parse('25 августа 2006 23:05'), (target, 3)) + target = datetime.datetime( + 2006, 8, 25, self.hr, self.mn, self.sec).timetuple() + self.assertExpectedResult( + self.cal.parse('25 августа 2006'), (target, 1)) self.assertEqual(self.cal.parse('23:05')[0][3], 23) self.assertEqual(self.cal.parse('23:05')[0][4], 5) def testConjugate(self): if sys.version_info >= (3, 0): target = datetime.datetime(2006, 9, 25, 23, 5).timetuple() - self.assertExpectedResult(self.cal.parse('25 сентября 2006 23:05'), (target, 3)) - # self.assertExpectedResult(self.cal.parse('25 сентябрь 2006 23:05'), (target, 3)) - - - + self.assertExpectedResult( + self.cal.parse('25 сентября 2006 23:05'), (target, 3)) + # self.assertExpectedResult( + # self.cal.parse('25 сентябрь 2006 23:05'), (target, 3)) # does not work with travis # datetime.now() return non correct data diff --git a/tests/TestSimpleDateTimes.py b/tests/TestSimpleDateTimes.py new file mode 100644 index 0000000..4885527 --- /dev/null +++ b/tests/TestSimpleDateTimes.py @@ -0,0 +1,560 @@ +# -*- coding: utf-8 -*- +""" +Test parsing of simple date and times +""" +from __future__ import unicode_literals + +import unittest +import time +import datetime +import string +import parsedatetime as pdt +from . import utils + + +class test(unittest.TestCase): + + @utils.assertEqualWithComparator + def assertExpectedResult(self, result, check, **kwargs): + return utils.compareResultByTimeTuplesAndFlags(result, check, **kwargs) + + def setUp(self): + self.cal = pdt.Calendar() + (self.yr, self.mth, self.dy, self.hr, + self.mn, self.sec, self.wd, self.yd, self.isdst) = time.localtime() + + def testDays(self): + s = datetime.datetime.now() + t = s + datetime.timedelta(days=1) + + start = s.timetuple() + target = t.timetuple() + + d = self.wd + 1 + + if d > 6: + d = 0 + + day = self.cal.ptc.Weekdays[d] + + self.assertExpectedResult(self.cal.parse(day, start), (target, 1)) + + t = s + datetime.timedelta(days=6) + + target = t.timetuple() + + d = self.wd - 1 + + if d < 0: + d = 6 + + day = self.cal.ptc.Weekdays[d] + + self.assertExpectedResult(self.cal.parse(day, start), (target, 1)) + + def testTimes(self): + start = datetime.datetime( + self.yr, self.mth, self.dy, self.hr, self.mn, self.sec).timetuple() + target = datetime.datetime( + self.yr, self.mth, self.dy, 23, 0, 0).timetuple() + + self.assertExpectedResult( + self.cal.parse('11:00:00 PM', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('11:00 PM', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('11 PM', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('11PM', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('2300', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('23:00', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('11p', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('11pm', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('11:00:00 P.M.', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('11:00 P.M.', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('11 P.M.', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('11P.M.', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('11p.m.', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('11 p.m.', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('"11 p.m."', start), (target, 2)) + + target = datetime.datetime( + self.yr, self.mth, self.dy, 11, 0, 0).timetuple() + + self.assertExpectedResult( + self.cal.parse('11:00:00 AM', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('11:00 AM', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('11 AM', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('11AM', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('1100', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('11:00', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('11a', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('11am', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('11:00:00 A.M.', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('11:00 A.M.', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('11 A.M.', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('11A.M.', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('11a.m.', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('11 a.m.', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('(11 a.m.)', start), (target, 2)) + + target = datetime.datetime( + self.yr, self.mth, self.dy, 7, 30, 0).timetuple() + + self.assertExpectedResult(self.cal.parse('730', start), (target, 2)) + self.assertExpectedResult(self.cal.parse('0730', start), (target, 2)) + self.assertExpectedResult(self.cal.parse('0730am', start), (target, 2)) + + target = datetime.datetime( + self.yr, self.mth, self.dy, 17, 30, 0).timetuple() + + self.assertExpectedResult(self.cal.parse('1730', start), (target, 2)) + self.assertExpectedResult(self.cal.parse('173000', start), (target, 2)) + + # Should not parse as a time due to prefix + self.assertExpectedResult(self.cal.parse('$300', start), (start, 0)) + self.assertExpectedResult(self.cal.parse('300ml', start), (start, 0)) + + def testDates(self): + start = datetime.datetime( + self.yr, self.mth, self.dy, self.hr, self.mn, self.sec).timetuple() + target = datetime.datetime( + 2006, 8, 25, self.hr, self.mn, self.sec).timetuple() + + self.assertExpectedResult( + self.cal.parse('08/25/2006', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('08.25.2006', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('2006/08/25', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('2006/8/25', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('2006-08-25', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('8/25/06', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('August 25, 2006', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('Aug 25, 2006', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('Aug. 25, 2006', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('August 25 2006', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('Aug 25 2006', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('Aug. 25 2006', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('25 August 2006', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('25 Aug 2006', start), (target, 1)) + + if self.mth > 8 or (self.mth == 8 and self.dy > 25): + target = datetime.datetime( + self.yr + 1, 8, 25, self.hr, self.mn, self.sec).timetuple() + else: + target = datetime.datetime( + self.yr, 8, 25, self.hr, self.mn, self.sec).timetuple() + + self.assertExpectedResult( + self.cal.parse('8/25', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('8.25', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('08/25', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('August 25', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('Aug 25', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('Aug. 25', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('"8.25"', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('(8.25)', start), (target, 1)) + + # Should not parse as dates + self.assertExpectedResult(self.cal.parse('$1.23', start), (start, 0)) + self.assertExpectedResult(self.cal.parse('$12.34', start), (start, 0)) + + # added test to ensure 4-digit year is recognized in the absence of day + target = datetime.datetime( + 2013, 8, 1, self.hr, self.mn, self.sec).timetuple() + self.assertExpectedResult( + self.cal.parse('Aug. 2013', start), (target, 1)) + + def testLeapDays(self): + start = datetime.datetime( + self.yr, self.mth, self.dy, self.hr, self.mn, self.sec).timetuple() + target = datetime.datetime( + 2000, 2, 29, self.hr, self.mn, self.sec).timetuple() + + self.assertExpectedResult( + self.cal.parse('02/29/2000', start), (target, 1)) + + target = datetime.datetime( + 2004, 2, 29, self.hr, self.mn, self.sec).timetuple() + + self.assertExpectedResult( + self.cal.parse('02/29/2004', start), (target, 1)) + + target = datetime.datetime( + 2008, 2, 29, self.hr, self.mn, self.sec).timetuple() + + self.assertExpectedResult( + self.cal.parse('02/29/2008', start), (target, 1)) + + target = datetime.datetime( + 2012, 2, 29, self.hr, self.mn, self.sec).timetuple() + + self.assertExpectedResult( + self.cal.parse('02/29/2012', start), (target, 1)) + + dNormal = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31) + dLeap = (31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31) + + for i in range(1, 12): + self.assertTrue(self.cal.ptc.daysInMonth(i, 1999), dNormal[i - 1]) + self.assertTrue(self.cal.ptc.daysInMonth(i, 2000), dLeap[i - 1]) + self.assertTrue(self.cal.ptc.daysInMonth(i, 2001), dNormal[i - 1]) + self.assertTrue(self.cal.ptc.daysInMonth(i, 2002), dNormal[i - 1]) + self.assertTrue(self.cal.ptc.daysInMonth(i, 2003), dNormal[i - 1]) + self.assertTrue(self.cal.ptc.daysInMonth(i, 2004), dLeap[i - 1]) + self.assertTrue(self.cal.ptc.daysInMonth(i, 2005), dNormal[i - 1]) + + def testDaySuffixes(self): + start = datetime.datetime( + self.yr, self.mth, self.dy, self.hr, self.mn, self.sec).timetuple() + target = datetime.datetime( + 2008, 8, 22, self.hr, self.mn, self.sec).timetuple() + + self.assertExpectedResult( + self.cal.parse('August 22nd, 2008', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('Aug 22nd, 2008', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('Aug. 22nd, 2008', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('August 22nd 2008', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('Aug 22nd 2008', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('Aug. 22nd 2008', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('22nd August 2008', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('22nd Aug 2008', start), (target, 1)) + + target = datetime.datetime( + 1949, 12, 31, self.hr, self.mn, self.sec).timetuple() + + self.assertExpectedResult( + self.cal.parse('December 31st, 1949', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('Dec 31st, 1949', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('December 31st 1949', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('Dec 31st 1949', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('31st December 1949', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('31st Dec 1949', start), (target, 1)) + + target = datetime.datetime( + 2008, 8, 23, self.hr, self.mn, self.sec).timetuple() + + self.assertExpectedResult( + self.cal.parse('August 23rd, 2008', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('Aug 23rd, 2008', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('Aug. 23rd, 2008', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('August 23rd 2008', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('Aug 23rd 2008', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('Aug. 23rd 2008', start), (target, 1)) + + target = datetime.datetime( + 2008, 8, 25, self.hr, self.mn, self.sec).timetuple() + + self.assertExpectedResult( + self.cal.parse('August 25th, 2008', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('Aug 25th, 2008', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('Aug. 25th, 2008', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('August 25th 2008', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('Aug 25th 2008', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('Aug. 25th 2008', start), (target, 1)) + + def testSpecialTimes(self): + start = datetime.datetime( + self.yr, self.mth, self.dy, self.hr, self.mn, self.sec).timetuple() + target = datetime.datetime( + self.yr, self.mth, self.dy, 6, 0, 0).timetuple() + + self.assertExpectedResult( + self.cal.parse('morning', start), (target, 2)) + + target = datetime.datetime( + self.yr, self.mth, self.dy, 8, 0, 0).timetuple() + + self.assertExpectedResult( + self.cal.parse('breakfast', start), (target, 2)) + + target = datetime.datetime( + self.yr, self.mth, self.dy, 12, 0, 0).timetuple() + + self.assertExpectedResult(self.cal.parse('lunch', start), (target, 2)) + + target = datetime.datetime( + self.yr, self.mth, self.dy, 13, 0, 0).timetuple() + + self.assertExpectedResult( + self.cal.parse('afternoon', start), (target, 2)) + + target = datetime.datetime( + self.yr, self.mth, self.dy, 18, 0, 0).timetuple() + + self.assertExpectedResult( + self.cal.parse('evening', start), (target, 2)) + + target = datetime.datetime( + self.yr, self.mth, self.dy, 19, 0, 0).timetuple() + + self.assertExpectedResult(self.cal.parse('dinner', start), (target, 2)) + + target = datetime.datetime( + self.yr, self.mth, self.dy, 21, 0, 0).timetuple() + + self.assertExpectedResult( + self.cal.parse('night', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('tonight', start), (target, 2)) + + def testMidnight(self): + start = datetime.datetime( + self.yr, self.mth, self.dy, self.hr, self.mn, self.sec).timetuple() + target = datetime.datetime( + self.yr, self.mth, self.dy, 0, 0, 0).timetuple() + + self.assertExpectedResult( + self.cal.parse('midnight', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('12:00:00 AM', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('12:00 AM', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('12 AM', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('12AM', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('12am', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('12a', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('0000', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('00:00', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('12:00:00 A.M.', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('12:00 A.M.', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('12 A.M.', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('12A.M.', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('12a.m.', start), (target, 2)) + + def testNoon(self): + start = datetime.datetime( + self.yr, self.mth, self.dy, self.hr, self.mn, self.sec).timetuple() + target = datetime.datetime( + self.yr, self.mth, self.dy, 12, 0, 0).timetuple() + + self.assertExpectedResult( + self.cal.parse('noon', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('12:00:00 PM', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('12:00 PM', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('12 PM', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('12PM', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('12pm', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('12p', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('1200', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('12:00', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('12:00:00 P.M.', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('12:00 P.M.', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('12 P.M.', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('12P.M.', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('12p.m.', start), (target, 2)) + + def testDaysOfWeek(self): + start = datetime.datetime( + 2014, 10, 25, self.hr, self.mn, self.sec).timetuple() + + target = datetime.datetime( + 2014, 10, 26, self.hr, self.mn, self.sec).timetuple() + self.assertExpectedResult( + self.cal.parse('sunday', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('sun', start), (target, 1)) + + target = datetime.datetime( + 2014, 10, 27, self.hr, self.mn, self.sec).timetuple() + self.assertExpectedResult( + self.cal.parse('Monday', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('mon', start), (target, 1)) + + target = datetime.datetime( + 2014, 10, 28, self.hr, self.mn, self.sec).timetuple() + self.assertExpectedResult( + self.cal.parse('tuesday', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('tues', start), (target, 1)) + + target = datetime.datetime( + 2014, 10, 29, self.hr, self.mn, self.sec).timetuple() + self.assertExpectedResult( + self.cal.parse('wednesday', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('wed', start), (target, 1)) + + target = datetime.datetime( + 2014, 10, 30, self.hr, self.mn, self.sec).timetuple() + self.assertExpectedResult( + self.cal.parse('thursday', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('thu', start), (target, 1)) + + target = datetime.datetime( + 2014, 10, 31, self.hr, self.mn, self.sec).timetuple() + self.assertExpectedResult( + self.cal.parse('friday', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('fri', start), (target, 1)) + + target = datetime.datetime( + 2014, 11, 1, self.hr, self.mn, self.sec).timetuple() + self.assertExpectedResult( + self.cal.parse('saturday', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('sat', start), (target, 1)) + + def testWordBoundaries(self): + # Ensure that keywords appearing at the start of a word are not parsed + # as if they were standalone keywords. For example, "10 dogs" should + # not be interpreted the same as "10 d" + start = datetime.datetime( + self.yr, self.mth, self.dy, self.hr, self.mn, self.sec).timetuple() + target = datetime.datetime.now().timetuple() + + keywords = [] + loc = self.cal.ptc.locale + + # Test all known keywords for the locale + keywords.extend(loc.meridian) + keywords.extend(loc.Weekdays) + keywords.extend(loc.shortWeekdays) + keywords.extend(loc.Months) + keywords.extend(loc.shortMonths) + keywords.extend(loc.numbers.keys()) + keywords.extend(loc.Modifiers.keys()) + keywords.extend(loc.dayOffsets.keys()) + keywords.extend(loc.re_sources.keys()) + keywords.extend(loc.small.keys()) + keywords.extend(loc.magnitude.keys()) + + for units in loc.units.values(): + keywords.extend(units) + + # Finally, test all lowercase letters to be particularly thorough - it + # would be very difficult to track down bugs due to single letters. + keywords.extend(list(string.ascii_lowercase)) + + for keyword in keywords: + phrase = '1 %sfoo' % keyword + self.assertExpectedResult( + self.cal.parse(phrase, start), (target, 0)) + + def testYearParseStyle(self): + config = pdt.Constants() + config.YearParseStyle = 0 + calendar = pdt.Calendar(config) + start = datetime.datetime(self.yr, self.mth, self.dy, + self.hr, self.mn, self.sec).timetuple() + target = datetime.datetime(self.yr, 7, 28, + self.hr, self.mn, self.sec).timetuple() + self.assertExpectedResult(calendar.parse('7/28', start), (target, 1)) + + # def testMonths(self): + + # start = datetime.datetime( + # self.yr, self.mth, self.dy, + # self.hr, self.mn, self.sec).timetuple() + + # target = datetime.datetime( + # self.yr, self.mth, self.dy, 12, 0, 0).timetuple() + + # self.assertExpectedResult(self.cal.parse('jun', start), (target, 2)) + # self.assertExpectedResult( + # self.cal.parse('12:00:00 PM', start), (target, 2)) + # self.assertExpectedResult( + # self.cal.parse('12:00 PM', start), (target, 2)) + # self.assertExpectedResult(self.cal.parse('12 PM', start), + # (target, 2)) + # self.assertExpectedResult(self.cal.parse('12PM', start), (target, 2)) + # self.assertExpectedResult(self.cal.parse('12pm', start), (target, 2)) + # self.assertExpectedResult(self.cal.parse('12p', start), (target, 2)) + # self.assertExpectedResult(self.cal.parse('1200', start), (target, 2)) + # self.assertExpectedResult(self.cal.parse('12:00', start), + # (target, 2)) + +if __name__ == "__main__": + unittest.main() diff --git a/tests/TestSimpleOffsets.py b/tests/TestSimpleOffsets.py new file mode 100644 index 0000000..3c4adf0 --- /dev/null +++ b/tests/TestSimpleOffsets.py @@ -0,0 +1,216 @@ +# -*- coding: utf-8 -*- +""" +Test parsing of 'simple' offsets +""" +from __future__ import unicode_literals + +import time +import datetime +import calendar +import unittest +import parsedatetime as pdt +from . import utils + + +def _truncateResult(result, trunc_seconds=True, trunc_hours=False): + try: + dt, flag = result + except ValueError: + # wtf?! + return result + if trunc_seconds: + dt = dt[:5] + (0,) * 4 + if trunc_hours: + dt = dt[:3] + (0,) * 6 + return dt, flag + +_tr = _truncateResult + + +class test(unittest.TestCase): + + @utils.assertEqualWithComparator + def assertExpectedResult(self, result, check, **kwargs): + return utils.compareResultByTimeTuplesAndFlags(result, check, **kwargs) + + def setUp(self): + self.cal = pdt.Calendar() + (self.yr, self.mth, self.dy, self.hr, + self.mn, self.sec, self.wd, self.yd, self.isdst) = time.localtime() + + def testNow(self): + s = datetime.datetime.now() + + start = s.timetuple() + target = s.timetuple() + + self.assertExpectedResult(self.cal.parse('now', start), (target, 2)) + + def testMinutesFromNow(self): + s = datetime.datetime.now() + t = s + datetime.timedelta(minutes=5) + + start = s.timetuple() + target = t.timetuple() + + self.assertExpectedResult( + self.cal.parse('5 minutes from now', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('5 min from now', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('5m from now', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('in 5 minutes', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('in 5 min', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('5 minutes', start), (target, 2)) + self.assertExpectedResult(self.cal.parse('5 min', start), (target, 2)) + self.assertExpectedResult(self.cal.parse('5m', start), (target, 2)) + + self.assertExpectedResult( + self.cal.parse('five minutes from now', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('five min from now', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('in five minutes', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('in five min', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('five minutes', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('five min', start), (target, 2)) + + def testMinutesBeforeNow(self): + s = datetime.datetime.now() + t = s + datetime.timedelta(minutes=-5) + + start = s.timetuple() + target = t.timetuple() + + self.assertExpectedResult( + self.cal.parse('5 minutes before now', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('5 min before now', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('5m before now', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('5 minutes ago', start), (target, 2)) + + self.assertExpectedResult( + self.cal.parse('five minutes before now', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('five min before now', start), (target, 2)) + + def testWeekFromNow(self): + s = datetime.datetime.now() + t = s + datetime.timedelta(weeks=1) + + start = s.timetuple() + target = t.timetuple() + + self.assertExpectedResult( + self.cal.parse('in 1 week', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('1 week from now', start), (target, 3)) + self.assertExpectedResult( + self.cal.parse('in one week', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('one week from now', start), (target, 3)) + self.assertExpectedResult( + self.cal.parse('in a week', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('a week from now', start), (target, 3)) + self.assertExpectedResult( + self.cal.parse('in 7 days', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('7 days from now', start), (target, 3)) + self.assertExpectedResult( + self.cal.parse('in seven days', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('seven days from now', start), (target, 3)) + self.assertEqual(_tr(self.cal.parse('next week', start), + trunc_hours=True), + _tr((target, 1), trunc_hours=True)) + + def testNextWeekDay(self): + start = datetime.datetime.now() + target = start + datetime.timedelta(days=4 + 7 - start.weekday()) + start = start.timetuple() + target = target.timetuple() + + self.assertExpectedResult(self.cal.parse('next friday', start), + (target, 1), dateOnly=True) + self.assertExpectedResult(self.cal.parse('next friday?', start), + (target, 1), dateOnly=True) + self.cal.ptc.StartTimeFromSourceTime = True + self.assertExpectedResult(self.cal.parse('next friday', start), + (target, 1)) + + def testWeekBeforeNow(self): + s = datetime.datetime.now() + t = s + datetime.timedelta(weeks=-1) + + start = s.timetuple() + target = t.timetuple() + + self.assertEqual(_tr(self.cal.parse('1 week before now', start)), + _tr((target, 3))) + self.assertEqual(_tr(self.cal.parse('one week before now', start)), + _tr((target, 3))) + self.assertEqual(_tr(self.cal.parse('a week before now', start)), + _tr((target, 3))) + self.assertEqual(_tr(self.cal.parse('7 days before now', start)), + _tr((target, 3))) + self.assertEqual(_tr(self.cal.parse('seven days before now', start)), + _tr((target, 3))) + self.assertEqual(_tr(self.cal.parse('1 week ago', start)), + _tr((target, 1))) + self.assertEqual(_tr(self.cal.parse('a week ago', start)), + _tr((target, 1))) + self.assertEqual(_tr(self.cal.parse('last week', start), + trunc_hours=True), + _tr((target, 1), trunc_hours=True)) + + def testNextMonth(self): + s = (datetime.datetime(self.yr, self.mth, self.dy, + self.hr, self.mn, self.sec) + + datetime.timedelta(days=1)) + t = self.cal.inc(s, year=1) + + start = s.timetuple() + target = t.timetuple() + + phrase = 'next %s %s' % (calendar.month_name[t.month], t.day) + + self.assertEqual(_tr(self.cal.parse(phrase, start)), + _tr((target, 1))) + + def testSpecials(self): + s = datetime.datetime.now() + t = datetime.datetime( + self.yr, self.mth, self.dy, 9, 0, 0) + datetime.timedelta(days=1) + + start = s.timetuple() + target = t.timetuple() + + self.assertExpectedResult( + self.cal.parse('tomorrow', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('next day', start), (target, 1)) + + t = datetime.datetime( + self.yr, self.mth, self.dy, 9, 0, 0) + datetime.timedelta(days=-1) + target = t.timetuple() + + self.assertExpectedResult( + self.cal.parse('yesterday', start), (target, 1)) + + t = datetime.datetime(self.yr, self.mth, self.dy, 9, 0, 0) + target = t.timetuple() + + self.assertExpectedResult(self.cal.parse('today', start), (target, 1)) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/TestSimpleOffsetsHours.py b/tests/TestSimpleOffsetsHours.py new file mode 100644 index 0000000..98dfefe --- /dev/null +++ b/tests/TestSimpleOffsetsHours.py @@ -0,0 +1,112 @@ +# -*- coding: utf-8 -*- +""" +Test parsing of 'simple' offsets +""" +from __future__ import unicode_literals + +import time +import datetime +import unittest +import parsedatetime as pdt +from . import utils + + +class test(unittest.TestCase): + + @utils.assertEqualWithComparator + def assertExpectedResult(self, result, check, **kwargs): + return utils.compareResultByTimeTuplesAndFlags(result, check, **kwargs) + + def setUp(self): + self.cal = pdt.Calendar() + (self.yr, self.mth, self.dy, self.hr, + self.mn, self.sec, self.wd, self.yd, self.isdst) = time.localtime() + + def testHoursFromNow(self): + s = datetime.datetime.now() + t = s + datetime.timedelta(hours=5) + + start = s.timetuple() + target = t.timetuple() + + self.assertExpectedResult( + self.cal.parse('5 hours from now', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('5 hour from now', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('5 hr from now', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('in 5 hours', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('in 5 hour', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('5 hours', start), (target, 2)) + self.assertExpectedResult(self.cal.parse('5 hr', start), (target, 2)) + self.assertExpectedResult(self.cal.parse('5h', start), (target, 2)) + + self.assertExpectedResult( + self.cal.parse('five hours from now', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('five hour from now', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('five hr from now', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('in five hours', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('in five hour', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('five hours', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('five hr', start), (target, 2)) + + # Test "an" + t = s + datetime.timedelta(hours=1) + target = t.timetuple() + + self.assertExpectedResult( + self.cal.parse('an hour from now', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('in an hour', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('an hour', start), (target, 2)) + self.assertExpectedResult(self.cal.parse('an hr', start), (target, 2)) + self.assertExpectedResult(self.cal.parse('an h', start), (target, 2)) + + # No match, should require a word boundary + self.assertExpectedResult(self.cal.parse('anhour', start), (start, 0)) + self.assertExpectedResult( + self.cal.parse('an hamburger', start), (start, 0)) + + def testHoursBeforeNow(self): + s = datetime.datetime.now() + t = s + datetime.timedelta(hours=-5) + + start = s.timetuple() + target = t.timetuple() + + self.assertExpectedResult( + self.cal.parse('5 hours before now', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('5 hr before now', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('5h before now', start), (target, 2)) + + self.assertExpectedResult( + self.cal.parse('five hours before now', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('five hr before now', start), (target, 2)) + + # Test "an" + t = s + datetime.timedelta(hours=-1) + target = t.timetuple() + + self.assertExpectedResult( + self.cal.parse('an hour before now', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('an hr before now', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('an h before now', start), (target, 2)) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/TestSimpleOffsetsNoon.py b/tests/TestSimpleOffsetsNoon.py new file mode 100644 index 0000000..015486f --- /dev/null +++ b/tests/TestSimpleOffsetsNoon.py @@ -0,0 +1,71 @@ +# -*- coding: utf-8 -*- +""" +Test parsing of 'simple' offsets +""" +from __future__ import unicode_literals + +import time +import datetime +import unittest +import parsedatetime as pdt +from . import utils + + +class test(unittest.TestCase): + + @utils.assertEqualWithComparator + def assertExpectedResult(self, result, check, **kwargs): + return utils.compareResultByTimeTuplesAndFlags(result, check, **kwargs) + + def setUp(self): + self.cal = pdt.Calendar() + (self.yr, self.mth, self.dy, self.hr, + self.mn, self.sec, self.wd, self.yd, self.isdst) = time.localtime() + + def testOffsetAfterNoon(self): + s = datetime.datetime(self.yr, self.mth, self.dy, 10, 0, 0) + t = datetime.datetime( + self.yr, self.mth, self.dy, 12, 0, 0) + datetime.timedelta(hours=5) + + start = s.timetuple() + target = t.timetuple() + + self.assertExpectedResult( + self.cal.parse('5 hours after 12pm', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('five hours after 12pm', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('5 hours after 12 pm', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('5 hours after 12:00pm', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('5 hours after 12:00 pm', start), (target, 2)) + # self.assertExpectedResult( + # self.cal.parse('5 hours after noon', start), (target, 2)) + # self.assertExpectedResult( + # self.cal.parse('5 hours from noon', start), (target, 2)) + + def testOffsetBeforeNoon(self): + s = datetime.datetime.now() + t = (datetime.datetime(self.yr, self.mth, self.dy, 12, 0, 0) + + datetime.timedelta(hours=-5)) + + start = s.timetuple() + target = t.timetuple() + + # self.assertExpectedResult( + # self.cal.parse('5 hours before noon', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('5 hours before 12pm', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('five hours before 12pm', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('5 hours before 12 pm', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('5 hours before 12:00pm', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('5 hours before 12:00 pm', start), (target, 2)) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/TestStartTimeFromSourceTime.py b/tests/TestStartTimeFromSourceTime.py new file mode 100644 index 0000000..2401874 --- /dev/null +++ b/tests/TestStartTimeFromSourceTime.py @@ -0,0 +1,64 @@ +# -*- coding: utf-8 -*- +""" +Test parsing of strings that are phrases with the +ptc.StartTimeFromSourceTime flag set to True +""" +from __future__ import unicode_literals + +import time +import datetime +import unittest +import parsedatetime as pdt +from . import utils + + +class test(unittest.TestCase): + + @utils.assertEqualWithComparator + def assertExpectedResult(self, result, check, **kwargs): + return utils.compareResultByTimeTuplesAndFlags(result, check, **kwargs) + + def setUp(self): + self.cal = pdt.Calendar() + self.cal.ptc.StartTimeFromSourceTime = True + (self.yr, self.mth, self.dy, self.hr, + self.mn, self.sec, self.wd, self.yd, self.isdst) = time.localtime() + + def testEndOfPhrases(self): + s = datetime.datetime.now() + + # find out what month we are currently on + # set the day to 1 and then go back a day + # to get the end of the current month + (yr, mth, dy, hr, mn, sec, _, _, _) = s.timetuple() + + m = mth + mth += 1 + if mth > 12: + mth = 1 + yr += 1 + + s = datetime.datetime(yr, m, dy, 13, 14, 15) + t = datetime.datetime( + yr, mth, 1, 13, 14, 15) + datetime.timedelta(days=-1) + + start = s.timetuple() + target = t.timetuple() + + self.assertExpectedResult(self.cal.parse('eom', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('meeting eom', start), (target, 1)) + + s = datetime.datetime.now() + + (yr, mth, dy, hr, mn, sec, wd, yd, isdst) = s.timetuple() + + s = datetime.datetime(yr, mth, 1, 13, 14, 15) + t = datetime.datetime(yr, 12, 31, 13, 14, 15) + + start = s.timetuple() + target = t.timetuple() + + self.assertExpectedResult(self.cal.parse('eoy', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('meeting eoy', start), (target, 1)) diff --git a/tests/TestUnits.py b/tests/TestUnits.py new file mode 100644 index 0000000..d572167 --- /dev/null +++ b/tests/TestUnits.py @@ -0,0 +1,187 @@ +# -*- coding: utf-8 -*- +""" +Test parsing of units +""" +from __future__ import unicode_literals + +import time +import datetime +import unittest +import parsedatetime as pdt +from . import utils + + +class test(unittest.TestCase): + + @utils.assertEqualWithComparator + def assertExpectedResult(self, result, check, **kwargs): + return utils.compareResultByTimeTuplesAndFlags(result, check, **kwargs) + + def setUp(self): + self.cal = pdt.Calendar() + (self.yr, self.mth, self.dy, self.hr, + self.mn, self.sec, self.wd, self.yd, self.isdst) = time.localtime() + + def testMinutes(self): + s = datetime.datetime.now() + t = s + datetime.timedelta(minutes=1) + h = s - datetime.timedelta(minutes=1) + + start = s.timetuple() + target = t.timetuple() + history = h.timetuple() + + self.assertExpectedResult( + self.cal.parse('1 minutes', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('1 minute', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('1 min', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('1min', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('1 m', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('1m', start), (target, 2)) + + self.assertExpectedResult( + self.cal.parse('1 minutes ago', start), (history, 2)) + self.assertExpectedResult( + self.cal.parse('1 minute ago', start), (history, 2)) + + def testHours(self): + s = datetime.datetime.now() + t = s + datetime.timedelta(hours=1) + h = s - datetime.timedelta(hours=1) + + start = s.timetuple() + target = t.timetuple() + history = h.timetuple() + + self.assertExpectedResult( + self.cal.parse('1 hour', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('1 hours', start), (target, 2)) + self.assertExpectedResult( + self.cal.parse('1 hr', start), (target, 2)) + + self.assertExpectedResult( + self.cal.parse('1 hour ago', start), (history, 2)) + self.assertExpectedResult( + self.cal.parse('1 hours ago', start), (history, 2)) + + def testDays(self): + s = datetime.datetime.now() + t = s + datetime.timedelta(days=1) + + start = s.timetuple() + target = t.timetuple() + + self.assertExpectedResult(self.cal.parse('1 day', start), (target, 1)) + self.assertExpectedResult(self.cal.parse('1 days', start), (target, 1)) + self.assertExpectedResult(self.cal.parse('1days', start), (target, 1)) + self.assertExpectedResult(self.cal.parse('1 dy', start), (target, 1)) + self.assertExpectedResult(self.cal.parse('1 d', start), (target, 1)) + + def testNegativeDays(self): + s = datetime.datetime.now() + t = s + datetime.timedelta(days=-1) + + start = s.timetuple() + target = t.timetuple() + + self.assertExpectedResult( + self.cal.parse('-1 day', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('-1 days', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('-1days', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('-1 dy', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('-1 d', start), (target, 1)) + + self.assertExpectedResult( + self.cal.parse('- 1 day', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('- 1 days', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('- 1days', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('- 1 dy', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('- 1 d', start), (target, 1)) + + self.assertExpectedResult( + self.cal.parse('1 day ago', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('1 days ago', start), (target, 1)) + + def testWeeks(self): + s = datetime.datetime.now() + t = s + datetime.timedelta(weeks=1) + h = s - datetime.timedelta(weeks=1) + + start = s.timetuple() + target = t.timetuple() + history = h.timetuple() + + self.assertExpectedResult( + self.cal.parse('1 week', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('1week', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('1 weeks', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('1 wk', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('1 w', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('1w', start), (target, 1)) + + self.assertExpectedResult( + self.cal.parse('1 week ago', start), (history, 1)) + self.assertExpectedResult( + self.cal.parse('1 weeks ago', start), (history, 1)) + + def testMonths(self): + s = datetime.datetime.now() + t = self.cal.inc(s, month=1) + h = self.cal.inc(s, month=-1) + + start = s.timetuple() + target = t.timetuple() + history = h.timetuple() + + self.assertExpectedResult( + self.cal.parse('1 month', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('1 months', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('1month', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('1 month ago', start), (history, 1)) + self.assertExpectedResult( + self.cal.parse('1 months ago', start), (history, 1)) + + def testYears(self): + s = datetime.datetime.now() + t = self.cal.inc(s, year=1) + + start = s.timetuple() + target = t.timetuple() + + self.assertExpectedResult( + self.cal.parse('1 year', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('1 years', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('1 yr', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('1 y', start), (target, 1)) + self.assertExpectedResult( + self.cal.parse('1y', start), (target, 1)) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..52c2bd7 --- /dev/null +++ b/tests/__init__.py @@ -0,0 +1,27 @@ + +""" +Unit tests for parsedatetime + +The tests can be run as a C{suite} by running:: + + nosetests + +Requires Python 3.0 or later +""" + +__author__ = 'Mike Taylor (bear@code-bear.com)' +__copyright__ = 'Copyright (c) 2004 Mike Taylor' +__license__ = 'Apache v2.0' +__version__ = '1.0.0' +__contributors__ = ['Darshana Chhajed', + 'Michael Lim (lim.ck.michael@gmail.com)', + 'Bernd Zeimetz (bzed@debian.org)', + ] +import logging + +log = logging.getLogger('parsedatetime') +echoHandler = logging.StreamHandler() +echoFormatter = logging.Formatter('%(levelname)-8s %(message)s') +log.addHandler(echoHandler) + +# log.setLevel(logging.DEBUG) diff --git a/parsedatetime/tests/__init__.py b/tests/utils.py similarity index 63% rename from parsedatetime/tests/__init__.py rename to tests/utils.py index d57da4f..3460c97 100644 --- a/parsedatetime/tests/__init__.py +++ b/tests/utils.py @@ -1,30 +1,9 @@ - +# -*- coding: utf-8 -*- """ -Unit tests for parsedatetime - -The tests can be run as a C{suite} by running:: - - python run_tests.py parsedatetime - -Requires Python 3.0 or later +Internal helper functions for unit tests of parsedatetime """ +from __future__ import unicode_literals -__author__ = 'Mike Taylor (bear@code-bear.com)' -__copyright__ = 'Copyright (c) 2004 Mike Taylor' -__license__ = 'Apache v2.0' -__version__ = '1.0.0' -__contributors__ = [ 'Darshana Chhajed', - 'Michael Lim (lim.ck.michael@gmail.com)', - 'Bernd Zeimetz (bzed@debian.org)', - ] -import logging - -log = logging.getLogger('parsedatetime') -echoHandler = logging.StreamHandler() -echoFormatter = logging.Formatter('%(levelname)-8s %(message)s') -log.addHandler(echoHandler) - -#log.setLevel(logging.DEBUG) def assertEqualWithComparator(comparator): """ @@ -32,21 +11,26 @@ def assertEqualWithComparator(comparator): result against a target value. Shows the result and the target in the failure message. """ + def decoratedComparator(self, result, check, **kwargs): equal = comparator(self, result, check, **kwargs) - failureMessage = 'Result does not match target value\n\n\tResult:\n\t%s\n\n\tExpected:\n\t%s' + failureMessage = ('Result does not match target value\n\n\t' + 'Result:\n\t%s\n\n\tExpected:\n\t%s') if not equal: self.fail(failureMessage % (result, check)) return decoratedComparator + def compareResultByTimeTuplesAndFlags(result, check, dateOnly=False): """ Ensures that flags are an exact match and time tuples a close match when given data in the format ((timetuple), flag) """ - return _compareTimeTuples(result[0], check[0], dateOnly) and _compareFlags(result[1], check[1]) + return (_compareTimeTuples(result[0], check[0], dateOnly) and + _compareFlags(result[1], check[1])) + def compareResultByFlags(result, check, dateOnly=False): """ @@ -55,12 +39,16 @@ def compareResultByFlags(result, check, dateOnly=False): """ return _compareFlags(result[1], check[1]) + def compareResultByTimeTupleRangesAndFlags(result, check, dateOnly=False): """ Ensures that flags are an exact match and time tuples a close match when given data in the format ((timetuple), (timetuple), flag) """ - return _compareTimeTuples(result[0], check[0], dateOnly) and _compareTimeTuples(result[1], check[1], dateOnly) and _compareFlags(result[2], check[2]) + return (_compareTimeTuples(result[0], check[0], dateOnly) and + _compareTimeTuples(result[1], check[1], dateOnly) and + _compareFlags(result[2], check[2])) + def _compareTimeTuples(target, value, dateOnly=False): """ @@ -78,5 +66,6 @@ def _compareTimeTuples(target, value, dateOnly=False): return ((t_yr == v_yr) and (t_mth == v_mth) and (t_dy == v_dy) and (t_hr == v_hr) and (t_min == v_min)) + def _compareFlags(result, check): return (result == check) diff --git a/tox.ini b/tox.ini index 5bf7ad1..b8bf26d 100644 --- a/tox.ini +++ b/tox.ini @@ -4,6 +4,6 @@ envlist = py27, py33, py34 [testenv] deps = PyICU -commands= {envpython} run_tests.py +commands= nosetests {posargs} setenv = PYTHONWARNINGS=always::DeprecationWarning