Skip to content

Commit 76fb7fb

Browse files
committed
Make tests a bit clearer using fixture
1 parent b6f910c commit 76fb7fb

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
@@ -580,7 +580,9 @@ def test_union_noncomparable(self):
580580
expected = Index(np.concatenate((other, self.index)))
581581
tm.assert_index_equal(result, expected)
582582

583-
def test_union(self):
583+
@pytest.fixture
584+
def union_inputs(self):
585+
"""Inputs for RangeIndex.union tests"""
584586
RI = RangeIndex
585587
I64 = Int64Index
586588

@@ -607,67 +609,73 @@ def test_union(self):
607609
(RI(0, 10, 1), I64([])),
608610
(RI(0), I64([1, 5, 6]))]
609611

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

672680
def test_nbytes(self):
673681

0 commit comments

Comments
 (0)