1 | from abc import ABC
|
---|
2 | from pyson.JsonSubTypes import JsonSubTypes
|
---|
3 | from pyson.JsonTools import getInitArgs, str_to_class
|
---|
4 | import unittest
|
---|
5 |
|
---|
6 |
|
---|
7 | @JsonSubTypes(["test.JsonToolsTest.B"])
|
---|
8 | class A:
|
---|
9 | pass
|
---|
10 |
|
---|
11 | class B(A):
|
---|
12 | pass
|
---|
13 |
|
---|
14 |
|
---|
15 | class JsonToolsTest(unittest.TestCase):
|
---|
16 |
|
---|
17 |
|
---|
18 | def testGetInitArgs(self):
|
---|
19 | class ClassWithInitVars:
|
---|
20 | def __init__(self, x:int):
|
---|
21 | y=2
|
---|
22 |
|
---|
23 | self.assertEqual({"x":int}, getInitArgs(ClassWithInitVars))
|
---|
24 |
|
---|
25 | def testGetInitArgsAbc(self):
|
---|
26 | class BaseClass(ABC):
|
---|
27 | pass
|
---|
28 |
|
---|
29 | class ClassWithInitVars2(BaseClass):
|
---|
30 | def __init__(self, x:int):
|
---|
31 | y=2
|
---|
32 |
|
---|
33 | self.assertEqual({"x":int}, getInitArgs(ClassWithInitVars2))
|
---|
34 |
|
---|
35 | # def testGetListTypePrimitiveMixed(self):
|
---|
36 | # self.assertEqual(None, getListClass([3, True]))
|
---|
37 |
|
---|
38 | # def testGetListTypePolymorphicNMixed(self):
|
---|
39 | # self.assertEqual(A, getListClass([B(), A(),B()]))
|
---|
40 |
|
---|
41 | def testStrToClassLocal(self):
|
---|
42 | str_to_class("test.JsonToolsTest.A")
|
---|
43 |
|
---|
44 | def testStrToClassRemoteWrongName(self):
|
---|
45 | # twrong name ValueDeserializer, see next test with correct name
|
---|
46 | self.assertRaises(AttributeError, lambda:str_to_class("test.MyDeserializer.ValueDeserializer"))
|
---|
47 |
|
---|
48 |
|
---|
49 | def testStrToClassRemote(self):
|
---|
50 | str_to_class("test.MyDeserializer.ValueDeserializer2")
|
---|