Ejemplo n.º 1
0
 def test_ReverseEntityAttributeInt(self):
     d = dict(type="number",
              value=1,
              metadata=dict(python=dict(type="dataType", value="int")))
     rea = ReverseEntityAttribute(d)
     self.assertEqual(1, rea.getValue())
     self.assertEqual(type(rea.getValue()), int)
Ejemplo n.º 2
0
    def setObject(self,
                  obj,
                  useMetaData=True,
                  ignoreWrongDataType=False,
                  setAttr=False):
        # Explicitly set id and type, always!
        setattr(obj, 'id', str(self.id))
        setattr(obj, 'type', str(self.type))

        for key, value in self.payload.items():
            rea = ReverseEntityAttribute(value, useMetaData)
            if (setAttr):
                # Just use setAttr
                setattr(obj, key, rea.getValue())
            elif key in obj.__dict__:
                if (ignoreWrongDataType):
                    # Ignoring expected Data-Type
                    obj.__dict__[key] = rea.getValue()
                else:
                    val = rea.getValue()
                    if type(obj.__dict__[key]) is not type(val):
                        raise TypeError(
                            MISMATCH_MESSAGE.format(type(obj.__dict__[key]),
                                                    type(val)))
                    else:
                        obj.__dict__[key] = val
Ejemplo n.º 3
0
 def test_ReverseEntityAttributeFloat(self):
     d = dict(type="number",
              value=2.132,
              metadata=dict(python=dict(type="dataType", value="float")))
     rea = ReverseEntityAttribute(d)
     self.assertEqual(2.132, rea.getValue())
     self.assertEqual(type(rea.getValue()), float)
Ejemplo n.º 4
0
 def test_ReverseEntityAttributeLong(self):
     if sys.version_info <= (3, 0):
         d = dict(type="number",
                  value=123456789123456789123,
                  metadata=dict(python=dict(type="dataType", value="long")))
         rea = ReverseEntityAttribute(d)
         # self.assertEqual(123456789123456789123, rea.getValue()) # Python2 is not able to convert it back properly
         self.assertEqual(type(rea.getValue()), long)
Ejemplo n.º 5
0
 def test_ReverseEntityAttributeUnicode(self):
     d = dict(type="string",
              value=u'Unicode',
              metadata=dict(python=dict(type="dataType", value="unicode")))
     rea = ReverseEntityAttribute(d)
     self.assertEqual(u'Unicode', rea.getValue())
     if sys.version_info <= (3, 0):
         self.assertEqual(type(rea.getValue()), unicode)
     else:
         self.assertEqual(type(rea.getValue()), str)
Ejemplo n.º 6
0
    def test_ReverseEntityAttributeForeignClass(self):
        d = dict(type="ClassInt",
                 value=dict(
                     int=dict(type="number",
                              value=1,
                              metadata=dict(
                                  python=dict(type="dataType", value="int")))),
                 metadata=dict(python="class"))

        rea = ReverseEntityAttribute(d)
        self.assertEqual(dict(int=1), rea.getValue())
        self.assertEqual(type(rea.getValue()), dict)
Ejemplo n.º 7
0
 def test_ReverseEntityAttributeTuple(self):
     d = dict(
         type="array",
         value=[
             dict(type="number",
                  value=1,
                  metadata=dict(python=dict(type="dataType", value="int"))),
             dict(type="number",
                  value=2,
                  metadata=dict(python=dict(type="dataType", value="int")))
         ],
         metadata=dict(python=dict(type="dataType", value="tuple")))
     rea = ReverseEntityAttribute(d)
     self.assertEqual(tuple((1, 2)), rea.getValue())
     self.assertEqual(type(rea.getValue()), tuple)
Ejemplo n.º 8
0
 def test_ReverseEntityAttributeComplex(self):
     d = dict(
         type="array",
         value=[
             dict(type="number",
                  value=0,
                  metadata=dict(python=dict(type="dataType", value="int"))),
             dict(type="number",
                  value=2.34,
                  metadata=dict(python=dict(type="dataType", value="int")))
         ],
         metadata=dict(python=dict(type="dataType", value="complex")))
     rea = ReverseEntityAttribute(d)
     self.assertEqual(2.34j, rea.getValue())
     self.assertEqual(type(rea.getValue()), complex)
Ejemplo n.º 9
0
 def test_ReverseEntityAttributeList(self):
     d = dict(
         type="array",
         value=[
             dict(type="number",
                  value=1,
                  metadata=dict(python=dict(type="dataType", value="int"))),
             dict(type="number",
                  value=2,
                  metadata=dict(python=dict(type="dataType", value="int")))
         ],
         metadata={})
     rea = ReverseEntityAttribute(d)
     self.assertEqual(list([1, 2]), rea.getValue())
     self.assertEqual(type(rea.getValue()), list)
Ejemplo n.º 10
0
    def test_ReverseEntityAttributeDict(self):
        d = dict(
            type="object",
            value=dict(a=dict(
                type="number",
                value=1,
                metadata=dict(python=dict(type="dataType", value="int"))),
                       b=dict(type="number",
                              value=2,
                              metadata=dict(
                                  python=dict(type="dataType", value="int")))),
            metadata={})

        rea = ReverseEntityAttribute(d)
        self.assertEqual(dict(a=1, b=2), rea.getValue())
        self.assertEqual(type(rea.getValue()), dict)
Ejemplo n.º 11
0
 def test_ReverseEntityAttributeString(self):
     d = dict(type="string", value="Hello world!", metadata={})
     rea = ReverseEntityAttribute(d)
     self.assertEqual("Hello world!", rea.getValue())
     self.assertEqual(type(rea.getValue()), str)
Ejemplo n.º 12
0
 def test_ReverseEntityAttributeBool(self):
     d = dict(type="boolean", value=True, metadata={})
     rea = ReverseEntityAttribute(d)
     self.assertEqual(True, rea.getValue())
     self.assertEqual(type(rea.getValue()), bool)
Ejemplo n.º 13
0
 def test_ReverseEntityAttributeBool_WithWrongValue(self):
     d = dict(type="boolean", value="False", metadata={})
     rea = ReverseEntityAttribute(d)
     self.assertEqual(False, rea.getValue())
     self.assertEqual(type(rea.getValue()), bool)