Skip to content

Commit f0b30ab

Browse files
committed
Improve the accessor tests to test both numerical and enum values
1 parent 259771a commit f0b30ab

File tree

1 file changed

+51
-23
lines changed

1 file changed

+51
-23
lines changed

pygmt/tests/test_accessor.py

Lines changed: 51 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,61 +17,89 @@
1717

1818
def test_accessor_gridline_cartesian():
1919
"""
20-
Check that a grid returns a registration value of 0 when Gridline registered, and a
21-
gtype value of 1 when using Geographic coordinates.
20+
Check that the accessor returns the correct registration and gtype values for a
21+
Cartesian, gridline-registered grid.
2222
"""
2323
fname = which(fname="@test.dat.nc", download="a")
24-
grid = xr.open_dataarray(fname)
24+
grid = xr.open_dataarray(fname, engine="netcdf4")
2525
assert grid.gmt.registration == GridReg.GRIDLINE
2626
assert grid.gmt.gtype == GridType.CARTESIAN
2727

2828

2929
def test_accessor_pixel_geographic():
3030
"""
31-
Check that a grid returns a registration value of 1 when Pixel registered, and a
32-
gtype value of 0 when using Cartesian coordinates.
31+
Check that the accessor returns the correct registration and gtype values for a
32+
geographic, pixel-registered grid.
3333
"""
3434
fname = which(fname="@earth_relief_01d_p", download="a")
3535
grid = xr.open_dataarray(fname, engine="netcdf4")
3636
assert grid.gmt.registration == GridReg.PIXEL
3737
assert grid.gmt.gtype == GridType.GEOGRAPHIC
3838

3939

40-
def test_accessor_set_pixel_registration():
40+
def test_accessor_set_registration():
4141
"""
42-
Check that we can set a grid to be Pixel registered with a registration value of 1.
42+
Check that we can set the registration of a grid.
4343
"""
4444
grid = xr.DataArray(data=[[0.1, 0.2], [0.3, 0.4]])
45-
assert grid.gmt.registration == GridReg.GRIDLINE
45+
assert grid.gmt.registration == GridReg.GRIDLINE == 0 # Default registration
46+
47+
# Set the registration to pixel
4648
grid.gmt.registration = GridReg.PIXEL
47-
assert grid.gmt.registration == GridReg.PIXEL
49+
assert grid.gmt.registration == GridReg.PIXEL == 1
50+
51+
# Set the registration to gridline
52+
grid.gmt.registration = GridReg.GRIDLINE
53+
assert grid.gmt.registration == GridReg.GRIDLINE == 0
54+
55+
# Set the registration to pixel but using a numerical value
56+
grid.gmt.registration = 1
57+
assert grid.gmt.registration == GridReg.PIXEL == 1
58+
59+
# Set the registration to gridline but using a numerical value
60+
grid.gmt.registration = 0
61+
assert grid.gmt.registration == GridReg.GRIDLINE == 0
4862

4963

5064
@pytest.mark.benchmark
51-
def test_accessor_set_geographic_cartesian_roundtrip():
65+
def test_accessor_set_gtype():
5266
"""
53-
Check that we can set a grid to switch between the default Cartesian coordinate type
54-
using a gtype of 1, set it to Geographic 0, and then back to Cartesian again 1.
67+
Check that we can set the gtype of a grid.
5568
"""
5669
grid = xr.DataArray(data=[[0.1, 0.2], [0.3, 0.4]])
57-
assert grid.gmt.gtype == GridType.CARTESIAN
70+
assert grid.gmt.gtype == GridType.CARTESIAN == 0 # Default gtype
71+
72+
# Set the gtype to geographic
5873
grid.gmt.gtype = GridType.GEOGRAPHIC
59-
assert grid.gmt.gtype == GridType.GEOGRAPHIC
74+
assert grid.gmt.gtype == GridType.GEOGRAPHIC == 1
75+
76+
# Set the gtype to Cartesian
6077
grid.gmt.gtype = GridType.CARTESIAN
61-
assert grid.gmt.gtype == GridType.CARTESIAN
78+
assert grid.gmt.gtype == GridType.CARTESIAN == 0
79+
80+
# Set the gtype to geographic but using a numerical value
81+
grid.gmt.gtype = 1
82+
assert grid.gmt.gtype == GridType.GEOGRAPHIC == 1
6283

84+
# Set the gtype to Cartesian but using a numerical value
85+
grid.gmt.gtype = 0
86+
assert grid.gmt.gtype == GridType.CARTESIAN == 0
6387

64-
def test_accessor_set_non_boolean():
88+
89+
def test_accessor_set_invalid_registration_and_gtype():
6590
"""
66-
Check that setting non boolean values on registration and gtype do not work.
91+
Check that setting invalid values on registration and gtype do not work.
6792
"""
6893
grid = xr.DataArray(data=[[0.1, 0.2], [0.3, 0.4]])
6994

7095
with pytest.raises(GMTInvalidInput):
7196
grid.gmt.registration = "2"
72-
97+
with pytest.raises(GMTInvalidInput):
98+
grid.gmt.registration = "pixel"
7399
with pytest.raises(GMTInvalidInput):
74100
grid.gmt.gtype = 2
101+
with pytest.raises(GMTInvalidInput):
102+
grid.gmt.gtype = "geographic"
75103

76104

77105
@pytest.mark.xfail(
@@ -105,23 +133,23 @@ def test_accessor_grid_source_file_not_exist():
105133
Check that the accessor fallbacks to the default registration and gtype when the
106134
grid source file (i.e., grid.encoding["source"]) doesn't exist.
107135
"""
108-
# Load the 05m earth relief grid, which is stored as tiles
136+
# Load the 05m earth relief grid, which is stored as tiles.
109137
grid = load_earth_relief(
110138
resolution="05m", region=[0, 5, -5, 5], registration="pixel"
111139
)
112-
# Registration and gtype are correct
140+
# Registration and gtype are correct.
113141
assert grid.gmt.registration == GridReg.PIXEL
114142
assert grid.gmt.gtype == GridType.GEOGRAPHIC
115143
# The source grid file is undefined.
116144
assert grid.encoding.get("source") is None
117145

118-
# For a sliced grid, fallback to default registration and gtype,
119-
# because the source grid file doesn't exist.
146+
# For a sliced grid, fallback to default registration and gtype, because the source
147+
# grid file doesn't exist.
120148
sliced_grid = grid[1:3, 1:3]
121149
assert sliced_grid.gmt.registration == GridReg.GRIDLINE
122150
assert sliced_grid.gmt.gtype == GridType.CARTESIAN
123151

124-
# Still possible to manually set registration and gtype
152+
# Still possible to manually set registration and gtype.
125153
sliced_grid.gmt.registration = GridReg.PIXEL
126154
sliced_grid.gmt.gtype = GridType.GEOGRAPHIC
127155
assert sliced_grid.gmt.registration == GridReg.PIXEL

0 commit comments

Comments
 (0)