File size: 2,271 Bytes
550665c |
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 |
from unittest import TestCase
from gcsa.serializers.base_serializer import BaseSerializer
class TestBaseSerializer(TestCase):
def test_ensure_dict(self):
json_str = """
{
"key": "value",
"list": [1, 2, 4]
}
"""
json_dict = {
"key": "value",
"list": [1, 2, 4]
}
json_object = (1, 2, 3) # non-json object
self.assertDictEqual(BaseSerializer.ensure_dict(json_str), json_dict)
self.assertDictEqual(BaseSerializer.ensure_dict(json_dict), json_dict)
with self.assertRaises(TypeError):
BaseSerializer.ensure_dict(json_object)
def test_subclass(self):
class Apple:
pass
# should not raise any exceptions
class AppleSerializer(BaseSerializer):
type_ = Apple
def __init__(self, apple):
super().__init__(apple)
@staticmethod
def _to_json(obj):
pass
@staticmethod
def _to_object(json_):
pass
with self.assertRaises(AssertionError):
# type_ not defined
class PeachSerializer(BaseSerializer):
def __init__(self, peach):
super().__init__(peach)
@staticmethod
def _to_json(obj):
pass
@staticmethod
def _to_object(json_):
pass
class Watermelon:
pass
with self.assertRaises(AssertionError):
# __init__ parameter should be "apple"
class WatermelonSerializer(BaseSerializer):
type_ = Watermelon
def __init__(self, peach):
super().__init__(peach)
@staticmethod
def _to_json(obj):
pass
@staticmethod
def _to_object(json_):
pass
with self.assertRaises(TypeError):
AppleSerializer(Watermelon)
with self.assertRaises(TypeError):
AppleSerializer.to_json(Watermelon)
|