def test_read_line_state_read_param_tag(self): v = ApiaryValidator() v._parameters = ['test'] state = _state_read_param_tag self._test_read_line(line=' ', state=state, validator=v, expected_state=state) self._test_read_line(line=_TEST_PARAMETER_TAG, state=state, validator=v, expected_error=ApiarySyntaxError()) self._test_read_line(line=_TEST_PARAMETER_STRING, state=state, validator=v, expected_state=state) self._test_read_line(line=_TEST_REQUEST_TAG, state=state, validator=v, expected_state=_state_read_request_tag) self._test_read_line(line=_TEST_RESPONSE_TAG, state=state, validator=v, expected_state=_state_read_response_tag)
def test_read_line_state_read_response_tag_with_consecutive_responses(self): v = ApiaryValidator() response_file = path.join(_current_file_path, 'response', 'response_good_example001.json') # setup the validator v.state = _state_read_response_tag v._read_newline = True self._load_content_to_validator(v, content_file=response_file) # test with the 2nd response tag valid, error = v._read_line(_TEST_RESPONSE_TAG) if error and DEBUG: print('\nUnexpected Error: %s' % error) self.assertTrue(valid) self.assertFalse(v._read_newline) self.assertIsNone(error)
def test_get_parameters(self): titles = ['## TEST API [/test/api/pattern/{param_1}/with/{param_2}/postfix{?p1,p2,p3}]', '## TEST API [/test/api/pattern/{param_1}/with/{param_2}{?p1,p2,p3}]'] expected = ['param_1', 'param_2', 'p1', 'p2', 'p3'] for title in titles: results = ApiaryValidator._get_parameters_from_api_title(title) self.assertEqual(results, expected)
def test_read_line_state_read_api_title(self): v = ApiaryValidator() state = _state_read_api_title self._test_read_line(line=' ', validator=v, state=state, expected_state=_state_read_api_title) for method in ['GET', 'POST', 'DELETE', 'PUT']: self._test_read_line(line=_TEST_API_METHOD_TEMPLATE % method, state=state, validator=v, expected_state=_state_read_api_method) self._test_read_line(line=_TEST_PARAMETER_TAG, state=state, validator=v, expected_error=ApiarySyntaxError()) self._test_read_line(line=_TEST_PARAMETER_STRING, state=state, validator=v, expected_error=ApiarySyntaxError()) self._test_read_line(line=_TEST_REQUEST_TAG, state=state, validator=v, expected_error=ApiarySyntaxError()) self._test_read_line(line=_TEST_RESPONSE_TAG, state=state, validator=v, expected_error=ApiarySyntaxError())
def test_read_line_state_read_response_tag_with_consecutive_responses( self): v = ApiaryValidator() response_file = path.join(_current_file_path, 'response', 'response_good_example001.json') # setup the validator v.state = _state_read_response_tag v._read_newline = True self._load_content_to_validator(v, content_file=response_file) # test with the 2nd response tag valid, error = v._read_line(_TEST_RESPONSE_TAG) if error and DEBUG: print('\nUnexpected Error: %s' % error) self.assertTrue(valid) self.assertFalse(v._read_newline) self.assertIsNone(error)
def test_read_line_state_read_request_with_invalid_request_content(self): v = ApiaryValidator() request_path = '%s/request/' % _current_file_path self._test_invalid_template(validator=v, template=path.join( request_path, 'request_template.json'), test_type=_TEST_TYPE_REQUEST_CONTENT)
def test_read_line_state_read_request_tag(self): v = ApiaryValidator() state = _state_read_request_tag self._test_read_line(line=' ', state=state, validator=v, expected_state=_state_read_request_tag)
def test_read_api_title_with_parameters(self): v = ApiaryValidator() test_title = '## TEST API [/test/api/pattern/{param_1}/with/{param_2}/{?p1,p2,p3}]' expected = ['param_1', 'param_2', 'p1', 'p2', 'p3'] for state in [_state_read_group_title, _state_read_response_tag]: v._parameters.clear() v.state = state # load the response content if necessary: if state == _state_read_response_tag: content_file = path.join(path.dirname(path.abspath(__file__)), 'response', 'response_good_example001.json') ApiaryTest._load_content_to_validator(v, content_file) v._read_line(test_title) results = v._parameters self.assertEqual(results, expected)
def test_get_parameters(self): titles = [ '## TEST API [/test/api/pattern/{param_1}/with/{param_2}/postfix{?p1,p2,p3}]', '## TEST API [/test/api/pattern/{param_1}/with/{param_2}{?p1,p2,p3}]' ] expected = ['param_1', 'param_2', 'p1', 'p2', 'p3'] for title in titles: results = ApiaryValidator._get_parameters_from_api_title(title) self.assertEqual(results, expected)
def test_indent_validation(self): self.assertTrue(ApiaryValidator._indent_validation('\r')) self.assertTrue(ApiaryValidator._indent_validation(' ')) self.assertTrue(ApiaryValidator._indent_validation(' test example 001')) self.assertTrue(ApiaryValidator._indent_validation('\t\ttest example 002')) self.assertFalse(ApiaryValidator._indent_validation('test example 003')) self.assertFalse(ApiaryValidator._indent_validation(' test example 004')) self.assertFalse(ApiaryValidator._indent_validation('\ttest example 005'))
def test_read_line_state_read_request_with_valid_request_content(self): v = ApiaryValidator() request_path = '%s/request/' % _current_file_path files = [ f for f in listdir(request_path) if path.isfile(path.join(request_path, f)) ] for f in files: if re.match(r'request_good_(.+).json', f): content_file = path.join(request_path, f) self._test_with_request_content(validator=v, content_file=content_file)
def test_read_line_state_init(self): v = ApiaryValidator() state = _state_init self._test_read_line(line=' ', validator=v, state=state, expected_state=_state_init) self._test_read_line(line=_TEST_GROUP_TITLE, validator=v, state=state, expected_state=_state_read_group_title)
def test_read_line_with_indent_validation(self): v = ApiaryValidator() valid_examples = [' ', ' {', '\t\t{'] invalid_examples = ['"test":123, ', ' "test": false, '] for state in [_state_read_response_tag, _state_read_request_tag]: for line in valid_examples: v.state = state v.decoder.clear() valid, error = v._read_line(line) if error and DEBUG: print('\nUnexpected Error: %s' % error) self.assertTrue(valid) self.assertEqual(error, None) for line in invalid_examples: v.state = state v.decoder.clear() valid, error = v._read_line(line) self.assertFalse(valid) self.assertEqual(error.type, ApiarySyntaxError().type)
def test_indent_validation(self): self.assertTrue(ApiaryValidator._indent_validation('\r')) self.assertTrue(ApiaryValidator._indent_validation(' ')) self.assertTrue( ApiaryValidator._indent_validation(' test example 001')) self.assertTrue( ApiaryValidator._indent_validation('\t\ttest example 002')) self.assertFalse( ApiaryValidator._indent_validation('test example 003')) self.assertFalse( ApiaryValidator._indent_validation(' test example 004')) self.assertFalse( ApiaryValidator._indent_validation('\ttest example 005'))
def test_for_check_code_block_newline(self): v = ApiaryValidator() # valid cases: newline_examples = [' ', '\n', '\r', ' \n', ' \r'] for newline in newline_examples: for state in [_state_read_response_tag, _state_read_request_tag]: v.decoder.clear() v.state = state v._read_newline = False v._read_line(newline) valid, error = v._read_line(' {') if error and DEBUG: print('\nUnexpected Error: %s' % error) self.assertTrue(valid) self.assertIsNone(error) # invalid cases: for state in [_state_read_request_tag, _state_read_request_tag]: v.decoder.clear() v.state = state v._read_newline = False valid, error = v._read_line(' {') self.assertFalse(valid) self.assertEqual(error.type, ApiarySyntaxError().type)
def test_check_if_parameter_is_defined(self): v = ApiaryValidator() v.state = _state_read_param_tag v._parameters = ['p1', 'p2', 'p3'] valid, error = v._read_line('+ p1 (string) ... test parameter 1') self.assertTrue(valid) valid, error = v._read_line( ' + p2 (string) ... test parameter 2') self.assertTrue(valid) valid, error = v._read_line('+ p4 (string) ... test parameter 3') self.assertFalse(valid) self.assertEqual(error.type, ApiaryParameterNotDefinedError(parameter='p4').type)
def test_check_if_parameter_is_defined(self): v = ApiaryValidator() v.state = _state_read_param_tag v._parameters = ['p1', 'p2', 'p3'] valid, error = v._read_line('+ p1 (string) ... test parameter 1') self.assertTrue(valid) valid, error = v._read_line(' + p2 (string) ... test parameter 2') self.assertTrue(valid) valid, error = v._read_line('+ p4 (string) ... test parameter 3') self.assertFalse(valid) self.assertEqual(error.type, ApiaryParameterNotDefinedError(parameter='p4').type)
def test_read_line_state_read_response_tag(self): v = ApiaryValidator() state = _state_read_response_tag self._test_read_line(line=' ', state=state, validator=v, expected_state=_state_read_response_tag) self._test_read_line(line=_TEST_API_TITLE, state=state, validator=v, expected_state=_state_read_api_title, load_content=True) for method in ['GET', 'POST', 'PUT', 'DELETE']: self._test_read_line(line=_TEST_API_METHOD_TEMPLATE % method, state=state, validator=v, expected_state=_state_read_api_method, load_content=True)
def test_get_parameter_from_parameter_string(self): expected = 'parameter_Name-000123' test_string = ' + %s (number) ... Some descriptions' % expected result = ApiaryValidator._get_parameter_from_parameter_string( test_string) self.assertEqual(result, expected)
def test_get_parameter_from_parameter_string(self): expected = 'parameter_Name-000123' test_string = ' + %s (number) ... Some descriptions' % expected result = ApiaryValidator._get_parameter_from_parameter_string(test_string) self.assertEqual(result, expected)