Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Make IGRA2 tests run offline (Fixes #204) #235

Merged
merged 2 commits into from
Jul 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ environment:
- PYTHON_VERSION: "3.6"

platform:
- x86
- x64

install:
Expand Down
11 changes: 7 additions & 4 deletions siphon/simplewebservice/igra2.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@
import pandas as pd

from .._tools import get_wind_components
try:
from urllib.request import urlopen
except ImportError:
from urllib2 import urlopen

warnings.filterwarnings('ignore', 'Pandas doesn\'t allow columns to be created', UserWarning)

Expand Down Expand Up @@ -106,6 +102,13 @@ def _get_data_raw(self):
Returns a tuple with a string for the body, string for the headers,
and a list of dates.
"""
# Import need to be here so we can monkeypatch urlopen for testing and avoid
# downloading live data for testing
try:
from urllib.request import urlopen
except ImportError:
from urllib2 import urlopen

with closing(urlopen(self.ftpsite + self.site_id + self.suffix + '.zip')) as url:
f = ZipFile(BytesIO(url.read()), 'r').open(self.site_id + self.suffix)

Expand Down
Binary file added siphon/tests/fixtures/USM00070026-derived.zip
Binary file not shown.
Binary file added siphon/tests/fixtures/USM00070026.zip
Binary file not shown.
23 changes: 18 additions & 5 deletions siphon/tests/test_igra2.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
"""Test IGRA2 upper air dataset access."""

from datetime import datetime
import os.path
try:
from urllib import request
except ImportError:
import urllib2 as request

from numpy.testing import assert_almost_equal

Expand All @@ -15,9 +20,13 @@


@recorder.use_cassette('igra2_sounding')
def test_igra2():
def test_igra2(monkeypatch):
"""Test that we are properly parsing data from the IGRA2 archive."""
df, header = IGRAUpperAir.request_data(datetime(2010, 6, 1, 12), 'USM00070026')
with monkeypatch.context() as m:
m.setattr(request, 'urlopen',
lambda _: open(os.path.join(os.path.dirname(__file__),
'fixtures', 'USM00070026.zip'), 'rb'))
df, header = IGRAUpperAir.request_data(datetime(2010, 6, 1, 12), 'USM00070026')

assert_almost_equal(df['lvltyp1'][5], 1, 1)
assert_almost_equal(df['lvltyp2'][5], 0, 1)
Expand Down Expand Up @@ -47,10 +56,14 @@ def test_igra2():


@recorder.use_cassette('igra2_derived')
def test_igra2_drvd():
def test_igra2_drvd(monkeypatch):
"""Test that we are properly parsing data from the IGRA2 archive."""
df, header = IGRAUpperAir.request_data(datetime(2014, 9, 10, 0),
'USM00070026', derived=True)
with monkeypatch.context() as m:
m.setattr(request, 'urlopen',
lambda _: open(os.path.join(os.path.dirname(__file__),
'fixtures', 'USM00070026-derived.zip'), 'rb'))
df, header = IGRAUpperAir.request_data(datetime(2014, 9, 10, 0),
'USM00070026', derived=True)

assert_almost_equal(df['pressure'][5], 947.43, 2)
assert_almost_equal(df['reported_height'][5], 610., 2)
Expand Down