Skip to content

Commit a2ae3fe

Browse files
committed
Table test touchups
1 parent aadf3e2 commit a2ae3fe

3 files changed

Lines changed: 169 additions & 82 deletions

File tree

‎astropy/table/row.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ class Row:
2626
----- -----
2727
2 4
2828
>>> row['a']
29-
2
29+
np.int32(2)
3030
>>> row[1]
31-
4
31+
np.int32(4)
3232
"""
3333

3434
def __init__(self, table, index):
@@ -132,9 +132,9 @@ def get(self, key, default=None, /):
132132
>>> from astropy.table import Table
133133
>>> t = Table({"a": [2, 3, 5], "b": [7, 11, 13]})
134134
>>> t[0].get("a")
135-
2
135+
np.int64(2)
136136
>>> t[1].get("b", 0)
137-
11
137+
np.int64(11)
138138
>>> t[2].get("c", 0)
139139
0
140140
"""

‎astropy/table/tests/test_mixin.py‎

Lines changed: 110 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
from astropy.table.column import BaseColumn
2828
from astropy.table.serialize import represent_mixins_as_columns
2929
from astropy.table.table_helpers import ArrayWrapper
30+
from astropy.utils.compat import NUMPY_LT_2_0
3031
from astropy.utils.data_info import ParentDtypeInfo
3132
from astropy.utils.exceptions import AstropyUserWarning
3233
from astropy.utils.metadata import MergeConflictWarning
@@ -756,56 +757,98 @@ def test_quantity_representation():
756757
]
757758

758759

759-
def test_representation_representation():
760+
@pytest.mark.parametrize(
761+
"data, pformat",
762+
[
763+
(
764+
coordinates.CartesianRepresentation([0], [1], [0], unit=u.one),
765+
[
766+
" col0 ",
767+
"------------",
768+
"(0., 1., 0.)",
769+
]
770+
if NUMPY_LT_2_0
771+
else [
772+
" col0 ",
773+
"---------------",
774+
"(0.0, 1.0, 0.0)",
775+
],
776+
),
777+
(
778+
coordinates.CartesianRepresentation([0], [1], [0], unit="m"),
779+
[
780+
" col0 ",
781+
" m ",
782+
"------------",
783+
"(0., 1., 0.)",
784+
]
785+
if NUMPY_LT_2_0
786+
else [
787+
" col0 ",
788+
" m ",
789+
"---------------",
790+
"(0.0, 1.0, 0.0)",
791+
],
792+
),
793+
(
794+
coordinates.SphericalRepresentation([10] * u.deg, [20] * u.deg, [1] * u.pc),
795+
[
796+
" col0 ",
797+
" deg, deg, pc ",
798+
"--------------",
799+
"(10., 20., 1.)",
800+
]
801+
if NUMPY_LT_2_0
802+
else [
803+
" col0 ",
804+
" deg, deg, pc ",
805+
"-----------------",
806+
"(10.0, 20.0, 1.0)",
807+
],
808+
),
809+
(
810+
coordinates.UnitSphericalRepresentation([10] * u.deg, [20] * u.deg),
811+
[
812+
" col0 ",
813+
" deg ",
814+
"----------",
815+
"(10., 20.)",
816+
]
817+
if NUMPY_LT_2_0
818+
else [
819+
" col0 ",
820+
" deg ",
821+
"------------",
822+
"(10.0, 20.0)",
823+
],
824+
),
825+
(
826+
coordinates.SphericalCosLatDifferential(
827+
[10] * u.mas / u.yr, [2] * u.mas / u.yr, [10] * u.km / u.s
828+
),
829+
[
830+
" col0 ",
831+
"mas / yr, mas / yr, km / s",
832+
"--------------------------",
833+
" (10., 2., 10.)",
834+
]
835+
if NUMPY_LT_2_0
836+
else [
837+
" col0 ",
838+
"mas / yr, mas / yr, km / s",
839+
"--------------------------",
840+
" (10.0, 2.0, 10.0)",
841+
],
842+
),
843+
],
844+
)
845+
def test_representation_representation(data, pformat):
760846
"""
761847
Test that Representations are represented correctly.
762848
"""
763849
# With no unit we get "None" in the unit row
764-
c = coordinates.CartesianRepresentation([0], [1], [0], unit=u.one)
765-
t = Table([c])
766-
assert t.pformat() == [
767-
" col0 ",
768-
"------------",
769-
"(0., 1., 0.)",
770-
]
771-
772-
c = coordinates.CartesianRepresentation([0], [1], [0], unit="m")
773-
t = Table([c])
774-
assert t.pformat() == [
775-
" col0 ",
776-
" m ",
777-
"------------",
778-
"(0., 1., 0.)",
779-
]
780-
781-
c = coordinates.SphericalRepresentation([10] * u.deg, [20] * u.deg, [1] * u.pc)
782-
t = Table([c])
783-
assert t.pformat() == [
784-
" col0 ",
785-
" deg, deg, pc ",
786-
"--------------",
787-
"(10., 20., 1.)",
788-
]
789-
790-
c = coordinates.UnitSphericalRepresentation([10] * u.deg, [20] * u.deg)
791-
t = Table([c])
792-
assert t.pformat() == [
793-
" col0 ",
794-
" deg ",
795-
"----------",
796-
"(10., 20.)",
797-
]
798-
799-
c = coordinates.SphericalCosLatDifferential(
800-
[10] * u.mas / u.yr, [2] * u.mas / u.yr, [10] * u.km / u.s
801-
)
802-
t = Table([c])
803-
assert t.pformat() == [
804-
" col0 ",
805-
"mas / yr, mas / yr, km / s",
806-
"--------------------------",
807-
" (10., 2., 10.)",
808-
]
850+
t = Table([data])
851+
assert t.pformat() == pformat
809852

