Skip to content

Commit 1121213

Browse files
committed
parameterized pytest functions to support local server
1 parent 7811b77 commit 1121213

File tree

3 files changed

+62
-41
lines changed

3 files changed

+62
-41
lines changed

tests/conftest.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import pytest
2+
3+
def pytest_addoption(parser):
4+
parser.addoption("--server", action="store", default="icesat2sliderule.org")
5+
parser.addoption("--asset", action="store", default="nsidc-s3")
6+
7+
#def pytest_generate_tests(metafunc):
8+
# # This is called for every test. Only get/set command line arguments
9+
# # if the argument is specified in the list of test "fixturenames".
10+
# option_value = metafunc.config.option.server
11+
# if 'server' in metafunc.fixturenames and option_value is not None:
12+
# metafunc.parametrize("server", [option_value])
13+
14+
@pytest.fixture(scope='session')
15+
def server(request):
16+
server_value = request.config.option.server
17+
if server_value is None:
18+
pytest.skip()
19+
return server_value
20+
21+
@pytest.fixture(scope='session')
22+
def asset(request):
23+
asset_value = request.config.option.asset
24+
if asset_value is None:
25+
pytest.skip()
26+
return asset_value

tests/test_api.py

Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
"""Tests for sliderule-python icesat2 api."""
22

33
import pytest
4-
from requests.exceptions import ConnectTimeout, ConnectionError
54
import sliderule
65
from sliderule import icesat2
7-
from pathlib import Path
8-
import os.path
96

10-
SERVER = "icesat2sliderule.org"
117
ASSET = "nsidc-s3"
128

139
ATL03_FILE1 = "ATL03_20181019065445_03150111_004_01.h5"
@@ -20,8 +16,8 @@
2016

