def foreach(self, *args) -> DictRuleForeach: """Check if the current path respect a list of rules for each elements. :return: The DictRule related with this rule. :rtype: DictRuleForeach """ rules = args_to_rule_decipher(*args) return DictRuleForeach(self.path, rules)
def respects(self, *args) -> "DictValidator": """Provide a bunch of rules that need to be respected by the self.dictionary dictionary. These rules can be provided using :class:`certum.rule.generic.abstract.DictRule` or lists of :class:`certum.rule.generic.abstract.DictRule`. :raises CertumException: if one argument is not a DictRule complient argument. :return: Itself. :rtype: DictValidator """ rules = args_to_rule_decipher(*args) return DictValidator( self.dictionary, self.rules + rules, self.sorting, self.filtering, self.printing, )
def forsome(self, *args, **kwargs) -> DictRuleForsome: """Check if the current path respect a list of rules for elements corresponding to some keys. .. note:: Forsome needs a parameter 'keys' that contains the keys to analyse. :return: The DictRule related with this rule. :rtype: DictRuleForsome """ try: keys = kwargs["keys"] if not isinstance(keys, list): raise CertumException("Forsome needs a 'keys' argument of type list") except KeyError as error: raise CertumException( "Forsome needs a 'keys' argument of type list" ) from error rules = args_to_rule_decipher(*args) return DictRuleForsome(self.path, keys, rules)
def test_sucess(): """Ensure that the decipher function can extract rules from arguments.""" rules = args_to_rule_decipher(this.equals(2), this.has_unique_elements()) assert len(rules) == 2
def test_failure_not_rule_in_list(): """Ensure that the decipher should not extract argument inside a list that are not rules and raise a :class:`certum.exception.CertumException`.""" with pytest.raises(CertumException): args_to_rule_decipher(["a"])