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 _ingest_expect_called(test_target, parse_results, file_name, line_number): key = (parse_results.stub_service, parse_results.stub_action) stub_config = test_target.setdefault('stubbed_actions', {}).setdefault(key, {}) if getattr(parse_results, 'not', False): if 'expect_request' in stub_config: raise FixtureSyntaxError( 'Cannot combine "expect called" and "expect not called" on the same stub action for {}.{}' .format(*key), file_name, line_number, ) stub_config['expect_not_called'] = True else: if 'expect_not_called' in stub_config: raise FixtureSyntaxError( 'Cannot combine "expect called" and "expect not called" on the same stub action for {}.{}' .format(*key), file_name, line_number, ) if getattr(parse_results, 'variable_name', None): path_put( stub_config.setdefault('expect_request', {}), parse_results.variable_name, get_parsed_data_type_value(parse_results, parse_results.value), ) else: stub_config.setdefault('expect_request', {})
def ingest_from_parsed_test_fixture(self, action_case, test_case, parse_results, file_name, line_number): if 'description' in test_case: raise FixtureSyntaxError('Duplicate test description directive for test case', file_name, line_number) path_put( test_case, 'description', '{}\n{}'.format(parse_results.description.strip(' \t\n'), file_name), )
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 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 ingest_from_parsed_test_fixture(self, action_case, test_case, parse_results, file_name, line_number): variable_name = parse_results.variable_name path = 'expects' if getattr(parse_results, 'not', None): path = 'not_expects' path_put( action_case, '{}.{}'.format(path, variable_name), get_parsed_data_type_value(parse_results, parse_results.value), )
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 ingest_from_parsed_test_fixture(self, action_case, test_case, parse_results, file_name, line_number): parsed_data_type_value = get_parsed_data_type_value(parse_results, parse_results.value) path = 'inputs' if parse_results.job_slot == 'control': path = 'job_control_' + path if parse_results.job_slot == 'context': path = 'job_context_' + path path_put( action_case, '{}.{}'.format(path, parse_results.variable_name), parsed_data_type_value, )
def _ingest_action_body(test_target, parse_results, file_name, line_number): key = (parse_results.stub_service, parse_results.stub_action) stub_config = test_target.setdefault('stubbed_actions', {}).setdefault(key, {}) if 'errors' in stub_config: raise FixtureSyntaxError( 'Cannot combine stub action body and errors (must choose one) for {}.{}'.format(*key), file_name, line_number, ) path_put( stub_config.setdefault('body', {}), parse_results.variable_name, get_parsed_data_type_value(parse_results, parse_results.value), )
def test_018_get_all_paths(self): path_list = [ 'foo.bar', 'foo.{bar.baz}', 'foo.{yea_bar.baz}.gar', 'foo.aba_bar.0', 'foo.sba_bar.0.baz', 'foo.nu_bar.0.0.baz', 'foo.ba_bar.2.baz', 'foo.re_bar.{2}.baz', '{record_transaction.0}.inputs.foo', 'transaction.metadata.references.0.reference_type', 'transaction.metadata.references.0.reference_ids.0', ] out = {} # type: Dict[six.text_type, Any] for path in path_list: path_put(out, path, 'blah_blah') self.assertEqual(sorted(path_list), sorted(get_all_paths(out)))
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_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_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 ingest_from_parsed_test_fixture(self, action_case, test_case, parse_results, file_name, line_number): path_put(test_case, 'skip', parse_results.reason.strip(' \t'))
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 ingest_from_parsed_test_fixture(self, action_case, test_case, parse_results, file_name, line_number): if 'name' in test_case: raise FixtureSyntaxError('Duplicate test name directive for test case', file_name, line_number) path_put(test_case, 'name', parse_results.name)
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')
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_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_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_001_simple_put(self): out = {} path_put(out, 'foo', 'bar') self.assertEqual(out, {'foo': 'bar'}) self.assertEqual(path_get(out, 'foo'), 'bar')
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_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_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_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_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 _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 = []