Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_any_node_empty_list(self):
inputs = get_input_set()
targeting_config_empty_list = {"ANY": []}
targeting_tree_empty_list = create_targeting_tree(targeting_config_empty_list)
self.assertFalse(targeting_tree_empty_list.evaluate(**inputs))
def test_comparison_lt_none(self):
inputs = get_input_set()
# test explicit none in inputs
targeting_config_none = {"LT": {"field": "explicit_none_field", "value": None}}
targeting_tree_none = create_targeting_tree(targeting_config_none)
self.assertFalse(targeting_tree_none.evaluate(**inputs))
# test field missing in inputs (implicit none)
targeting_config_none = {"LT": {"field": "implicit_none_field", "value": None}}
targeting_tree_none = create_targeting_tree(targeting_config_none)
self.assertFalse(targeting_tree_none.evaluate(**inputs))
def test_le_node(self):
inputs = get_input_set()
targeting_config_eq = {"LE": {"field": "num_field", "value": 5}}
targeting_tree_eq = create_targeting_tree(targeting_config_eq)
self.assertTrue(targeting_tree_eq.evaluate(**inputs))
targeting_config_lt = {"LE": {"field": "num_field", "value": 4}}
targeting_tree_lt = create_targeting_tree(targeting_config_lt)
self.assertFalse(targeting_tree_lt.evaluate(**inputs))
targeting_config_gt = {"LE": {"field": "num_field", "value": 6}}
targeting_tree_gt = create_targeting_tree(targeting_config_gt)
self.assertTrue(targeting_tree_gt.evaluate(**inputs))
with self.assertRaises(TargetingNodeError):
create_targeting_tree(targeting_config_empty)
targeting_config_one_arg = {"LE": {"field": "some_field"}}
with self.assertRaises(TargetingNodeError):
create_targeting_tree(targeting_config_one_arg)
targeting_config_three_args = {
"LE": {"field": "some_field", "values": ["one", True], "value": "str_arg"}
}
with self.assertRaises(TargetingNodeError):
create_targeting_tree(targeting_config_three_args)
targeting_config_no_field = {"LE": {"fields": "some_field", "value": "str_arg"}}
with self.assertRaises(TargetingNodeError):
create_targeting_tree(targeting_config_no_field)
targeting_config_no_value = {"LE": {"field": "some_field", "valu": "str_arg"}}
with self.assertRaises(TargetingNodeError):
create_targeting_tree(targeting_config_no_value)
def test_le_node(self):
inputs = get_input_set()
targeting_config_eq = {"LE": {"field": "num_field", "value": 5}}
targeting_tree_eq = create_targeting_tree(targeting_config_eq)
self.assertTrue(targeting_tree_eq.evaluate(**inputs))
targeting_config_lt = {"LE": {"field": "num_field", "value": 4}}
targeting_tree_lt = create_targeting_tree(targeting_config_lt)
self.assertFalse(targeting_tree_lt.evaluate(**inputs))
targeting_config_gt = {"LE": {"field": "num_field", "value": 6}}
targeting_tree_gt = create_targeting_tree(targeting_config_gt)
self.assertTrue(targeting_tree_gt.evaluate(**inputs))
def test_bad_inputs(self):
inputs = get_input_set()
targeting_config_str = {"OVERRIDE": "string"}
targeting_tree_str = create_targeting_tree(targeting_config_str)
self.assertFalse(targeting_tree_str.evaluate(**inputs))
targeting_config_struct = {"OVERRIDE": {"key": "value"}}
targeting_tree_struct = create_targeting_tree(targeting_config_struct)
self.assertFalse(targeting_tree_struct.evaluate(**inputs))
def test_lt_node(self):
inputs = get_input_set()
targeting_config_eq = {"LT": {"field": "num_field", "value": 5}}
targeting_tree_eq = create_targeting_tree(targeting_config_eq)
self.assertFalse(targeting_tree_eq.evaluate(**inputs))
targeting_config_lt = {"LT": {"field": "num_field", "value": 4}}
targeting_tree_lt = create_targeting_tree(targeting_config_lt)
self.assertFalse(targeting_tree_lt.evaluate(**inputs))
targeting_config_gt = {"LT": {"field": "num_field", "value": 6}}
targeting_tree_gt = create_targeting_tree(targeting_config_gt)
self.assertTrue(targeting_tree_gt.evaluate(**inputs))
with self.assertRaises(TargetingNodeError):
create_targeting_tree(targeting_config_empty)
targeting_config_one_arg = {"EQ": {"field": "some_field"}}
with self.assertRaises(TargetingNodeError):
create_targeting_tree(targeting_config_one_arg)
targeting_config_three_args = {
"EQ": {"field": "some_field", "values": ["one", True], "value": "str_arg"}
}
with self.assertRaises(TargetingNodeError):
create_targeting_tree(targeting_config_three_args)
targeting_config_no_field = {"EQ": {"fields": "some_field", "value": "str_arg"}}
with self.assertRaises(TargetingNodeError):
create_targeting_tree(targeting_config_no_field)
targeting_config_no_value = {"EQ": {"field": "some_field", "valu": "str_arg"}}
with self.assertRaises(TargetingNodeError):
create_targeting_tree(targeting_config_no_value)
def test_comparison_le_none(self):
inputs = get_input_set()
# test explicit none in inputs
targeting_config_none = {"LE": {"field": "explicit_none_field", "value": None}}
targeting_tree_none = create_targeting_tree(targeting_config_none)
self.assertFalse(targeting_tree_none.evaluate(**inputs))
# test field missing in inputs (implicit none)
targeting_config_none = {"LE": {"field": "implicit_none_field", "value": None}}
targeting_tree_none = create_targeting_tree(targeting_config_none)
self.assertFalse(targeting_tree_none.evaluate(**inputs))
def _generate_targeting(targeting_config: Optional[Dict[str, Any]]) -> Targeting:
"""Generate the targeting tree for this experiment.
If no config is provided, then assume we want to target all. If an invalid
config is provided, then target none.
"""
if targeting_config is None:
return create_targeting_tree({"OVERRIDE": True})
try:
return create_targeting_tree(targeting_config)
except Exception:
logger.error("Unable to create targeting tree. No targeting applied.")
return create_targeting_tree({"OVERRIDE": False})