Skip to content

Commit

Permalink
Merge pull request #2520 from bsipocz/TST_fix_warnings
Browse files Browse the repository at this point in the history
TST: fix warnings and stop ignoring them
  • Loading branch information
bsipocz authored Oct 2, 2022
2 parents 59e6f84 + c19f188 commit 4b4af55
Show file tree
Hide file tree
Showing 26 changed files with 252 additions and 128 deletions.
4 changes: 3 additions & 1 deletion astroquery/alfalfa/tests/test_alfalfa.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ def patch_get_readable_fileobj(request):
@contextmanager
def get_readable_fileobj_mockreturn(filename, **kwargs):
file_obj = data_path(DATA_FILES['spectrum']) # TODO: add images option
yield open(file_obj, 'rb') # read as bytes, assuming FITS
# read as bytes, assuming FITS
with open(file_obj, 'rb') as inputfile:
yield inputfile

mp = request.getfixturevalue("monkeypatch")

Expand Down
7 changes: 3 additions & 4 deletions astroquery/besancon/tests/test_besancon.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,10 @@ def get_readable_fileobj_mockreturn(filename, **kwargs):
if isinstance(filename, str):
if '1376235131.430670' in filename:
is_binary = kwargs.get('encoding', None) == 'binary'
file_obj = open(data_path('1376235131.430670.resu'),
"r" + ('b' if is_binary else ''))
with open(data_path('1376235131.430670.resu'), "r" + ('b' if is_binary else '')) as file_obj:
yield file_obj
else:
file_obj = filename
yield file_obj
yield filename

mp = request.getfixturevalue("monkeypatch")

