def testSelfValidation(self): # assign a dictionary after creation p = Policy(self.getTestDictionary("types_policy_good.paf")) p.loadPolicyFiles(self.getTestDictionary(), True) typesDict = Dictionary(self.getTestDictionary("types_dictionary.paf")) valuesDict = Dictionary(self.getTestDictionary("values_dictionary.paf")) self.assert_(not p.canValidate()) p.setDictionary(typesDict) self.assert_(p.canValidate()) p.validate() p.set("bool_type", True) self.assertValidationError( ValidationError.WRONG_TYPE, p.set, "bool_type", "foo") # create with dictionary p = Policy.createPolicy(self.getTestDictionary("types_dictionary.paf"), "", True) self.assert_(p.canValidate()) p.set("bool_type", True) self.assertValidationError( ValidationError.WRONG_TYPE, p.set, "bool_type", "foo") # assign a dictionary after invalid modifications p = Policy() p.set("bool_type", "foo") p.setDictionary(typesDict) ve = ValidationError("Dictionary_1.py", 1, "testSelfValidation") p.validate(ve) self.assert_(ve.getErrors("bool_type") == ValidationError.WRONG_TYPE) try: p.validate() except ValidationError, e: ve = e.args[0] self.assert_(ve.getErrors("bool_type") == ValidationError.WRONG_TYPE) self.assert_(ve.getParamCount() == 1)
def testSelfValidation(self): # assign a dictionary after creation p = Policy(self.getTestDictionary("types_policy_good.paf")) p.loadPolicyFiles(self.getTestDictionary(), True) typesDict = Dictionary(self.getTestDictionary("types_dictionary.paf")) valuesDict = Dictionary( self.getTestDictionary("values_dictionary.paf")) self.assertFalse(p.canValidate()) p.setDictionary(typesDict) self.assertTrue(p.canValidate()) p.validate() p.set("bool_type", True) self.assertValidationError(ValidationError.WRONG_TYPE, p.set, "bool_type", "foo") # create with dictionary p = Policy.createPolicy(self.getTestDictionary("types_dictionary.paf"), "", True) self.assertTrue(p.canValidate()) p.set("bool_type", True) self.assertValidationError(ValidationError.WRONG_TYPE, p.set, "bool_type", "foo") # assign a dictionary after invalid modifications p = Policy() p.set("bool_type", "foo") p.setDictionary(typesDict) ve = ValidationError("Dictionary_1.py", 1, "testSelfValidation") p.validate(ve.cpp) self.assertEqual(ve.getErrors("bool_type"), ValidationError.WRONG_TYPE) try: p.validate() except ValidationError as ve: self.assertEqual(ve.getErrors("bool_type"), ValidationError.WRONG_TYPE) self.assertEqual(ve.getParamCount(), 1) p.set("bool_type", True) p.set("int_type", 1) p.validate() # switch dictionaries oldD = p.getDictionary() p.setDictionary(valuesDict) try: p.validate() except ValidationError as ve: self.assertEqual(ve.getErrors("bool_type"), ValidationError.UNKNOWN_NAME) p.set("string_range_type", "moo") try: p.set("string_range_type", "victor") except ValidationError as ve: self.assertEqual(ve.getErrors("string_range_type"), ValidationError.VALUE_OUT_OF_RANGE) p.setDictionary(oldD) p.remove("string_range_type") p.validate()
def testSelfValidation(self): # assign a dictionary after creation p = Policy(self.getTestDictionary("types_policy_good.paf")) p.loadPolicyFiles(self.getTestDictionary(), True) typesDict = Dictionary(self.getTestDictionary("types_dictionary.paf")) valuesDict = Dictionary(self.getTestDictionary("values_dictionary.paf")) self.assert_(not p.canValidate()) p.setDictionary(typesDict) self.assert_(p.canValidate()) p.validate() p.set("bool_type", True) self.assertValidationError( ValidationError.WRONG_TYPE, p.set, "bool_type", "foo") # create with dictionary p = Policy.createPolicy(self.getTestDictionary("types_dictionary.paf"), "", True) self.assert_(p.canValidate()) p.set("bool_type", True) self.assertValidationError( ValidationError.WRONG_TYPE, p.set, "bool_type", "foo") # assign a dictionary after invalid modifications p = Policy() p.set("bool_type", "foo") p.setDictionary(typesDict) ve = ValidationError("Dictionary_1.py", 1, "testSelfValidation") p.validate(ve.cpp) self.assert_(ve.getErrors("bool_type") == ValidationError.WRONG_TYPE) try: p.validate() except ValidationError as ve: self.assert_(ve.getErrors("bool_type") == ValidationError.WRONG_TYPE) self.assert_(ve.getParamCount() == 1) p.set("bool_type", True) p.set("int_type", 1) p.validate() # switch dictionaries oldD = p.getDictionary() p.setDictionary(valuesDict) try: p.validate() except ValidationError as ve: self.assert_(ve.getErrors("bool_type") == ValidationError.UNKNOWN_NAME) p.set("string_range_type", "moo") try: p.set("string_range_type", "victor") except ValidationError as ve: self.assert_(ve.getErrors("string_range_type") == ValidationError.VALUE_OUT_OF_RANGE) p.setDictionary(oldD) p.remove("string_range_type") p.validate()
def testSampleCode(self): policyFile = DefaultPolicyFile("pex_policy", "defaults_dictionary_complete.paf", "tests/dictionary") defaults = Policy.createPolicy(policyFile, policyFile.getRepositoryPath(), True) policy = Policy() policy.mergeDefaults(defaults) self.assertTrue(policy.canValidate()) policy = Policy() policy.mergeDefaults(defaults, False) self.assertFalse(policy.canValidate()) # even if not keeping it around for future validation, validate the merge now policy = Policy() policy.set("bad", "worse") self.assertValidationError(ValidationError.UNKNOWN_NAME, policy.mergeDefaults, defaults, False) self.assertFalse(policy.canValidate())
def testSampleCode(self): policyFile = DefaultPolicyFile("pex_policy", "defaults_dictionary_complete.paf", "tests/dictionary") defaults = Policy.createPolicy(policyFile, policyFile.getRepositoryPath(), True) policy = Policy() policy.mergeDefaults(defaults) self.assert_(policy.canValidate()) policy = Policy() policy.mergeDefaults(defaults, False) self.assert_(not policy.canValidate()) # even if not keeping it around for future validation, validate the merge now policy = Policy() policy.set("bad", "worse") self.assertValidationError(ValidationError.UNKNOWN_NAME, policy.mergeDefaults, defaults, False) self.assert_(not policy.canValidate())
def testMergeDefaults(self): # from a non-trivial dictionary p = Policy(self.getTestDictionary("defaults_policy_partial.paf")) p.set("required", "foo") d = Dictionary(self.getTestDictionary("defaults_dictionary_good.paf")) d.loadPolicyFiles(self.getTestDictionary(), True) self.assertEqual(p.nameCount(), 2) p.mergeDefaults(d) self.assertEqual(p.valueCount("int_range_count"), 3) self.assertEqual(p.nameCount(), 7) # from a policy that's really a dictionary p = Policy() pd = Policy(self.getTestDictionary("defaults_dictionary_indirect.paf")) p.mergeDefaults(pd) self.assertEqual(p.getString("string_type"), "foo") self.assertTrue(p.getDictionary().isDictionary()) # from a policy that's really a non-trivial dictionary p = Policy(self.getTestDictionary("defaults_policy_partial.paf")) p.set("required", "foo") pd = Policy(self.getTestDictionary("defaults_dictionary_policy.paf")) pd.loadPolicyFiles(self.getTestDictionary(), True) self.assertEqual(p.nameCount(), 2) p.mergeDefaults(pd) self.assertEqual(p.valueCount("int_range_count"), 3) self.assertEqual(p.nameCount(), 5) # ensure post-load validation p.set("int_range_count", -5) self.assertValidationError(ValidationError.UNKNOWN_NAME, p.add, "unknown", 0) # test throwing validation p = Policy(self.getTestDictionary("defaults_policy_partial.paf")) try: p.mergeDefaults(pd) except ValidationError as ve: self.assertEqual(ve.getErrors("required"), ValidationError.MISSING_REQUIRED) # non-throwing validation p = Policy(self.getTestDictionary("defaults_policy_partial.paf")) ve = ValidationError("Dictionary_1.py", 1, "testMergeDefaults") p.mergeDefaults(pd, False, ve.cpp) self.assertEqual(ve.getErrors("required"), ValidationError.MISSING_REQUIRED) self.assertEqual(ve.getParamCount(), 1) # non-retention p = Policy(self.getTestDictionary("defaults_policy_partial.paf")) p.set("required", "foo") p.mergeDefaults(pd, False) # make sure validate() fails gracefully when no dictionary present self.assertRaiseLCE(DictionaryError, "No dictionary", p.validate, "No dictionary assigned") p.add("unknown", 0) # would be rejected if dictionary was kept # deep merge from a Policy that's not a Dictionary p = Policy(self.getTestDictionary("defaults_policy_partial.paf")) p.mergeDefaults(Policy(self.getTestDictionary("defaults_policy_most.paf"))) self.assertEqual(p.nameCount(), 3) self.assertIs(p.getBool("bool_set_count"), True) self.assertEqual(p.getString("indirect.string_type"), "bar") # propagation of a Dictionary from one Policy to another via mergeDefaults d = Dictionary(self.getTestDictionary("defaults_dictionary_complete.paf")) d.loadPolicyFiles(self.getTestDictionary()) pEmpty = Policy() pEmpty.mergeDefaults(d) self.assertTrue(pEmpty.canValidate()) pPartial = Policy(self.getTestDictionary("defaults_policy_partial.paf")) pPartial.mergeDefaults(pEmpty) self.assertTrue(pPartial.canValidate(), "Dictionary handed off via mergeDefaults.")
def testMergeDefaults(self): # from a non-trivial dictionary p = Policy(self.getTestDictionary("defaults_policy_partial.paf")) p.set("required", "foo") d = Dictionary(self.getTestDictionary("defaults_dictionary_good.paf")) d.loadPolicyFiles(self.getTestDictionary(), True) self.assert_(p.nameCount() == 2) p.mergeDefaults(d) self.assert_(p.valueCount("int_range_count") == 3) self.assert_(p.nameCount() == 7) # from a policy that's really a dictionary p = Policy() pd = Policy(self.getTestDictionary("defaults_dictionary_indirect.paf")) p.mergeDefaults(pd) self.assert_(p.getString("string_type") == "foo") self.assert_(p.getDictionary().isDictionary()) # from a policy that's really a non-trivial dictionary p = Policy(self.getTestDictionary("defaults_policy_partial.paf")) p.set("required", "foo") pd = Policy(self.getTestDictionary("defaults_dictionary_policy.paf")) pd.loadPolicyFiles(self.getTestDictionary(), True) self.assert_(p.nameCount() == 2) p.mergeDefaults(pd) self.assert_(p.valueCount("int_range_count") == 3) self.assert_(p.nameCount() == 5) # ensure post-load validation p.set("int_range_count", -5) self.assertValidationError(ValidationError.UNKNOWN_NAME, p.add, "unknown", 0) # test throwing validation p = Policy(self.getTestDictionary("defaults_policy_partial.paf")) try: p.mergeDefaults(pd) except ValidationError as ve: self.assert_(ve.getErrors("required") == ValidationError.MISSING_REQUIRED) # non-throwing validation p = Policy(self.getTestDictionary("defaults_policy_partial.paf")) ve = ValidationError("Dictionary_1.py", 1, "testMergeDefaults") p.mergeDefaults(pd, False, ve.cpp) self.assert_(ve.getErrors("required") == ValidationError.MISSING_REQUIRED) self.assert_(ve.getParamCount() == 1) # non-retention p = Policy(self.getTestDictionary("defaults_policy_partial.paf")) p.set("required", "foo") p.mergeDefaults(pd, False) # make sure validate() fails gracefully when no dictionary present self.assertRaiseLCE(DictionaryError, "No dictionary", p.validate, "No dictionary assigned") p.add("unknown", 0) # would be rejected if dictionary was kept # deep merge from a Policy that's not a Dictionary p = Policy(self.getTestDictionary("defaults_policy_partial.paf")) p.mergeDefaults(Policy(self.getTestDictionary("defaults_policy_most.paf"))) self.assert_(p.nameCount() == 3) self.assert_(p.getBool("bool_set_count") == True) self.assert_(p.getString("indirect.string_type") == "bar") # propagation of a Dictionary from one Policy to another via mergeDefaults d = Dictionary(self.getTestDictionary("defaults_dictionary_complete.paf")) d.loadPolicyFiles(self.getTestDictionary()) pEmpty = Policy() pEmpty.mergeDefaults(d) self.assert_(pEmpty.canValidate()) pPartial = Policy(self.getTestDictionary("defaults_policy_partial.paf")) pPartial.mergeDefaults(pEmpty) self.assert_(pPartial.canValidate(), "Dictionary handed off via mergeDefaults.")
p.validate, "No dictionary assigned") p.add("unknown", 0) # would be rejected if dictionary was kept # deep merge from a Policy that's not a Dictionary p = Policy(self.getTestDictionary("defaults_policy_partial.paf")) p.mergeDefaults(Policy(self.getTestDictionary("defaults_policy_most.paf"))) self.assert_(p.nameCount() == 3) self.assert_(p.getBool("bool_set_count") == True) self.assert_(p.getString("indirect.string_type") == "bar") # propagation of a Dictionary from one Policy to another via mergeDefaults d = Dictionary(self.getTestDictionary("defaults_dictionary_complete.paf")) d.loadPolicyFiles(self.getTestDictionary()) pEmpty = Policy() pEmpty.mergeDefaults(d) self.assert_(pEmpty.canValidate()) pPartial = Policy(self.getTestDictionary("defaults_policy_partial.paf")) pPartial.mergeDefaults(pEmpty) self.assert_(pPartial.canValidate(), "Dictionary handed off via mergeDefaults.") # test the sample code at http://dev.lsstcorp.org/trac/wiki/PolicyHowto def testSampleCode(self): policyFile = DefaultPolicyFile("pex_policy", "defaults_dictionary_complete.paf", "tests/dictionary") defaults = Policy.createPolicy(policyFile, policyFile.getRepositoryPath(), True) policy = Policy() policy.mergeDefaults(defaults) self.assert_(policy.canValidate()) policy = Policy() policy.mergeDefaults(defaults, False)