Skip to content

Commit

Permalink
Merge pull request #272 from bear/update-tests-to-use-contexts
Browse files Browse the repository at this point in the history
Update tests to use contexts
  • Loading branch information
bear authored Oct 11, 2021
2 parents 44f1610 + 8a109c7 commit 54e4443
Show file tree
Hide file tree
Showing 23 changed files with 899 additions and 548 deletions.
2 changes: 1 addition & 1 deletion parsedatetime/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ class Calendar(object):
The text can either be 'normal' date values or it can be human readable.
"""

def __init__(self, constants=None, version=VERSION_FLAG_STYLE):
def __init__(self, constants=None, version=VERSION_CONTEXT_STYLE):
"""
Default constructor for the L{Calendar} class.
Expand Down
2 changes: 0 additions & 2 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
[pytest]
norecursedirs = .tox
verbosity = 2
python_files=Test*.py
with-coverage = true
47 changes: 24 additions & 23 deletions tests/TestAlternativeAbbreviations.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import unittest
import parsedatetime as pdt
from parsedatetime.pdt_locales import get_icu
from parsedatetime.context import pdtContext
from . import utils


Expand Down Expand Up @@ -45,50 +46,50 @@ def testDaysOfWeek(self):

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))
self.assertExpectedResult(self.cal.parse('sunday', start), (target, pdtContext(pdtContext.ACU_DAY)))
self.assertExpectedResult(self.cal.parse('sun', start), (target, pdtContext(pdtContext.ACU_DAY)))
self.assertExpectedResult(self.cal.parse('su', start), (target, pdtContext(pdtContext.ACU_DAY)))

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))
self.assertExpectedResult(self.cal.parse('Monday', start), (target, pdtContext(pdtContext.ACU_DAY)))
self.assertExpectedResult(self.cal.parse('mon', start), (target, pdtContext(pdtContext.ACU_DAY)))
self.assertExpectedResult(self.cal.parse('mond', start), (target, pdtContext(pdtContext.ACU_DAY)))

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))
self.cal.parse('tuesday', start), (target, pdtContext(pdtContext.ACU_DAY)))
self.assertExpectedResult(self.cal.parse('tues', start), (target, pdtContext(pdtContext.ACU_DAY)))
self.assertExpectedResult(self.cal.parse('tue', start), (target, pdtContext(pdtContext.ACU_DAY)))

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))
self.cal.parse('wednesday', start), (target, pdtContext(pdtContext.ACU_DAY)))
self.assertExpectedResult(self.cal.parse('wedn', start), (target, pdtContext(pdtContext.ACU_DAY)))
self.assertExpectedResult(self.cal.parse('wed', start), (target, pdtContext(pdtContext.ACU_DAY)))

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))
self.cal.parse('thursday', start), (target, pdtContext(pdtContext.ACU_DAY)))
self.assertExpectedResult(self.cal.parse('thu', start), (target, pdtContext(pdtContext.ACU_DAY)))
self.assertExpectedResult(self.cal.parse('thur', start), (target, pdtContext(pdtContext.ACU_DAY)))
self.assertExpectedResult(self.cal.parse('thurs', start), (target, pdtContext(pdtContext.ACU_DAY)))

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))
self.assertExpectedResult(self.cal.parse('friday', start), (target, pdtContext(pdtContext.ACU_DAY)))
self.assertExpectedResult(self.cal.parse('fri', start), (target, pdtContext(pdtContext.ACU_DAY)))
self.assertExpectedResult(self.cal.parse('frid', start), (target, pdtContext(pdtContext.ACU_DAY)))

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))
self.cal.parse('saturday', start), (target, pdtContext(pdtContext.ACU_DAY)))
self.assertExpectedResult(self.cal.parse('sat', start), (target, pdtContext(pdtContext.ACU_DAY)))
self.assertExpectedResult(self.cal.parse('sa', start), (target, pdtContext(pdtContext.ACU_DAY)))

def testMonths(self):
start = datetime.datetime(
Expand Down Expand Up @@ -122,4 +123,4 @@ def testMonths(self):
for dateText in dates.split("|"):
# print dateText
self.assertExpectedResult(
self.cal.parse(dateText, start), (expected_date, 1))
self.cal.parse(dateText, start), (expected_date, pdtContext(pdtContext.ACU_MONTH)))
60 changes: 30 additions & 30 deletions tests/TestAustralianLocale.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import datetime
import unittest
import parsedatetime as pdt
from parsedatetime.context import pdtContext
from . import utils


Expand Down Expand Up @@ -34,41 +35,41 @@ def testTimes(self):
self.yr, self.mth, self.dy, 23, 0, 0).timetuple()

