[826] | 1 |
|
---|
| 2 | from abc import ABC
|
---|
| 3 | from datetime import datetime
|
---|
| 4 | from decimal import Decimal
|
---|
| 5 | import json
|
---|
| 6 | import re
|
---|
| 7 | import sys, traceback
|
---|
| 8 | from typing import Dict, List, Set, Any, Union, Optional, Collection
|
---|
| 9 | import unittest
|
---|
| 10 | from uuid import uuid4, UUID
|
---|
| 11 |
|
---|
| 12 | from pyson.Deserializer import Deserializer
|
---|
| 13 | from pyson.Serializer import Serializer
|
---|
| 14 | from pyson.JsonSerialize import JsonSerialize
|
---|
| 15 | from pyson.JsonDeserialize import JsonDeserialize
|
---|
| 16 | from pyson.JsonGetter import JsonGetter
|
---|
| 17 | from pyson.JsonSubTypes import JsonSubTypes
|
---|
| 18 | from pyson.JsonTypeInfo import Id, As
|
---|
| 19 | from pyson.JsonTypeInfo import JsonTypeInfo
|
---|
| 20 | from pyson.JsonValue import JsonValue
|
---|
| 21 | from pyson.ObjectMapper import ObjectMapper
|
---|
| 22 | from pickle import NONE
|
---|
| 23 | from test.MyDeserializer import ValueDeserializer2
|
---|
| 24 | from plum import dispatch
|
---|
| 25 |
|
---|
| 26 | '''
|
---|
| 27 | Testing deserialization of overloaded class constructors.
|
---|
| 28 | Note that plum/dispatch is a third party library,
|
---|
| 29 | only imported here for testing. It's NOT part
|
---|
| 30 | of the standard pyson library and you can theoretically
|
---|
| 31 | use other libraries for this.
|
---|
| 32 |
|
---|
| 33 | Also note that MYPY type editor will not like these overloads.
|
---|
| 34 | '''
|
---|
| 35 | class Ovload:
|
---|
| 36 | @dispatch # signature of primary constructor
|
---|
| 37 | def __init__(self, data: int ):
|
---|
| 38 | self.data=data
|
---|
| 39 | @dispatch
|
---|
| 40 | def __init__(self, data:str):
|
---|
| 41 | self.data=int(data)
|
---|
| 42 | def getData(self)->int:
|
---|
| 43 | return self.data
|
---|
| 44 | def __eq__(self, other):
|
---|
| 45 | return isinstance(other, self.__class__) and \
|
---|
| 46 | self.data==other.data
|
---|
| 47 | def __repr__(self):
|
---|
| 48 | return "Ovload:"+str(self.data)
|
---|
| 49 |
|
---|
| 50 |
|
---|
| 51 | class OverloadTest(unittest.TestCase):
|
---|
| 52 | '''
|
---|
| 53 | The class has an overloaded constructor.
|
---|
| 54 | Check if it works.
|
---|
| 55 | '''
|
---|
| 56 | pyson=ObjectMapper()
|
---|
| 57 |
|
---|
| 58 | def testBasicOverload(self):
|
---|
| 59 | o=Ovload(1)
|
---|
| 60 | print(o)
|
---|
| 61 | print(Ovload("2"))
|
---|
| 62 |
|
---|
| 63 | def testRawPlum(self):
|
---|
| 64 | clazz = Ovload
|
---|
| 65 | # we can call the constructor only with a list, not with a dict. #293
|
---|
| 66 | clazz(*[1])
|
---|
| 67 | clazz(*["1"])
|
---|
| 68 |
|
---|
| 69 | def testOverloadSerialize(self):
|
---|
| 70 | objson = self.pyson.toJson(Ovload(1))
|
---|
| 71 | self.assertEqual({'data': 1},objson)
|
---|
| 72 |
|
---|
| 73 | def testOverloadDeserialize(self):
|
---|
| 74 | objson= {'data': 1}
|
---|
| 75 | self.assertEqual(Ovload(1), self.pyson.parse(objson, Ovload))
|
---|
| 76 |
|
---|
| 77 |
|
---|
| 78 |
|
---|