def test_stash(self): # No match. constraint = NameConstraint(STASH="m01s00i444") result = self.cubes.extract(constraint) self.assertFalse(result) # Match. constraint = NameConstraint(STASH=self.stash) result = self.cubes.extract(constraint, strict=True) self.assertIsNotNone(result) self.assertEqual(str(result.attributes["STASH"]), self.stash) # Match - callable. kwargs = dict(STASH=lambda stash: stash.item == 4) constraint = NameConstraint(**kwargs) result = self.cubes.extract(constraint, strict=True) self.assertIsNotNone(result)
def test_stash__None(self): cube = self.cubes[self.index] del cube.attributes["STASH"] constraint = NameConstraint(standard_name=self.standard_name, STASH=None) result = self.cubes.extract(constraint, strict=True) self.assertIsNotNone(result) self.assertEqual(result.standard_name, self.standard_name) self.assertIsNone(result.attributes.get("STASH"))
def test_var_name__None(self): cube = self.cubes[self.index] cube.var_name = None constraint = NameConstraint(standard_name=self.standard_name, var_name=None) result = self.cubes.extract(constraint, strict=True) self.assertIsNotNone(result) self.assertEqual(result.standard_name, self.standard_name) self.assertIsNone(result.var_name)
def test_var_name(self): # No match. constraint = NameConstraint(var_name="wibble") result = self.cubes.extract(constraint) self.assertFalse(result) # Match. constraint = NameConstraint(var_name=self.var_name) result = self.cubes.extract(constraint, strict=True) self.assertIsNotNone(result) self.assertEqual(result.var_name, self.var_name) # Match - callable. kwargs = dict(var_name=lambda item: item.startswith("ap")) constraint = NameConstraint(**kwargs) result = self.cubes.extract(constraint, strict=True) self.assertIsNotNone(result) self.assertEqual(result.var_name, self.var_name)
def test_long_name__None(self): cube = self.cubes[self.index] cube.long_name = None constraint = NameConstraint(standard_name=self.standard_name, long_name=None) result = self.cubes.extract_cube(constraint) self.assertIsNotNone(result) self.assertEqual(result.standard_name, self.standard_name) self.assertIsNone(result.long_name)
def test_standard_name(self): # No match. constraint = NameConstraint(standard_name="wibble") result = self.cubes.extract(constraint) self.assertFalse(result) # Match. constraint = NameConstraint(standard_name=self.standard_name) result = self.cubes.extract_cube(constraint) self.assertIsNotNone(result) self.assertEqual(result.standard_name, self.standard_name) # Match - callable. kwargs = dict(standard_name=lambda item: item.startswith("air_pot")) constraint = NameConstraint(**kwargs) result = self.cubes.extract_cube(constraint) self.assertIsNotNone(result) self.assertEqual(result.standard_name, self.standard_name)
def test_unknown(self): # No match. constraint = NameConstraint(None, None, None, None) result = self.cubes.extract(constraint) self.assertFalse(result) # Match. cube = self.cubes[self.index] cube.standard_name = None cube.long_name = None cube.var_name = None cube.attributes = None constraint = NameConstraint(None, None, None, None) result = self.cubes.extract(constraint, strict=True) self.assertIsNotNone(result) self.assertIsNone(result.standard_name) self.assertIsNone(result.long_name) self.assertIsNone(result.var_name) self.assertIsNone(result.attributes.get("STASH"))
def test_compound(self): # Match. constraint = NameConstraint(standard_name=self.standard_name, long_name=self.long_name) result = self.cubes.extract(constraint, strict=True) self.assertIsNotNone(result) self.assertEqual(result.standard_name, self.standard_name) # No match - var_name. constraint = NameConstraint( standard_name=self.standard_name, long_name=self.long_name, var_name="wibble", ) result = self.cubes.extract(constraint) self.assertFalse(result) # Match. constraint = NameConstraint( standard_name=self.standard_name, long_name=self.long_name, var_name=self.var_name, ) result = self.cubes.extract(constraint, strict=True) self.assertIsNotNone(result) self.assertEqual(result.standard_name, self.standard_name) self.assertEqual(result.long_name, self.long_name) self.assertEqual(result.var_name, self.var_name) # No match - STASH. constraint = NameConstraint( standard_name=self.standard_name, long_name=self.long_name, var_name=self.var_name, STASH="m01s00i444", ) result = self.cubes.extract(constraint) self.assertFalse(result) # Match. constraint = NameConstraint( standard_name=self.standard_name, long_name=self.long_name, var_name=self.var_name, STASH=self.stash, ) result = self.cubes.extract(constraint, strict=True) self.assertIsNotNone(result) self.assertEqual(result.standard_name, self.standard_name) self.assertEqual(result.long_name, self.long_name) self.assertEqual(result.var_name, self.var_name) self.assertEqual(result.var_name, self.var_name) # No match - standard_name. constraint = NameConstraint( standard_name="wibble", long_name=self.long_name, var_name=self.var_name, STASH=self.stash, ) result = self.cubes.extract(constraint) self.assertFalse(result)