Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@
# See LICENSE in the root of the repository for full licensing details.
"""Integration tests for the `iris.experimental.geovista.cube_to_polydata` function."""

import numpy as np

from iris import load_cube
from iris.experimental.geovista import cube_to_polydata
from iris.tests import get_data_path
from iris.tests import _shared_utils


def test_integration_2d():
file_path = get_data_path(
file_path = _shared_utils.get_data_path(
[
"NetCDF",
"ORCA2",
Expand All @@ -26,11 +24,13 @@ def test_integration_2d():
# This is a known good output, we have plotted the result and checked it.
assert polydata.GetNumberOfCells() == 26640
assert polydata.GetNumberOfPoints() == 26969
np.testing.assert_array_equal(cube[0, 1, :].data.flatten(), polydata.active_scalars)
_shared_utils.assert_array_equal(
cube[0, 1, :].data.flatten(), polydata.active_scalars
)


def test_integration_1d():
file_path = get_data_path(
file_path = _shared_utils.get_data_path(
[
"NetCDF",
"global",
Expand All @@ -44,11 +44,11 @@ def test_integration_1d():
# This is a known good output, we have plotted the result and checked it.
assert polydata.GetNumberOfCells() == 51200
assert polydata.GetNumberOfPoints() == 51681
np.testing.assert_array_equal(cube[0, :].data.flatten(), polydata.active_scalars)
_shared_utils.assert_array_equal(cube[0, :].data.flatten(), polydata.active_scalars)


def test_integration_mesh():
file_path = get_data_path(
file_path = _shared_utils.get_data_path(
[
"NetCDF",
"unstructured_grid",
Expand All @@ -62,4 +62,4 @@ def test_integration_mesh():
# This is a known good output, we have plotted the result and checked it.
assert polydata.GetNumberOfCells() == 864
assert polydata.GetNumberOfPoints() == 866
np.testing.assert_array_equal(polydata.active_scalars, cube[0, :].data)
_shared_utils.assert_array_equal(polydata.active_scalars, cube[0, :].data)
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@

from iris import load_cube
from iris.experimental.geovista import cube_to_polydata, extract_unstructured_region
from iris.tests import get_data_path
from iris.tests import _shared_utils


def test_face_region_extraction():
file_path = get_data_path(
file_path = _shared_utils.get_data_path(
[
"NetCDF",
"unstructured_grid",
Expand Down
97 changes: 47 additions & 50 deletions lib/iris/tests/integration/experimental/test_CubeRepresentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,22 @@
# See LICENSE in the root of the repository for full licensing details.
"""Integration tests for cube html representation."""

# Import iris.tests first so that some things can be initialised before
# importing anything else.
import iris.tests as tests # isort:skip

from html import escape

import numpy as np
import pytest

from iris.cube import Cube
from iris.experimental.representation import CubeRepresentation
from iris.tests import _shared_utils
import iris.tests.stock as stock


@tests.skip_data
class TestNoMetadata(tests.IrisTest):
@_shared_utils.skip_data
class TestNoMetadata:
# Test the situation where we have a cube with no metadata at all.
def setUp(self):
@pytest.fixture(autouse=True)
def _setup(self):
self.shape = (2, 3, 4)
self.cube = Cube(np.arange(24).reshape(self.shape))
self.representer = CubeRepresentation(self.cube)
Expand All @@ -29,26 +28,27 @@ def setUp(self):
def test_cube_name(self):
expected = "Unknown" # This cube has no metadata.
result = self.representer.name
self.assertEqual(expected, result)
assert expected == result

def test_cube_units(self):
expected = "unknown" # This cube has no metadata.
result = self.representer.units
self.assertEqual(expected, result)
assert expected == result

def test_dim_names(self):
expected = ["--"] * len(self.shape)
result = self.representer.names
self.assertEqual(expected, result)
assert expected == result

def test_shape(self):
result = self.representer.shapes
self.assertEqual(result, self.shape)
assert result == self.shape


@tests.skip_data
class TestMissingMetadata(tests.IrisTest):
def setUp(self):
@_shared_utils.skip_data
class TestMissingMetadata:
@pytest.fixture(autouse=True)
def _setup(self):
self.cube = stock.realistic_3d()

def test_no_coords(self):
Expand All @@ -57,107 +57,104 @@ def test_no_coords(self):
self.cube.remove_coord(coord)
representer = CubeRepresentation(self.cube)
result = representer.repr_html().lower()
self.assertNotIn("dimension coordinates", result)
self.assertNotIn("auxiliary coordinates", result)
self.assertNotIn("scalar coordinates", result)
self.assertIn("attributes", result)
assert "dimension coordinates" not in result
assert "auxiliary coordinates" not in result
assert "scalar coordinates" not in result
assert "attributes" in result

def test_no_dim_coords(self):
dim_coords = [c.name() for c in self.cube.coords(dim_coords=True)]
for coord in dim_coords:
self.cube.remove_coord(coord)
representer = CubeRepresentation(self.cube)
result = representer.repr_html().lower()
self.assertNotIn("dimension coordinates", result)
self.assertIn("auxiliary coordinates", result)
self.assertIn("scalar coordinates", result)
self.assertIn("attributes", result)
assert "dimension coordinates" not in result
assert "auxiliary coordinates" in result
assert "scalar coordinates" in result
assert "attributes" in result

def test_no_aux_coords(self):
aux_coords = ["forecast_period"]
for coord in aux_coords:
self.cube.remove_coord(coord)
representer = CubeRepresentation(self.cube)
result = representer.repr_html().lower()
self.assertIn("dimension coordinates", result)
self.assertNotIn("auxiliary coordinates", result)
self.assertIn("scalar coordinates", result)
self.assertIn("attributes", result)
assert "dimension coordinates" in result
assert "auxiliary coordinates" not in result
assert "scalar coordinates" in result
assert "attributes" in result

def test_no_scalar_coords(self):
aux_coords = ["air_pressure"]
for coord in aux_coords:
self.cube.remove_coord(coord)
representer = CubeRepresentation(self.cube)
result = representer.repr_html().lower()
self.assertIn("dimension coordinates", result)
self.assertIn("auxiliary coordinates", result)
self.assertNotIn("scalar coordinates", result)
self.assertIn("attributes", result)
assert "dimension coordinates" in result
assert "auxiliary coordinates" in result
assert "scalar coordinates" not in result
assert "attributes" in result

def test_no_attrs(self):
self.cube.attributes = {}
representer = CubeRepresentation(self.cube)
result = representer.repr_html().lower()
self.assertIn("dimension coordinates", result)
self.assertIn("auxiliary coordinates", result)
self.assertIn("scalar coordinates", result)
self.assertNotIn("attributes", result)
assert "dimension coordinates" in result
assert "auxiliary coordinates" in result
assert "scalar coordinates" in result
assert "attributes" not in result

def test_no_cell_methods(self):
representer = CubeRepresentation(self.cube)
result = representer.repr_html().lower()
self.assertNotIn("cell methods", result)
assert "cell methods" not in result


@tests.skip_data
class TestScalarCube(tests.IrisTest):
def setUp(self):
@_shared_utils.skip_data
class TestScalarCube:
@pytest.fixture(autouse=True)
def _setup(self):
self.cube = stock.realistic_3d()[0, 0, 0]
self.representer = CubeRepresentation(self.cube)
self.representer.repr_html()

def test_identfication(self):
# Is this scalar cube accurately identified?
self.assertTrue(self.representer.scalar_cube)
assert self.representer.scalar_cube

def test_header__name(self):
header = self.representer._make_header()
expected_name = escape(self.cube.name().title().replace("_", " "))
self.assertIn(expected_name, header)
assert expected_name in header

def test_header__units(self):
header = self.representer._make_header()
expected_units = escape(self.cube.units.symbol)
self.assertIn(expected_units, header)
assert expected_units in header

def test_header__scalar_str(self):
# Check that 'scalar cube' is placed in the header.
header = self.representer._make_header()
expected_str = "(scalar cube)"
self.assertIn(expected_str, header)
assert expected_str in header

def test_content__scalars(self):
# Check an element "Scalar coordinates" is present in the main content.
content = self.representer._make_content()
expected_str = "Scalar coordinates"
self.assertIn(expected_str, content)
assert expected_str in content

def test_content__specific_scalar_coord(self):
# Check a specific scalar coord is present in the main content.
content = self.representer._make_content()
expected_coord = self.cube.coords()[0]
expected_coord_name = escape(expected_coord.name())
self.assertIn(expected_coord_name, content)
assert expected_coord_name in content
expected_coord_val = escape(str(expected_coord.points[0]))
self.assertIn(expected_coord_val, content)
assert expected_coord_val in content

def test_content__attributes(self):
# Check an element "attributes" is present in the main content.
content = self.representer._make_content()
expected_str = "Attributes"
self.assertIn(expected_str, content)


if __name__ == "__main__":
tests.main()
assert expected_str in content
Loading
Loading