|
| 1 | +import unittest |
| 2 | +import os |
| 3 | +import hydrus |
| 4 | + |
| 5 | +from hydrus.hydraspec.doc_writer import HydraClass |
| 6 | +from hydrus.parser import openapi_parser |
| 7 | +import yaml |
| 8 | + |
| 9 | + |
| 10 | +def import_doc(): |
| 11 | + print("Importing Open Api Documentation ..") |
| 12 | + abs_path = os.path.abspath("{}/samples/petstore_openapi.yaml".format(os.path.dirname( |
| 13 | + hydrus.__file__))) |
| 14 | + with open(abs_path, 'r') as stream: |
| 15 | + try: |
| 16 | + return yaml.load(stream) |
| 17 | + except yaml.YAMLError as exc: |
| 18 | + print(exc) |
| 19 | + |
| 20 | + |
| 21 | +class TestParser(unittest.TestCase): |
| 22 | + @classmethod |
| 23 | + def setUpClass(self): |
| 24 | + doc = import_doc() |
| 25 | + self.doc = doc |
| 26 | + |
| 27 | + @classmethod |
| 28 | + def tearDownClass(cls): |
| 29 | + pass |
| 30 | + |
| 31 | + def test_generate_empty_object(self): |
| 32 | + """Test if the empty object is being generated correctly """ |
| 33 | + object_ = openapi_parser.generate_empty_object() |
| 34 | + assert isinstance(object_["prop_definition"], list) |
| 35 | + assert isinstance(object_["op_definition"], list) |
| 36 | + assert isinstance(object_["class_definition"], type) |
| 37 | + assert isinstance(object_["collection"], bool) |
| 38 | + |
| 39 | + def test_valid_endpoint(self): |
| 40 | + """Test if the endpoint is valid and can be parsed """ |
| 41 | + path = 'A/B/{id}/C/D' |
| 42 | + result = openapi_parser.valid_endpoint(path) |
| 43 | + assert result is "False" |
| 44 | + assert isinstance(result, str) |
| 45 | + path = 'A/B/{id}' |
| 46 | + result = openapi_parser.valid_endpoint(path) |
| 47 | + assert result is "Collection" |
| 48 | + assert isinstance(result, str) |
| 49 | + path = 'A/B/id' |
| 50 | + result = openapi_parser.valid_endpoint(path) |
| 51 | + assert result is "True" |
| 52 | + assert isinstance(result, str) |
| 53 | + |
| 54 | + def test_get_class_name(self): |
| 55 | + """Test if the class name is being extracted properly from the path """ |
| 56 | + path = "A/B/C/Pet" |
| 57 | + path_list = path.split('/') |
| 58 | + result = openapi_parser.get_class_name(path_list) |
| 59 | + assert result is path_list[3] |
| 60 | + assert isinstance(result, str) |
| 61 | + |
| 62 | + def test_get_data_from_location(self): |
| 63 | + """Test if the data from the location given is being fetched correctly""" |
| 64 | + path = '#/definitions/Order' |
| 65 | + path_list = path.split('/') |
| 66 | + result = openapi_parser.get_data_at_location(path_list, self.doc) |
| 67 | + response = self.doc["definitions"]["Order"] |
| 68 | + assert response is result |
| 69 | + |
| 70 | + def test_sanitise_path(self): |
| 71 | + """Test if the variables can be removed from the path""" |
| 72 | + path = "A/B/C/{id}" |
| 73 | + result = openapi_parser.sanitise_path(path) |
| 74 | + assert isinstance(result, str) |
| 75 | + |
| 76 | + def test_allow_parameter(self): |
| 77 | + """Test if the rules are being followed """ |
| 78 | + parameter_block = self.doc["paths"]["/pet"]["post"]["parameters"][0] |
| 79 | + result = openapi_parser.allow_parameter(parameter_block) |
| 80 | + assert result is True |
| 81 | + assert isinstance(result, bool) |
| 82 | + parameter_block = self.doc["paths"]["/pet"]["get"]["parameters"][0] |
| 83 | + result = openapi_parser.allow_parameter(parameter_block) |
| 84 | + assert result is False |
| 85 | + assert isinstance(result, bool) |
| 86 | + |
| 87 | + def test_parse(self): |
| 88 | + """Test the hydra documentation """ |
| 89 | + result = openapi_parser.parse(self.doc) |
| 90 | + assert isinstance(result, dict) |
| 91 | + |
| 92 | + def test_check_collection(self): |
| 93 | + """Test if collections are being identified properly""" |
| 94 | + schema_block = { |
| 95 | + 'type': 'array', 'items': { |
| 96 | + '$ref': '#/definitions/Pet'}} |
| 97 | + method = "/Pet" |
| 98 | + result = openapi_parser.check_collection(schema_block, method) |
| 99 | + assert isinstance(result, bool) |
| 100 | + assert result |
| 101 | + |
| 102 | + def test_check_collection_false(self): |
| 103 | + "Test if non collections are identified" |
| 104 | + schema = {'$ref': '#/definitions/User'} |
| 105 | + method = "/Pet" |
| 106 | + result = openapi_parser.check_collection(schema, method) |
| 107 | + assert isinstance(result, bool) |
| 108 | + assert not result |
| 109 | + |
| 110 | + |
| 111 | +if __name__ == '__main__': |
| 112 | + print("Starting tests ..") |
| 113 | + unittest.main() |
0 commit comments