Example #1
0
 def test_object_factory(self):
     self.assertIsInstance(
         ObjectSample.factory(Object.factory("object", "v1")),
         ObjectObjectSample)
     self.assertIsInstance(
         ObjectSample.factory(Object.factory("array", "v1")),
         ObjectArraySample)
     self.assertIsInstance(
         ObjectSample.factory(Object.factory("number", "v1")), ObjectSample)
     self.assertIsInstance(
         ObjectSample.factory(Object.factory("string", "v1")), ObjectSample)
     self.assertIsInstance(
         ObjectSample.factory(Object.factory("bool", "v1")), ObjectSample)
     self.assertIsInstance(
         ObjectSample.factory(Object.factory("type", "v1")),
         ObjectTypeSample)
     self.assertIsInstance(
         ObjectSample.factory(Object.factory("none", "v1")), ObjectSample)
     self.assertIsInstance(
         ObjectSample.factory(Object.factory("dynamic", "v1")),
         ObjectDynamicSample)
     self.assertIsInstance(
         ObjectSample.factory(Object.factory("const", "v1")),
         ObjectConstSample)
     self.assertIsInstance(
         ObjectSample.factory(Object.factory("enum", "v1")),
         ObjectEnumSample)
Example #2
0
    def test_enum_compare__with_major(self):
        enum1 = ObjectEnumDto(Object.factory("enum", "v1"))
        enum2 = ObjectEnumDto(Object.factory("enum", "v1"))
        enum1.name = "a"
        enum2.name = "b"

        self.assertTrue(enum1 < enum2)
Example #3
0
    def test_enum_compare__with_major(self):
        enum1 = ObjectEnumDto(Object.factory("enum", "v1"))
        enum2 = ObjectEnumDto(Object.factory("enum", "v1"))
        enum1.name = "a"
        enum2.name = "b"

        self.assertTrue(enum1 < enum2)
Example #4
0
    def test_object_compare__with_name(self):
        object1 = ObjectDto(Object())
        object2 = ObjectDto(Object())

        object1.name = "a"
        object2.name = "b"

        self.assertEqual(object1, sorted([object2, object1])[0])
Example #5
0
 def test_object_factory(self):
     self.assertIsInstance(ObjectDto.factory(Object.factory("object", "v1")), ObjectObjectDto)
     self.assertIsInstance(ObjectDto.factory(Object.factory("array", "v1")), ObjectArrayDto)
     self.assertIsInstance(ObjectDto.factory(Object.factory("number", "v1")), ObjectDto)
     self.assertIsInstance(ObjectDto.factory(Object.factory("string", "v1")), ObjectDto)
     self.assertIsInstance(ObjectDto.factory(Object.factory("bool", "v1")), ObjectDto)
     self.assertIsInstance(ObjectDto.factory(Object.factory("type", "v1")), ObjectTypeDto)
     self.assertIsInstance(ObjectDto.factory(Object.factory("none", "v1")), ObjectDto)
     self.assertIsInstance(ObjectDto.factory(Object.factory("dynamic", "v1")), ObjectDynamicDto)
     self.assertIsInstance(ObjectDto.factory(Object.factory("const", "v1")), ObjectConstDto)
     self.assertIsInstance(ObjectDto.factory(Object.factory("enum", "v1")), ObjectEnumDto)
Example #6
0
 def test_object_factory(self):
     self.assertIsInstance(ObjectDto.factory(Object.factory("object", "v1")), ObjectObjectDto)
     self.assertIsInstance(ObjectDto.factory(Object.factory("array", "v1")), ObjectArrayDto)
     self.assertIsInstance(ObjectDto.factory(Object.factory("number", "v1")), ObjectDto)
     self.assertIsInstance(ObjectDto.factory(Object.factory("string", "v1")), ObjectDto)
     self.assertIsInstance(ObjectDto.factory(Object.factory("boolean", "v1")), ObjectDto)
     self.assertIsInstance(ObjectDto.factory(Object.factory("type", "v1")), ObjectTypeDto)
     self.assertIsInstance(ObjectDto.factory(Object.factory("none", "v1")), ObjectDto)
     self.assertIsInstance(ObjectDto.factory(Object.factory("dynamic", "v1")), ObjectDynamicDto)
     self.assertIsInstance(ObjectDto.factory(Object.factory("const", "v1")), ObjectConstDto)
     self.assertIsInstance(ObjectDto.factory(Object.factory("enum", "v1")), ObjectEnumDto)
