def test_remove_hidden_elements(self): root = Root() version1 = Version() version2 = Version() category1 = Category("c1") category2 = Category("c2") method1 = Method() method2 = Method() root.versions = {"v1": version1, "v2": version2} root.categories = {"c1": category1, "c2": category2} version1.methods = {"m1": method1, "m2": method2} version2.methods = {"m1": method1, "m2": method2} method1.category = "c1" method2.category = "c2" version1.display = False category2.display = False self.source.remove_hidden_elements(root) self.assertEqual(1, len(root.versions)) self.assertEqual(version2, root.versions["v2"]) self.assertEqual(1, len(root.versions["v2"].methods)) self.assertEqual(method1, root.versions["v2"].methods["m1"])
def test_get_uri_with_missing_parameters(self): root = Root() version = Version() version.name = "v" method = Method() method.name = "m" method.category = "a" method.full_uri = "/{foo}/{p}" parameter = Parameter() parameter.name = "p" parameter.type = "string" method.request_parameters = {"p": parameter} root.versions = {"v": version} version.methods = {"m": method} version.types = {"m": type} root_dto = RootDto() hydrator = Hydrator(version, {"v": version}, []) hydrator.hydrate_method(root_dto, root, method) parameters = self.source.validate(root_dto) parameters = self.source.get_uri_with_missing_parameters(root_dto) self.assertEqual([ {"name": "foo", "uri": "/{foo}/{p}", "method": "m", "version": "v"}, ], parameters)
def test_hydrate_method(self): root = Root() version = Version() version.name = "v" method = Method() method.category = "a" method.full_uri = "/{p}/" parameter = Parameter() parameter.name = "p" parameter.type = "string" method.request_parameters = {"p": parameter} root.versions = {"v": version} version.methods = {"m": method} version.types = {"m": type} root_dto = RootDto() hydrator = Hydrator(version, {"v": version}, []) hydrator.hydrate_method(root_dto, root, method) self.assertEqual(1, len(root_dto.method_categories)) self.assertEqual(0, len(root_dto.type_categories)) self.assertEqual(1, len(root_dto.method_categories[0].methods)) self.assertEqual(1, len(root_dto.method_categories[0].methods[0].request_parameters)) self.assertIsInstance(root_dto.method_categories[0].methods[0].request_parameters[0].value, RequestParameter) self.assertEqual(1, root_dto.method_categories[0].methods[0].request_parameters[0].value.position)
def test_create_from_root(self): root = Root() version = Version() version.name = "v" method = Method() method.category = "a" type = Type() type.category = "b" type.item = ObjectString() root.versions = {"v": version} version.methods = {"m": method} version.types = {"m": type} response = self.factory.create_from_root(root) self.assertIsInstance(response, RootDto) self.assertEqual(1, len(response.method_categories)) self.assertEqual("a", response.method_categories[0].name) self.assertEqual(1, len(response.method_categories[0].methods)) self.assertEqual(1, len(response.type_categories)) self.assertEqual("b", response.type_categories[0].name) self.assertEqual(1, len(response.type_categories[0].types))
def test_create_from_root(self): root = Root() version = Version() version.name = "v" method = Method() method.category = "a" type = Type() type.category = "b" type.item = ObjectString() root.versions = {"v": version} version.methods = {"m": method} version.types = {"m": type} response = self.factory.create_from_root(root) self.assertIsInstance(response, RootDto) self.assertEqual(1, len(response.method_categories)) self.assertEqual("a", response.method_categories[0].name) self.assertEqual(1, len(response.method_categories[0].methods)) self.assertEqual(1, len(response.type_categories)) self.assertEqual("b", response.type_categories[0].name) self.assertEqual(1, len(response.type_categories[0].types))
def test_get_uri_with_missing_parameters(self): root = Root() version = Version() version.name = "v" method = Method() method.name = "m" method.category = "a" method.full_uri = "/{foo}/{p}" parameter = Parameter() parameter.name = "p" parameter.type = "string" method.request_parameters = {"p": parameter} root.versions = {"v": version} version.methods = {"m": method} version.types = {"m": type} root_dto = RootDto() hydrator = Hydrator(version, {"v": version}, []) hydrator.hydrate_method(root_dto, root, method) parameters = self.source.validate(root_dto) parameters = self.source.get_uri_with_missing_parameters(root_dto) self.assertEqual([ { "name": "foo", "uri": "/{foo}/{p}", "method": "m", "version": "v" }, ], parameters)
def create_from_name_and_dictionary(self, name, datas): """Return a populated object Method from dictionary datas """ method = ObjectMethod() self.set_common_datas(method, name, datas) if "category" in datas: method.category = str(datas["category"]) if "code" in datas: method.code = int(datas["code"]) if "uri" in datas: method.uri = str(datas["uri"]) if "method" in datas: method.method = self.get_enum("method", ObjectMethod.Methods, datas) method.request_headers = self.parameter_factory.create_dictionary_of_element_from_dictionary( "request_headers", datas) method.request_parameters = self.parameter_factory.create_dictionary_of_element_from_dictionary( "request_parameters", datas) method.response_codes = self.response_code_factory.create_list_of_element_from_dictionary( "response_codes", datas) if "request_body" in datas and datas["request_body"]: method.request_body = self.object_factory.create_from_name_and_dictionary( "request", datas["request_body"]) if "response_body" in datas and datas["response_body"]: method.response_body = self.object_factory.create_from_name_and_dictionary( "response", datas["response_body"]) return method
def test_add_missing_categories(self): root = Root() version1 = Version() category1 = Category("c1") method1 = Method() method2 = Method() root.versions = {"v1": version1} root.categories = {"c1": category1} version1.methods = {"m1": method1, "m2": method2} method1.category = "c1" method2.category = "c2" self.source.add_missing_categories(root) self.assertEqual(2, len(root.categories)) self.assertIsInstance(root.categories["c2"], Category)
def test_hydrate_method__with_multiple_methds(self): root = Root() version = Version() version.name = "v" method1 = Method() method1.category = "c" method2 = Method() method2.category = "c" root.versions = {"v": version} version.methods = {"m1": method1, "m2": method2} version.types = {"m": type} root_dto = RootDto() hydrator = Hydrator(version, {"v": version}, []) hydrator.hydrate_method(root_dto, root, method1) hydrator.hydrate_method(root_dto, root, method2) self.assertEqual(1, len(root_dto.method_categories))
def test_hydrate_method__with_multiple_methds(self): root = Root() version = Version() version.name = "v" method1 = Method() method1.category = "c" method2 = Method() method2.category = "c" root.versions = {"v": version} version.methods = {"m1": method1, "m2": method2} version.types = {"m": type} root_dto = RootDto() hydrator = Hydrator(version, {"v": version}, []) hydrator.hydrate_method(root_dto, root, method1) hydrator.hydrate_method(root_dto, root, method2) self.assertEqual(1, len(root_dto.method_categories))
def create_from_name_and_dictionary(self, name, datas): """Return a populated object Method from dictionary datas """ method = ObjectMethod() self.set_common_datas(method, name, datas) if "category" in datas: method.category = str(datas["category"]) if "code" in datas: method.code = int(datas["code"]) if "uri" in datas: method.uri = str(datas["uri"]) if "method" in datas: method.method = self.get_enum("method", ObjectMethod.Methods, datas) method.request_headers = self.parameter_factory.create_dictionary_of_element_from_dictionary("request_headers", datas) method.request_parameters = self.parameter_factory.create_dictionary_of_element_from_dictionary("request_parameters", datas) method.response_codes = self.response_code_factory.create_list_of_element_from_dictionary("response_codes", datas) if "request_body" in datas and datas["request_body"]: method.request_body = self.object_factory.create_from_name_and_dictionary("request", datas["request_body"]) if "response_body" in datas and datas["response_body"]: method.response_body = self.object_factory.create_from_name_and_dictionary("response", datas["response_body"]) return method
def test_hydrate_method__with_known_category(self): root = Root() version = Version() version.name = "v" method = Method() method.category = "c" root.versions = {"v": version} version.methods = {"m": method} version.types = {"m": type} category = Category("c") category.description = "d" root.categories = {"c": category} root_dto = RootDto() hydrator = Hydrator(version, {"v": version}, []) hydrator.hydrate_method(root_dto, root, method) self.assertEqual("d", root_dto.method_categories[0].description)
def test_hydrate_method__with_known_category(self): root = Root() version = Version() version.name = "v" method = Method() method.category = "c" root.versions = {"v": version} version.methods = {"m": method} version.types = {"m": type} category = Category("c") category.description = "d" root.categories = {"c": category} root_dto = RootDto() hydrator = Hydrator(version, {"v": version}, []) hydrator.hydrate_method(root_dto, root, method) self.assertEqual("d", root_dto.method_categories[0].description)
def test_hydrate_method(self): root = Root() version = Version() version.name = "v" method = Method() method.category = "a" method.full_uri = "/{p}/" parameter = Parameter() parameter.name = "p" parameter.type = "string" method.request_parameters = {"p": parameter} root.versions = {"v": version} version.methods = {"m": method} version.types = {"m": type} root_dto = RootDto() hydrator = Hydrator(version, {"v": version}, []) hydrator.hydrate_method(root_dto, root, method) self.assertEqual(1, len(root_dto.method_categories)) self.assertEqual(0, len(root_dto.type_categories)) self.assertEqual(1, len(root_dto.method_categories[0].methods)) self.assertEqual( 1, len(root_dto.method_categories[0].methods[0].request_parameters)) self.assertIsInstance( root_dto.method_categories[0].methods[0].request_parameters[0]. value, RequestParameter) self.assertEqual( 1, root_dto.method_categories[0].methods[0].request_parameters[0]. value.position)