def testBadDictionary(self): d = Dictionary(self.getTestDictionary("dictionary_bad_policyfile.paf")) self.assertRaiseLCE(DictionaryError, "Illegal type: \"PolicyFile\"", d.makeDef("file_type").getType, "Dictionary specified PolicyFile type") d = Dictionary(self.getTestDictionary("dictionary_bad_unknown_type.paf")) self.assertRaiseLCE(DictionaryError, "Unknown type: \"NotAType\"", d.makeDef("something").getType, "Dictionary specifies unknown types") d = Dictionary(self.getTestDictionary("dictionary_bad_type_type.paf")) self.assertRaiseLCE(DictionaryError, "Expected string", d.makeDef("something").getType, "Expected string \"type\" type") self.assertRaiseLCE(DictionaryError, "property found at bad: min_occurs", Dictionary, "Dictionary has mispelled keyword \"min_occurs\".", self.getTestDictionary("dictionary_bad_keyword.paf")) dbmd = self.getTestDictionary("dictionary_bad_multiple_definitions.paf") self.assertRaiseLCE(DictionaryError, "expected a single", Dictionary, "Dictionary has two 'definitions' sections", dbmd) p = Policy(self.getTestDictionary("values_policy_good_1.paf")) d = Dictionary(self.getTestDictionary("dictionary_bad_multiple_min.paf")) self.assertRaiseLCE(DictionaryError, "Min value for int_ra", d.validate, "Two mins specified.", p) d = Dictionary(self.getTestDictionary("dictionary_bad_multiple_max.paf")) self.assertRaiseLCE(DictionaryError, "Max value for int_ra", d.validate, "Two maxes specified.", p) d = Dictionary(self.getTestDictionary("dictionary_bad_min_wrong_type.paf")) self.assertRaiseLCE(DictionaryError, "Wrong type for int_range_count_type min", d.validate, "Wrong min type.", p) d = Dictionary(self.getTestDictionary("dictionary_bad_max_wrong_type.paf")) self.assertRaiseLCE(DictionaryError, "Wrong type for int_range_count_type max", d.validate, "Wrong max type.", p) # conflict between minOccurs and maxOccurs d = Dictionary(self.getTestDictionary("conflict_occurs_dictionary.paf")) p = Policy(self.getTestDictionary("conflict_occurs_policy_1.paf")) ve = ValidationError("Dictionary_1.py", 1, "testBadDictionary") d.validate(p, ve.cpp) self.assertEqual(ve.getErrors("1to0"), ValidationError.TOO_MANY_VALUES) self.assertEqual(ve.getErrors("2to1"), ValidationError.NOT_AN_ARRAY) self.assertEqual(ve.getParamCount(), 2) p = Policy(self.getTestDictionary("conflict_occurs_policy_2.paf")) ve = ValidationError("Dictionary_1.py", 1, "testBadDictionary") d.validate(p, ve.cpp) self.assertEqual(ve.getErrors("1to0"), ValidationError.MISSING_REQUIRED) self.assertEqual(ve.getErrors("2to1"), ValidationError.TOO_MANY_VALUES) self.assertEqual(ve.getParamCount(), 2)
def testBadDictionary(self): d = Dictionary(self.getTestDictionary("dictionary_bad_policyfile.paf")) self.assertRaiseLCE(DictionaryError, "Illegal type: \"PolicyFile\"", d.makeDef("file_type").getType, "Dictionary specified PolicyFile type") d = Dictionary(self.getTestDictionary("dictionary_bad_unknown_type.paf")) self.assertRaiseLCE(DictionaryError, "Unknown type: \"NotAType\"", d.makeDef("something").getType, "Dictionary specifies unknown types") d = Dictionary(self.getTestDictionary("dictionary_bad_type_type.paf")) self.assertRaiseLCE(DictionaryError, "Expected string", d.makeDef("something").getType, "Expected string \"type\" type") self.assertRaiseLCE(DictionaryError, "property found at bad: min_occurs", Dictionary, "Dictionary has mispelled keyword \"min_occurs\".", self.getTestDictionary("dictionary_bad_keyword.paf")) dbmd = self.getTestDictionary("dictionary_bad_multiple_definitions.paf") self.assertRaiseLCE(DictionaryError, "expected a single", Dictionary, "Dictionary has two 'definitions' sections", dbmd) p = Policy(self.getTestDictionary("values_policy_good_1.paf")) d = Dictionary(self.getTestDictionary("dictionary_bad_multiple_min.paf")) self.assertRaiseLCE(DictionaryError, "Min value for int_ra", d.validate, "Two mins specified.", p) d = Dictionary(self.getTestDictionary("dictionary_bad_multiple_max.paf")) self.assertRaiseLCE(DictionaryError, "Max value for int_ra", d.validate, "Two maxes specified.", p) d = Dictionary(self.getTestDictionary("dictionary_bad_min_wrong_type.paf")) self.assertRaiseLCE(DictionaryError, "Wrong type for int_range_count_type min", d.validate, "Wrong min type.", p) d = Dictionary(self.getTestDictionary("dictionary_bad_max_wrong_type.paf")) self.assertRaiseLCE(DictionaryError, "Wrong type for int_range_count_type max", d.validate, "Wrong max type.", p) # conflict between minOccurs and maxOccurs d = Dictionary(self.getTestDictionary("conflict_occurs_dictionary.paf")) p = Policy(self.getTestDictionary("conflict_occurs_policy_1.paf")) ve = ValidationError("Dictionary_1.py", 1, "testBadDictionary") d.validate(p, ve.cpp) self.assert_(ve.getErrors("1to0") == ValidationError.TOO_MANY_VALUES) self.assert_(ve.getErrors("2to1") == ValidationError.NOT_AN_ARRAY) self.assert_(ve.getParamCount() == 2) p = Policy(self.getTestDictionary("conflict_occurs_policy_2.paf")) ve = ValidationError("Dictionary_1.py", 1, "testBadDictionary") d.validate(p, ve.cpp) self.assert_(ve.getErrors("1to0") == ValidationError.MISSING_REQUIRED) self.assert_(ve.getErrors("2to1") == ValidationError.TOO_MANY_VALUES) self.assert_(ve.getParamCount() == 2)
def testTypeValidation(self): d = Dictionary(self.getTestDictionary("types_dictionary.paf")) self.assertEqual( d.makeDef("undef_type").getType(), Policy.UNDEF, "UNDEF definition type") self.assertEqual( d.makeDef("bool_type").getType(), Policy.BOOL, "BOOL definition type") self.assertEqual( d.makeDef("int_type").getType(), Policy.INT, "INT definition type") self.assertEqual( d.makeDef("double_type").getType(), Policy.DOUBLE, "DOUBLE definition type") self.assertEqual( d.makeDef("string_type").getType(), Policy.STRING, "STRING definition type") self.assertEqual( d.makeDef("policy_type").getType(), Policy.POLICY, "POLICY definition type") self.assertEqual( d.makeDef("file_type").getType(), Policy.POLICY, "POLICY definition type (substituted for PolicyFile)") p = Policy(self.getTestDictionary("types_policy_good.paf")) ve = ValidationError("Dictionary_1.py", 0, "testTypeValidation") d.validate(p, ve.cpp) self.assertEqual(ve.getErrors("file_type"), ValidationError.NOT_LOADED, "require loadPolicyFiles before validating") self.assertEqual(ve.getErrors("undef_file"), ValidationError.NOT_LOADED, "require loadPolicyFiles before validating") self.assertEqual(ve.getErrors(), ValidationError.NOT_LOADED, "no other errors") self.assertEqual(ve.getParamCount(), 2, "no other errors") p.loadPolicyFiles(self.getTestDictionary(), True) ve = ValidationError("Dictionary_1.py", 0, "testTypeValidation") d.validate(p, ve.cpp) self.assertEqual(ve.getErrors("undef_type"), 0, "no errors with undef") self.assertEqual(ve.getErrors("int_type"), 0, "no errors with int") self.assertEqual(ve.getErrors("double_type"), 0, "no errors with double") self.assertEqual(ve.getErrors("bool_type"), 0, "no errors with bool") self.assertEqual(ve.getErrors("string_type"), 0, "no errors with string") self.assertEqual(ve.getErrors("policy_type"), 0, "no errors with policy") self.assertEqual(ve.getErrors("file_type"), 0, "no errors with file") self.assertEqual(ve.getErrors(), 0, "no errors overall")
def testTypeValidation(self): d = Dictionary(self.getTestDictionary("types_dictionary.paf")) self.assert_(d.makeDef("undef_type") .getType() == Policy.UNDEF, "UNDEF definition type") self.assert_(d.makeDef("bool_type") .getType() == Policy.BOOL, "BOOL definition type") self.assert_(d.makeDef("int_type") .getType() == Policy.INT, "INT definition type") self.assert_(d.makeDef("double_type").getType() == Policy.DOUBLE, "DOUBLE definition type") self.assert_(d.makeDef("string_type").getType() == Policy.STRING, "STRING definition type") self.assert_(d.makeDef("policy_type").getType() == Policy.POLICY, "POLICY definition type") self.assert_(d.makeDef("file_type").getType() == Policy.POLICY, "POLICY definition type (substituted for PolicyFile)") p = Policy(self.getTestDictionary("types_policy_good.paf")) ve = ValidationError("Dictionary_1.py", 0, "testTypeValidation") d.validate(p, ve.cpp) self.assert_(ve.getErrors("file_type") == ValidationError.NOT_LOADED, "require loadPolicyFiles before validating") self.assert_(ve.getErrors("undef_file") == ValidationError.NOT_LOADED, "require loadPolicyFiles before validating") self.assert_(ve.getErrors() == ValidationError.NOT_LOADED, "no other errors") self.assert_(ve.getParamCount() == 2, "no other errors") p.loadPolicyFiles(self.getTestDictionary(), True) ve = ValidationError("Dictionary_1.py", 0, "testTypeValidation") d.validate(p, ve.cpp) self.assert_(ve.getErrors("undef_type") == 0, "no errors with undef") self.assert_(ve.getErrors("int_type") == 0, "no errors with int") self.assert_(ve.getErrors("double_type") == 0, "no errors with double") self.assert_(ve.getErrors("bool_type") == 0, "no errors with bool") self.assert_(ve.getErrors("string_type") == 0, "no errors with string") self.assert_(ve.getErrors("policy_type") == 0, "no errors with policy") self.assert_(ve.getErrors("file_type") == 0, "no errors with file") self.assert_(ve.getErrors() == 0, "no errors overall")
def testNested(self): self.assertRaiseLCE(DictionaryError, "policy_bad_subdef.dictionary is a string", Dictionary, "Malformed subdictionary", self.getTestDictionary("nested_dictionary_bad_1.paf")) p = Policy(self.getTestDictionary("nested_policy_good.paf")) self.assertRaiseLCE(DictionaryError, "Unknown Dictionary property", Dictionary, "Malformed subdictionary", self.getTestDictionary("nested_dictionary_bad_2.paf")) d = Dictionary(self.getTestDictionary("nested_dictionary_good.paf")) d.check() self.assertRaiseLCE(lsst.pex.exceptions.LogicError, "dictionaryFile needs to be loaded", d.validate, "dictionaryFile not loaded", p) self.assertFalse(d.hasSubDictionary("policy_1")) self.assertTrue(d.hasSubDictionary("policy_2")) self.assertFalse(d.hasSubDictionary("policy_load")) n = d.loadPolicyFiles(self.getTestDictionary(), True) self.assertTrue(d.hasSubDictionary("policy_load")) self.assertEqual(n, 1) # number of files loaded d.validate(p) ve = ValidationError("Dictionary_1.py", 1, "testNested") p = Policy(self.getTestDictionary("nested_policy_bad.paf")) d.validate(p, ve.cpp) self.assertEqual(ve.getErrors("policy_1"), ValidationError.WRONG_TYPE) self.assertEqual(ve.getErrors("policy_2.foo"), ValidationError.VALUE_DISALLOWED) self.assertEqual(ve.getErrors("policy_2.bar"), ValidationError.MISSING_REQUIRED) self.assertEqual(ve.getErrors("policy_3.baz.qux"), ValidationError.WRONG_TYPE) self.assertEqual(ve.getErrors("policy_3.baz.paisley"), ValidationError.MISSING_REQUIRED) self.assertEqual(ve.getErrors("policy_3.baz.paisley"), ValidationError.MISSING_REQUIRED) self.assertEqual(ve.getErrors("policy_load.height"), ValidationError.MISSING_REQUIRED) self.assertEqual(ve.getParamCount(), 6) # multiple nesting p = Policy(self.getTestDictionary("nested_policy_1.paf")) n = p.loadPolicyFiles(self.getTestDictionary()) self.assertEqual(n, 3) self.assertEqual(p.getString("1.2b.foo"), "bar") d = Dictionary(self.getTestDictionary("nested_dictionary_1.paf")) n = d.loadPolicyFiles(self.getTestDictionary()) self.assertEqual(n, 3) p = Policy(True, d) # load from defaults self.assertEqual(p.getString("1.2a.foo"), "bar") self.assertEqual(p.getString("1.2b.foo"), "bar") # error in child d = Dictionary(self.getTestDictionary("nested_dictionary_bad_child.paf")) d.loadPolicyFiles(self.getTestDictionary()) # this should really be caught during loadPolicyFiles(), above self.assertRaiseLCE(DictionaryError, "Unknown type: \"NotAType\"", d.makeDef("sub.something").getType, "Loaded sub-dictionary specified a bogus type")
def testNested(self): self.assertRaiseLCE(DictionaryError, "policy_bad_subdef.dictionary is a string", Dictionary, "Malformed subdictionary", self.getTestDictionary("nested_dictionary_bad_1.paf")) p = Policy(self.getTestDictionary("nested_policy_good.paf")) self.assertRaiseLCE(DictionaryError, "Unknown Dictionary property", Dictionary, "Malformed subdictionary", self.getTestDictionary("nested_dictionary_bad_2.paf")) d = Dictionary(self.getTestDictionary("nested_dictionary_good.paf")) d.check() self.assertRaiseLCE(lsst.pex.exceptions.LogicError, "dictionaryFile needs to be loaded", d.validate, "dictionaryFile not loaded", p) self.assert_(not d.hasSubDictionary("policy_1")) self.assert_(d.hasSubDictionary("policy_2")) self.assert_(not d.hasSubDictionary("policy_load")) n = d.loadPolicyFiles(self.getTestDictionary(), True) self.assert_(d.hasSubDictionary("policy_load")) self.assert_(n == 1) # number of files loaded d.validate(p) ve = ValidationError("Dictionary_1.py", 1, "testNested") p = Policy(self.getTestDictionary("nested_policy_bad.paf")) d.validate(p, ve.cpp) self.assert_(ve.getErrors("policy_1") == ValidationError.WRONG_TYPE) self.assert_(ve.getErrors("policy_2.foo") == ValidationError.VALUE_DISALLOWED) self.assert_(ve.getErrors("policy_2.bar") == ValidationError.MISSING_REQUIRED) self.assert_(ve.getErrors("policy_3.baz.qux") == ValidationError.WRONG_TYPE) self.assert_(ve.getErrors("policy_3.baz.paisley") == ValidationError.MISSING_REQUIRED) self.assert_(ve.getErrors("policy_3.baz.paisley") == ValidationError.MISSING_REQUIRED) self.assert_(ve.getErrors("policy_load.height") == ValidationError.MISSING_REQUIRED) self.assert_(ve.getParamCount() == 6) # multiple nesting p = Policy(self.getTestDictionary("nested_policy_1.paf")) n = p.loadPolicyFiles(self.getTestDictionary()) self.assert_(n == 3) self.assert_(p.getString("1.2b.foo") == "bar") d = Dictionary(self.getTestDictionary("nested_dictionary_1.paf")) n = d.loadPolicyFiles(self.getTestDictionary()) self.assert_(n == 3) p = Policy(True, d) # load from defaults self.assert_(p.getString("1.2a.foo") == "bar") self.assert_(p.getString("1.2b.foo") == "bar") # error in child d = Dictionary(self.getTestDictionary("nested_dictionary_bad_child.paf")) d.loadPolicyFiles(self.getTestDictionary()) # this should really be caught during loadPolicyFiles(), above self.assertRaiseLCE(DictionaryError, "Unknown type: \"NotAType\"", d.makeDef("sub.something").getType, "Loaded sub-dictionary specified a bogus type")