forked from iml130/FiwareObjectConverter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestObjectFiwareConverter.py
More file actions
106 lines (84 loc) · 4.16 KB
/
Copy pathtestObjectFiwareConverter.py
File metadata and controls
106 lines (84 loc) · 4.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# Copyright 2018 Fraunhofer IML
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
__author__ = "Dominik Lux"
__credits__ = ["Peter Detzner"]
__maintainer__ = "Dominik Lux"
__version__ = "0.0.1a"
__status__ = "Developement"
import unittest
import os
import sys
sys.path.insert(0, os.path.dirname(os.path.realpath(__file__)))
from objectFiwareConverter import ObjectFiwareConverter
from ObjectToJson.entity import Entity
from JsonToObject.reverseEntity import ReverseEntity
sys.path.pop(0)
class Test_JsonConverter(unittest.TestCase):
def test_2Fiware(self):
ObjectFiwareConverter.obj2Fiware(TestClass(), ind=4)
def test_2Obj(self):
json = """{"type": "TestClass", "id": "TestClass1", "val": {"type": "number", "value": 1, "metadata": {} } }"""
tc = TestClass()
tc.val = 123456789123456789123456789 # Evaluates automatically to Long for Python 2
ObjectFiwareConverter.fiware2Obj(json, tc)
self.assertEqual(tc.val, 1)
def test_2ObjWithOutMetaData_Unicode(self):
json = """{"type":"TestClass","id":"ID","val":{"type":"string","value":"i am unicode","metadata":{"python":{"type":"dataType","value":"unicode"}}}}"""
tc = TestClass()
tc.val = str(" ")
ObjectFiwareConverter.fiware2Obj(json, tc, useMetaData=False)
self.assertEqual(type(tc.val), str)
self.assertEqual(tc.val, "i am unicode") # Not unicode
def test_2ObjWithOutMetaDataAndTypeCheck_Unicode(self):
json = """{"type":"TestClass","id":"ID","val":{"type":"string","value":"i am unicode","metadata":{"python":{"type":"dataType","value":"unicode"}}}}"""
tc = TestClass()
ObjectFiwareConverter.fiware2Obj(
json, tc, useMetaData=False, ignoreWrongDataType=True)
self.assertEqual(type(tc.val), str)
self.assertEqual(tc.val, "i am unicode") # Not unicode
def test_2ObjWithOutMetaData_Complex(self):
json = """{"type": "TestClass","id": "ID","val":
{"type": "array","value":
[{"type": "number","value": 0.0,"metadata": {"python": {"type": "dataType","value": "float"}}},
{"type": "number","value": 2.1,"metadata": {"python": {"type": "dataType","value": "float"}}}]
,"metadata": {"python": {"type": "dataType","value": "complex"}}}}"""
tc = TestClass()
tc.val = list()
ObjectFiwareConverter.fiware2Obj(json, tc, useMetaData=False)
self.assertEqual(type(tc.val), list)
self.assertEqual(tc.val, [0, 2.1]) # Not unicode
def test_2Fiware2Obj(self):
json = ObjectFiwareConverter.obj2Fiware(TestClass())
tc = TestClass()
tc.val = 42
ObjectFiwareConverter.fiware2Obj(json, tc)
self.assertEqual(tc.val, 1)
def test_2Fiware2Obj_WithOut_ID_Value(self):
json = ObjectFiwareConverter.obj2Fiware(TestClass(), showIdValue=False)
tc = TestClass()
tc.val = 42
ObjectFiwareConverter.fiware2Obj(json, tc)
self.assertEqual(tc.val, 1)
def test_IntegerType(self): ### TODO Accept Integers and other primitives?
json = """{"id":"Task1","type":"Task","task":{"type":"Integer","value":0}}"""
tc = TestClass()
tc.val = 1 # set Number/Integer
ObjectFiwareConverter.fiware2Obj(json, tc, setAttr=True)
self.assertEqual(getattr(tc, 'task'), 0)
self.assertEqual(getattr(tc, 'id'), 'Task1')
self.assertEqual(getattr(tc, 'type'), 'Task')
self.assertEqual(tc.val, 1)
class TestClass(object):
def __init__(self):
self.val = 1