Esempio n. 1
0
    def test_autoregistration_implicit(self):
        json_converter = JsonConverter()

        @json_converter.serializable
        class Autoserialize(object):
            def __init__(self, attr):
                self.attr = attr

            def _to_json_dict(self):
                return {"attr": self.attr}

            @classmethod
            def _from_json_dict(cls, item):
                return cls(**item)

            def __eq__(self, other):
                return self.attr == other.attr

        self.assertEqual(Autoserialize._json_id, "test_json.Autoserialize")

        autoserialize = Autoserialize("an attr")
        converted = json_converter.dumps(autoserialize)
        reencoded = json_converter.loads(converted)
        self.assertEqual(autoserialize, reencoded)

        res_dict = json.loads(converted)
        self.assertEqual(res_dict, {"__id__": "test_json.Autoserialize", "__value__": {"attr": "an attr"}})
Esempio n. 2
0
    def test_autoregistration_implicit(self):
        json_converter = JsonConverter()

        @json_converter.serializable
        class Autoserialize(object):
            def __init__(self, attr):
                self.attr = attr

            def _to_json_dict(self):
                return {"attr": self.attr}

            @classmethod
            def _from_json_dict(cls, item):
                return cls(**item)

            def __eq__(self, other):
                return self.attr == other.attr

        self.assertEqual(Autoserialize._json_id, "test_json.Autoserialize")

        autoserialize = Autoserialize("an attr")
        converted = json_converter.dumps(autoserialize)
        reencoded = json_converter.loads(converted)
        self.assertEqual(autoserialize, reencoded)

        res_dict = json.loads(converted)
        self.assertEqual(res_dict, {
            "__id__": "test_json.Autoserialize",
            "__value__": {
                "attr": "an attr"
            }
        })
Esempio n. 3
0
    def test_double_identifier(self):
        class AA(A):
            pass

        converter = JsonConverter()
        converter.register(A)
        self.assertRaises(ValueError, converter.register, AA)
Esempio n. 4
0
    def test_unknown(self):
        converter = JsonConverter()
        converter.register(A)

        unknown_json = """{"__id__": "unknown", "__value__": {"a": [2, 4, 6]}}"""
        res_with_converter = converter.loads(unknown_json)
        res_without_converter = converter.loads(unknown_json)

        self.assertEqual(res_with_converter, res_without_converter)
Esempio n. 5
0
    def test_can_decode(self):
        converter = JsonConverter()
        converter.register(A)

        json_code = """{"__id__": "pelita.test.A", "__value__": {"a": ["1", "2", "3"]}}"""

        decoded = converter.loads(json_code)

        a = A(['1', '2', '3'])
        self.assertEqual(decoded, a)
Esempio n. 6
0
    def test_can_decode(self):
        converter = JsonConverter()
        converter.register(A)

        json_code = """{"__id__": "pelita.test.A", "__value__": {"a": ["1", "2", "3"]}}"""

        decoded = converter.loads(json_code)

        a = A(['1', '2', '3'])
        self.assertEqual(decoded, a)
Esempio n. 7
0
    def test_can_encode(self):
        converter = JsonConverter()
        converter.register(A)

        a = A(['1', '2', '3'])
        encoded = converter.dumps(a)

        should_be = {"__id__": "pelita.test.A", "__value__": {"a": ["1", "2", "3"]}}
        decoded = json.loads(encoded)
        self.assertEqual(decoded, should_be)
Esempio n. 8
0
    def test_can_encode(self):
        converter = JsonConverter()
        converter.register(A)

        a = A(['1', '2', '3'])
        encoded = converter.dumps(a)

        should_be = {
            "__id__": "pelita.test.A",
            "__value__": {
                "a": ["1", "2", "3"]
            }
        }
        decoded = json.loads(encoded)
        self.assertEqual(decoded, should_be)