Expand Down
5 changes: 3 additions & 2 deletions astroquery/cadc/tests/test_cadctap.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"""
from io import BytesIO
from urllib.parse import urlsplit, parse_qs
from pathlib import Path
import os
import sys

Expand Down Expand Up @@ -177,7 +178,7 @@ def raise_for_status(self):
class CapabilitiesResponse:
def __init__(self):
caps_file = data_path('tap_caps.xml')
self.text = open(caps_file, 'r').read()
self.text = Path(caps_file).read_text()

def raise_for_status(self):
pass
Expand Down Expand Up @@ -406,7 +407,7 @@ def test_get_images():
Mock(side_effect=lambda x, y, z: ['https://some.url']))
def test_get_images_async():
with patch('astroquery.utils.commons.get_readable_fileobj', autospec=True) as readable_fobj_mock:
readable_fobj_mock.return_value = open(data_path('query_images.fits'), 'rb')
readable_fobj_mock.return_value = Path(data_path('query_images.fits'))

cadc = Cadc()
readable_objs = cadc.get_images_async('08h45m07.5s +54d18m00s',
Expand Down
23 changes: 20 additions & 3 deletions astroquery/cadc/tests/test_cadctap_remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import pytest
import os
import requests
from pathlib import Path
from datetime import datetime
from astropy.coordinates import SkyCoord
from astropy.io import fits
Expand Down Expand Up @@ -66,7 +67,8 @@ def test_query_region(self):
assert len(result) == len(result2[result2['collection'] == 'CFHT'])

# search for a target
results = cadc.query_region(SkyCoord.from_name('M31'), radius=0.016)
with pytest.warns(UserWarning, match='Radius should be of '):
results = cadc.query_region(SkyCoord.from_name('M31'), radius=0.016)
assert len(results) > 20

def test_query_name(self):
Expand Down Expand Up @@ -179,7 +181,6 @@ def test_authsession(self):
result = cadc.exec_sync(query)
assert len(result) == 0

@pytest.mark.xfail(reason='#2325')
def test_get_images(self):
cadc = Cadc()
coords = '08h45m07.5s +54d18m00s'
Expand Down Expand Up @@ -223,7 +224,6 @@ def test_get_images_against_AS(self):

assert len(filtered_resp_urls) == len(image_urls)

@pytest.mark.xfail(reason='#2325')
def test_get_images_async(self):
cadc = Cadc()
coords = '01h45m07.5s +23d18m00s'
Expand Down Expand Up @@ -293,3 +293,20 @@ def test_list_jobs(self):
if len(jobs) > 5:
jobs_subset = cadc.list_async_jobs(last=5)
assert len(jobs_subset) == 5

@pytest.mark.xfail(reason='https://github.com/astropy/astroquery/issues/2538')
def test_uploads(self, tmp_path):
cadc = Cadc()
# save a few observations on a local file
output_file = Path(tmp_path, 'my_observations.xml')
cadc.exec_sync("SELECT TOP 3 observationID FROM caom2.Observation",
output_file=output_file)
assert output_file.exists()

# now use them to join with the remote table
results = cadc.exec_sync(
"SELECT o.observationID, intent FROM caom2.Observation o JOIN "
"tap_upload.test_upload tu ON o.observationID=tu.observationID",
uploads={'test_upload': output_file})

assert len(results) == 3
13 changes: 9 additions & 4 deletions astroquery/casda/tests/test_casda.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import astropy.units as u
from astropy.table import Table, Column
from astropy.io.votable import parse
from astropy.io.votable.exceptions import W03, W50
from astroquery import log
import numpy as np

Expand Down Expand Up @@ -269,7 +270,8 @@ def test_query_region_async_box(patch_get):


def test_filter_out_unreleased():
all_records = parse(data_path('partial_unreleased.xml'), verify='warn').get_first_table().to_table()
with pytest.warns(W03):
all_records = parse(data_path('partial_unreleased.xml'), verify='warn').get_first_table().to_table()
assert all_records[0]['obs_release_date'] == '2017-08-02T03:51:19.728Z'
assert all_records[1]['obs_release_date'] == '2218-01-02T16:51:00.728Z'
assert all_records[2]['obs_release_date'] == ''
Expand Down Expand Up @@ -331,7 +333,8 @@ def test_stage_data(patch_get):
casda = Casda()
fake_login(casda, USERNAME, PASSWORD)
casda.POLL_INTERVAL = 1
urls = casda.stage_data(table, verbose=True)
with pytest.warns(W50, match="Invalid unit string 'pixels'"):
urls = casda.stage_data(table, verbose=True)
assert urls == ['http://casda.csiro.au/download/web/111-000-111-000/askap_img.fits.checksum',
'http://casda.csiro.au/download/web/111-000-111-000/askap_img.fits']

Expand All @@ -348,7 +351,8 @@ def test_cutout(patch_get):
casda = Casda()
fake_login(casda, USERNAME, PASSWORD)
casda.POLL_INTERVAL = 1
urls = casda.cutout(table, coordinates=centre, radius=radius, verbose=True)
with pytest.warns(W50, match="Invalid unit string 'pixels'"):
urls = casda.cutout(table, coordinates=centre, radius=radius, verbose=True)
assert urls == ['http://casda.csiro.au/download/web/111-000-111-000/cutout.fits.checksum',
'http://casda.csiro.au/download/web/111-000-111-000/cutout.fits']

Expand All @@ -363,7 +367,8 @@ def test_cutout_no_args(patch_get):
casda.POLL_INTERVAL = 1
with pytest.raises(ValueError,
match=r"Please provide cutout parameters such as coordinates, band or channel\."):
casda.cutout(table)
with pytest.warns(W50, match="Invalid unit string 'pixels'"):
casda.cutout(table)


def test_cutout_unauthorised(patch_get):
Expand Down
41 changes: 21 additions & 20 deletions astroquery/eso/tests/test_eso_remote.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Licensed under a 3-clause BSD style license - see LICENSE.rst
import numpy as np
import pytest
import warnings

from astroquery.exceptions import LoginError, NoResultsWarning
from astroquery.eso import Eso
Expand All @@ -15,6 +16,9 @@
# TODO: make this a configuration item
SKIP_SLOW = True

SGRA_SURVEYS = ['195.B-0283', 'GIRAFFE', 'HARPS', 'HAWKI', 'KMOS',
'MW-BULGE-PSFPHOT', 'VPHASplus', 'VVV', 'VVVX', 'XSHOOTER']


@pytest.mark.remote_data
class TestEso:
Expand Down Expand Up @@ -81,6 +85,7 @@ def test_empty_return(self):
eso = Eso()
surveys = eso.list_surveys(cache=False)
assert len(surveys) > 0

# Avoid SESAME
with pytest.warns(NoResultsWarning):
result_s = eso.query_surveys(surveys[0], coord1=202.469575,
Expand Down Expand Up @@ -142,6 +147,8 @@ def test_retrieve_data_and_calib(self):
# list.
assert len(result) == 1

# TODO: remove filter when https://github.com/astropy/astroquery/issues/2539 is fixed
@pytest.mark.filterwarnings("ignore::pytest.PytestUnraisableExceptionWarning")
@pytest.mark.parametrize('instrument', instrument_list)
def test_help(self, instrument):
eso = Eso()
Expand All @@ -166,7 +173,7 @@ def test_each_instrument_SgrAstar(self, tmp_path):
instruments = eso.list_instruments(cache=False)

for instrument in instruments:
with pytest.warns(None) as record:
with warnings.catch_warnings(record=True) as record:
result_i = eso.query_instrument(instrument, coord1=266.41681662,
coord2=-29.00782497, cache=False)
# Sometimes there are ResourceWarnings, we ignore those for this test
Expand All @@ -175,36 +182,30 @@ def test_each_instrument_SgrAstar(self, tmp_path):
else:
assert len(result_i) > 0

@pytest.mark.filterwarnings("ignore::ResourceWarning")
def test_each_survey_SgrAstar(self, tmp_path):
def test_each_survey_and_SgrAstar(self, tmp_path):
eso = Eso()
eso.cache_location = tmp_path
eso.ROW_LIMIT = 5


surveys = eso.list_surveys(cache=False)
for survey in surveys:
with pytest.warns(None) as record:
if survey in SGRA_SURVEYS:
result_s = eso.query_surveys(survey, coord1=266.41681662,
coord2=-29.00782497,
box='01 00 00',
cache=False)
# Sometimes there are ResourceWarnings, we ignore those for this test
if len(record) > 0 and NoResultsWarning in {record[i].category for i in range(len(record))}:
assert len(result_s) > 0
else:
with pytest.warns(NoResultsWarning):
result_s = eso.query_surveys(survey, coord1=266.41681662,
coord2=-29.00782497,
box='01 00 00',
cache=False)
assert result_s is None
else:
print([record[i].message for i in range(len(record))])
assert len(result_s) > 0

@pytest.mark.skipif("SKIP_SLOW")
@pytest.mark.parametrize('cache', (False, True))
def test_each_survey_nosource(self, tmp_path, cache):
eso = Eso()
eso.cache_location = tmp_path
eso.ROW_LIMIT = 5

surveys = eso.list_surveys(cache=cache)
for survey in surveys:
# just test that it doesn't crash
eso.query_surveys(survey, cache=cache)
generic_result = eso.query_surveys(survey)
assert len(generic_result) > 0

def test_mixed_case_instrument(self, tmp_path):
eso = Eso()
Expand Down
13 changes: 9 additions & 4 deletions astroquery/exoplanet_orbit_database/exoplanet_orbit_database.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Licensed under a 3-clause BSD style license - see LICENSE.rst
import json
import os
import warnings

from astropy.utils.data import download_file
from astropy.io import ascii
Expand Down Expand Up @@ -30,9 +31,9 @@ def __init__(self):
def param_units(self):
if self._param_units is None:
module_dir = os.path.dirname(os.path.abspath(__file__))
units_file = open(os.path.join(module_dir, 'data',
'exoplanet_orbit_database_units.json'))
self._param_units = json.load(units_file)
filename = os.path.join(module_dir, 'data', 'exoplanet_orbit_database_units.json')
with open(filename) as units_file:
self._param_units = json.load(units_file)

return self._param_units

Expand Down Expand Up @@ -82,7 +83,11 @@ def get_table(self, cache=True, show_progress=True, table_path=None):
except ValueError:
print(f"WARNING: Unit {self.param_units[col]} not recognised")

self._table = QTable(exoplanets_table)
# Masked quantities are not supported in older astropy, warnings are raised for <v5.0
with warnings.catch_warnings():
warnings.filterwarnings('ignore', message='dropping mask in Quantity column',
category=UserWarning)
self._table = QTable(exoplanets_table)

return self._table

Expand Down
3 changes: 2 additions & 1 deletion astroquery/heasarc/tests/test_heasarc_remote_isdc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pytest
import requests
from astropy.time import Time, TimeDelta
import astropy.units as u

from ...heasarc import Heasarc, Conf
from ...utils import commons
Expand Down Expand Up @@ -83,7 +84,7 @@ def test_compare_time(self, patch_get):

heasarc = Heasarc()

month_ago = (Time.now() - TimeDelta(30)).isot[:10]
month_ago = (Time.now() - TimeDelta(30 * u.day)).isot[:10]
today = Time.now().isot[:10]
T = month_ago + " .. " + today

Expand Down
7 changes: 3 additions & 4 deletions astroquery/ipac/irsa/ibe/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
https://irsa.ipac.caltech.edu/ibe/
"""


