Skip to content

Commit

Permalink
o Fix all test cases, add unittest.TestCase inheritance. TestCase all…
Browse files Browse the repository at this point in the history
…ows the class to inherit unittest’s testing functionality, such as assertEqual, assertTrue, and automatic test discovery. This change is needed for writing and executing proper unit tests in a consistent framework, improving code quality and enabling integration with test runners.
  • Loading branch information
rajeeja committed Dec 6, 2024
1 parent 1d20619 commit 9859bb6
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 89 deletions.
10 changes: 6 additions & 4 deletions test/test_cross_sections.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import uxarray as ux
import pytest
import numpy as np

from unittest import TestCase
from pathlib import Path
import os

Expand All @@ -17,7 +19,7 @@



class TestQuadHex:
class TestQuadHex(TestCase):
"""The quad hexagon grid contains four faces.
Top Left Face: Index 1
Expand Down Expand Up @@ -108,7 +110,7 @@ def test_constant_lon_cross_section_uxds(self):
uxds['t2m'].cross_section.constant_longitude(lon=10.0, )


class TestCubeSphere:
class TestCubeSphere(TestCase):

def test_north_pole(self):
uxgrid = ux.open_grid(cube_sphere_grid)
Expand All @@ -132,7 +134,7 @@ def test_south_pole(self):



class TestCandidateFacesUsingBounds:
class TestCandidateFacesUsingBounds(TestCase):

def test_constant_lat(self):
bounds = np.array([
Expand Down Expand Up @@ -173,4 +175,4 @@ def test_constant_lat_out_of_bounds(self):
face_bounds_lat=bounds_rad[:, 0],
)

assert len(candidate_faces) == 0
assert len(candidate_faces) == 0
44 changes: 22 additions & 22 deletions test/test_esmf.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import uxarray as ux

from unittest import TestCase
import os
from pathlib import Path

Expand All @@ -12,36 +12,36 @@



class Test_ESMF(TestCase):
def test_read_esmf(self):
"""Tests the reading of an ESMF grid file and its encoding into the UGRID
conventions."""

def test_read_esmf():
"""Tests the reading of an ESMF grid file and its encoding into the UGRID
conventions."""

uxgrid = ux.open_grid(esmf_ne30_grid_path)
uxgrid = ux.open_grid(esmf_ne30_grid_path)

dims = ['n_node', 'n_face', 'n_max_face_nodes']
dims = ['n_node', 'n_face', 'n_max_face_nodes']

coords = ['node_lon', 'node_lat', 'face_lon', 'face_lat']
coords = ['node_lon', 'node_lat', 'face_lon', 'face_lat']

conns = ['face_node_connectivity', 'n_nodes_per_face']
conns = ['face_node_connectivity', 'n_nodes_per_face']

for dim in dims:
assert dim in uxgrid._ds.dims
for dim in dims:
assert dim in uxgrid._ds.dims

for coord in coords:
assert coord in uxgrid._ds
for coord in coords:
assert coord in uxgrid._ds

for conn in conns:
assert conn in uxgrid._ds
for conn in conns:
assert conn in uxgrid._ds

def test_read_esmf_dataset():
"""Tests the constructing of a UxDataset from an ESMF Grid and Data
File."""
def test_read_esmf_dataset(self):
"""Tests the constructing of a UxDataset from an ESMF Grid and Data
File."""

uxds = ux.open_dataset(esmf_ne30_grid_path, esmf_ne30_data_path)
uxds = ux.open_dataset(esmf_ne30_grid_path, esmf_ne30_data_path)


dims = ['n_node', 'n_face']
dims = ['n_node', 'n_face']

for dim in dims:
assert dim in uxds.dims
for dim in dims:
assert dim in uxds.dims
98 changes: 49 additions & 49 deletions test/test_from_topology.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import uxarray as ux

from unittest import TestCase
from uxarray.constants import INT_FILL_VALUE
import numpy.testing as nt
import os
Expand All @@ -18,70 +18,70 @@



class TestFromTopology(TestCase):
def test_minimal_class_method(self):
"""Tests the minimal required variables for constructing a grid using the
from topology class method."""

def test_minimal_class_method():
"""Tests the minimal required variables for constructing a grid using the
from topology class method."""
for grid_path in GRID_PATHS:
uxgrid = ux.open_grid(grid_path)

for grid_path in GRID_PATHS:
uxgrid = ux.open_grid(grid_path)
uxgrid_ft = ux.Grid.from_topology(node_lon=uxgrid.node_lon.values,
node_lat=uxgrid.node_lat.values,
face_node_connectivity=uxgrid.face_node_connectivity.values,
fill_value=INT_FILL_VALUE,
start_index=0)

uxgrid_ft = ux.Grid.from_topology(node_lon=uxgrid.node_lon.values,
node_lat=uxgrid.node_lat.values,
face_node_connectivity=uxgrid.face_node_connectivity.values,
fill_value=INT_FILL_VALUE,
start_index=0)
nt.assert_array_equal(uxgrid.node_lon.values, uxgrid_ft.node_lon.values)
nt.assert_array_equal(uxgrid.node_lat.values, uxgrid_ft.node_lat.values)
nt.assert_array_equal(uxgrid.face_node_connectivity.values, uxgrid_ft.face_node_connectivity.values)

nt.assert_array_equal(uxgrid.node_lon.values, uxgrid_ft.node_lon.values)
nt.assert_array_equal(uxgrid.node_lat.values, uxgrid_ft.node_lat.values)
nt.assert_array_equal(uxgrid.face_node_connectivity.values, uxgrid_ft.face_node_connectivity.values)

def test_minimal_api(self):
"""Tests the minimal required variables for constructing a grid using the
``ux.open_dataset`` method."""

def test_minimal_api():
"""Tests the minimal required variables for constructing a grid using the
``ux.open_dataset`` method."""
for grid_path in GRID_PATHS:
uxgrid = ux.open_grid(grid_path)

for grid_path in GRID_PATHS:
uxgrid = ux.open_grid(grid_path)
uxgrid_ft = ux.Grid.from_topology(node_lon=uxgrid.node_lon.values,
node_lat=uxgrid.node_lat.values,
face_node_connectivity=uxgrid.face_node_connectivity.values,
fill_value=INT_FILL_VALUE,
start_index=0)

uxgrid_ft = ux.Grid.from_topology(node_lon=uxgrid.node_lon.values,
node_lat=uxgrid.node_lat.values,
face_node_connectivity=uxgrid.face_node_connectivity.values,
fill_value=INT_FILL_VALUE,
start_index=0)
grid_topology = {'node_lon': uxgrid.node_lon.values,
'node_lat': uxgrid.node_lat.values,
'face_node_connectivity': uxgrid.face_node_connectivity.values,
'fill_value': INT_FILL_VALUE,
'start_index': 0}

grid_topology = {'node_lon': uxgrid.node_lon.values,
'node_lat': uxgrid.node_lat.values,
'face_node_connectivity': uxgrid.face_node_connectivity.values,
'fill_value': INT_FILL_VALUE,
'start_index': 0}
uxgrid_ft = ux.open_grid(grid_topology)

uxgrid_ft = ux.open_grid(grid_topology)

nt.assert_array_equal(uxgrid.node_lon.values, uxgrid_ft.node_lon.values)
nt.assert_array_equal(uxgrid.node_lat.values, uxgrid_ft.node_lat.values)
nt.assert_array_equal(uxgrid.face_node_connectivity.values, uxgrid_ft.face_node_connectivity.values)
nt.assert_array_equal(uxgrid.node_lon.values, uxgrid_ft.node_lon.values)
nt.assert_array_equal(uxgrid.node_lat.values, uxgrid_ft.node_lat.values)
nt.assert_array_equal(uxgrid.face_node_connectivity.values, uxgrid_ft.face_node_connectivity.values)


def test_dataset():
uxds = ux.open_dataset(GRID_PATHS[0], GRID_PATHS[0])
def test_dataset(self):
uxds = ux.open_dataset(GRID_PATHS[0], GRID_PATHS[0])

grid_topology = {'node_lon': uxds.uxgrid.node_lon.values,
'node_lat': uxds.uxgrid.node_lat.values,
'face_node_connectivity': uxds.uxgrid.face_node_connectivity.values,
'fill_value': INT_FILL_VALUE,
'start_index': 0,
"dims_dict" : {"nVertices": "n_node"}}
grid_topology = {'node_lon': uxds.uxgrid.node_lon.values,
'node_lat': uxds.uxgrid.node_lat.values,
'face_node_connectivity': uxds.uxgrid.face_node_connectivity.values,
'fill_value': INT_FILL_VALUE,
'start_index': 0,
"dims_dict" : {"nVertices": "n_node"}}


uxds_ft = ux.open_grid(grid_topology, GRID_PATHS[1])
uxds_ft = ux.open_grid(grid_topology, GRID_PATHS[1])

uxgrid = uxds.uxgrid
uxgrid_ft = uxds_ft
uxgrid = uxds.uxgrid
uxgrid_ft = uxds_ft


nt.assert_array_equal(uxgrid.node_lon.values, uxgrid_ft.node_lon.values)
nt.assert_array_equal(uxgrid.node_lat.values, uxgrid_ft.node_lat.values)
nt.assert_array_equal(uxgrid.face_node_connectivity.values, uxgrid_ft.face_node_connectivity.values)
nt.assert_array_equal(uxgrid.node_lon.values, uxgrid_ft.node_lon.values)
nt.assert_array_equal(uxgrid.node_lat.values, uxgrid_ft.node_lat.values)
nt.assert_array_equal(uxgrid.face_node_connectivity.values, uxgrid_ft.face_node_connectivity.values)

assert uxds_ft.dims == {'n_face', 'n_node', 'n_max_face_nodes'}
assert uxds_ft.dims == {'n_face', 'n_node', 'n_max_face_nodes'}
29 changes: 15 additions & 14 deletions test/test_geos.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
import uxarray as ux

import os
from unittest import TestCase
from pathlib import Path

current_path = Path(os.path.dirname(os.path.realpath(__file__)))

gridfile_geos_cs = current_path / "meshfiles" / "geos-cs" / "c12" / "test-c12.native.nc4"


class Test_GEOS(TestCase):
def test_read_geos_cs_grid(self):
"""Tests the conversion of a CS12 GEOS-CS Grid to the UGRID conventions.
def test_read_geos_cs_grid():
"""Tests the conversion of a CS12 GEOS-CS Grid to the UGRID conventions.
A CS12 grid has 6 faces, each with 12x12 faces and 13x13 nodes each.
"""

A CS12 grid has 6 faces, each with 12x12 faces and 13x13 nodes each.
"""
uxgrid = ux.open_grid(gridfile_geos_cs)

uxgrid = ux.open_grid(gridfile_geos_cs)
n_face = 6 * 12 * 12
n_node = 6 * 13 * 13

n_face = 6 * 12 * 12
n_node = 6 * 13 * 13
assert uxgrid.n_face == n_face
assert uxgrid.n_node == n_node

assert uxgrid.n_face == n_face
assert uxgrid.n_node == n_node

def test_read_geos_cs_uxds(self):
"""Tests the creating of a UxDataset from a CS12 GEOS-CS Grid."""
uxds = ux.open_dataset(gridfile_geos_cs, gridfile_geos_cs)

def test_read_geos_cs_uxds():
"""Tests the creating of a UxDataset from a CS12 GEOS-CS Grid."""
uxds = ux.open_dataset(gridfile_geos_cs, gridfile_geos_cs)

assert uxds['T'].shape[-1] == uxds.uxgrid.n_face
assert uxds['T'].shape[-1] == uxds.uxgrid.n_face

0 comments on commit 9859bb6

Please sign in to comment.