def test_lbpack_raw(self): lbpack = 4321 proxy = PPDataProxy(None, None, None, None, None, lbpack, None, None) self.assertEqual(proxy.lbpack, lbpack) self.assertIsNot(proxy.lbpack, lbpack) self.assertIsInstance(proxy.lbpack, SplittableInt) self.assertEqual(proxy.lbpack.n1, lbpack % 10) self.assertEqual(proxy.lbpack.n2, lbpack // 10 % 10) self.assertEqual(proxy.lbpack.n3, lbpack // 100 % 10) self.assertEqual(proxy.lbpack.n4, lbpack // 1000 % 10)
def _check_slicing(self, test_shape, indices, result_shape, data_was_fetched=True): # Check behaviour of the getitem call with specific slicings. # Especially: check cases where a fetch does *not* read from the file. # This is necessary because, since Dask 2.0, the "from_array" function # takes a zero-length slice of its array argument, to capture array # metadata, and in those cases we want to avoid file access. test_dtype = np.dtype(np.float32) proxy = PPDataProxy( shape=test_shape, src_dtype=test_dtype, path=None, offset=None, data_len=None, lbpack=0, # Note: a 'real' value is needed. boundary_packing=None, mdi=None, ) # Mock out the file-open call, to see if the file would be read. builtin_open_func_name = "builtins.open" mock_fileopen = self.patch(builtin_open_func_name) # Also mock out the 'databytes_to_shaped_array' call, to fake minimal # operation in the cases where file-open *does* get called. fake_data = np.zeros(test_shape, dtype=test_dtype) self.patch( "iris.fileformats.pp._data_bytes_to_shaped_array", mock.MagicMock(return_value=fake_data), ) # Test the requested indexing operation. result = proxy.__getitem__(indices) # Check the behaviour and results were as expected. self.assertEqual(mock_fileopen.called, data_was_fetched) self.assertIsInstance(result, np.ndarray) self.assertEqual(result.dtype, test_dtype) self.assertEqual(result.shape, result_shape)
def test_lbpack_SplittableInt(self): lbpack = mock.Mock(spec_set=SplittableInt) proxy = PPDataProxy(None, None, None, None, None, lbpack, None, None) self.assertEqual(proxy.lbpack, lbpack) self.assertIs(proxy.lbpack, lbpack)