self.assertExpectedResult(
self.cal.parse('11:00:00 PM', start), (target, 2))
self.cal.parse('11:00:00 PM', start), (target, pdtContext(pdtContext.ACU_HOUR | pdtContext.ACU_MIN | pdtContext.ACU_SEC)))
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.cal.parse('11:00 PM', start), (target, pdtContext(pdtContext.ACU_HOUR | pdtContext.ACU_MIN)))
self.assertExpectedResult(self.cal.parse('11 PM', start), (target, pdtContext(pdtContext.ACU_HOUR)))
self.assertExpectedResult(self.cal.parse('11PM', start), (target, pdtContext(pdtContext.ACU_HOUR)))
self.assertExpectedResult(self.cal.parse('2300', start), (target, pdtContext(pdtContext.ACU_HOUR | pdtContext.ACU_MIN)))
self.assertExpectedResult(self.cal.parse('23:00', start), (target, pdtContext(pdtContext.ACU_HOUR | pdtContext.ACU_MIN)))
self.assertExpectedResult(self.cal.parse('11p', start), (target, pdtContext(pdtContext.ACU_HOUR)))
self.assertExpectedResult(self.cal.parse('11pm', start), (target, pdtContext(pdtContext.ACU_HOUR)))

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.cal.parse('11:00:00 AM', start), (target, pdtContext(pdtContext.ACU_HOUR | pdtContext.ACU_MIN | pdtContext.ACU_SEC)))
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.cal.parse('11:00 AM', start), (target, pdtContext(pdtContext.ACU_HOUR | pdtContext.ACU_MIN)))
self.assertExpectedResult(self.cal.parse('11 AM', start), (target, pdtContext(pdtContext.ACU_HOUR)))
self.assertExpectedResult(self.cal.parse('11AM', start), (target, pdtContext(pdtContext.ACU_HOUR)))
self.assertExpectedResult(self.cal.parse('1100', start), (target, pdtContext(pdtContext.ACU_HOUR | pdtContext.ACU_MIN)))
self.assertExpectedResult(self.cal.parse('11:00', start), (target, pdtContext(pdtContext.ACU_HOUR | pdtContext.ACU_MIN)))
self.assertExpectedResult(self.cal.parse('11a', start), (target, pdtContext(pdtContext.ACU_HOUR)))
self.assertExpectedResult(self.cal.parse('11am', start), (target, pdtContext(pdtContext.ACU_HOUR)))

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('730', start), (target, pdtContext(pdtContext.ACU_HOUR | pdtContext.ACU_MIN)))
self.assertExpectedResult(self.cal.parse('0730', start), (target, pdtContext(pdtContext.ACU_HOUR | pdtContext.ACU_MIN)))

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))
self.assertExpectedResult(self.cal.parse('1730', start), (target, pdtContext(pdtContext.ACU_HOUR | pdtContext.ACU_MIN)))
self.assertExpectedResult(self.cal.parse('173000', start), (target, pdtContext(pdtContext.ACU_HOUR | pdtContext.ACU_MIN | pdtContext.ACU_SEC)))

def testDates(self):
start = datetime.datetime(
Expand All @@ -77,15 +78,15 @@ def testDates(self):
2006, 8, 25, self.hr, self.mn, self.sec).timetuple()

self.assertExpectedResult(
self.cal.parse('25-08-2006', start), (target, 1))
self.cal.parse('25-08-2006', start), (target, pdtContext(pdtContext.ACU_YEAR | pdtContext.ACU_MONTH | pdtContext.ACU_DAY)))
self.assertExpectedResult(
self.cal.parse('25/08/2006', start), (target, 1))
self.cal.parse('25/08/2006', start), (target, pdtContext(pdtContext.ACU_YEAR | pdtContext.ACU_MONTH | pdtContext.ACU_DAY)))
self.assertExpectedResult(
self.cal.parse('25.08.2006', start), (target, 1))
self.cal.parse('25.08.2006', start), (target, pdtContext(pdtContext.ACU_YEAR | pdtContext.ACU_MONTH | pdtContext.ACU_DAY)))
self.assertExpectedResult(
self.cal.parse('25-8-06', start), (target, 1))
self.cal.parse('25-8-06', start), (target, pdtContext(pdtContext.ACU_YEAR | pdtContext.ACU_MONTH | pdtContext.ACU_DAY)))
self.assertExpectedResult(
self.cal.parse('25/8/06', start), (target, 1))
self.cal.parse('25/8/06', start), (target, pdtContext(pdtContext.ACU_YEAR | pdtContext.ACU_MONTH | pdtContext.ACU_DAY)))

if self.mth > 8 or (self.mth == 8 and self.dy > 25):
target = datetime.datetime(
Expand All @@ -94,11 +95,10 @@ def testDates(self):
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))
self.assertExpectedResult(self.cal.parse('25/8', start), (target, pdtContext(pdtContext.ACU_MONTH | pdtContext.ACU_DAY)))
self.assertExpectedResult(self.cal.parse('25.8', start), (target, pdtContext(pdtContext.ACU_MONTH | pdtContext.ACU_DAY)))
self.assertExpectedResult(self.cal.parse('25-08', start), (target, pdtContext(pdtContext.ACU_MONTH | pdtContext.ACU_DAY)))
self.assertExpectedResult(self.cal.parse('25/08', start), (target, pdtContext(pdtContext.ACU_MONTH | pdtContext.ACU_DAY)))


if __name__ == "__main__":
Expand Down
Loading

0 comments on commit 54e4443

Please sign in to comment.