Skip to content

Commit 320872c

Browse files
committed
Make tests a bit clearer using fixture
1 parent 2a2a596 commit 320872c

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

581-
def test_union(self):
581+
@pytest.fixture
582+
def union_inputs(self):
583+
"""Inputs for RangeIndex.union tests"""
582584
RI = RangeIndex
583585
I64 = Int64Index
584586

@@ -605,67 +607,73 @@ def test_union(self):
605607
(RI(0, 10, 1), I64([])),
606608
(RI(0), I64([1, 5, 6]))]
607609

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

670678
def test_nbytes(self):
671679

0 commit comments

Comments
 (0)