Skip to content

Commit

Permalink
Add test units for visualizion utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
ghiggi committed Nov 23, 2023
1 parent cea363f commit b9cda35
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 1 deletion.
3 changes: 2 additions & 1 deletion pyresample/test/test_boundary/test_sides.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""Test the BoundarySides objects."""

import pytest
import numpy as np
import pytest

from pyresample.boundary.sides import BoundarySides


Expand Down
87 changes: 87 additions & 0 deletions pyresample/test/test_visualization/test_geometries.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# pyresample, Resampling of remote sensing image data in python
#
# Copyright (C) 2010-2022 Pyresample developers
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""Test cartopy plotting utilities."""

import pytest
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
from shapely.geometry import Polygon
from pyresample.visualization.geometries import (
_add_map_background,
_check_subplot_kw,
_initialize_plot,
plot_geometries,
)


class TestPlotFunctions:
"""Test suite for the provided plotting functions."""

def test_add_map_background(self):
"""Test adding a map background to an axis."""
fig, ax = plt.subplots(subplot_kw={'projection': ccrs.PlateCarree()})
result_ax = _add_map_background(ax)
assert isinstance(result_ax, plt.Axes)

def test_check_subplot_kw_valid(self):
"""Test _check_subplot_kw with valid input."""
valid_kw = {'projection': ccrs.PlateCarree()}
assert _check_subplot_kw(valid_kw) == valid_kw

def test_check_subplot_kw_none(self):
"""Test _check_subplot_kw with None input."""
assert 'projection' in _check_subplot_kw(None)

def test_check_subplot_kw_invalid(self):
"""Test _check_subplot_kw with invalid input."""
with pytest.raises(TypeError):
_check_subplot_kw("invalid")

with pytest.raises(TypeError):
_check_subplot_kw(2)

with pytest.raises(TypeError):
_check_subplot_kw([2])

with pytest.raises(ValueError):
_check_subplot_kw({})

def test_initialize_plot_with_ax(self):
"""Test _initialize_plot with an existing ax."""
fig, ax = plt.subplots()
_, result_ax, initialized_here = _initialize_plot(ax=ax)
assert result_ax == ax
assert not initialized_here

@pytest.mark.parametrize("ax_provided", [True, False])
def test_plot_geometries(self, ax_provided):
"""Test plot_geometries function returns the correct type based on ax_provided."""
vertices1 = [(0,0),(0,1), (1, 0)]
vertices2 = [(0,0),(0,2), (2, 0)]
geometries = [Polygon(vertices1), Polygon(vertices2)]
crs = ccrs.PlateCarree()
ax = plt.axes(projection=crs) if ax_provided else None
result = plot_geometries(geometries, crs, ax=ax)

if ax_provided:
assert isinstance(result, plt.Axes)
else:
assert isinstance(result, plt.Figure)


0 comments on commit b9cda35

Please sign in to comment.