def setUp(self): s1 = ThresholdSpecification(Name('validate_drp.AM1.design_r'), 5. * u.marcsec, '<', metadata_query={'filter_name': 'r'}) s2 = ThresholdSpecification(Name('validate_drp.AM1.design_i'), 5. * u.marcsec, '<', metadata_query={'filter_name': 'i'}) s3 = ThresholdSpecification(Name('validate_drp.AM1.design_HSC_r'), 5. * u.marcsec, '<', metadata_query={ 'filter_name': 'r', 'camera': 'HSC' }) s4 = ThresholdSpecification(Name('validate_drp.PA1.design_r'), 10 * u.mmag, '<', metadata_query={'filter_name': 'r'}) s5 = ThresholdSpecification(Name('validate_drp.AM1.design'), 5. * u.marcsec, '<', metadata_query={}) self.spec_set = SpecificationSet([s1, s2, s3, s4, s5])
def setUp(self): self.spec_PA1_design = ThresholdSpecification( 'validate_drp.PA1.design', 5. * u.mmag, '<') self.spec_PA1_stretch = ThresholdSpecification( 'validate_drp.PA1.stretch', 3. * u.mmag, '<') self.spec_PA2_design = ThresholdSpecification( 'validate_drp.PA2_design_gri.srd', 15. * u.mmag, '<=') specs = [ self.spec_PA1_design, self.spec_PA1_stretch, self.spec_PA2_design ] partial_PA1_doc = OrderedDict([ ('id', 'validate_drp.LPM-17-PA1#PA1-Base'), ('threshold', OrderedDict([('unit', 'mag')])) ]) partial_PA1 = SpecificationPartial(partial_PA1_doc) self.spec_set = SpecificationSet(specifications=specs, partials=[partial_PA1])
def test_mapping(self): spec_PA1_design = ThresholdSpecification( 'validate_drp.PA1.design', 5. * u.mmag, '<') spec_PA1_stretch = ThresholdSpecification( 'validate_drp.PA1.stretch', 3. * u.mmag, '<') spec_set = SpecificationSet() self.assertEqual(len(spec_set), 0) # This syntax is slightly awkward, which is why we have `insert()` too spec_set[spec_PA1_design.name] = spec_PA1_design self.assertEqual(len(spec_set), 1) self.assertTrue('validate_drp.PA1.design' in spec_set) # Insert spec_set.insert(spec_PA1_stretch) self.assertEqual(len(spec_set), 2) self.assertTrue('validate_drp.PA1.stretch' in spec_set) # Delete del spec_set['validate_drp.PA1.stretch'] self.assertEqual(len(spec_set), 1) self.assertFalse('validate_drp.PA1.stretch' in spec_set) # Insert duplicate spec_set[spec_PA1_design.name] = spec_PA1_design self.assertEqual(len(spec_set), 1) self.assertTrue('validate_drp.PA1.design' in spec_set) # Insert weird value with self.assertRaises(TypeError): spec_set['validate_drp.PAX.design'] = 10 # __setitem__ insert with a conflicting key. # This is why insert() is preferred. with self.assertRaises(KeyError): spec_set['validate_drp.hello.world'] = spec_PA1_design
def test_equality(self): """test __eq__.""" s1 = ThresholdSpecification(Name('validate_drp.AM1.design'), 5. * u.marcsec, '<') # with compatible units s3 = ThresholdSpecification(Name('validate_drp.AM1.design'), 5e-3 * u.arcsec, '<') self.assertEqual(s1, s3) # incompatible names s4 = ThresholdSpecification(Name('validate_drp.AM1.stretch'), 5. * u.marcsec, '<') self.assertNotEqual(s1, s4) # incompatible threshold s5 = ThresholdSpecification(Name('validate_drp.AM1.design'), 5. * u.arcsec, '<') self.assertNotEqual(s1, s5) # incompatible operator s6 = ThresholdSpecification(Name('validate_drp.AM1.design'), 5. * u.marcsec, '>') self.assertNotEqual(s1, s6)
def test_init(self): """Test initialization patterns.""" # with a fully-specified Name s1 = ThresholdSpecification(Name('validate_drp.AM1.design'), 5. * u.marcsec, '<') # with a fully-specified string-based name s2 = ThresholdSpecification('validate_drp.AM1.design', 5. * u.marcsec, '<') self.assertEqual(s1, s2) # bad operator with self.assertRaises(TypeError): ThresholdSpecification('validate_drp.AM1.design', 5. * u.marcsec, '<<') # bad quantity with self.assertRaises(TypeError): ThresholdSpecification('validate_drp.AM1.design', 5., '<') # bad name with self.assertRaises(TypeError): ThresholdSpecification(Name(metric='validate_drp'), 5. * u.marcsec, '<')
def test_query_metadata(self): job = Job(meta={'filter_name': 'r', 'camera': 'MegaCam'}) s1 = ThresholdSpecification(Name('validate_drp.AM1.design_r'), 5. * u.marcsec, '<', metadata_query={'filter_name': 'r'}) s2 = ThresholdSpecification(Name('validate_drp.AM1.design_i'), 5. * u.marcsec, '<', metadata_query={'filter_name': 'i'}) s3 = ThresholdSpecification(Name('validate_drp.AM1.design_HSC_r'), 5. * u.marcsec, '<', metadata_query={ 'filter_name': 'r', 'camera': 'HSC' }) self.assertTrue(s1.query_metadata(job.meta)) self.assertFalse(s2.query_metadata(job.meta)) self.assertFalse(s3.query_metadata(job.meta))
def test_convert_operator_str(self): """Test that strings can be converted into operators.""" self.assertEqual(ThresholdSpecification.convert_operator_str('<'), operator.lt) self.assertEqual(ThresholdSpecification.convert_operator_str('<='), operator.le) self.assertEqual(ThresholdSpecification.convert_operator_str('>'), operator.gt) self.assertEqual(ThresholdSpecification.convert_operator_str('>='), operator.ge) self.assertEqual(ThresholdSpecification.convert_operator_str('=='), operator.eq) self.assertEqual(ThresholdSpecification.convert_operator_str('!='), operator.ne) with self.assertRaises(ValueError): ThresholdSpecification.convert_operator_str('<<'),
def test_spec(self): """Test creating and accessing a specification from a quantity.""" s = ThresholdSpecification('design', 5 * u.mag, '<') self.assertEqual(s.name, Name(spec='design')) self.assertEqual(s.type, 'threshold') self.assertEqual(s.threshold.value, 5.) self.assertEqual(s.threshold.unit, u.mag) self.assertEqual(s.operator_str, '<') # Sanity-check repr self.assertIn('ThresholdSpecification', repr(s)) self.assertIn('design', repr(s)) self.assertIn('Quantity', repr(s)) self.assertIn('mag', repr(s)) self.assertIn('5', repr(s)) # Test specification check method self.assertTrue(s.check(4 * u.mag)) self.assertTrue(s.check(4000 * u.mmag)) self.assertFalse(s.check(6. * u.mag)) self.assertFalse(s.check(6000. * u.mmag)) with self.assertRaises(u.UnitConversionError): s.check(2. * u.arcmin) with self.assertRaises(u.UnitsError): s.check(2.) # test json output json_data = s.json self.assertEqual(json_data['name'], 'design') self.assertEqual(json_data['threshold']['value'], 5.) self.assertEqual(json_data['threshold']['unit'], 'mag') self.assertEqual(json_data['threshold']['operator'], '<') # rebuild from json s3 = ThresholdSpecification.deserialize(**json_data) self.assertEqual(s, s3) # test datum output d = s.datum self.assertEqual(d.quantity, 5. * u.mag) self.assertEqual(d.label, 'design')