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_get_used_types_in_object__additional_properties(self): object = ObjectObject() array = ObjectArray() dynamic = ObjectDynamic() reference = ObjectType() reference.type_name = "t1" object.additional_properties = array array.items = dynamic dynamic.items = reference response = self.source.get_used_types_in_object(object) self.assertEqual(["t1"], response)
def test_replace_types_in_object__unknwon(self): object = ObjectObject() array = ObjectArray() dynamic = ObjectDynamic() reference = ObjectType() reference.type_name = "t2" type1 = Type() object.properties = {"p1": array} array.items = dynamic dynamic.items = reference with self.assertRaises(ValueError): self.source.replace_types_in_object(object, {"t1": type1})
def test_replace_types_in_object__additional_properties(self): object = ObjectObject() array = ObjectArray() dynamic = ObjectDynamic() reference = ObjectType() reference.type_name = "t1" type1 = Type() object.additional_properties = array array.items = dynamic dynamic.items = reference self.source.replace_types_in_object(object, {"t1": type1}) self.assertEqual(type1, reference.type_object)
def test_replace_references_in_object__additional_properties(self): object = ObjectObject() array = ObjectArray() dynamic = ObjectDynamic() reference = ObjectReference() reference.reference_name = "r1" string1 = ObjectString() object.additional_properties = array array.items = dynamic dynamic.items = reference self.source.replace_references_in_object(object, {"r1": string1}) self.assertEqual(Object.Types.string, dynamic.items.type)
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_objectObject(self): object = ObjectObject() object.name = "a" object.type = Object.Types.object object2 = ObjectArray() object2.name = "b" object2.type = Object.Types.array object.properties = {"b": object2} object_sample = ObjectObjectSample(object) self.assertEqual("a", object_sample.name) self.assertIsInstance(object_sample.properties, dict) self.assertEqual(1, len(object_sample.properties)) self.assertIsInstance(object_sample.properties["b"], ObjectArraySample)
def test_objectObject(self): object = ObjectObject() object.name = "a" object.type = Object.Types.object object2 = ObjectArray() object2.name = "b" object2.type = Object.Types.array object.properties = {"b": object2} object_sample = ObjectObjectSample(object) self.assertEqual("a", object_sample.name) self.assertIsInstance(object_sample.properties, dict) self.assertEqual(1, len(object_sample.properties)) self.assertIsInstance(object_sample.properties["b"], ObjectArraySample)
def test_hydrate_object(self): version1 = Version() version1.name = "v1" version2 = Version() version2.name = "v2" versions = {"v1": version1, "v2": version2} object_dto = [] object1 = ObjectObject() object2 = ObjectObject() object3 = ObjectObject() object1.name = "a" object2.name = "a" object3.name = "b" array = ObjectArray() array.name = "b" dynamic = ObjectDynamic() dynamic.name = "c" string = ObjectString() string.name = "d" type = ObjectType() type.name = "e" type.type_object = "f" const = ObjectConst() const.value = "g" enum = ObjectEnum() enum.name = "h" enum.values = ["h"] object1.properties = {"p1": array, "p3": const, "p4": enum} object2.properties = {"p1": array, "p3": const, "p4": enum, "p2": type} array.items = dynamic dynamic.items = string response = Hydrator(version1, versions, []).hydrate_object(object_dto, object1) response = Hydrator(version2, versions, []).hydrate_object(object_dto, object2) response = Hydrator(version1, versions, []).hydrate_object(object_dto, object3) self.assertEqual(1, response) self.assertEqual(2, len(object_dto)) self.assertIn(version1.name, object_dto[0].versions) self.assertIn(version1.name, object_dto[0].versions) self.assertNotIn(version2.name, object_dto[1].versions) self.assertEqual("a", object_dto[0].value.name) self.assertEqual("b", object_dto[0].value.properties["p1"][0].value.name) self.assertEqual("c", object_dto[0].value.properties["p1"][0].value.items[0].value.name) self.assertEqual("d", object_dto[0].value.properties["p1"][0].value.items[0].value.items[0].value.name) self.assertEqual("e", object_dto[0].value.properties["p2"][0].value.name) self.assertEqual("f", object_dto[0].value.properties["p2"][0].value.type_object) self.assertEqual("g", object_dto[0].value.properties["p3"][0].value.value) self.assertEqual("h", object_dto[0].value.properties["p4"][0].value.values[0].value)
def create_from_name_and_dictionary(self, name, datas): """Return a populated object Object from dictionary datas """ if "type" not in datas: str_type = "any" else: str_type = str(datas["type"]).lower() if str_type not in ObjectRaw.Types: type = ObjectRaw.Types("type") else: type = ObjectRaw.Types(str_type) if type is ObjectRaw.Types.object: object = ObjectObject() if "properties" in datas: object.properties = self.create_dictionary_of_element_from_dictionary( "properties", datas) if "patternProperties" in datas: object.pattern_properties = self.create_dictionary_of_element_from_dictionary( "patternProperties", datas) if "additionalProperties" in datas: if isinstance(datas["additionalProperties"], dict): object.additional_properties = self.create_from_name_and_dictionary( "additionalProperties", datas["additionalProperties"]) elif not to_boolean(datas["additionalProperties"]): object.additional_properties = None else: raise ValueError( "AdditionalProperties doe not allow empty value (yet)") elif type is ObjectRaw.Types.array: object = ObjectArray() if "items" in datas: object.items = self.create_from_name_and_dictionary( "items", datas["items"]) else: object.items = ObjectObject() if "sample_count" in datas: object.sample_count = int(datas["sample_count"]) elif type is ObjectRaw.Types.number: object = ObjectNumber() elif type is ObjectRaw.Types.integer: object = ObjectInteger() elif type is ObjectRaw.Types.string: object = ObjectString() elif type is ObjectRaw.Types.boolean: object = ObjectBoolean() if "sample" in datas: object.sample = to_boolean(datas["sample"]) elif type is ObjectRaw.Types.reference: object = ObjectReference() if "reference" in datas: object.reference_name = str(datas["reference"]) elif type is ObjectRaw.Types.type: object = ObjectType() object.type_name = str(datas["type"]) elif type is ObjectRaw.Types.none: object = ObjectNone() elif type is ObjectRaw.Types.dynamic: object = ObjectDynamic() if "items" in datas: object.items = self.create_from_name_and_dictionary( "items", datas["items"]) if "sample" in datas: if isinstance(datas["sample"], dict): object.sample = {} for k, v in datas["sample"].items(): object.sample[str(k)] = str(v) else: raise ValueError( "A dictionnary is expected for dynamic\s object in \"%s\"" % name) elif type is ObjectRaw.Types.const: object = ObjectConst() if "const_type" in datas: const_type = str(datas["const_type"]) if const_type not in ObjectConst.Types: raise ValueError("Const type \"%s\" unknwon" % const_type) else: const_type = ObjectConst.Types.string object.const_type = const_type if "value" not in datas: raise ValueError("Missing const value") object.value = datas["value"] elif type is ObjectRaw.Types.enum: object = ObjectEnum() if "values" not in datas or not isinstance(datas['values'], list): raise ValueError("Missing enum values") object.values = [str(value) for value in datas["values"]] if "descriptions" in datas and isinstance(datas['descriptions'], dict): for (value_name, value_description) in datas["descriptions"].items(): value = EnumValue() value.name = value_name value.description = value_description object.descriptions.append(value) descriptions = [ description.name for description in object.descriptions ] for value_name in [ x for x in object.values if x not in descriptions ]: value = EnumValue() value.name = value_name object.descriptions.append(value) else: object = ObjectRaw() self.set_common_datas(object, name, datas) if isinstance(object, Constraintable): self.set_constraints(object, datas) object.type = type if "optional" in datas: object.optional = to_boolean(datas["optional"]) return object
def create_from_name_and_dictionary(self, name, datas): """Return a populated object Object from dictionary datas """ if "type" not in datas: str_type = "any" else: str_type = str(datas["type"]).lower() if not str_type in ObjectRaw.Types: type = ObjectRaw.Types("type") else: type = ObjectRaw.Types(str_type) if type is ObjectRaw.Types.object: object = ObjectObject() if "properties" in datas: object.properties = self.create_dictionary_of_element_from_dictionary("properties", datas) if "patternProperties" in datas: object.pattern_properties = self.create_dictionary_of_element_from_dictionary("patternProperties", datas) if "additionalProperties" in datas: if isinstance(datas["additionalProperties"], dict): object.additional_properties = self.create_from_name_and_dictionary("additionalProperties", datas["additionalProperties"]) elif not to_boolean(datas["additionalProperties"]): object.additional_properties = None else: raise ValueError("AdditionalProperties doe not allow empty value (yet)") elif type is ObjectRaw.Types.array: object = ObjectArray() if "items" in datas: object.items = self.create_from_name_and_dictionary("items", datas["items"]) else: object.items = ObjectObject() if "sample_count" in datas: object.sample_count = int(datas["sample_count"]) elif type is ObjectRaw.Types.number: object = ObjectNumber() elif type is ObjectRaw.Types.integer: object = ObjectInteger() elif type is ObjectRaw.Types.string: object = ObjectString() elif type is ObjectRaw.Types.boolean: object = ObjectBoolean() if "sample" in datas: object.sample = to_boolean(datas["sample"]) elif type is ObjectRaw.Types.reference: object = ObjectReference() if "reference" in datas: object.reference_name = str(datas["reference"]) elif type is ObjectRaw.Types.type: object = ObjectType() object.type_name = str(datas["type"]) elif type is ObjectRaw.Types.none: object = ObjectNone() elif type is ObjectRaw.Types.dynamic: object = ObjectDynamic() if "items" in datas: object.items = self.create_from_name_and_dictionary("items", datas["items"]) if "sample" in datas: if isinstance(datas["sample"], dict): object.sample = {} for k, v in datas["sample"].items(): object.sample[str(k)] = str(v) else: raise ValueError("A dictionnary is expected for dynamic\s object in \"%s\"" % name) elif type is ObjectRaw.Types.const: object = ObjectConst() if "const_type" in datas: const_type = str(datas["const_type"]) if not const_type in ObjectConst.Types: raise ValueError("Const type \"%s\" unknwon" % const_type) else: const_type = ObjectConst.Types.string object.const_type = const_type if not "value" in datas: raise ValueError("Missing const value") object.value = datas["value"] elif type is ObjectRaw.Types.enum: object = ObjectEnum() if not "values" in datas or not isinstance(datas['values'], list): raise ValueError("Missing enum values") object.values = [str(value) for value in datas["values"]] if "descriptions" in datas and isinstance(datas['descriptions'], dict): for (value_name, value_description) in datas["descriptions"].items(): value = EnumValue() value.name = value_name value.description = value_description object.descriptions.append(value) descriptions = [description.name for description in object.descriptions] for value_name in [value for value in object.values if value not in descriptions]: value = EnumValue() value.name = value_name object.descriptions.append(value) else: object = ObjectRaw() self.set_common_datas(object, name, datas) if isinstance(object, Constraintable): self.set_constraints(object, datas) object.type = type if "optional" in datas: object.optional = to_boolean(datas["optional"]) return object
def test_hydrate_object(self): version1 = Version() version1.name = "v1" version2 = Version() version2.name = "v2" versions = {"v1": version1, "v2": version2} object_dto = [] object1 = ObjectObject() object2 = ObjectObject() object3 = ObjectObject() object1.name = "a" object2.name = "a" object3.name = "b" array = ObjectArray() array.name = "b" dynamic = ObjectDynamic() dynamic.name = "c" string = ObjectString() string.name = "d" type = ObjectType() type.name = "e" type.type_object = "f" const = ObjectConst() const.value = "g" enum = ObjectEnum() enum.name = "h" enum.values = ["h"] object1.properties = {"p1": array, "p3": const, "p4": enum} object2.properties = {"p1": array, "p3": const, "p4": enum, "p2": type} array.items = dynamic dynamic.items = string response = Hydrator(version1, versions, []).hydrate_object(object_dto, object1) response = Hydrator(version2, versions, []).hydrate_object(object_dto, object2) response = Hydrator(version1, versions, []).hydrate_object(object_dto, object3) self.assertEqual(1, response) self.assertEqual(2, len(object_dto)) self.assertIn(version1.name, object_dto[0].versions) self.assertIn(version1.name, object_dto[0].versions) self.assertNotIn(version2.name, object_dto[1].versions) self.assertEqual("a", object_dto[0].value.name) self.assertEqual("b", object_dto[0].value.properties["p1"][0].value.name) self.assertEqual( "c", object_dto[0].value.properties["p1"][0].value.items[0].value.name) self.assertEqual( "d", object_dto[0].value.properties["p1"] [0].value.items[0].value.items[0].value.name) self.assertEqual("e", object_dto[0].value.properties["p2"][0].value.name) self.assertEqual( "f", object_dto[0].value.properties["p2"][0].value.type_object) self.assertEqual("g", object_dto[0].value.properties["p3"][0].value.value) self.assertEqual( "h", object_dto[0].value.properties["p4"][0].value.values[0].value)