Skip to content

Commit

Permalink
Merge pull request #142 from philiptzou/pull-request-icu-test
Browse files Browse the repository at this point in the history
Fixed all PyICU test failure
  • Loading branch information
bear committed Nov 7, 2015
2 parents b7b5663 + ae04ff7 commit cd5847e
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 54 deletions.
8 changes: 6 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@ python:
- "2.7"
- "3.5"
- "pypy"
env:
- WITHPYICU=yes
- WITHPYICU=no

before_install:
- pip install codecov

install:
- travis_retry pip install .
- pip install -r requirements.txt
- if [[ $WITHPYICU == 'yes' ]]; then pip install PyICU; fi

script:
- nosetests
- if [[ $TRAVIS_PYTHON_VERSION == 'pypy' && $WITHPYICU == 'yes' ]]; then echo; else nosetests; fi

after_success:
- codecov
- if [[ $TRAVIS_PYTHON_VERSION == 'pypy' && $WITHPYICU == 'yes' ]]; then echo; else codecov; fi
7 changes: 3 additions & 4 deletions parsedatetime/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2378,7 +2378,7 @@ def _getLocaleDataAdjusted(localeData):
"""
adjusted = []
for d in localeData:
if len(d.split('|')) > 0:
if '|' in d:
adjusted += d.split("|")
else:
adjusted.append(d)
Expand Down Expand Up @@ -2421,9 +2421,8 @@ def _getLocaleDataAdjusted(localeData):
def _buildOffsets(offsetDict, localeData, indexStart):
o = indexStart
for key in localeData:
key_split = key.split('|')
if len(key_split) > 0:
for k in key_split:
if '|' in key:
for k in key.split('|'):
offsetDict[k] = o
else:
offsetDict[key] = o
Expand Down
2 changes: 1 addition & 1 deletion parsedatetime/pdt_locales/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
]

shortWeekdays = [
'mon', 'tues', 'wed', 'thu', 'fri', 'sat', 'sun',
'mon', 'tues|tue', 'wed', 'thu', 'fri', 'sat', 'sun',
]

Months = [
Expand Down
22 changes: 18 additions & 4 deletions parsedatetime/pdt_locales/icu.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ def icu_object(mapping):
return type('_icu', (object,), mapping)


def merge_weekdays(base_wd, icu_wd):
result = []
for left, right in zip(base_wd, icu_wd):
if left == right:
result.append(left)
continue
left = set(left.split('|'))
right = set(right.split('|'))
result.append('|'.join(left | right))
return result


def get_icu(locale):
from . import base
result = dict([(key, getattr(base, key))
Expand All @@ -40,7 +52,7 @@ def get_icu(locale):

# grab spelled out format of all numbers from 0 to 100
rbnf = pyicu.RuleBasedNumberFormat(pyicu.URBNFRuleSetTag.SPELLOUT, icu)
result['numbers'] = dict([(rbnf.format(i), i) for i in range(0, 100)])
result['numbers'].update([(rbnf.format(i), i) for i in range(0, 100)])

symbols = result['symbols'] = pyicu.DateFormatSymbols(icu)

Expand All @@ -50,8 +62,10 @@ def get_icu(locale):
swd = [sw.lower() for sw in symbols.getShortWeekdays()[1:]]

# store them in our list with Monday first (ICU puts Sunday first)
result['Weekdays'] = wd[1:] + wd[0:1]
result['shortWeekdays'] = swd[1:] + swd[0:1]
result['Weekdays'] = merge_weekdays(result['Weekdays'],
wd[1:] + wd[0:1])
result['shortWeekdays'] = merge_weekdays(result['shortWeekdays'],
swd[1:] + swd[0:1])
result['Months'] = [m.lower() for m in symbols.getMonths()]
result['shortMonths'] = [sm.lower() for sm in symbols.getShortMonths()]
keys = ['full', 'long', 'medium', 'short']
Expand Down Expand Up @@ -107,7 +121,7 @@ def get_icu(locale):
pm = s.replace('45', '').replace(ts, '').strip()

result['timeSep'] = [ts]
result['meridian'] = [am, pm]
result['meridian'] = [am, pm] if am and pm else []

o = result['icu_df']['short']
s = o.format(datetime.datetime(2003, 10, 30, 11, 45))
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def extract_metaitem(meta):
packages=find_packages(exclude=['tests', 'docs']),
platforms=['Any'],
install_requires=read('requirements.txt').split('\n'),
tests_require=('PyICU','nose'),
long_description=read('README.rst'),
test_suite='nose.collector',
classifiers=[
Expand Down
65 changes: 30 additions & 35 deletions tests/TestComplexDateTimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from __future__ import unicode_literals

import time
import datetime
from datetime import datetime
import unittest
import parsedatetime as pdt

Expand All @@ -24,52 +24,47 @@ def setUp(self):
self.mn, self.sec, self.wd, self.yd, self.isdst) = time.localtime()

def testDate3ConfusedHourAndYear(self):
start = datetime.datetime(
start = datetime(
self.yr, self.mth, self.dy, self.hr, self.mn, self.sec).timetuple()
self.assertExpectedResult(
self.cal.parse('Aug 05, 2014 4:15 AM'),
(datetime.datetime(2014, 8, 5, 4, 15, 0).timetuple(), 3))
(datetime(2014, 8, 5, 4, 15, 0).timetuple(), 3))
self.assertExpectedResult(
self.cal.parse('Aug 05, 2003 3:15 AM'),
(datetime.datetime(2003, 8, 5, 3, 15, 0).timetuple(), 3))
(datetime(2003, 8, 5, 3, 15, 0).timetuple(), 3))
self.assertExpectedResult(
self.cal.parse('Aug 05, 2003 03:15 AM'),
(datetime.datetime(2003, 8, 5, 3, 15, 0).timetuple(), 3))
(datetime(2003, 8, 5, 3, 15, 0).timetuple(), 3))
self.assertExpectedResult(
self.cal.parse('June 30th 12PM', start),
(datetime.datetime(self.yr
if (self.mth < 6 and
self.dy < 30 and self.hr < 12)
else self.yr + 1,
6, 30, 12, 0, 0).timetuple(), 3))
(datetime(self.yr
if (self.mth < 6 and
self.dy < 30 and self.hr < 12)
else self.yr + 1,
6, 30, 12, 0, 0).timetuple(), 3))
self.assertExpectedResult(
self.cal.parse('June 30th 12:00', start),
(datetime.datetime(self.yr
if (self.mth < 6 and
self.dy < 30 and self.hr < 12)
else self.yr + 1,
6, 30, 12, 0, 0).timetuple(), 3))
(datetime(self.yr
if datetime.now() < datetime(self.yr, 6, 30, 12)
else self.yr + 1,
6, 30, 12, 0, 0).timetuple(), 3))
self.assertExpectedResult(
self.cal.parse('December 30th 23PM', start),
(datetime.datetime(self.yr
if (self.mth < 12 and
self.dy < 30 and self.hr < 23)
else self.yr + 1,
12, 30, 23, 0, 0).timetuple(), 3))
(datetime(self.yr
if datetime.now() < datetime(self.yr, 12, 30, 23)
else self.yr + 1,
12, 30, 23, 0, 0).timetuple(), 3))
self.assertExpectedResult(
self.cal.parse('December 30th 23:02', start),
(datetime.datetime(self.yr
if (self.mth < 12 and
self.dy < 30 and
self.hr < 23 and
self.mn < 2)
else self.yr + 1,
12, 30, 23, 2, 0).timetuple(), 3))
(datetime(self.yr
if datetime.now() < datetime(self.yr, 12, 30, 23, 2)
else self.yr + 1,
12, 30, 23, 2, 0).timetuple(), 3))

def testDates(self):
start = datetime.datetime(
start = datetime(
self.yr, self.mth, self.dy, self.hr, self.mn, self.sec).timetuple()
target = datetime.datetime(2006, 8, 25, 17, 0, 0).timetuple()
target = datetime(2006, 8, 25, 17, 0, 0).timetuple()

self.assertExpectedResult(
self.cal.parse('08/25/2006 5pm', start), (target, 3))
Expand All @@ -93,9 +88,9 @@ def testDates(self):
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()
target = datetime(self.yr + 1, 8, 5, 17, 0, 0).timetuple()
else:
target = datetime.datetime(self.yr, 8, 5, 17, 0, 0).timetuple()
target = datetime(self.yr, 8, 5, 17, 0, 0).timetuple()

self.assertExpectedResult(
self.cal.parse('8/5 at 5pm', start), (target, 3))
Expand Down Expand Up @@ -126,9 +121,9 @@ def testDates(self):
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()
target = datetime(self.yr + 1, 8, 5, 12, 0, 0).timetuple()
else:
target = datetime.datetime(self.yr, 8, 5, 12, 0, 0).timetuple()
target = datetime(self.yr, 8, 5, 12, 0, 0).timetuple()

self.assertExpectedResult(
self.cal.parse('August 5th 12:00', start), (target, 3))
Expand All @@ -142,10 +137,10 @@ def testDates(self):
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(
target = datetime(
self.yr + 1, 8, 22, 3, 26, 0).timetuple()
else:
target = datetime.datetime(self.yr, 8, 22, 3, 26, 0).timetuple()
target = datetime(self.yr, 8, 22, 3, 26, 0).timetuple()

self.assertExpectedResult(
self.cal.parse('August 22nd 3:26', start), (target, 3))
Expand Down
6 changes: 4 additions & 2 deletions tests/TestLocaleBase.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
pdtLocale_fr = get_icu('fr_FR')
pdtLocale_fr.dayOffsets.update({"aujourd'hui": 0, 'demain': 1, 'hier': -1})


@unittest.skipIf(not pdtLocale_fr, "French Locale not found")
class test(unittest.TestCase):

Expand Down Expand Up @@ -84,9 +85,9 @@ def testDates(self):
self.assertExpectedResult(
self.cal.parse('25/8/06', start), (target, 1))
self.assertExpectedResult(
self.cal.parse('ao\xfbt 25, 2006', start), (target, 1))
self.cal.parse('août 25, 2006', start), (target, 1))
self.assertExpectedResult(
self.cal.parse('ao\xfbt 25 2006', start), (target, 1))
self.cal.parse('août 25 2006', start), (target, 1))

if self.mth > 8 or (self.mth == 8 and self.dy > 25):
target = datetime.datetime(
Expand Down Expand Up @@ -125,6 +126,7 @@ def testWeekDays(self):
self.ptc.CurrentDOWParseStyle = o1
self.ptc.DOWParseStyle = o2


@unittest.skipIf(not pdtLocale_fr, "French Locale not found")
class TestDayOffsets(test):
# test how Aujourd'hui/Demain/Hier are parsed
Expand Down
10 changes: 7 additions & 3 deletions tests/TestSimpleDateTimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,10 +498,13 @@ def testWordBoundaries(self):
keywords = []
loc = self.cal.ptc.locale

def flattenWeekdays(wds):
return sum([wd.split('|') for wd in wds], [])

# Test all known keywords for the locale
keywords.extend(loc.meridian)
keywords.extend(loc.Weekdays)
keywords.extend(loc.shortWeekdays)
keywords.extend(flattenWeekdays(loc.Weekdays))
keywords.extend(flattenWeekdays(loc.shortWeekdays))
keywords.extend(loc.Months)
keywords.extend(loc.shortMonths)
keywords.extend(loc.numbers.keys())
Expand All @@ -521,7 +524,8 @@ def testWordBoundaries(self):
for keyword in keywords:
phrase = '1 %sfoo' % keyword
self.assertExpectedResult(
self.cal.parse(phrase, start), (target, 0))
self.cal.parse(phrase, start), (target, 0),
'Result does not match target value: %s' % repr(phrase))

def testYearParseStyle(self):
config = pdt.Constants()
Expand Down
7 changes: 4 additions & 3 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ def assertEqualWithComparator(comparator):
failure message.
"""

def decoratedComparator(self, result, check, **kwargs):
def decoratedComparator(self, result, check, errMsg=None, **kwargs):
errMsg = errMsg or 'Result does not match target value'
equal = comparator(self, result, check, **kwargs)
failureMessage = ('Result does not match target value\n\n\t'
failureMessage = ('%s\n\n\t'
'Result:\n\t%s\n\n\tExpected:\n\t%s')

if not equal:
self.fail(failureMessage % (result, check))
self.fail(failureMessage % (errMsg, result, check))

return decoratedComparator

Expand Down

0 comments on commit cd5847e

Please sign in to comment.