def test_fail_not_a_list(self): obj, test, value = True, "==", 1 with self.assertRaises(Exception) as exc: index_of(obj, test, value, tests=self._tests) self.assertIn( "a list is required, was passed a 'bool'", str(exc.exception) )
def test_fail_on_missing(self): obj, test, value, key = [{"a": True}, {"c": False}], "==", True, "a" with self.assertRaises(Exception) as exc: index_of( obj, test, value, key, fail_on_missing=True, tests=self._tests ) self.assertIn("'a' was not found", str(exc.exception))
def test_fail_wantlist_not_a_bool(self): obj, test, value = [1, 2], "==", 1 with self.assertRaises(Exception) as exc: index_of(obj, test, value, wantlist=42, tests=self._tests) self.assertIn( "'wantlist' is required to be a bool, was passed a 'int'", str(exc.exception), )
def test_fail_no_qualfier(self): obj, test, value = [1, 2], "@@", 1 with self.assertRaises(Exception) as exc: index_of(obj, test, value, tests=self._tests) self.assertIn("the test '@@' was not found", str(exc.exception)) obj, test, value, key = [{"a": 1}], "@@", 1, "a" with self.assertRaises(Exception) as exc: index_of(obj, test, value, key, tests=self._tests) self.assertIn("the test '@@' was not found", str(exc.exception))
def run(self, terms, variables, **kwargs): kwargs["tests"] = self._templar.environment.tests if isinstance(terms, dict): terms.update(kwargs) res = index_of(**terms) else: res = index_of(*terms, **kwargs) if not isinstance(res, list): return [res] return res
def test_simple_dict(self): objs = [ ([{"a": 1}], "==", 1, "a", 0), ([{"a": 1}], "==", 1, "b", []), ([{"a": 1}], "==", 2, "a", []), ( [{"a": 1}, {"a": 1}, {"a": 1}, {"a": 2}], "==", 1, "a", [0, 1, 2], ), ( [{"a": "abc"}, {"a": "def"}, {"a": "ghi"}, {"a": "jkl"}], "ansible.builtin.match", "^a", "a", 0, ), ( [{"a": "abc"}, {"a": "def"}, {"a": "ghi"}, {"a": "jkl"}], "ansible.builtin.search", "e", "a", 1, ), ] for entry in objs: obj, test, value, key, answer = entry result = index_of(obj, test, value, key, tests=self._tests) self.assertEqual(result, answer)
def test_simple_lists(self): objs = [ ([1, 2, 3], "==", 2, 1), (["a", "b", "c"], "eq", "c", 2), ([True, False, 0, 1], "equalto", False, [1, 2]), ([True, False, "0", "1"], "==", False, 1), ([True, False, "", "1"], "==", False, 1), ([True, False, "", "1"], "in", False, 1), ([True, False, "", "1", "a"], "in", [False, "1"], [1, 3]), ([1, 2, 3, "a", "b", "c"], "!=", "c", [0, 1, 2, 3, 4]), ([1, 2, 3], "!<", 3, 2), ] for entry in objs: obj, test, value, answer = entry result = index_of(obj, test, value, tests=self._tests) expected = answer self.assertEqual(result, expected)
def test_just_test(self): """Limit to jinja < 2.11 tests""" objs = [ # ([True], "true", 0), # ([False], "not false", []), # ([False, 5], "boolean", 0), # ([0, False], "false", 1), ([3, 4], "even", 1), ([3, 3], "even", []), ([3, 3, 3, 4], "odd", [0, 1, 2]), # ([3.3, 3.4], "float", [0, 1]), ] for entry in objs: obj, test, answer = entry result = index_of(obj, test, tests=self._tests) expected = answer self.assertEqual(result, expected)
def run(self, terms, variables, **kwargs): if isinstance(terms, list): keys = [ "data", "test", "value", "key", "fail_on_missing", "wantlist", ] terms = dict(zip(keys, terms)) terms.update(kwargs) aav = AnsibleArgSpecValidator( data=terms, schema=DOCUMENTATION, name="index_of" ) valid, errors, updated_data = aav.validate() if not valid: raise AnsibleLookupError(errors) updated_data["wantlist"] = True updated_data["tests"] = self._templar.environment.tests res = index_of(**updated_data) return res
def _index_of(*args, **kwargs): """Find the indicies of items in a list matching some criteria.""" keys = [ "environment", "data", "test", "value", "key", "fail_on_missing", "wantlist", ] data = dict(zip(keys, args)) data.update(kwargs) environment = data.pop("environment") aav = AnsibleArgSpecValidator(data=data, schema=DOCUMENTATION, name="index_of") valid, errors, updated_data = aav.validate() if not valid: raise AnsibleFilterError(errors) updated_data["tests"] = environment.tests return index_of(**updated_data)
def test_fail_key_not_valid(self): obj, test, value, key = [{"a": "b"}], "==", "b", [1, 2] with self.assertRaises(Exception) as exc: index_of(obj, test, value, key, tests=self._tests) self.assertIn("Unknown key type", str(exc.exception))
def test_fail_mixed_list(self): obj, test, value, key = [{"a": "b"}, True, 1, "a"], "==", "b", "a" with self.assertRaises(Exception) as exc: index_of(obj, test, value, key, tests=self._tests) self.assertIn("required to be dictionaries", str(exc.exception))
def _index_of(*args, **kwargs): """Find the indicies of items in a list matching some criteria. [See examples](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.utils.index_of_lookup.rst)""" kwargs["tests"] = args[0].tests args = args[1:] return index_of(*args, **kwargs)