Skip to content

Commit 1e11abb

Browse files
committed
Make tests a bit clearer using fixture
1 parent fadd52e commit 1e11abb

File tree

1 file changed

+66
-58
lines changed

1 file changed

+66
-58
lines changed

pandas/tests/indexes/test_range.py

+66-58
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,9 @@ def test_union_noncomparable(self):
583583
expected = Index(np.concatenate((other, self.index)))
584584
tm.assert_index_equal(result, expected)
585585

586-
def test_union(self):
586+
@pytest.fixture
587+
def union_inputs(self):
588+
"""Inputs for RangeIndex.union tests"""
587589
RI = RangeIndex
588590
I64 = Int64Index
589591

@@ -610,67 +612,73 @@ def test_union(self):
610612
(RI(0, 10, 1), I64([])),
611613
(RI(0), I64([1, 5, 6]))]
612614

613-
expected_sorted = [RI(0, 10, 1),
614-
RI(0, 20, 1),
615-
RI(0, 20, 1),
616-
RI(0, -10, -1),
617-
RI(-19, 1, 1),
618-
RI(0, 10, 1),
619-
RI(0, 12, 1),
620-
RI(-2, 24, 2),
621-
RI(-19, 1, 1),
622-
RI(0, 100, 5),
623-
RI(-95, 10, 5),
624-
RI(-11, 2, 1),
625-
RI(0),
626-
RI(0, -10, -2),
627-
RI(0, 102, 2),
628-
RI(-100, 4, 2),
629-
RI(-99, 1, 1),
630-
RI(0, 6, 5),
631-
RI(-5, 10, 5),
632-
I64([0, 1, 2, 4]),
633-
RI(0, 10, 1),
634-
I64([1, 5, 6])]
635-
636-
for ((idx1, idx2), expected) in zip(inputs, expected_sorted):
615+
return inputs
616+
617+
def test_union_sorted(self, union_inputs):
618+
RI = RangeIndex
619+
I64 = Int64Index
620+
621+
expected = [RI(0, 10, 1),
622+
RI(0, 20, 1),
623+
RI(0, 20, 1),
624+
RI(0, -10, -1),
625+
RI(-19, 1, 1),
626+
RI(0, 10, 1),
627+
RI(0, 12, 1),
628+
RI(-2, 24, 2),
629+
RI(-19, 1, 1),
630+
RI(0, 100, 5),
631+
RI(-95, 10, 5),
632+
RI(-11, 2, 1),
633+
RI(0),
634+
RI(0, -10, -2),
635+
RI(0, 102, 2),
636+
RI(-100, 4, 2),
637+
RI(-99, 1, 1),
638+
RI(0, 6, 5),
639+
RI(-5, 10, 5),
640+
I64([0, 1, 2, 4]),
641+
RI(0, 10, 1),
642+
I64([1, 5, 6])]
643+
644+
for ((idx1, idx2), expected_sorted) in zip(union_inputs, expected):
637645
res1 = idx1.union(idx2)
646+
tm.assert_index_equal(res1, expected_sorted, exact=True)
638647
res2 = idx2.union(idx1)
639648
res3 = idx1._int64index.union(idx2)
640-
tm.assert_index_equal(res1, expected, exact=True)
641-
tm.assert_index_equal(res2, expected, exact=True)
642-
tm.assert_index_equal(res3, expected)
643-
644-
expected_notsorted = [RI(0, 10, 1),
645-
I64(range(20)),
646-
I64(range(20)),
647-
RI(0, -10, -1),
648-
I64(range(0, -20, -1)),
649-
I64(list(range(0, 10, 2)) +
650-
list(range(1, 10, 2))),
651-
I64(list(range(0, 11, 2)) +
652-
list(range(1, 12, 2))),
653-
I64(list(range(0, 21, 4)) +
654-
list(range(-2, 24, 4))),
655-
I64(list(range(0, -20, -2)) +
656-
list(range(-1, -21, -2))),
657-
I64(range(0, 100, 5)),
658-
I64(list(range(0, -100, -5)) + [5]),
659-
I64(list(range(0, -11, -1)) + [1, -11]),
660-
RI(0),
661-
RI(0, -10, -2),
662-
I64(range(0, 102, 2)),
663-
I64(list(range(0, -100, -2)) + [-100, 2]),
664-
I64(list(range(0, -100, -1))),
665-
I64([0, 5]),
666-
I64([0, 5, -5]),
667-
I64([0, 1, 2, 4]),
668-
RI(0, 10, 1),
669-
I64([1, 5, 6])]
670-
671-
for ((idx1, idx2), expected) in zip(inputs, expected_notsorted):
649+
tm.assert_index_equal(res2, expected_sorted, exact=True)
650+
tm.assert_index_equal(res3, expected_sorted)
651+
652+
def test_union_notsorted(self, union_inputs):
653+
RI = RangeIndex
654+
I64 = Int64Index
655+
656+
expected = [RI(0, 10, 1),
657+
I64(range(20)),
658+
I64(range(20)),
659+
RI(0, -10, -1),
660+
I64(range(0, -20, -1)),
661+
I64(list(range(0, 10, 2)) + list(range(1, 10, 2))),
662+
I64(list(range(0, 11, 2)) + list(range(1, 12, 2))),
663+
I64(list(range(0, 21, 4)) + list(range(-2, 24, 4))),
664+
I64(list(range(0, -20, -2)) + list(range(-1, -21, -2))),
665+
I64(range(0, 100, 5)),
666+
I64(list(range(0, -100, -5)) + [5]),
667+
I64(list(range(0, -11, -1)) + [1, -11]),
668+
RI(0),
669+
RI(0, -10, -2),
670+
I64(range(0, 102, 2)),
671+
I64(list(range(0, -100, -2)) + [-100, 2]),
672+
I64(list(range(0, -100, -1))),
673+
I64([0, 5]),
674+
I64([0, 5, -5]),
675+
I64([0, 1, 2, 4]),
676+
RI(0, 10, 1),
677+
I64([1, 5, 6])]
678+
679+
for ((idx1, idx2), expected_notsorted) in zip(union_inputs, expected):
672680
res1 = idx1.union(idx2, sort=False)
673-
tm.assert_index_equal(res1, expected, exact=True)
681+
tm.assert_index_equal(res1, expected_notsorted, exact=True)
674682

675683
def test_nbytes(self):
676684

0 commit comments

Comments
 (0)