2117
@pytest.mark.network
2218
class TestApi:
23-
def test_time(self):
24-
icesat2.init(SERVER)
19+
def test_time(self, server):
20+
icesat2.init(server)
2521
rqst = {
2622
"time": "NOW",
2723
"input": "NOW",
@@ -39,37 +35,37 @@ def test_time(self):
3935
again = d["time"]
4036
assert now == again
4137

42-
def test_h5(self):
43-
icesat2.init(SERVER)
44-
epoch_offset = icesat2.h5("ancillary_data/atlas_sdp_gps_epoch", ATL03_FILE1, ASSET)[0]
38+
def test_h5(self, server, asset):
39+
icesat2.init(server)
40+
epoch_offset = icesat2.h5("ancillary_data/atlas_sdp_gps_epoch", ATL03_FILE1, asset)[0]
4541
assert epoch_offset == 1198800018.0
4642

47-
def test_h5_types(self):
48-
icesat2.init(SERVER)
49-
heights_64 = icesat2.h5("/gt1l/land_ice_segments/h_li", ATL06_FILE1, ASSET)
43+
def test_h5_types(self, server, asset):
44+
icesat2.init(server)
45+
heights_64 = icesat2.h5("/gt1l/land_ice_segments/h_li", ATL06_FILE1, asset)
5046
expected_64 = [45.95665, 45.999374, 46.017857, 46.015575, 46.067562, 46.099796, 46.14037, 46.105526, 46.096024, 46.12297]
51-
heights_32 = icesat2.h5("/gt1l/land_ice_segments/h_li", ATL06_FILE2, ASSET)
47+
heights_32 = icesat2.h5("/gt1l/land_ice_segments/h_li", ATL06_FILE2, asset)
5248
expected_32 = [350.46988, 352.08688, 352.43243, 353.19345, 353.69543, 352.25998, 350.15366, 346.37888, 342.47903, 341.51]
53-
bckgrd_32nf = icesat2.h5("/gt1l/bckgrd_atlas/bckgrd_rate", ATL03_FILE2, ASSET)
49+
bckgrd_32nf = icesat2.h5("/gt1l/bckgrd_atlas/bckgrd_rate", ATL03_FILE2, asset)
5450
expected_32nf = [29311.684, 6385.937, 6380.8413, 28678.951, 55349.168, 38201.082, 19083.434, 38045.67, 34942.434, 38096.266]
5551
for c in zip(heights_64, expected_64, heights_32, expected_32, bckgrd_32nf, expected_32nf):
5652
assert (round(c[0]) == round(c[1])) and (round(c[2]) == round(c[3])) and (round(c[4]) == round(c[5]))
5753

58-
def test_variable_length(self):
59-
icesat2.init(SERVER)
60-
v = icesat2.h5("/gt1r/geolocation/segment_ph_cnt", ATL03_FILE1, ASSET)
54+
def test_variable_length(self, server, asset):
55+
icesat2.init(server)
56+
v = icesat2.h5("/gt1r/geolocation/segment_ph_cnt", ATL03_FILE1, asset)
6157
assert v[0] == 258 and v[1] == 256 and v[2] == 273
6258

63-
def test_h5p(self):
64-
icesat2.init(SERVER)
59+
def test_h5p(self, server, asset):
60+
icesat2.init(server)
6561
datasets = [
6662
{"dataset": "/gt1l/land_ice_segments/h_li", "numrows": 5},
6763
{"dataset": "/gt1r/land_ice_segments/h_li", "numrows": 5},
6864
{"dataset": "/gt2l/land_ice_segments/h_li", "numrows": 5},
6965
{"dataset": "/gt2r/land_ice_segments/h_li", "numrows": 5},
7066
{"dataset": "/gt3l/land_ice_segments/h_li", "numrows": 5},
7167
{"dataset": "/gt3r/land_ice_segments/h_li", "numrows": 5} ]
72-
rsps = icesat2.h5p(datasets, ATL06_FILE1, ASSET)
68+
rsps = icesat2.h5p(datasets, ATL06_FILE1, asset)
7369
expected = {'/gt1l/land_ice_segments/h_li': [45.95665, 45.999374, 46.017857, 46.015575, 46.067562],
7470
'/gt1r/land_ice_segments/h_li': [45.980865, 46.02602, 46.02262, 46.03137, 46.073578],
7571
'/gt2l/land_ice_segments/h_li': [45.611526, 45.588196, 45.53242, 45.48105, 45.443752],
@@ -80,10 +76,10 @@ def test_h5p(self):
8076
for index in range(len(expected[dataset])):
8177
assert round(rsps[dataset][index]) == round(expected[dataset][index])
8278

83-
def test_geospatial1(self):
84-
icesat2.init(SERVER)
79+
def test_geospatial1(self, server, asset):
80+
icesat2.init(server)
8581
test = {
86-
"asset": ASSET,
82+
"asset": asset,
8783
"pole": "north",
8884
"lat": 40.0,
8985
"lon": 60.0,
@@ -125,10 +121,10 @@ def test_geospatial1(self):
125121
assert d["lat"] == 40.0 and d["lon"] == 60.0
126122
assert d["x"] == 0.466307658155 and d["y"] == 0.80766855588292
127123

128-
def test_geospatial2(self):
129-
icesat2.init(SERVER)
124+
def test_geospatial2(self, server, asset):
125+
icesat2.init(server)
130126
test = {
131-
"asset": ASSET,
127+
"asset": asset,
132128
"pole": "north",
133129
"lat": 30.0,
134130
"lon": 100.0,
@@ -138,10 +134,10 @@ def test_geospatial2(self):
138134
d = sliderule.source("geo", test)
139135
assert abs(d["lat"] - 30.0) < 0.0001 and d["lon"] == 100.0
140136

141-
def test_geospatial3(self):
142-
icesat2.init(SERVER)
137+
def test_geospatial3(self, server, asset):
138+
icesat2.init(server)
143139
test = {
144-
"asset": ASSET,
140+
"asset": asset,
145141
"pole": "north",
146142
"lat": 30.0,
147143
"lon": 100.0,
@@ -151,10 +147,10 @@ def test_geospatial3(self):
151147
d = sliderule.source("geo", test)
152148
assert abs(d["lat"] - 30.0) < 0.0001 and d["lon"] == -100.0
153149

154-
def test_geospatial4(self):
155-
icesat2.init(SERVER)
150+
def test_geospatial4(self, server, asset):
151+
icesat2.init(server)
156152
test = {
157-
"asset": ASSET,
153+
"asset": asset,
158154
"pole": "north",
159155
"lat": 30.0,
160156
"lon": 100.0,
@@ -164,16 +160,16 @@ def test_geospatial4(self):
164160
d = sliderule.source("geo", test)
165161
assert abs(d["lat"] - 30.0) < 0.0001 and d["lon"] == -80.0
166162

167-
def test_definition(self):
168-
icesat2.init(SERVER)
163+
def test_definition(self, server):
164+
icesat2.init(server)
169165
rqst = {
170166
"rectype": "atl06rec.elevation",
171167
}
172168
d = sliderule.source("definition", rqst)
173169
assert d["delta_time"]["offset"] == 192
174170

175-
def test_version(self):
176-
icesat2.init(SERVER)
171+
def test_version(self, server):
172+
icesat2.init(server)
177173
rsps = sliderule.source("version", {})
178174
assert 'server' in rsps
179175
assert 'version' in rsps['server']

tests/test_icesat2.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import os.path
99

1010
TESTDIR = Path(__file__).parent
11-
SERVER = "icesat2sliderule.org"
1211

1312
# Change connection timeout from default 10s to 1s
1413
sliderule.set_rqst_timeout((1, 60))
@@ -43,14 +42,14 @@ def test_init_badurl(self):
4342
with pytest.raises( (ConnectTimeout, ConnectionError) ):
4443
icesat2.init('incorrect.org')
4544

46-
def test_get_version(self):
47-
icesat2.init(SERVER)
45+
def test_get_version(self, server):
46+
icesat2.init(server)
4847
version = icesat2.get_version()
4948
assert isinstance(version, dict)
5049
assert {'icesat2', 'server', 'client'} <= version.keys()
5150

52-
def test_cmr(self, grandmesa):
53-
icesat2.init(SERVER)
51+
def test_cmr(self, grandmesa, server):
52+
icesat2.init(server)
5453
granules = icesat2.cmr(polygon=grandmesa,
5554
time_start='2018-10-01',
5655
time_end='2018-12-01')

0 commit comments

Comments
 (0)