def test_017_escaped_array_of_dict(self): out = {} path_put(out, 'transaction.metadata.references.0.reference_type', 'FOO') path_put(out, 'transaction.metadata.references.0.reference_ids.0', '1234') self.assertEqual( out, { 'transaction': { 'metadata': { 'references': [{ 'reference_type': 'FOO', 'reference_ids': ['1234'] }] } } }, ) self.assertEqual( path_get(out, 'transaction.metadata.references.0.reference_type'), 'FOO') self.assertEqual( path_get(out, 'transaction.metadata.references.0.reference_ids.0'), '1234')
def test_016_escaped_array_index_key(self): out = {} path_put(out, '{record_transaction.0}.inputs.foo', 'bar') self.assertEqual(out, {'record_transaction.0': { 'inputs': { 'foo': 'bar' } }}) self.assertEqual(path_get(out, '{record_transaction.0}.inputs.foo'), 'bar')
def assert_not_present(not_present, actual, msg=None): """ Assert that none of the keys in not_present exist in actual """ present = [] for path in get_all_paths(not_present): try: path_get(actual, path) present.append(path) except (KeyError, IndexError): pass if present: msg = make_error_msg_header(msg, 'Not present') msg += make_failed_comparision_error_message( 'SHOULD NOT be present', pprint.pformat(present), pprint.pformat(actual), ) raise AssertionError(msg)
def test_005_02_bracket_name_then_list(self): out = {} path_put(out, 'foo.bar.{thing.in.bracket}.0', 'la la la') self.assertEqual(out, {'foo': { 'bar': { 'thing.in.bracket': ['la la la'] } }}) self.assertEqual(path_get(out, 'foo.bar.{thing.in.bracket}.0'), 'la la la')
def test_020_nested_brackets(self): out = {} path_put(out, 'charge.cost_components.{{item.gross}}', {'MISSING'}) self.assertEqual( out, {'charge': { 'cost_components': { '{item.gross}': {'MISSING'} } }}) self.assertEqual( path_get(out, 'charge.cost_components.{{item.gross}}'), {'MISSING'})
def test_019_nested_brackets(self): out = {} # type: Dict[six.text_type, Any] path_put(out, 'charge.cost_components.{{item.gross}}', {'MISSING'}) self.assertEqual( out, {'charge': { 'cost_components': { '{item.gross}': {'MISSING'} } }}) self.assertEqual( path_get(out, 'charge.cost_components.{{item.gross}}'), {'MISSING'})
def assert_not_present( not_present, # type: Union[Mapping, List, Tuple, AbstractSet] actual, # type: Union[Mapping, List, Tuple, AbstractSet] msg=None, # type: Optional[six.text_type] ): # type: (...) -> None """ Assert that none of the keys in not_present exist in actual """ present = [] # type: List[six.text_type] for path in get_all_paths(not_present): try: path_get(actual, path) present.append(path) except (KeyError, IndexError): pass if present: msg = make_error_msg_header(msg, 'Not present') msg += make_failed_comparision_error_message( 'SHOULD NOT be present', pprint.pformat(present), pprint.pformat(actual), ) raise AssertionError(msg)
def ingest_from_parsed_test_fixture(self, action_case, test_case, parse_results, file_name, line_number): path = 'expects_{not_q}{job_q}{exact_q}error'.format( not_q='not_' if getattr(parse_results, 'not', None) else '', job_q='job_' if parse_results.job else '', exact_q='exact_' if parse_results.exact else '', ) try: errors = path_get(action_case, path) except (KeyError, IndexError): errors = [] path_put(action_case, path, errors) # noinspection PyTypeChecker errors.append(Error( # type: ignore code=parse_results.error_code, message=getattr(parse_results, 'error_message', None) or AnyValue('str'), # type: ignore field=getattr(parse_results, 'field_name', None) or AnyValue('str', permit_none=True), # type: ignore traceback=AnyValue('str', permit_none=True), # type: ignore variables=AnyValue('dict', permit_none=True), # type: ignore denied_permissions=AnyValue('list', permit_none=True), # type: ignore is_caller_error=AnyValue('bool'), # type: ignore ))
def ingest_from_parsed_test_fixture(self, action_case, test_case, parse_results, file_name, line_number): path = 'expects_{not_q}{job_q}{exact_q}error'.format( not_q='not_' if getattr(parse_results, 'not', None) else '', job_q='job_' if parse_results.job else '', exact_q='exact_' if parse_results.exact else '', ) try: errors = path_get(action_case, path) except (KeyError, IndexError): errors = [] path_put(action_case, path, errors) errors.append( Error( code=parse_results.error_code, message=getattr(parse_results, 'error_message', None) or AnyValue('str'), field=getattr(parse_results, 'field_name', None) or AnyValue('str', permit_none=True), traceback=AnyValue('str', permit_none=True), variables=AnyValue('list', permit_none=True), ))
def test_003_bracket_name_put(self): out = {} # type: Dict[six.text_type, Any] path_put(out, 'foo.{bar.baz}', 'moo') self.assertEqual(out, {'foo': {'bar.baz': 'moo'}}) self.assertEqual(path_get(out, 'foo.{bar.baz}'), 'moo')
def test_002_nested_put(self): out = {} # type: Dict[six.text_type, Any] path_put(out, 'foo.bar', 'baz') self.assertEqual(out, {'foo': {'bar': 'baz'}}) self.assertEqual(path_get(out, 'foo.bar'), 'baz')
def test_001_simple_put(self): out = {} # type: Dict[six.text_type, Any] path_put(out, 'foo', 'bar') self.assertEqual(out, {'foo': 'bar'}) self.assertEqual(path_get(out, 'foo'), 'bar')
def test_015_path_get_missing_nested_array_further_index(self): with self.assertRaises(IndexError): path_get({'foo': [[{}, {}]]}, 'foo.0.4')
def test_009_numeric_dictionary_keys(self): out = {} path_put(out, 'foo.bar.{2}.baz', 'thing') self.assertEquals(out, {'foo': {'bar': {'2': {'baz': 'thing'}}}}) self.assertEquals(path_get(out, 'foo.bar.{2}.baz'), 'thing')
def test_009_numeric_dictionary_keys(self): out = {} # type: Dict[six.text_type, Any] path_put(out, 'foo.bar.{2}.baz', 'thing') self.assertEqual(out, {'foo': {'bar': {'2': {'baz': 'thing'}}}}) self.assertEqual(path_get(out, 'foo.bar.{2}.baz'), 'thing')
def test_007_array_with_nested_array(self): out = {} # type: Dict[six.text_type, Any] path_put(out, 'foo.bar.0.0.baz', 'thing') self.assertEqual(out, {'foo': {'bar': [[{'baz': 'thing'}]]}}) self.assertEqual(path_get(out, 'foo.bar.0.0.baz'), 'thing')
def test_011_path_get_missing_nested_key(self): with self.assertRaises(KeyError): path_get({'foo': {'bar': 'baz'}}, 'foo.blah')
def test_005_simple_array_put(self): out = {} path_put(out, 'foo.bar.0', 'thing') self.assertEqual(out, {'foo': {'bar': ['thing']}}) self.assertEqual(path_get(out, 'foo.bar.0'), 'thing')
def test_003_bracket_name_put(self): out = {} path_put(out, 'foo.{bar.baz}', 'moo') self.assertEqual(out, {'foo': {'bar.baz': 'moo'}}) self.assertEqual(path_get(out, 'foo.{bar.baz}'), 'moo')
def test_002_nested_put(self): out = {} path_put(out, 'foo.bar', 'baz') self.assertEqual(out, {'foo': {'bar': 'baz'}}) self.assertEqual(path_get(out, 'foo.bar'), 'baz')
def test_001_simple_put(self): out = {} path_put(out, 'foo', 'bar') self.assertEqual(out, {'foo': 'bar'}) self.assertEqual(path_get(out, 'foo'), 'bar')
def test_004_bracket_name_then_more_depth(self): out = {} # type: Dict[six.text_type, Any] path_put(out, 'foo.{bar.baz}.gar', 'moo') self.assertEqual(out, {'foo': {'bar.baz': {'gar': 'moo'}}}) self.assertEqual(path_get(out, 'foo.{bar.baz}.gar'), 'moo')
def test_005_simple_array_put(self): out = {} # type: Dict[six.text_type, Any] path_put(out, 'foo.bar.0', 'thing') self.assertEqual(out, {'foo': {'bar': ['thing']}}) self.assertEqual(path_get(out, 'foo.bar.0'), 'thing')
def test_013_path_get_missing_array_further_index(self): with self.assertRaises(IndexError): path_get({'foo': [{}, {}]}, 'foo.2')
def test_008_array_with_missing_index(self): out = {} # type: Dict[six.text_type, Any] path_put(out, 'foo.bar.2.baz', 'thing') self.assertEqual(out, {'foo': {'bar': [{}, {}, {'baz': 'thing'}]}}) self.assertEqual(path_get(out, 'foo.bar.2.baz'), 'thing')
def test_014_path_get_missing_nested_array(self): with self.assertRaises(IndexError): path_get({'foo': [[]]}, 'foo.0.0')
def test_010_path_get_missing_key(self): with self.assertRaises(KeyError): path_get({}, 'foo')
def _finalize_test_case(self, active_string, location, _): # type: (six.text_type, int, Optional[ParseResults]) -> None """ Called by PyParsing at the end of each test case. :param active_string: The contents of the fixture file :param location: The file location of the current parsing activity """ self._fixture_source.append('') if self._working_action_case: # We're done parsing the test case and still need to wrap up the last action in the test case for dc in get_all_directives(): dc().post_parse_test_case_action( self._working_action_case, self._working_test_case or self._global_directives, ) self._working_action_case = {} if not self._working_test_case: # just a blank line before any test cases, probably after globals or an extra blank line between tests return self._working_test_case['line_number'] = self._working_test_case_line_number self._working_test_case_line_number = 0 self._working_test_case['fixture_name'] = self._fixture_name self._working_test_case['fixture_file_name'] = self._fixture_file_name self._working_test_case['source'] = self._working_test_case_source line_number = get_parse_line_number(location, active_string) if not self._working_test_case.get('name'): raise FixtureSyntaxError( '{}:{}: Test case without name'.format(self._fixture_file_name, line_number), file_name=self._fixture_file_name, line_number=line_number - 1, ) if not self._working_test_case.get('description'): raise FixtureSyntaxError( '{}:{}: Test case without description'.format(self._fixture_file_name, line_number), file_name=self._fixture_file_name, line_number=line_number - 1, ) if not self._working_test_case.get('actions') and not self._global_directives: raise FixtureSyntaxError( '{}:{}: Empty test case'.format(self._fixture_file_name, line_number), file_name=self._fixture_file_name, line_number=line_number - 1, ) if self._global_directives: # merge, but make sure current overlays global where there is conflict test_case = {} # type: TestCase for path in get_all_paths(self._global_directives, allow_blank=True): try: value = path_get(self._global_directives, path) path_put(test_case, path, copy.copy(value)) except (KeyError, IndexError): raise FixtureSyntaxError( 'Invalid path: `{}`'.format(path), file_name=self._fixture_file_name, line_number=line_number, ) for path in get_all_paths(self._working_test_case, allow_blank=True): try: path_put(test_case, path, path_get(self._working_test_case, path)) except (KeyError, IndexError): raise FixtureSyntaxError( 'Invalid path: `{}`'.format(path), file_name=self._fixture_file_name, line_number=line_number, ) for directive_class in get_all_directives(): directive_class().post_parse_test_case(test_case) else: for directive_class in get_all_directives(): directive_class().post_parse_test_case(self._working_test_case) test_case = copy.deepcopy(self._working_test_case) test_case['fixture_source'] = self._fixture_source self.test_cases.append(test_case) self._working_test_case.clear() self._working_test_case_source = []
def test_012_path_get_missing_array(self): with self.assertRaises(KeyError): path_get({'foo': {'bar': 'baz'}}, 'foo.0')
def test_006_array_with_nested_dict(self): out = {} path_put(out, 'foo.bar.0.baz', 'thing') self.assertEqual(out, {'foo': {'bar': [{'baz': 'thing'}]}}) self.assertEqual(path_get(out, 'foo.bar.0.baz'), 'thing')