Example #7
0
 def test_object_factory(self):
     self.assertIsInstance(ObjectSample.factory(Object.factory("object", "v1")), ObjectObjectSample)
     self.assertIsInstance(ObjectSample.factory(Object.factory("array", "v1")), ObjectArraySample)
     self.assertIsInstance(ObjectSample.factory(Object.factory("number", "v1")), ObjectSample)
     self.assertIsInstance(ObjectSample.factory(Object.factory("string", "v1")), ObjectSample)
     self.assertIsInstance(ObjectSample.factory(Object.factory("boolean", "v1")), ObjectSample)
     self.assertIsInstance(ObjectSample.factory(Object.factory("type", "v1")), ObjectTypeSample)
     self.assertIsInstance(ObjectSample.factory(Object.factory("none", "v1")), ObjectSample)
     self.assertIsInstance(ObjectSample.factory(Object.factory("dynamic", "v1")), ObjectDynamicSample)
     self.assertIsInstance(ObjectSample.factory(Object.factory("const", "v1")), ObjectConstSample)
     self.assertIsInstance(ObjectSample.factory(Object.factory("enum", "v1")), ObjectEnumSample)
Example #8
0
    def test_object_compare__with_type(self):
        object1 = ObjectDto(Object())
        object2 = ObjectDto(Object())

        object1.name = "a"
        object1.description = "a"
        object1.type = Object.Types.bool
        object2.name = "a"
        object2.description = "a"
        object2.type = Object.Types.string

        self.assertEqual(object1, sorted([object2, object1])[0])
Example #9
0
    def test_object(self):
        object = Object()
        object.name = "a"
        object.type = Object.Types.string
        object.optional = True

        object_sample = ObjectSample(object)

        self.assertEqual("a", object_sample.name)
        self.assertEqual(Object.Types.string, object_sample.type)
        self.assertTrue(object_sample)
        self.assertEqual("my_a", object_sample.sample)
Example #10
0
    def test_object(self):
        object = Object()
        object.name = "a"
        object.type = Object.Types.string
        object.optional = True

        object_sample = ObjectSample(object)

        self.assertEqual("a", object_sample.name)
        self.assertEqual(Object.Types.string, object_sample.type)
        self.assertTrue(object_sample)
        self.assertEqual("my_a", object_sample.sample)
Example #11
0
    def test_object_compare__with_optional(self):
        object1 = ObjectDto(Object())
        object2 = ObjectDto(Object())

        object1.name = "a"
        object1.description = "a"
        object1.type = Object.Types.bool
        object1.optional = False
        object2.name = "a"
        object2.description = "a"
        object2.type = Object.Types.bool
        object2.optional = True

        self.assertEqual(object1, sorted([object2, object1])[0])
Example #12
0
    def test_object_factory(self):
        ObjectRaw.Types.foo = "foo"

        self.assertIsInstance(Object.factory("object", "v1"), ObjectObject)
        self.assertIsInstance(Object.factory("array", "v1"), ObjectArray)
        self.assertIsInstance(Object.factory("number", "v1"), ObjectNumber)
        self.assertIsInstance(Object.factory("string", "v1"), ObjectString)
        self.assertIsInstance(Object.factory("bool", "v1"), ObjectBool)
        self.assertIsInstance(Object.factory("reference", "v1"), ObjectReference)
        self.assertIsInstance(Object.factory("type", "v1"), ObjectType)
        self.assertIsInstance(Object.factory("none", "v1"), ObjectNone)
        self.assertIsInstance(Object.factory("dynamic", "v1"), ObjectDynamic)
        self.assertIsInstance(Object.factory("foo", "v1"), ObjectRaw)

        del ObjectRaw.Types.foo
Example #13
0
    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
Example #14
0
    def test_objectDynamic(self):
        object_dto = ObjectDynamicDto(Object())

        self.assertEqual(None, object_dto.items)
Example #15
0
    def test_objectArray(self):
        object_dto = ObjectArrayDto(Object())

        self.assertEqual(None, object_dto.items)
Example #16
0
    def test_objectObject(self):
        object_dto = ObjectObjectDto(Object())

        self.assertEqual({}, object_dto.properties)