Skip to content

Commit

Permalink
Remove __file__ usage in test units for test_files path (#563)
Browse files Browse the repository at this point in the history
Remove `__file__` usage in test units for `test_files` path
  • Loading branch information
mraspaud authored Dec 8, 2023
2 parents c39f51e + de6049b commit 78b19ca
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 53 deletions.
18 changes: 10 additions & 8 deletions pyresample/test/test_area_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,16 @@

import numpy as np

from pyresample.test.utils import TEST_FILES_PATH


class TestLegacyAreaParser(unittest.TestCase):
"""Test legacy .cfg parsing."""

def test_area_parser_legacy(self):
"""Test legacy area parser."""
from pyresample import parse_area_file
ease_nh, ease_sh = parse_area_file(os.path.join(os.path.dirname(__file__), 'test_files', 'areas.cfg'),
ease_nh, ease_sh = parse_area_file(os.path.join(TEST_FILES_PATH, 'areas.cfg'),
'ease_nh', 'ease_sh')

# pyproj 2.0+ adds some extra parameters
Expand Down Expand Up @@ -63,7 +65,7 @@ def test_area_parser_legacy(self):

def test_load_area(self):
from pyresample import load_area
ease_nh = load_area(os.path.join(os.path.dirname(__file__), 'test_files', 'areas.cfg'), 'ease_nh')
ease_nh = load_area(os.path.join(TEST_FILES_PATH, 'areas.cfg'), 'ease_nh')
# pyproj 2.0+ adds some extra parameters
projection = ("{'R': '6371228', 'lat_0': '90', 'lon_0': '0', "
"'no_defs': 'None', 'proj': 'laea', 'type': 'crs', "
Expand All @@ -87,11 +89,11 @@ def test_area_file_not_found_exception(self):
def test_not_found_exception(self):
from pyresample.area_config import AreaNotFound, parse_area_file
self.assertRaises(AreaNotFound, parse_area_file,
os.path.join(os.path.dirname(__file__), 'test_files', 'areas.cfg'), 'no_area')
os.path.join(TEST_FILES_PATH, 'areas.cfg'), 'no_area')

def test_commented(self):
from pyresample import parse_area_file
areas = parse_area_file(os.path.join(os.path.dirname(__file__), 'test_files', 'areas.cfg'))
areas = parse_area_file(os.path.join(TEST_FILES_PATH, 'areas.cfg'))
self.assertNotIn('commented', [area.name for area in areas])


Expand All @@ -101,7 +103,7 @@ class TestYAMLAreaParser(unittest.TestCase):
def test_area_parser_yaml(self):
"""Test YAML area parser."""
from pyresample import parse_area_file
test_area_file = os.path.join(os.path.dirname(__file__), 'test_files', 'areas.yaml')
test_area_file = os.path.join(TEST_FILES_PATH, 'areas.yaml')
test_areas = parse_area_file(test_area_file, 'ease_nh', 'ease_sh', 'test_meters', 'test_degrees',
'test_latlong')
ease_nh, ease_sh, test_m, test_deg, test_latlong = test_areas
Expand Down Expand Up @@ -158,7 +160,7 @@ def test_dynamic_area_parser_yaml(self):
"""Test YAML area parser on dynamic areas."""
from pyresample import parse_area_file
from pyresample.geometry import DynamicAreaDefinition
test_area_file = os.path.join(os.path.dirname(__file__), 'test_files', 'areas.yaml')
test_area_file = os.path.join(TEST_FILES_PATH, 'areas.yaml')
test_area = parse_area_file(test_area_file, 'test_dynamic_resolution')[0]

self.assertIsInstance(test_area, DynamicAreaDefinition)
Expand All @@ -168,7 +170,7 @@ def test_dynamic_area_parser_yaml(self):
# lat/lon
from pyresample import parse_area_file
from pyresample.geometry import DynamicAreaDefinition
test_area_file = os.path.join(os.path.dirname(__file__), 'test_files', 'areas.yaml')
test_area_file = os.path.join(TEST_FILES_PATH, 'areas.yaml')
test_area = parse_area_file(test_area_file, 'test_dynamic_resolution_ll')[0]

self.assertIsInstance(test_area, DynamicAreaDefinition)
Expand All @@ -178,7 +180,7 @@ def test_dynamic_area_parser_yaml(self):
def test_dynamic_area_parser_passes_resolution(self):
"""Test that the resolution from the file is passed to a dynamic area."""
from pyresample import parse_area_file
test_area_file = os.path.join(os.path.dirname(__file__), 'test_files', 'areas.yaml')
test_area_file = os.path.join(TEST_FILES_PATH, 'areas.yaml')
test_area = parse_area_file(test_area_file, 'omerc_bb_1000')[0]
assert test_area.resolution == (1000, 1000)

Expand Down
8 changes: 3 additions & 5 deletions pyresample/test/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import numpy

from pyresample import geometry, image, utils
from pyresample.test.utils import TEST_FILES_PATH


class Test(unittest.TestCase):
Expand Down Expand Up @@ -108,8 +109,7 @@ def test_masked_image(self):
area_con = msg_con.resample(self.area_def)
res = area_con.image_data
resampled_mask = res.mask.astype('int')
expected = numpy.fromfile(os.path.join(os.path.dirname(__file__),
'test_files', 'mask_grid.dat'),
expected = numpy.fromfile(os.path.join(TEST_FILES_PATH, 'mask_grid.dat'),
sep=' ').reshape((800, 800))
self.assertTrue(numpy.array_equal(resampled_mask, expected))

Expand All @@ -123,9 +123,7 @@ def test_masked_image_fill(self):
area_con = msg_con.resample(self.area_def)
res = area_con.image_data
resampled_mask = res.mask.astype('int')
expected = numpy.fromfile(os.path.join(os.path.dirname(__file__),
'test_files',
'mask_grid.dat'),
expected = numpy.fromfile(os.path.join(TEST_FILES_PATH, 'mask_grid.dat'),
sep=' ').reshape((800, 800))
self.assertTrue(numpy.array_equal(resampled_mask, expected))

Expand Down
38 changes: 10 additions & 28 deletions pyresample/test/test_kd_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import pytest

from pyresample import geometry, kd_tree, utils
from pyresample.test.utils import catch_warnings
from pyresample.test.utils import TEST_FILES_PATH, catch_warnings


class Test(unittest.TestCase):
Expand Down Expand Up @@ -496,13 +496,9 @@ def test_masked_nearest(self):
masked_data = np.ma.array(data, mask=mask)
res = kd_tree.resample_nearest(swath_def, masked_data.ravel(),
self.area_def, 50000, segments=1)
expected_mask = np.fromfile(os.path.join(os.path.dirname(__file__),
'test_files',
'mask_test_nearest_mask.dat'),
expected_mask = np.fromfile(os.path.join(TEST_FILES_PATH, 'mask_test_nearest_mask.dat'),
sep=' ').reshape((800, 800))
expected_data = np.fromfile(os.path.join(os.path.dirname(__file__),
'test_files',
'mask_test_nearest_data.dat'),
expected_data = np.fromfile(os.path.join(TEST_FILES_PATH, 'mask_test_nearest_data.dat'),
sep=' ').reshape((800, 800))
self.assertTrue(np.array_equal(expected_mask, res.mask))
self.assertTrue(np.array_equal(expected_data, res.data))
Expand Down Expand Up @@ -531,13 +527,9 @@ def test_masked_gauss(self):
masked_data = np.ma.array(data, mask=mask)
res = kd_tree.resample_gauss(swath_def, masked_data.ravel(),
self.area_def, 50000, 25000, segments=1)
expected_mask = np.fromfile(os.path.join(os.path.dirname(__file__),
'test_files',
'mask_test_mask.dat'),
expected_mask = np.fromfile(os.path.join(TEST_FILES_PATH, 'mask_test_mask.dat'),
sep=' ').reshape((800, 800))
expected_data = np.fromfile(os.path.join(os.path.dirname(__file__),
'test_files',
'mask_test_data.dat'),
expected_data = np.fromfile(os.path.join(TEST_FILES_PATH, 'mask_test_data.dat'),
sep=' ').reshape((800, 800))
expected = expected_data.sum()
cross_sum = res.data.sum()
Expand All @@ -552,9 +544,7 @@ def test_masked_fill_float(self):
swath_def = geometry.SwathDefinition(lons=lons, lats=lats)
res = kd_tree.resample_nearest(swath_def, data.ravel(),
self.area_def, 50000, fill_value=None, segments=1)
expected_fill_mask = np.fromfile(os.path.join(os.path.dirname(__file__),
'test_files',
'mask_test_fill_value.dat'),
expected_fill_mask = np.fromfile(os.path.join(TEST_FILES_PATH, 'mask_test_fill_value.dat'),
sep=' ').reshape((800, 800))
fill_mask = res.mask
self.assertTrue(np.array_equal(fill_mask, expected_fill_mask))
Expand All @@ -566,9 +556,7 @@ def test_masked_fill_int(self):
swath_def = geometry.SwathDefinition(lons=lons, lats=lats)
res = kd_tree.resample_nearest(swath_def, data.ravel(),
self.area_def, 50000, fill_value=None, segments=1)
expected_fill_mask = np.fromfile(os.path.join(os.path.dirname(__file__),
'test_files',
'mask_test_fill_value.dat'),
expected_fill_mask = np.fromfile(os.path.join(TEST_FILES_PATH, 'mask_test_fill_value.dat'),
sep=' ').reshape((800, 800))
fill_mask = res.mask
self.assertTrue(np.array_equal(fill_mask, expected_fill_mask))
Expand All @@ -586,9 +574,7 @@ def test_masked_full(self):
masked_data.ravel(
), self.area_def, 50000,
fill_value=None, segments=1)
expected_fill_mask = np.fromfile(os.path.join(os.path.dirname(__file__),
'test_files',
'mask_test_full_fill.dat'),
expected_fill_mask = np.fromfile(os.path.join(TEST_FILES_PATH, 'mask_test_full_fill.dat'),
sep=' ').reshape((800, 800))
fill_mask = res.mask

Expand All @@ -614,9 +600,7 @@ def test_masked_full_multi(self):
res = kd_tree.resample_nearest(swath_def,
masked_data, self.area_def, 50000,
fill_value=None, segments=1)
expected_fill_mask = np.fromfile(os.path.join(os.path.dirname(__file__),
'test_files',
'mask_test_full_fill_multi.dat'),
expected_fill_mask = np.fromfile(os.path.join(TEST_FILES_PATH, 'mask_test_full_fill_multi.dat'),
sep=' ').reshape((800, 800, 3))
fill_mask = res.mask
cross_sum = res.sum()
Expand Down Expand Up @@ -750,9 +734,7 @@ def test_masked_multi_from_sample(self):
valid_input_index,
valid_output_index, index_array,
fill_value=None)
expected_fill_mask = np.fromfile(os.path.join(os.path.dirname(__file__),
'test_files',
'mask_test_full_fill_multi.dat'),
expected_fill_mask = np.fromfile(os.path.join(TEST_FILES_PATH, 'mask_test_full_fill_multi.dat'),
sep=' ').reshape((800, 800, 3))
fill_mask = res.mask
self.assertTrue(np.array_equal(fill_mask, expected_fill_mask))
Expand Down
13 changes: 7 additions & 6 deletions pyresample/test/test_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

import numpy as np

from pyresample.test.utils import TEST_FILES_PATH

try:
import matplotlib
matplotlib.use('Agg')
Expand All @@ -48,8 +50,7 @@
class Test(unittest.TestCase):
"""Test the plot utilities."""

filename = os.path.abspath(os.path.join(os.path.dirname(__file__),
'test_files', 'ssmis_swath.npz'))
filename = os.path.abspath(os.path.join(TEST_FILES_PATH, 'ssmis_swath.npz'))
data = np.load(filename)['data']
lons = data[:, 0].astype(np.float64)
lats = data[:, 1].astype(np.float64)
Expand Down Expand Up @@ -79,7 +80,7 @@ def test_ellps2axis(self):
def test_area_def2basemap(self):
"""Test the area to Basemap object conversion function."""
from pyresample import parse_area_file, plot
area_def = parse_area_file(os.path.join(os.path.dirname(__file__), 'test_files', 'areas.yaml'), 'ease_sh')[0]
area_def = parse_area_file(os.path.join(TEST_FILES_PATH, 'areas.yaml'), 'ease_sh')[0]
bmap = plot.area_def2basemap(area_def)
self.assertTrue(bmap.rmajor == bmap.rminor and bmap.rmajor == 6371228.0,
'Failed to create Basemap object')
Expand Down Expand Up @@ -158,7 +159,7 @@ def test_translate_coast_res(self):
def test_plate_carreeplot(self):
"""Test the Plate Caree plotting functionality."""
from pyresample import geometry, kd_tree, parse_area_file, plot
area_def = parse_area_file(os.path.join(os.path.dirname(__file__), 'test_files', 'areas.yaml'), 'pc_world')[0]
area_def = parse_area_file(os.path.join(TEST_FILES_PATH, 'areas.yaml'), 'pc_world')[0]
swath_def = geometry.SwathDefinition(self.lons, self.lats)
result = kd_tree.resample_nearest(swath_def, self.tb37v, area_def,
radius_of_influence=20000,
Expand All @@ -171,7 +172,7 @@ def test_plate_carreeplot(self):
def test_easeplot(self):
"""Test the plotting on the ease grid area."""
from pyresample import geometry, kd_tree, parse_area_file, plot
area_def = parse_area_file(os.path.join(os.path.dirname(__file__), 'test_files', 'areas.yaml'), 'ease_sh')[0]
area_def = parse_area_file(os.path.join(TEST_FILES_PATH, 'areas.yaml'), 'ease_sh')[0]
swath_def = geometry.SwathDefinition(self.lons, self.lats)
result = kd_tree.resample_nearest(swath_def, self.tb37v, area_def,
radius_of_influence=20000,
Expand All @@ -181,7 +182,7 @@ def test_easeplot(self):
def test_orthoplot(self):
"""Test the ortho plotting."""
from pyresample import geometry, kd_tree, parse_area_file, plot
area_def = parse_area_file(os.path.join(os.path.dirname(__file__), 'test_files', 'areas.cfg'), 'ortho')[0]
area_def = parse_area_file(os.path.join(TEST_FILES_PATH, 'areas.cfg'), 'ortho')[0]
swath_def = geometry.SwathDefinition(self.lons, self.lats)
result = kd_tree.resample_nearest(swath_def, self.tb37v, area_def,
radius_of_influence=20000,
Expand Down
5 changes: 2 additions & 3 deletions pyresample/test/test_swath.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,15 @@
import numpy as np

from pyresample import geometry, kd_tree
from pyresample.test.utils import catch_warnings
from pyresample.test.utils import TEST_FILES_PATH, catch_warnings

warnings.simplefilter("always")


class Test(unittest.TestCase):
"""Tests for swath definitions."""

filename = os.path.abspath(os.path.join(os.path.dirname(__file__),
'test_files', 'ssmis_swath.npz'))
filename = os.path.abspath(os.path.join(TEST_FILES_PATH, 'ssmis_swath.npz'))
data = np.load(filename)['data']
lons = data[:, 0].astype(np.float64)
lats = data[:, 1].astype(np.float64)
Expand Down
7 changes: 4 additions & 3 deletions pyresample/test/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

import pyresample
from pyresample.test.utils import (
TEST_FILES_PATH,
assert_future_geometry,
create_test_latitude,
create_test_longitude,
Expand Down Expand Up @@ -238,7 +239,7 @@ def test_def2yaml_converter(self):
import tempfile

from pyresample import convert_def_to_yaml, parse_area_file
def_file = os.path.join(os.path.dirname(__file__), 'test_files', 'areas.cfg')
def_file = os.path.join(TEST_FILES_PATH, 'areas.cfg')
filehandle, yaml_file = tempfile.mkstemp()
os.close(filehandle)
try:
Expand Down Expand Up @@ -460,12 +461,12 @@ class TestLoadCFAreaPublic:
"""Test public API load_cf_area() for loading an AreaDefinition from netCDF/CF files."""

def test_load_cf_no_exist(self):
cf_file = os.path.join(os.path.dirname(__file__), 'test_files', 'does_not_exist.nc')
cf_file = os.path.join(TEST_FILES_PATH, 'does_not_exist.nc')
with pytest.raises(FileNotFoundError):
load_cf_area(cf_file)

def test_load_cf_from_not_nc(self):
cf_file = os.path.join(os.path.dirname(__file__), 'test_files', 'areas.yaml')
cf_file = os.path.join(TEST_FILES_PATH, 'areas.yaml')
with pytest.raises((ValueError, OSError)):
load_cf_area(cf_file)

Expand Down
4 changes: 4 additions & 0 deletions pyresample/test/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
This mostly takes from astropy's method for checking collected_warnings during
tests.
"""
import os
import sys
import types
import warnings
Expand All @@ -39,6 +40,9 @@
AstropyPendingDeprecationWarning = None


TEST_FILES_PATH = os.path.join(os.path.dirname(__file__), 'test_files')


def treat_deprecations_as_exceptions():
"""Turn all DeprecationWarnings into exceptions.
Expand Down

0 comments on commit 78b19ca

Please sign in to comment.