810853

811854
def test_skycoord_representation():
@@ -916,15 +959,27 @@ def test_ndarray_mixin(as_ndarray_mixin):
916959
assert t[1]["d"][0] == d[1][0]
917960
assert t[1]["d"][1] == d[1][1]
918961

919-
assert t.pformat(show_dtype=True) == [
920-
" a [f0, f1] b [x, y] c [rx, ry] d ",
921-
"(int32, str1) (int32, str2) (float64, str3) int64[2]",
922-
"------------- ------------- --------------- --------",
923-
" (1, 'a') (10, 'aa') (100., 'raa') 0 .. 1",
924-
" (2, 'b') (20, 'bb') (200., 'rbb') 2 .. 3",
925-
" (3, 'c') (30, 'cc') (300., 'rcc') 4 .. 5",
926-
" (4, 'd') (40, 'dd') (400., 'rdd') 6 .. 7",
927-
]
962+
assert (
963+
t.pformat(show_dtype=True)
964+
== [
965+
" a [f0, f1] b [x, y] c [rx, ry] d ",
966+
"(int32, str1) (int32, str2) (float64, str3) int64[2]",
967+
"------------- ------------- --------------- --------",
968+
]
969+
+ [
970+
" (1, 'a') (10, 'aa') (100., 'raa') 0 .. 1",
971+
" (2, 'b') (20, 'bb') (200., 'rbb') 2 .. 3",
972+
" (3, 'c') (30, 'cc') (300., 'rcc') 4 .. 5",
973+
" (4, 'd') (40, 'dd') (400., 'rdd') 6 .. 7",
974+
]
975+
if NUMPY_LT_2_0
976+
else [
977+
" (1, 'a') (10, 'aa') (100.0, 'raa') 0 .. 1",
978+
" (2, 'b') (20, 'bb') (200.0, 'rbb') 2 .. 3",
979+
" (3, 'c') (30, 'cc') (300.0, 'rcc') 4 .. 5",
980+
" (4, 'd') (40, 'dd') (400.0, 'rdd') 6 .. 7",
981+
]
982+
)
928983

