Skip to content

Commit fcc4865

Browse files
authored
Merge pull request #56 from scottyhq/pytest
basic test framework and github actions
2 parents 3b40649 + b2d67e2 commit fcc4865

File tree

9 files changed

+198
-2
lines changed

9 files changed

+198
-2
lines changed

.github/workflows/test.yml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
name: Tests
2+
3+
on:
4+
push:
5+
branches: "*"
6+
pull_request:
7+
branches: main
8+
9+
jobs:
10+
test:
11+
name: ${{ matrix.CONDA_ENV }}-test
12+
runs-on: ubuntu-20.04
13+
strategy:
14+
fail-fast: false
15+
matrix:
16+
CONDA_ENV: [3.8, 3.9]
17+
steps:
18+
- name: Checkout
19+
uses: actions/checkout@v2
20+
21+
- name: Cache conda environment
22+
uses: actions/cache@v2
23+
env:
24+
# Increase this value to reset cache if environment.yml has not changed
25+
CACHE_NUMBER: 0
26+
with:
27+
path: ~/conda_pkgs_dir
28+
key: >
29+
${{ format('{0}-conda-{1}-{2}',
30+
runner.os,
31+
env.CACHE_NUMBER,
32+
hashFiles(format('ci/environment-{0}.yml', matrix.CONDA_ENV))) }}
33+
34+
- name: Setup miniconda
35+
uses: conda-incubator/setup-miniconda@v2
36+
with:
37+
auto-update-conda: true
38+
auto-activate-base: false
39+
activate-environment: sliderule-ci
40+
environment-file: ci/environment-${{ matrix.CONDA_ENV }}.yml
41+
use-only-tar-bz2: true
42+
43+
- name: Install sliderule from current commit
44+
shell: bash -l {0}
45+
run: |
46+
python -m pip install .
47+
conda list
48+
49+
- name: Run tests
50+
shell: bash -l {0}
51+
run: |
52+
python -m pytest --verbose

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# sliderule-python
2-
2+
[![Tests](https://github.com/ICESat2-SlideRule/sliderule-python/actions/workflows/test.yml/badge.svg)](https://github.com/ICESat2-SlideRule/sliderule-python/actions/workflows/test.yml)
33
[![Binder](https://mybinder.org/badge_logo.svg)](https://gke.mybinder.org/v2/gh/ICESat2-SlideRule/sliderule-python/main?urlpath=lab)
44
[![badge](https://img.shields.io/static/v1.svg?logo=Jupyter&label=PangeoBinderAWS&message=us-west-2&color=orange)](https://aws-uswest2-binder.pangeo.io/v2/gh/ICESat2-SlideRule/sliderule-python/main?urlpath=lab)
55
[![DOI](https://zenodo.org/badge/311384982.svg)](https://zenodo.org/badge/latestdoi/311384982)
@@ -35,7 +35,7 @@ python3 setup.py install
3535

3636
### Dependencies
3737

38-
To use SlideRule's Python client, the only non-standard packages it depends on are `requests` and `numpy`. But if you intend on running the example notebooks, please refer to the package requirements listed in `environment.yml` for a full list of dependencies needed by those notebooks.
38+
Basic functionality of sliderule-python depends on `requests` and `numpy`. But if you intend on running the example notebooks, please refer to the package requirements listed in `environment.yml` for a full list of recommended python libraries.
3939

4040
## II. Getting Started Using SlideRule
4141

ci/environment-3.8.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
name: sliderule-ci
2+
channels:
3+
- conda-forge
4+
dependencies:
5+
- python=3.8
6+
- geopandas
7+
- numpy
8+
- pip
9+
- pytest
10+
- requests
11+
- setuptools_scm

ci/environment-3.9.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
name: sliderule-ci
2+
channels:
3+
- conda-forge
4+
dependencies:
5+
- python=3.9
6+
- geopandas
7+
- numpy
8+
- pip
9+
- pytest
10+
- requests
11+
- setuptools_scm

pytest.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[pytest]
2+
markers =
3+
network: mark a test that requires a network connection.

tests/__init__.py

Whitespace-only changes.

tests/data/polygon.geojson

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"type": "FeatureCollection",
3+
"features": [
4+
{
5+
"type": "Feature",
6+
"properties": {},
7+
"geometry": {
8+
"type": "Polygon",
9+
"coordinates": [
10+
[
11+
[
12+
-108.3087158203125,
13+
38.81670618152057
14+
],
15+
[
16+
-107.87406921386717,
17+
38.81670618152057
18+
],
19+
[
20+
-107.87406921386717,
21+
39.122602866278996
22+
],
23+
[
24+
-108.3087158203125,
25+
39.122602866278996
26+
],
27+
[
28+
-108.3087158203125,
29+
38.81670618152057
30+
]
31+
]
32+
]
33+
}
34+
}
35+
]
36+
}
37+
38+

tests/test_client.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""Tests for sliderule-python."""
2+
3+
import pytest
4+
from requests.exceptions import ConnectTimeout, ConnectionError
5+
import sliderule
6+
7+
class TestLocal:
8+
def test_version(self):
9+
assert hasattr(sliderule, '__version__')
10+
assert isinstance(sliderule.__version__, str)
11+
12+
def test_seturl_empty(self):
13+
with pytest.raises(TypeError, match=('url')):
14+
sliderule.set_url()
15+
16+
def test_gps2utc(self):
17+
utc = sliderule.gps2utc(1235331234)
18+
assert utc == '2019-02-27 19:34:03'
19+
20+
@pytest.mark.network
21+
class TestRemote:
22+
def test_init_badurl(self):
23+
with pytest.raises( (ConnectTimeout, ConnectionError) ):
24+
sliderule.set_rqst_timeout((1, 60))
25+
sliderule.set_url('incorrect.org')

tests/test_icesat2.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
"""Tests for sliderule-python icesat2 api."""
2+
3+
import pytest
4+
from requests.exceptions import ConnectTimeout, ConnectionError
5+
import sliderule
6+
from sliderule import icesat2
7+
from pathlib import Path
8+
import os.path
9+
10+
TESTDIR = Path(__file__).parent
11+
SERVER = "icesat2sliderule.org"
12+
13+
# Change connection timeout from default 10s to 1s
14+
sliderule.set_rqst_timeout((1, 60))
15+
16+
@pytest.fixture(scope='module')
17+
def grandmesa():
18+
return [ {"lon": -108.3435200747503, "lat": 38.89102961045247},
19+
{"lon": -107.7677425431139, "lat": 38.90611184543033},
20+
{"lon": -107.7818591266989, "lat": 39.26613714985466},
21+
{"lon": -108.3605610678553, "lat": 39.25086131372244},
22+
{"lon": -108.3435200747503, "lat": 38.89102961045247} ]
23+
24+
25+
class TestLocal:
26+
def test_init_empty_raises(self):
27+
with pytest.raises(TypeError, match=('url')):
28+
icesat2.init()
29+
30+
def test_toregion_empty_raises(self):
31+
with pytest.raises(TypeError, match=('filename')):
32+
region = icesat2.toregion()
33+
34+
def test_toregion(self):
35+
region = icesat2.toregion(os.path.join(TESTDIR, 'data/polygon.geojson'))
36+
assert len(region) == 1 # single polygon bbox
37+
assert len(region[0]) == 5 # 5 coordinate pairs
38+
assert {'lon', 'lat'} <= region[0][0].keys()
39+
40+
@pytest.mark.network
41+
class TestRemote:
42+
def test_init_badurl(self):
43+
with pytest.raises( (ConnectTimeout, ConnectionError) ):
44+
icesat2.init('incorrect.org')
45+
46+
def test_get_version(self):
47+
icesat2.init(SERVER)
48+
version = icesat2.get_version()
49+
assert isinstance(version, dict)
50+
assert {'icesat2', 'server', 'client'} <= version.keys()
51+
52+
def test_cmr(self, grandmesa):
53+
icesat2.init(SERVER)
54+
granules = icesat2.cmr(grandmesa, time_start='2018-10-01', time_end='2018-12-01')
55+
assert isinstance(granules, list)
56+
assert 'ATL03_20181017222812_02950102_004_01.h5' in granules

0 commit comments

Comments
 (0)