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