Skip to content

Commit e3a849e

Browse files
author
How Si Wei
committed
Update tests
1 parent 170d08b commit e3a849e

File tree

2 files changed

+90
-53
lines changed

2 files changed

+90
-53
lines changed

pandas/tests/tseries/offsets/test_offsets.py

+89-53
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from datetime import date, datetime, timedelta
1+
from datetime import date, time, datetime, timedelta
22
from distutils.version import LooseVersion
33

44
import numpy as np
@@ -767,37 +767,53 @@ def setup_method(self, method):
767767
self.offset10 = BusinessHour(n=-1, start=['23:00', '13:00'],
768768
end=['02:00', '17:00'])
769769

770-
def test_constructor_errors(self):
771-
from datetime import time as dt_time
772-
with pytest.raises(ValueError,
773-
match='time data must be specified only with hour '
774-
'and minute'):
775-
BusinessHour(start=dt_time(11, 0, 5))
776-
with pytest.raises(ValueError,
777-
match="time data must match '%H:%M' format"):
778-
BusinessHour(start='AAA')
779-
with pytest.raises(ValueError,
780-
match="time data must match '%H:%M' format"):
781-
BusinessHour(start='14:00:05')
782-
with pytest.raises(ValueError,
783-
match='Must include at least 1 start time'):
784-
BusinessHour(start=[])
785-
with pytest.raises(ValueError,
786-
match='Must include at least 1 end time'):
787-
BusinessHour(end=[])
770+
@pytest.mark.parametrize("start,end,match", [
771+
(
772+
time(11, 0, 5),
773+
'17:00',
774+
"time data must be specified only with hour and minute"
775+
),
776+
(
777+
'AAA',
778+
'17:00',
779+
"time data must match '%H:%M' format"
780+
),
781+
(
782+
'14:00:05',
783+
'17:00',
784+
"time data must match '%H:%M' format"
785+
),
786+
(
787+
[],
788+
'17:00',
789+
"Must include at least 1 start time"
790+
),
791+
(
792+
'09:00',
793+
[],
794+
"Must include at least 1 end time"
795+
),
796+
(
797+
['09:00', '11:00'],
798+
'17:00',
799+
"number of starting time and ending time must be the same"
800+
),
801+
(
802+
['09:00', '11:00'],
803+
['10:00'],
804+
"number of starting time and ending time must be the same"
805+
),
806+
(
807+
['09:00', '11:00'],
808+
['12:00', '20:00'],
809+
r"invalid starting and ending time\(s\): opening hours should not "
810+
"touch or overlap with one another"
811+
),
812+
])
813+
def test_constructor_errors(self, start, end, match):
788814
with pytest.raises(ValueError,
789-
match='number of starting time and ending time '
790-
'must be the same'):
791-
BusinessHour(start=['09:00', '11:00'])
792-
with pytest.raises(ValueError,
793-
match='number of starting time and ending time '
794-
'must be the same'):
795-
BusinessHour(start=['09:00', '11:00'], end=['10:00'])
796-
with pytest.raises(ValueError,
797-
match=r'invalid starting and ending time\(s\): '
798-
'opening hours should not touch or overlap with '
799-
'one another'):
800-
BusinessHour(start=['09:00', '11:00'], end=['12:00', '20:00'])
815+
match=match):
816+
BusinessHour(start=start, end=end)
801817

802818
def test_different_normalize_equals(self):
803819
# GH#21404 changed __eq__ to return False when `normalize` doesnt match
@@ -827,30 +843,50 @@ def test_with_offset(self):
827843
assert self.d + BusinessHour() * 3 == expected
828844
assert self.d + BusinessHour(n=3) == expected
829845

830-
def test_eq(self):
831-
for offset in [self.offset1, self.offset2, self.offset3, self.offset4,
832-
self.offset8, self.offset9, self.offset10]:
833-
assert offset == offset
846+
@pytest.mark.parametrize("offset_name", [
847+
"offset1",
848+
"offset2",
849+
"offset3",
850+
"offset4",
851+
"offset8",
852+
"offset9",
853+
"offset10"
854+
])
855+
def test_eq_attribute(self, offset_name):
856+
offset = getattr(self, offset_name)
857+
assert offset == offset
858+
859+
@pytest.mark.parametrize("offset1,offset2", [
860+
(BusinessHour(start='09:00'), BusinessHour()),
861+
(BusinessHour(start=['23:00', '13:00'], end=['12:00', '17:00']),
862+
BusinessHour(start=['13:00', '23:00'], end=['17:00', '12:00'])),
863+
])
864+
def test_eq(self, offset1, offset2):
865+
assert offset1 == offset2
834866

835-
assert BusinessHour() != BusinessHour(-1)
836-
assert BusinessHour(start='09:00') == BusinessHour()
837-
assert BusinessHour(start='09:00') != BusinessHour(start='09:01')
838-
assert (BusinessHour(start='09:00', end='17:00') !=
839-
BusinessHour(start='17:00', end='09:01'))
840-
841-
assert (BusinessHour(start=['23:00', '13:00'],
842-
end=['12:00', '17:00']) ==
843-
BusinessHour(start=['13:00', '23:00'],
844-
end=['17:00', '12:00']))
845-
assert (BusinessHour(start=['13:00', '23:00'],
846-
end=['18:00', '07:00']) !=
847-
BusinessHour(start=['13:00', '23:00'],
848-
end=['17:00', '12:00']))
867+
@pytest.mark.parametrize("offset1,offset2", [
868+
(BusinessHour(), BusinessHour(-1)),
869+
(BusinessHour(start='09:00'), BusinessHour(start='09:01')),
870+
(BusinessHour(start='09:00', end='17:00'),
871+
BusinessHour(start='17:00', end='09:01')),
872+
(BusinessHour(start=['13:00', '23:00'], end=['18:00', '07:00']),
873+
BusinessHour(start=['13:00', '23:00'], end=['17:00', '12:00'])),
874+
])
875+
def test_neq(self, offset1, offset2):
876+
assert offset1 != offset2
849877

850-
def test_hash(self):
851-
for offset in [self.offset1, self.offset2, self.offset3, self.offset4,
852-
self.offset8, self.offset9, self.offset10]:
853-
assert hash(offset) == hash(offset)
878+
@pytest.mark.parametrize("offset_name", [
879+
"offset1",
880+
"offset2",
881+
"offset3",
882+
"offset4",
883+
"offset8",
884+
"offset9",
885+
"offset10"
886+
])
887+
def test_hash(self, offset_name):
888+
offset = getattr(self, offset_name)
889+
assert offset == offset
854890

855891
def test_call(self):
856892
assert self.offset1(self.d) == datetime(2014, 7, 1, 11)

pandas/tseries/offsets.py

+1
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,7 @@ def __init__(self, start='09:00', end='17:00', offset=timedelta(0)):
605605

606606
# sort starting and ending time by starting time
607607
index = np.argsort(start)
608+
# convert to tuple so that start and end are hashable
608609
start = tuple(start[index])
609610
end = tuple(end[index])
610611

0 commit comments

Comments
 (0)