929984

930985
def test_possible_string_format_functions():

‎astropy/table/tests/test_operations.py‎

Lines changed: 55 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from astropy.time import Time, TimeDelta
2727
from astropy.units.quantity import Quantity
2828
from astropy.utils import metadata
29+
from astropy.utils.compat import NUMPY_LT_2_0
2930
from astropy.utils.compat.optional_deps import HAS_SCIPY
3031
from astropy.utils.metadata import MergeConflictError
3132

@@ -944,13 +945,24 @@ def test_join_structured_column(self):
944945
names=["structured", "string"],
945946
)
946947
t12 = table.join(t1, t2, ["structured"], join_type="outer")
947-
assert t12.pformat() == [
948-
"structured [f, i] string_1 string_2",
949-
"----------------- -------- --------",
950-
" (1., 1) one --",
951-
" (2., 2) two three",
952-
" (4., 4) -- four",
953-
]
948+
assert (
949+
t12.pformat()
950+
== [
951+
"structured [f, i] string_1 string_2",
952+
"----------------- -------- --------",
953+
" (1., 1) one --",
954+
" (2., 2) two three",
955+
" (4., 4) -- four",
956+
]
957+
if NUMPY_LT_2_0
958+
else [
959+
"structured [f, i] string_1 string_2",
960+
"----------------- -------- --------",
961+
" (1.0, 1) one --",
962+
" (2.0, 2) two three",
963+
" (4.0, 4) -- four",
964+
]
965+
)
954966

955967

956968
class TestSetdiff:
@@ -1519,15 +1531,26 @@ def test_vstack_structured_column(self):
15191531
names=["structured", "string"],
15201532
)
15211533
t12 = table.vstack([t1, t2])
1522-
assert t12.pformat() == [
1523-
"structured [f, i] string",
1524-
"----------------- ------",
1525-
" (1., 1) one",
1526-
" (2., 2) two",
1527-
" (3., 3) three",
1528-
" (4., 4) four",
1529-
]
1530-
1534+
assert (
1535+
t12.pformat()
1536+
== [
1537+
"structured [f, i] string",
1538+
"----------------- ------",
1539+
]
1540+
+ [
1541+
" (1., 1) one",
1542+
" (2., 2) two",
1543+
" (3., 3) three",
1544+
" (4., 4) four",
1545+
]
1546+
if NUMPY_LT_2_0
1547+
else [
1548+
" (1.0, 1) one",
1549+
" (2.0, 2) two",
1550+
" (3.0, 3) three",
1551+
" (4.0, 4) four",
1552+
]
1553+
)
15311554
# One table without the structured column.
15321555
t3 = t2[("string",)]
15331556
t13 = table.vstack([t1, t3])
@@ -1732,13 +1755,22 @@ def test_dstack_structured_column(self):
17321755
names=["structured", "string"],
17331756
)
17341757
t12 = table.dstack([t1, t2])
1735-
assert t12.pformat() == [
1736-
"structured [f, i] string ",
1737-
"------------------ ------------",
1738-
"(1., 1) .. (3., 3) one .. three",
1739-
"(2., 2) .. (4., 4) two .. four",
1740-
]
1741-
1758+
assert (
1759+
t12.pformat()
1760+
== [
1761+
"structured [f, i] string ",
1762+
"------------------ ------------",
1763+
"(1., 1) .. (3., 3) one .. three",
1764+
"(2., 2) .. (4., 4) two .. four",
1765+
]
1766+
if NUMPY_LT_2_0
1767+
else [
1768+
" structured [f, i] string ",
1769+
"-------------------- ------------",
1770+
"(1.0, 1) .. (3.0, 3) one .. three",
1771+
"(2.0, 2) .. (4.0, 4) two .. four",
1772+
]
1773+
)
17421774
# One table without the structured column.
17431775
t3 = t2[("string",)]
17441776
t13 = table.dstack([t1, t3])

0 commit comments

Comments
 (0)