def test_coords_deleted_fails(self): """Test update_coord fails to delete coord of len > 1. """ cube = create_cube_with_threshold() changes = 'delete' msg = "Can only remove a coordinate of length 1" with self.assertRaisesRegex(ValueError, msg): update_coord(cube, 'time', changes)
def test_coords_update_fail_bounds(self): """Test update_coord fails if shape of new bounds do not match. """ cube = create_cube_with_threshold(threshold_values=[2.0, 3.0]) changes = {'bounds': [0.1, 2.0]} msg = "The shape of the bounds array should be" with self.assertRaisesRegex(ValueError, msg): update_coord(cube, 'threshold', changes)
def test_coords_update_fail_points(self): """Test that update_coord fails if points do not match. """ cube = create_cube_with_threshold() changes = {'points': [2.0, 3.0]} msg = "Mismatch in points in existing coord and updated metadata" with self.assertRaisesRegex(ValueError, msg): update_coord(cube, 'threshold', changes)
def test_alternative_incompatible_changes_requested(self): """Test that update_coord raises an exception if 'bounds' and 'units' are requested to be changed.""" cube = create_cube_with_threshold() changes = {'bounds': [0.1, 2.0], 'units': 'mm/hr'} msg = "When updating a coordinate" with self.assertRaisesRegex(ValueError, msg): update_coord(cube, self.coord_name, changes)
def test_warning_messages_with_update(self, warning_list=None): """Test warning message is raised correctly when updating coord. """ changes = {'points': [2.0], 'bounds': [0.1, 2.0]} warning_msg = "Updated coordinate" update_coord(self.cube, self.coord_name, changes, warnings_on=True) self.assertTrue( any(item.category == UserWarning for item in warning_list)) self.assertTrue(any(warning_msg in str(item) for item in warning_list))
def test_warning_messages_with_delete(self, warning_list=None): """Test warning message is raised correctly when deleting coord. """ changes = 'delete' warning_msg = "Deleted coordinate" update_coord(self.cube, self.coord_name, changes, warnings_on=True) self.assertTrue( any(item.category == UserWarning for item in warning_list)) self.assertTrue(any(warning_msg in str(item) for item in warning_list))
def test_incompatible_changes_requested(self): """Test that update_coord raises an exception if 'points' and 'units' are requested to be changed.""" cube = create_cube_with_threshold() changes = {'points': [2.0, 3.0], 'units': 'mm/hr'} msg = "When updating a coordinate" with self.assertRaisesRegex(ValueError, msg): update_coord(cube, 'threshold', changes)
def test_coords_update_fails_bounds_differ(self): """Test that update_coord fails if bounds differ.""" cube = create_cube_with_threshold(threshold_values=[2.0, 3.0]) cube.coord('threshold').guess_bounds() changes = {'bounds': [[0.1, 2.0], [2.0, 3.0], [3.0, 4.0]]} msg = "Mismatch in bounds in existing coord and updated metadata" with self.assertRaisesRegex(ValueError, msg): update_coord(cube, 'threshold', changes)
def test_warning_messages_with_update(self): """Test warning message is raised correctly when updating coord. """ coord_name = 'threshold' cube = create_cube_with_threshold() changes = {'points': [2.0], 'bounds': [0.1, 2.0], 'units': 'mm'} warning_msg = "Updated coordinate" with warnings.catch_warnings(record=True) as warning_list: warnings.simplefilter("always") update_coord(cube, coord_name, changes, warnings_on=True) self.assertTrue(any(item.category == UserWarning for item in warning_list)) self.assertTrue(any(warning_msg in str(item) for item in warning_list))
def test_warning_messages_with_delete(self): """Test warning message is raised correctly when deleting coord. """ coord_name = 'threshold' cube = create_cube_with_threshold() changes = 'delete' warning_msg = "Deleted coordinate" with warnings.catch_warnings(record=True) as warning_list: warnings.simplefilter("always") update_coord(cube, coord_name, changes, warnings_on=True) self.assertTrue(any(item.category == UserWarning for item in warning_list)) self.assertTrue(any(warning_msg in str(item) for item in warning_list))
def test_coords_deleted(self): """Test update_coord deletes coordinate. """ cube = create_cube_with_threshold() changes = 'delete' result = update_coord(cube, 'threshold', changes) found_key = 'threshold' in [coord.name() for coord in result.coords()] self.assertArrayEqual(found_key, False)
def test_coords_deleted(self): """Test update_coord deletes coordinate. """ changes = 'delete' result = update_coord(self.cube, self.coord_name, changes) found_key = self.coord_name in [ coord.name() for coord in result.coords()] self.assertArrayEqual(found_key, False)
def test_coords_update_bounds_succeed(self): """Test that update_coord succeeds if bounds do match """ cube = create_cube_with_threshold(threshold_values=[2.0, 3.0]) cube.coord('threshold').guess_bounds() changes = {'bounds': [[0.1, 2.0], [2.0, 3.0]]} result = update_coord(cube, 'threshold', changes) self.assertArrayEqual(result.coord('threshold').bounds, np.array([[0.1, 2.0], [2.0, 3.0]]))
def test_update_attributes(self): """Test update attributes associated with a coordinate.""" cube = create_cube_with_threshold() changes = {'attributes': {'spp__relative_to_threshold': "below"}} result = update_coord(cube, self.coord_name, changes) self.assertIsInstance(result, Cube) self.assertEqual( result.coord(self.coord_name).attributes, changes["attributes"])
def test_basic(self): """Test update_coord returns a Cube and updates coord correctly. """ changes = {'points': [2.0], 'bounds': [0.1, 2.0]} result = update_coord(self.cube, self.coord_name, changes) self.assertIsInstance(result, Cube) self.assertArrayAlmostEqual(result.coord(self.coord_name).points, np.array([2.0], dtype=np.float32)) self.assertArrayAlmostEqual(result.coord(self.coord_name).bounds, np.array([[0.1, 2.0]], dtype=np.float32))
def test_convert_units(self): """Test update_coord returns a Cube and converts units correctly. """ cube = create_cube_with_threshold() changes = {'units': 'km s-1'} result = update_coord(cube, self.coord_name, changes) self.assertIsInstance(result, Cube) self.assertEqual(result.coord(self.coord_name).points, np.array([0.001], dtype=np.float32)) self.assertEqual(str(result.coord(self.coord_name).units), 'km s-1')
def test_coords_update_bounds_succeed(self): """Test that update_coord succeeds if bounds do match """ cube = create_cube_with_threshold(threshold_values=self.thresholds) cube.coord(self.coord_name).guess_bounds() changes = {'bounds': [[0.1, 2.0], [2.0, 3.0]]} result = update_coord(cube, self.coord_name, changes) self.assertArrayEqual(result.coord(self.coord_name).bounds, np.array([[0.1, 2.0], [2.0, 3.0]], dtype=np.float32))
def test_basic(self): """Test update_coord returns a Cube and updates coord correctly. """ cube = create_cube_with_threshold() changes = {'points': [2.0], 'bounds': [0.1, 2.0], 'units': 'mm'} result = update_coord(cube, 'threshold', changes) self.assertIsInstance(result, Cube) self.assertArrayEqual( result.coord('threshold').points, np.array([2.0])) self.assertArrayEqual( result.coord('threshold').bounds, np.array([[0.1, 2.0]])) self.assertEqual(str(result.coord('threshold').units), 'mm')
def test_coords_update_fail_points(self): """Test that update_coord fails if points do not match. """ changes = {'points': [2.0, 3.0]} msg = "Mismatch in points in existing coord and updated metadata" with self.assertRaisesRegex(ValueError, msg): update_coord(self.cube, self.coord_name, changes)