Esempio n. 9
0
    def test_encoding_methods(self):
        converter = JsonConverter()

        def encoder(obj):
            return {"a": [val * 2 for val in obj.a]}

        def decoder(obj):
            obj["a"] = [val // 2 for val in obj["a"]]
            return A(**obj)

        converter.register(A, encoder=encoder, decoder=decoder)
        a = A([1, 2, 3])
        res = converter.dumps(a)
        res_dict = json.loads(res)

        self.assertEqual(res_dict, {"__id__": "pelita.test.A", "__value__": {"a": [2, 4, 6]}})

        reencoded = converter.loads(res)

        self.assertEqual(a, reencoded)
Esempio n. 10
0
    def test_encoding_methods(self):
        converter = JsonConverter()

        def encoder(obj):
            return {"a": [val * 2 for val in obj.a]}

        def decoder(obj):
            obj["a"] = [val // 2 for val in obj["a"]]
            return A(**obj)

        converter.register(A, encoder=encoder, decoder=decoder)
        a = A([1, 2, 3])
        res = converter.dumps(a)
        res_dict = json.loads(res)

        self.assertEqual(res_dict, {
            "__id__": "pelita.test.A",
            "__value__": {
                "a": [2, 4, 6]
            }
        })

        reencoded = converter.loads(res)

        self.assertEqual(a, reencoded)
Esempio n. 11
0
    def test_can_recode_nested(self):
        converter = JsonConverter()
        converter.register(A,
                           encoder=A._to_json_dict,
                           decoder=A._from_json_dict)
        converter.register(B)

        a1 = A(1)
        a2 = A("2")
        a3 = A(['1', '2', '3'])
        bb = B("B", a1, a2, a3)

        dumped = converter.dumps(bb)
        loaded = converter.loads(dumped)

        self.assertEqual(bb, loaded)
Esempio n. 12
0
    def test_unknown(self):
        converter = JsonConverter()
        converter.register(A)

        unknown_json = """{"__id__": "unknown", "__value__": {"a": [2, 4, 6]}}"""
        res_with_converter = converter.loads(unknown_json)
        res_without_converter = converter.loads(unknown_json)

        self.assertEqual(res_with_converter, res_without_converter)
Esempio n. 13
0
    def test_wrong_classes(self):
        converter = JsonConverter()

        class NoId(object):
            def _to_json_dict(self):
                return {}

            @classmethod
            def _from_json_dict(cls, item):
                return cls()

        self.assertRaises(ValueError, converter.register, NoId)

        @json_id("pelita.test.NoToDict")
        class NoToDict(object):
            @classmethod
            def _from_json_dict(cls, item):
                return cls()

        self.assertRaises(ValueError, converter.register, NoToDict)

        @json_id("pelita.test.NoFromDict")
        class NoFromDict(object):
            def _to_json_dict(self):
                return {}

        self.assertRaises(ValueError, converter.register, NoFromDict)

        @json_id("pelita.test.BadToDict")
        class BadToDict(object):
            @classmethod
            def _to_json_dict(cls, item):
                return {}

            @classmethod
            def _from_json_dict(cls, item):
                return cls()

        self.assertRaises(ValueError, converter.register, BadToDict)

        @json_id("pelita.test.BadFromDict")
        class BadFromDict(object):
            def _to_json_dict(self):
                return {}

            def _from_json_dict(item):
                return item

        self.assertRaises(ValueError, converter.register, BadFromDict)
Esempio n. 14
0
    def test_can_recode_nested(self):
        converter = JsonConverter()
        converter.register(A, encoder=A._to_json_dict, decoder=A._from_json_dict)
        converter.register(B)

        a1 = A(1)
        a2 = A("2")
        a3 = A(['1', '2', '3'])
        bb = B("B", a1, a2, a3)

        dumped = converter.dumps(bb)
        loaded = converter.loads(dumped)

        self.assertEqual(bb, loaded)
Esempio n. 15
0
    def test_autoregistration_gone_wrong(self):
        json_converter = JsonConverter()

        def test_method():
            @json_converter.serializable(1)
            class Autoserialize(object):
                def __init__(self, attr):
                    self.attr = attr

                def _to_json_dict(self):
                    return {"attr": self.attr}

                @classmethod
                def _from_json_dict(cls, item):
                    return cls(**item)

                def __eq__(self, other):
                    return self.attr == other.attr

        self.assertRaises(TypeError, test_method)
Esempio n. 16
0
 def test_double_identifier(self):
     class AA(A):
         pass
     converter = JsonConverter()
     converter.register(A)
     self.assertRaises(ValueError, converter.register, AA)
Esempio n. 17
0
    def test_unregistered(self):
        converter = JsonConverter()
        a = A("value")

        self.assertRaises(TypeError, converter.dumps, a)