import os
import webbrowser
from bs4 import BeautifulSoup
Expand Down Expand Up @@ -270,7 +269,7 @@ def list_missions(self, cache=True):
response = self._request('GET', url, timeout=self.TIMEOUT,
cache=cache)

root = BeautifulSoup(response.text)
root = BeautifulSoup(response.text, 'html5lib')
links = root.findAll('a')

missions = [os.path.basename(a.attrs['href'].rstrip('/'))
Expand Down Expand Up @@ -308,7 +307,7 @@ def list_datasets(self, mission=None, cache=True):
response = self._request('GET', url, timeout=self.TIMEOUT,
cache=cache)

root = BeautifulSoup(response.text)
root = BeautifulSoup(response.text, 'html5lib')
links = root.findAll('a')
datasets = [a.text for a in links
if a.attrs['href'].count('/') >= 4 # shown as '..'; ignore
Expand Down Expand Up @@ -362,7 +361,7 @@ def list_tables(self, mission=None, dataset=None, cache=True):
response = self._request('GET', url, timeout=self.TIMEOUT,
cache=cache)

root = BeautifulSoup(response.text)
root = BeautifulSoup(response.text, 'html5lib')
return [tr.find('td').string for tr in root.findAll('tr')[1:]]

# Unfortunately, the URL construction for each data set is different, and
Expand Down
4 changes: 2 additions & 2 deletions astroquery/ipac/irsa/sha/tests/test_sha.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ def data_path(filename):
def get_mockreturn(url, params=None, stream=False, timeout=10, **kwargs):
if stream:
filename = data_path(DATA_FILES['img'])
return MockResponse(open(filename, 'rb').read(),
content_type='image/fits', **kwargs)
with open(filename, 'rb') as infile:
return MockResponse(infile.read(), content_type='image/fits', **kwargs)
elif params['RA'] == 163.6136:
filename = data_path(DATA_FILES['pos_t'])
elif params['NAIFID'] == 2003226:
Expand Down
5 changes: 3 additions & 2 deletions astroquery/ipac/irsa/tests/test_irsa_remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ def test_query_region_async_polygon(self):

def test_query_region_polygon(self):
polygon = [(10.1, 10.1), (10.0, 10.1), (10.0, 10.0)]
result = Irsa.query_region(
"m31", catalog="fp_psc", spatial="Polygon", polygon=polygon, cache=False)
with pytest.warns(UserWarning, match='Polygon endpoints are being interpreted'):
result = Irsa.query_region("m31", catalog="fp_psc", spatial="Polygon",
polygon=polygon, cache=False)

assert isinstance(result, Table)

Expand Down
8 changes: 6 additions & 2 deletions astroquery/ipac/ned/tests/test_ned.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Licensed under a 3-clause BSD style license - see LICENSE.rst

import os
from contextlib import contextmanager

from numpy import testing as npt
import pytest
Expand Down Expand Up @@ -47,12 +48,14 @@ def patch_get(request):

@pytest.fixture
def patch_get_readable_fileobj(request):
@contextmanager
def get_readable_fileobj_mockreturn(filename, cache=True, encoding=None,
show_progress=True):
# Need to read FITS files with binary encoding: should raise error
# otherwise
assert encoding == 'binary'
return open(data_path(DATA_FILES['image']), 'rb')
with open(data_path(DATA_FILES['image']), 'rb') as infile:
yield infile

mp = request.getfixturevalue("monkeypatch")

Expand Down Expand Up @@ -138,7 +141,8 @@ def test_photometry(patch_get):


def test_extract_image_urls():
html_in = open(data_path(DATA_FILES['extract_urls']), 'r').read()
with open(data_path(DATA_FILES['extract_urls']), 'r') as infile:
html_in = infile.read()
url_list = ned.core.Ned._extract_image_urls(html_in)
assert len(url_list) == 5
for url in url_list:
Expand Down
Loading

0 comments on commit 4b4af55

Please sign in to comment.