tests: turn strftime test function into an unit test

This commit is contained in:
Frédéric Péters 2014-12-24 13:19:46 +01:00
parent 7649121e8c
commit 21c8296d30
2 changed files with 32 additions and 36 deletions

31
tests/test_strftime.py Normal file
View File

@ -0,0 +1,31 @@
import datetime
from wcs.qommon.strftime import strftime
def test():
# Make sure that the day names are in order
# from 1/1/1800 until 1/1/2100
s = strftime("%Y has the same days as 1980 and 2008",
datetime.date(1800, 9, 23))
if s != "1800 has the same days as 1980 and 2008":
raise AssertionError(s)
days = []
for i in range(1, 10):
days.append(datetime.date(2000, 1, i).strftime("%A"))
nextday = {}
for i in range(8):
nextday[days[i]] = days[i+1]
startdate = datetime.date(1800, 1, 1)
enddate = datetime.date(2100, 1, 1)
prevday = strftime('%A', startdate)
one_day = datetime.timedelta(1)
testdate = startdate + one_day
while testdate < enddate:
day = strftime('%A', testdate)
if nextday[prevday] != day:
raise AssertionError(str(testdate))
prevday = day
testdate = testdate + one_day

View File

@ -24,7 +24,7 @@ def _findall(text, substr):
def strftime(fmt, dt):
if not dt:
return ''
if not isinstance(dt, datetime.datetime):
if not isinstance(dt, datetime.datetime) and not isinstance(dt, datetime.date):
# consider it a 9 elements tuple
dt = datetime.datetime(*dt[:6])
# WARNING: known bug with "%s", which is the number
@ -60,38 +60,3 @@ def strftime(fmt, dt):
for site in sites:
s = s[:site] + syear + s[site+4:]
return s
# Make sure that the day names are in order
# from 1/1/1 until August 2000
def test():
s = strftime(datetime.date(1800, 9, 23),
"%Y has the same days as 1980 and 2008")
if s != "1800 has the same days as 1980 and 2008":
raise AssertionError(s)
print "Testing all day names from 0001/01/01 until 2000/08/01"
days = []
for i in range(1, 10):
days.append(datetime.date(2000, 1, i).strftime("%A"))
nextday = {}
for i in range(8):
nextday[days[i]] = days[i+1]
startdate = datetime.date(1, 1, 1)
enddate = datetime.date(2000, 8, 1)
prevday = strftime(startdate, "%A")
one_day = datetime.timedelta(1)
testdate = startdate + one_day
while testdate < enddate:
if (testdate.day == 1 and testdate.month == 1 and
(testdate.year % 100 == 0)):
print testdate.year
day = strftime(testdate, "%A")
if nextday[prevday] != day:
raise AssertionError(str(testdate))
prevday = day
testdate = testdate + one_day
if __name__ == "__main__":
test()