def test_dict_nested_subset_exact_keys(self): context = ExecutionContext() small_dict = {'first':'Apple', 'second':'Banana'} small_nested_dict = {'outer':small_dict} # In dictionary we want exact key matches, not subkeys. operand = {'out':{'first':'Apple'}} subset_pred = jp.DICT_SUBSET(operand) self.assertEqual( jp.MissingPathError(source=small_nested_dict, target_path='out', path_value=PathValue('', small_nested_dict)), subset_pred(context, small_nested_dict)) # In dictionary we want exact key matches, not subkeys. nested_operand = {'fir':'Apple'} operand = {'outer':nested_operand} subset_pred = jp.DICT_SUBSET(operand) self.assertEqual( jp.MissingPathError(source=small_nested_dict, target_path=jp.PATH_SEP.join(['outer', 'fir']), path_value=PathValue('outer', small_dict)), subset_pred(context, small_nested_dict))
def test_path_found_in_array(self): pred = jp.PathPredicate(PATH_SEP.join(['outer', 'inner', 'a'])) simple = {'a': 'A', 'b': 'B'} source = {'outer': [{'middle': simple}, {'inner': simple}]} found = [PathValue(PATH_SEP.join(['outer[1]', 'inner', 'a']), 'A')] pruned = [ jp.MissingPathError(source['outer'][0], 'inner', path_value=PathValue('outer[0]', source['outer'][0])) ] expect = _make_result(pred, None, source, found, [], pruned) self.assertEqual(expect, pred(source))
def test_dict_match_missing_path(self): context = ExecutionContext() source = {'n': 10} want = {'missing': jp.NUM_EQ(10)} result = jp.DICT_MATCHES(want)(context, source) expect = (jp.KeyedPredicateResultBuilder( jp.DICT_MATCHES(want)).add_result( 'missing', jp.MissingPathError(source=source, target_path='missing')).build(False)) self.assertFalse(result) self.assertEquals(expect, result)
def test_dict_simple_subset(self): context = ExecutionContext() letters = {'a':'A', 'b':'B', 'c':'C'} operand = {'a': 'A', 'b': 'B'} subset_pred = jp.DICT_SUBSET(operand) self.assertGoodResult(PathValue('', letters), subset_pred, subset_pred(context, letters)) self.assertGoodResult(PathValue('', operand), subset_pred, subset_pred(context, operand)) source = {'a':'A'} self.assertEqual( jp.MissingPathError(source=source, target_path='b', path_value=PathValue('', source)), subset_pred(context, source))
def test_dict_subset_with_array_nodes(self): # See test_collect_with_dict_subset for comparison. context = ExecutionContext() letters = {'a':'A', 'b':'B', 'c':'C'} numbers = {'a':1, 'b':2} both = [letters, numbers] source = {'items': both} # This doesnt traverse through the list because we are looking for # a dictionary subset. We were expecting a dict with an 'a' but we # found only a dict with 'items'. letter_subset_pred = jp.DICT_SUBSET({'a':'A'}) self.assertEqual( jp.MissingPathError(source=source, target_path='a', path_value=PathValue('', source)), letter_subset_pred(context, source))