コード例 #1
0
    def _check_bounds_setting(self, climatological=False):
        # Generic test that can run with or without a climatological coord.
        cube = stock.climatology_3d()
        coord = cube.coord("time").copy()
        # Over-write original value from stock.climatology_3d with test value.
        coord.climatological = climatological

        # Set up expected strings.
        if climatological:
            property_name = "climatology"
            varname_extra = "climatology"
        else:
            property_name = "bounds"
            varname_extra = "bnds"
        boundsvar_name = "time_" + varname_extra

        # Set up arguments for testing _create_cf_bounds.
        saver = mock.MagicMock(spec=Saver)
        # NOTE: 'saver' must have spec=Saver to fake isinstance(save, Saver),
        # so it can pass as 'self' in the call to _create_cf_cbounds.
        # Mock a '_dataset' property; not automatic because 'spec=Saver'.
        saver._dataset = mock.MagicMock()
        # Mock the '_ensure_valid_dtype' method to return an object with a
        # suitable 'shape' and 'dtype'.
        saver._ensure_valid_dtype.return_value = mock.Mock(
            shape=coord.bounds.shape, dtype=coord.bounds.dtype
        )
        var = mock.MagicMock(spec=nc.Variable)

        # Make the main call.
        Saver._create_cf_bounds(saver, coord, var, "time")

        # Test the call of _setncattr in _create_cf_bounds.
        setncattr_call = mock.call(
            property_name, boundsvar_name.encode(encoding="ascii")
        )
        self.assertEqual(setncattr_call, var.setncattr.call_args)

        # Test the call of createVariable in _create_cf_bounds.
        dataset = saver._dataset
        expected_dimensions = var.dimensions + ("bnds",)
        create_var_call = mock.call(
            boundsvar_name, coord.bounds.dtype, expected_dimensions
        )
        self.assertEqual(create_var_call, dataset.createVariable.call_args)