source: pyson/test/JsonSubTypesTest.py@ 1360

Last change on this file since 1360 was 1359, checked in by wouter, 4 days ago

#420 added test showing JsonSubTypes issue

File size: 2.5 KB
Line 
1from pyson.JsonSubTypes import JsonSubTypes, getSubTypes
2from pyson.JsonTypeInfo import JsonTypeInfo
3from pyson.JsonTypeInfo import Id,As
4
5import unittest
6
7
8@JsonSubTypes(['test.JsonSubTypesTest.B','test.JsonSubTypesTest.C','test.JsonSubTypesTest.D'])
9class A:
10 pass
11
12class B(A):
13 pass
14
15class C(B):
16 pass
17
18class D(A):
19 pass
20
21@JsonSubTypes(['test.JsonSubTypesTest.Q'])
22class P:
23 pass
24
25@JsonSubTypes(['test.JsonSubTypesTest.R'])
26class Q(P):
27 pass
28
29class R(Q):
30 pass
31
32
33class JsonSubTypesTest(unittest.TestCase):
34 def test1(self):
35 @JsonSubTypes(["first.sub.class","subclss2"])
36 class superclass:
37 def print(self, *args):
38 for arg in args:
39 print(arg)
40
41
42
43 class derivedclass(superclass):
44 pass
45
46 print(type(superclass))
47 print(superclass.__jsonsubtypes__)
48
49 pr = superclass()
50 pr.print(1, 2, 3)
51 print(type(pr))
52 print(pr.__jsonsubtypes__)
53
54 print("checking __jsonsubtypes__ function")
55 # WHY is self not added in this call???
56 print(superclass.__jsonsubtypes__)
57
58 print(derivedclass.__jsonsubtypes__)
59
60 class super2nojason:
61 pass
62
63 # even with mixedclass, we inherit the annotation
64 class mixedclass(superclass, super2nojason):
65 pass
66
67 print(mixedclass.__jsonsubtypes__)
68
69
70 @JsonTypeInfo(use=Id.NAME, include=As.WRAPPER_OBJECT)
71 @JsonSubTypes(["Cat"])
72 class Animal:
73 def __init__(self, age:int):
74 self._age=age
75 def getage(self):
76 return self_age
77
78
79 class Cat(Animal):
80 def __init__(self, age:int, color:str):
81 super().__init__(age)
82 self._color=color
83 def getcolor(self):
84 return self._color
85
86 cat=Cat(1,"brown")
87 print(cat.__jsonsubtypes__)
88 print(str(cat.__jsontypeinfo__))
89
90 def testInheritance(self):
91 self.assertEqual({A,B,C,D} ,getSubTypes(A))
92 self.assertEqual({B,C} ,getSubTypes(B))
93 self.assertEqual({C} ,getSubTypes(C))
94 self.assertEqual({D} ,getSubTypes(D))
95
96 def testInheritance2(self):
97 self.assertEqual({P,Q,R} ,getSubTypes(P))
98 self.assertEqual({Q,R} ,getSubTypes(Q))
99 self.assertEqual({R} ,getSubTypes(R))
100
101
102
Note: See TracBrowser for help on using the repository browser.