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_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_method(self): method = Method() method.name = "a" method.method = Method.Methods.post method.code = 200 method.full_uri = "c" method.absolute_uri = "d" parameter = Parameter() parameter.type = "string" method.request_headers = {"e": parameter} method.request_parameters = {"f": parameter} method.request_body = ObjectObject() method.response_body = ObjectArray() method_sample = MethodSample(method) self.assertEqual("a", method_sample.name) self.assertEqual(Method.Methods.post, method_sample.method) self.assertEqual(200, method_sample.code) self.assertEqual("OK", method_sample.message) self.assertEqual("c", method_sample.full_uri) self.assertEqual("d", method_sample.absolute_uri) self.assertIsInstance(method_sample.request_headers, list) self.assertEqual(1, len(method_sample.request_headers)) self.assertIsInstance(method_sample.request_headers[0], ParameterSample) self.assertIsInstance(method_sample.request_parameters, dict) self.assertEqual(1, len(method_sample.request_parameters.values())) self.assertIsInstance(method_sample.request_parameters["f"], ParameterSample) self.assertIsInstance(method_sample.request_body, ObjectObjectSample) self.assertIsInstance(method_sample.response_body, ObjectArraySample)
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_define_changes_status__updated(self): root_dto = RootDto() version1 = Version() version1.name = "v1" version1.major = 1 version1 = VersionDto(version1) version2 = Version() version2.name = "v2" version2.major = 2 version2 = VersionDto(version2) category1 = MethodCategory(Category("c1")) method1 = Method() method1.changes_status = {"v1": MultiVersion.Changes.new, "v2": MultiVersion.Changes.updated} category1.methods = [method1] root_dto.versions = [version1, version2] root_dto.method_categories = [category1] self.factory.define_changes_status(root_dto) self.assertEqual(MultiVersion.Changes.new, method1.changes_status["v1"]) self.assertEqual(MultiVersion.Changes.updated, method1.changes_status["v2"])
def test_method(self): method = Method() method.name = "a" method.description = "b" method.method = Method.Methods.put parameter1 = RequestParameterDto(Parameter()) parameter1.position = 2 parameter2 = RequestParameterDto(Parameter()) parameter2.position = -1 method.parameter = {"p1": parameter1, "p2": parameter2} method_dto = MethodDto(method) self.assertEqual("a", method_dto.name) self.assertEqual("put", str(method_dto.method)) self.assertEqual([], method_dto.description) self.assertEqual([], method_dto.code) self.assertEqual([], method_dto.full_uri) self.assertEqual([], method_dto.absolute_uri) self.assertEqual([], method_dto.request_headers) self.assertEqual([], method_dto.request_parameters) self.assertEqual([], method_dto.request_body) self.assertEqual([], method_dto.response_codes) self.assertEqual([], method_dto.response_body) self.assertEqual([], method_dto.versions) self.assertEqual({}, method_dto.changes_status) self.assertEqual({}, method_dto.samples) m1 = MultiVersionDto(parameter1, "v1") m2 = MultiVersionDto(parameter2, "v1") method_dto.request_parameters = [m1, m2] self.assertEqual([m1], method_dto.request_uri_parameters) self.assertEqual([m2], method_dto.request_query_string_parameters)
def test_replace_references(self): root = Root() version1 = Version() method1 = Method() type1 = Type() object_reference1 = ObjectReference() object_reference1.reference_name = "r1" object_reference1.name = "a" object_reference2 = ObjectReference() object_reference2.reference_name = "r1" object_reference2.name = "a" object1 = ObjectObject() object1.description = "b" method1.request_body = object_reference1 type1.item = object_reference2 version1.references = {"r1": object1} version1.methods = {"m1": method1} version1.types = {"t1": type1} root.versions = {"v1": version1} self.source.replace_references(root) self.assertEqual("a", method1.request_body.name) self.assertEqual("b", method1.request_body.description) self.assertEqual("a", type1.item.name) self.assertEqual("b", type1.item.description)
def test_replace_type(self): root = Root() version1 = Version() method1 = Method() object_type1 = ObjectType() object_type1.type_name = "t1" type1 = Type() parameter1 = Parameter() parameter1.type = "t1" parameter2 = Parameter() parameter2.type = "t1" method1.request_body = object_type1 method1.request_parameters = {"p1": parameter1} method1.request_headers = {"p1": parameter2} version1.types = {"t1": type1} version1.methods = {"m1": method1} root.versions = {"v1": version1} self.source.replace_types(root) self.assertEqual(type1, method1.request_body.type_object) self.assertEqual(type1, method1.request_parameters["p1"].type_object) self.assertEqual(type1, method1.request_headers["p1"].type_object)
def test_define_changes_status__updated(self): root_dto = RootDto() version1 = Version() version1.name = "v1" version1.major = 1 version1 = VersionDto(version1) version2 = Version() version2.name = "v2" version2.major = 2 version2 = VersionDto(version2) category1 = MethodCategory(Category("c1")) method1 = Method() method1.changes_status = { "v1": MultiVersion.Changes.new, "v2": MultiVersion.Changes.updated } category1.methods = [method1] root_dto.versions = [version1, version2] root_dto.method_categories = [category1] self.factory.define_changes_status(root_dto) self.assertEqual(MultiVersion.Changes.new, method1.changes_status["v1"]) self.assertEqual(MultiVersion.Changes.updated, method1.changes_status["v2"])
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_method_compare__with_label(self): method1 = MethodDto(Method()) method2 = MethodDto(Method()) method1.label = "a" method2.label = "b" self.assertEqual(method1, sorted([method2, method1])[0])
def test_method_compare__with_name(self): method_sample1 = MethodSample(Method()) method_sample2 = MethodSample(Method()) method_sample1.name = "a" method_sample2.name = "b" self.assertTrue(method_sample1 < method_sample2)
def test_method_message(self): method = Method() code = ResponseCode() code.code = 100 code.message = "foo" method.code = 100 method.response_codes = [code] self.assertEqual("foo", method.message)
def test_method_message__failled_when_no_code_found(self): method = Method() code = ResponseCode() code.code = 100 code.message = "foo" method.code = 300 method.response_codes = [code] with self.assertRaises(ValueError): method.message
def test_hydrate_method_changed(self): root = Root() version1 = Version() version1.name = "v1" version2 = Version() version2.name = "v2" method1 = Method() method1.name = "m1" method1.description = "b" method2 = Method() method2.name = "m1" method2.description = "c" root.versions = {"v1": version1, "v2": version2} version1.methods = {"m1": method1} version2.methods = {"m1": method2} root_dto = RootDto() Hydrator(version1, root.versions, []).hydrate_method(root_dto, root, method1) Hydrator(version2, root.versions, []).hydrate_method(root_dto, root, method2) self.assertEqual(1, len(root_dto.method_categories)) self.assertEqual(1, len(root_dto.method_categories[0].methods)) self.assertEqual( 2, len(root_dto.method_categories[0].methods[0].description)) self.assertEqual( MultiVersion.Changes.updated, root_dto.method_categories[0].methods[0].changes_status["v2"])
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_hydrate_method_changed(self): root = Root() version1 = Version() version1.name = "v1" version2 = Version() version2.name = "v2" method1 = Method() method1.name = "m1" method1.description = "b" method2 = Method() method2.name = "m1" method2.description = "c" root.versions = {"v1": version1, "v2": version2} version1.methods = {"m1": method1} version2.methods = {"m1": method2} root_dto = RootDto() Hydrator(version1, root.versions, []).hydrate_method(root_dto, root, method1) Hydrator(version2, root.versions, []).hydrate_method(root_dto, root, method2) self.assertEqual(1, len(root_dto.method_categories)) self.assertEqual(1, len(root_dto.method_categories[0].methods)) self.assertEqual(2, len(root_dto.method_categories[0].methods[0].description)) self.assertEqual(MultiVersion.Changes.updated, root_dto.method_categories[0].methods[0].changes_status["v2"])
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_define_changes_status(self): root_dto = RootDto() version1 = Version() version1.name = "v1" version1.major = 1 version1 = VersionDto(version1) version2 = Version() version2.name = "v2" version2.major = 2 version2 = VersionDto(version2) category1 = MethodCategory(Category("c1")) category2 = TypeCategory(Category("c2")) method1 = Method() method1.changes_status = {"v1": MultiVersion.Changes.none} method2 = Method() method2.changes_status = {"v1": MultiVersion.Changes.new} method3 = Method() method3.changes_status = {"v2": MultiVersion.Changes.new} category1.methods = [method1, method2, method3] root_dto.versions = [version1, version2] root_dto.method_categories = [category1] root_dto.type_categories = [category2] self.factory.define_changes_status(root_dto) self.assertEqual(MultiVersion.Changes.new, method1.changes_status["v1"]) self.assertEqual(MultiVersion.Changes.deleted, method2.changes_status["v2"]) self.assertEqual(MultiVersion.Changes.none, method3.changes_status["v1"])
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_remove_unused_types(self): root = Root() version1 = Version() type1 = Type() type2 = Type() method1 = Method() parameter1 = Parameter() parameter1.type = "t1" method1.request_parameters = {"p1": parameter1} version1.types = {"t1": type1, "t2": type2} version1.methods = {"m1": method1} root.versions = {"v1": version1} self.source.remove_unused_types(root) self.assertEqual({"t1": type1}, version1.types)
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)
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_method_message__ok(self): method = Method() method.code = 200 self.assertEqual("OK", method.message)
def test_sort_method_lt(self): v1 = Method() v1.name = "a" v2 = Method() v2.name = "b" self.assertLess(v1, v2)
def test_sort_method_equals(self): v1 = Method() v1.name = "a" v2 = Method() v2.name = "a" self.assertEqual(v1, v2)