Skip to content

Commit ec062e0

Browse files
How Si Weihowsiwei
How Si Wei
authored andcommitted
Update tests
1 parent 170d08b commit ec062e0

File tree

2 files changed

+90
-52
lines changed

2 files changed

+90
-52
lines changed

pandas/tests/tseries/offsets/test_offsets.py

+89-52
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from datetime import date, datetime, timedelta
2+
from datetime import time as dt_time
23
from distutils.version import LooseVersion
34

45
import numpy as np
@@ -767,37 +768,53 @@ def setup_method(self, method):
767768
self.offset10 = BusinessHour(n=-1, start=['23:00', '13:00'],
768769
end=['02:00', '17:00'])
769770

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

802819
def test_different_normalize_equals(self):
803820
# GH#21404 changed __eq__ to return False when `normalize` doesnt match
@@ -827,30 +844,50 @@ def test_with_offset(self):
827844
assert self.d + BusinessHour() * 3 == expected
828845
assert self.d + BusinessHour(n=3) == expected
829846

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
847+
@pytest.mark.parametrize("offset_name", [
848+
"offset1",
849+
"offset2",
850+
"offset3",
851+
"offset4",
852+
"offset8",
853+
"offset9",
854+
"offset10"
855+
])
856+
def test_eq_attribute(self, offset_name):
857+
offset = getattr(self, offset_name)
858+
assert offset == offset
859+
860+
@pytest.mark.parametrize("offset1,offset2", [
861+
(BusinessHour(start='09:00'), BusinessHour()),
862+
(BusinessHour(start=['23:00', '13:00'], end=['12:00', '17:00']),
863+
BusinessHour(start=['13:00', '23:00'], end=['17:00', '12:00'])),
864+
])
865+
def test_eq(self, offset1, offset2):
866+
assert offset1 == offset2
834867

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']))
868+
@pytest.mark.parametrize("offset1,offset2", [
869+
(BusinessHour(), BusinessHour(-1)),
870+
(BusinessHour(start='09:00'), BusinessHour(start='09:01')),
871+
(BusinessHour(start='09:00', end='17:00'),
872+
BusinessHour(start='17:00', end='09:01')),
873+
(BusinessHour(start=['13:00', '23:00'], end=['18:00', '07:00']),
874+
BusinessHour(start=['13:00', '23:00'], end=['17:00', '12:00'])),
875+
])
876+
def test_neq(self, offset1, offset2):
877+
assert offset1 != offset2
849878

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)
879+
@pytest.mark.parametrize("offset_name", [
880+
"offset1",
881+
"offset2",
882+
"offset3",
883+
"offset4",
884+
"offset8",
885+
"offset9",
886+
"offset10"
887+
])
888+
def test_hash(self, offset_name):
889+
offset = getattr(self, offset_name)
890+
assert offset == offset
854891

855892
def test_call(self):
856893
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)